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, 300)
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 */
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
135 response = MHD_create_response_from_buffer (strlen (url),
137 MHD_RESPMEM_MUST_COPY);
138 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
139 MHD_destroy_response (response);
149 if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
151 GNUNET_SCHEDULER_cancel (mhd_task_id);
152 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
154 if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
156 GNUNET_SCHEDULER_cancel (curl_task_id);
157 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
159 if (ctrl_c_task_id != GNUNET_SCHEDULER_NO_TASK)
161 GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
162 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
166 MHD_stop_daemon (mhd);
171 GNUNET_VPN_cancel_request (rr);
176 GNUNET_VPN_disconnect (vpn);
179 GNUNET_free_non_null (url);
185 * Function to run the HTTP client.
192 curl_task (void *cls,
193 const struct GNUNET_SCHEDULER_TaskContext *tc)
195 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
207 struct GNUNET_NETWORK_FDSet nrs;
208 struct GNUNET_NETWORK_FDSet nws;
209 struct GNUNET_TIME_Relative delay;
218 curl_multi_perform (multi, &running);
221 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
222 if (msg->msg == CURLMSG_DONE)
224 if (msg->data.result != CURLE_OK)
227 "%s failed at %s:%d: `%s'\n",
228 "curl_multi_perform",
230 __LINE__, curl_easy_strerror (msg->data.result));
234 curl_multi_remove_handle (multi, curl);
235 curl_multi_cleanup (multi);
236 curl_easy_cleanup (curl);
239 if (cbc.pos != strlen ("/hello_world"))
244 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
253 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
254 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
256 delay = GNUNET_TIME_UNIT_SECONDS;
258 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
259 GNUNET_NETWORK_fdset_copy_native (&nrs,
262 GNUNET_NETWORK_fdset_copy_native (&nws,
265 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
275 * Callback invoked from the VPN service once a redirection is
276 * available. Provides the IP address that can now be used to
277 * reach the requested destination (in our case, the MHD server)
280 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
281 * will match 'result_af' from the request
282 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
283 * that the VPN allocated for the redirection;
284 * traffic to this IP will now be redirected to the
285 * specified target peer; NULL on error
288 allocation_cb (void *cls,
292 char ips[INET6_ADDRSTRLEN];
298 "VPN failed to allocate appropriate address\n");
299 GNUNET_SCHEDULER_shutdown ();
302 GNUNET_asprintf (&url,
303 "http://%s:%u/hello_world",
304 inet_ntop (af, address, ips, sizeof (ips)),
305 (unsigned int) PORT);
306 curl = curl_easy_init ();
307 curl_easy_setopt (curl, CURLOPT_URL, url);
308 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
309 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
310 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
311 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
312 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
313 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
315 multi = curl_multi_init ();
316 GNUNET_assert (multi != NULL);
317 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
324 * Function to keep the HTTP server running.
332 const struct GNUNET_SCHEDULER_TaskContext *tc)
334 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
341 ctrl_c_shutdown (void *cls,
342 const struct GNUNET_SCHEDULER_TaskContext *tc)
344 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
354 struct GNUNET_NETWORK_FDSet nrs;
355 struct GNUNET_NETWORK_FDSet nws;
360 unsigned MHD_LONG_LONG timeout;
361 struct GNUNET_TIME_Relative delay;
363 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
368 GNUNET_assert (MHD_YES ==
369 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
370 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
371 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
372 (unsigned int) timeout);
374 delay = GNUNET_TIME_UNIT_FOREVER_REL;
375 GNUNET_NETWORK_fdset_copy_native (&nrs,
378 GNUNET_NETWORK_fdset_copy_native (&nws,
381 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
391 run (void *cls, char *const *args, const char *cfgfile,
392 const struct GNUNET_CONFIGURATION_Handle *cfg)
399 vpn = GNUNET_VPN_connect (cfg);
400 GNUNET_assert (NULL != vpn);
401 flags = MHD_USE_DEBUG;
402 if (AF_INET6 == dest_af)
403 flags |= MHD_USE_IPv6;
404 mhd = MHD_start_daemon (flags,
409 GNUNET_assert (NULL != mhd);
415 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
419 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
425 rr = GNUNET_VPN_redirect_to_ip (vpn,
430 GNUNET_TIME_UNIT_FOREVER_ABS,
431 &allocation_cb, NULL);
432 ctrl_c_task_id = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
439 setup_peer (struct PeerContext *p, const char *cfgname)
441 p->cfg = GNUNET_CONFIGURATION_create ();
444 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
445 "gnunet-service-arm",
446 "-c", cfgname, NULL);
448 GNUNET_assert (NULL != p->arm_proc);
449 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
454 stop_peer (struct PeerContext *p)
457 if (NULL != p->arm_proc)
459 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
460 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
461 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
462 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
464 GNUNET_OS_process_get_pid (p->arm_proc));
465 GNUNET_OS_process_close (p->arm_proc);
469 GNUNET_CONFIGURATION_destroy (p->cfg);
474 * Test if the given AF is supported by this system.
477 * @return GNUNET_OK if the AF is supported
484 s = socket (af, SOCK_STREAM, 0);
487 if (EAFNOSUPPORT == errno)
489 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
490 return GNUNET_SYSERR;
499 main (int argc, char *const *argv)
503 char *const argvx[] = {
506 "test_gnunet_vpn.conf",
509 struct GNUNET_GETOPT_CommandLineOption options[] = {
510 GNUNET_GETOPT_OPTION_END
513 if (0 != ACCESS ("/dev/net/tun", R_OK))
515 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
519 "WARNING: System unable to run test, skipping.\n");
523 GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
525 GNUNET_OS_check_helper_binary ("gnunet-helper-exit")) )
528 "WARNING: gnunet-helper-{exit,vpn} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
530 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
533 GNUNET_CRYPTO_setup_hostkey ("test_gnunet_vpn.conf");
535 if (NULL != strstr (bin, "lt-"))
536 bin = strstr (bin, "lt-") + 4;
537 type = strstr (bin, "-");
540 fprintf (stderr, "invalid binary name\n");
544 if (0 == strcmp (type, "4_to_6"))
546 dest_ip = "FC5A:04E1:C2BA::1";
550 else if (0 == strcmp (type, "6_to_4"))
552 dest_ip = "169.254.86.1";
556 else if (0 == strcmp (type, "4_over"))
558 dest_ip = "169.254.86.1";
562 else if (0 == strcmp (type, "6_over"))
564 dest_ip = "FC5A:04E1:C2BA::1";
570 fprintf (stderr, "invalid binary suffix `%s'\n", type);
573 if ( (GNUNET_OK != test_af (src_af)) ||
574 (GNUNET_OK != test_af (dest_af)) )
577 "Required address families not supported by this system, skipping test.\n");
582 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
584 fprintf (stderr, "failed to initialize curl\n");
587 setup_peer (&p1, "test_gnunet_vpn.conf");
588 GNUNET_log_setup ("test_gnunet_vpn",
591 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
592 "test_gnunet_vpn", "nohelp", options, &run, NULL);
594 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
598 /* end of test_gnunet_vpn.c */