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_testing_lib-new.h"
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
38 * Return value for 'main'.
40 static int global_ret;
42 static struct GNUNET_VPN_Handle *vpn;
44 static struct MHD_Daemon *mhd;
46 static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
48 static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
50 static GNUNET_SCHEDULER_TaskIdentifier ctrl_c_task_id;
52 static struct GNUNET_VPN_RedirectionRequest *rr;
61 * IP address of the ultimate destination.
63 static const char *dest_ip;
66 * Address family of the dest_ip.
71 * Address family to use by the curl client.
82 static struct CBC cbc;
86 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
88 struct CBC *cbc = ctx;
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;
100 struct MHD_Connection *connection,
104 const char *upload_data, size_t *upload_data_size,
108 struct MHD_Response *response;
111 if (0 != strcmp ("GET", method))
112 return MHD_NO; /* unexpected method */
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
120 response = MHD_create_response_from_buffer (strlen (url),
122 MHD_RESPMEM_MUST_COPY);
123 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
124 MHD_destroy_response (response);
134 if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
136 GNUNET_SCHEDULER_cancel (mhd_task_id);
137 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
139 if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
141 GNUNET_SCHEDULER_cancel (curl_task_id);
142 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
144 if (ctrl_c_task_id != GNUNET_SCHEDULER_NO_TASK)
146 GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
147 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
151 MHD_stop_daemon (mhd);
156 GNUNET_VPN_cancel_request (rr);
161 GNUNET_VPN_disconnect (vpn);
164 GNUNET_free_non_null (url);
170 * Function to run the HTTP client.
177 curl_task (void *cls,
178 const struct GNUNET_SCHEDULER_TaskContext *tc)
180 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
192 struct GNUNET_NETWORK_FDSet nrs;
193 struct GNUNET_NETWORK_FDSet nws;
194 struct GNUNET_TIME_Relative delay;
203 curl_multi_perform (multi, &running);
206 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
207 if (msg->msg == CURLMSG_DONE)
209 if (msg->data.result != CURLE_OK)
212 "%s failed at %s:%d: `%s'\n",
213 "curl_multi_perform",
215 __LINE__, curl_easy_strerror (msg->data.result));
219 curl_multi_remove_handle (multi, curl);
220 curl_multi_cleanup (multi);
221 curl_easy_cleanup (curl);
224 if (cbc.pos != strlen ("/hello_world"))
229 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
238 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
239 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
241 delay = GNUNET_TIME_UNIT_SECONDS;
243 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
244 GNUNET_NETWORK_fdset_copy_native (&nrs,
247 GNUNET_NETWORK_fdset_copy_native (&nws,
250 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
260 * Callback invoked from the VPN service once a redirection is
261 * available. Provides the IP address that can now be used to
262 * reach the requested destination (in our case, the MHD server)
265 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
266 * will match 'result_af' from the request
267 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
268 * that the VPN allocated for the redirection;
269 * traffic to this IP will now be redirected to the
270 * specified target peer; NULL on error
273 allocation_cb (void *cls,
277 char ips[INET6_ADDRSTRLEN];
283 "VPN failed to allocate appropriate address\n");
284 GNUNET_SCHEDULER_shutdown ();
287 GNUNET_asprintf (&url,
288 "http://%s:%u/hello_world",
289 inet_ntop (af, address, ips, sizeof (ips)),
290 (unsigned int) PORT);
291 curl = curl_easy_init ();
292 curl_easy_setopt (curl, CURLOPT_URL, url);
293 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
294 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
295 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
296 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
297 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
298 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
300 multi = curl_multi_init ();
301 GNUNET_assert (multi != NULL);
302 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
309 * Function to keep the HTTP server running.
317 const struct GNUNET_SCHEDULER_TaskContext *tc)
319 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
326 ctrl_c_shutdown (void *cls,
327 const struct GNUNET_SCHEDULER_TaskContext *tc)
329 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
339 struct GNUNET_NETWORK_FDSet nrs;
340 struct GNUNET_NETWORK_FDSet nws;
345 unsigned MHD_LONG_LONG timeout;
346 struct GNUNET_TIME_Relative delay;
348 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
353 GNUNET_assert (MHD_YES ==
354 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
355 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
356 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
357 (unsigned int) timeout);
359 delay = GNUNET_TIME_UNIT_FOREVER_REL;
360 GNUNET_NETWORK_fdset_copy_native (&nrs,
363 GNUNET_NETWORK_fdset_copy_native (&nws,
366 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
377 const struct GNUNET_CONFIGURATION_Handle *cfg,
378 struct GNUNET_TESTING_Peer *peer)
385 vpn = GNUNET_VPN_connect (cfg);
386 GNUNET_assert (NULL != vpn);
387 flags = MHD_USE_DEBUG;
388 if (AF_INET6 == dest_af)
389 flags |= MHD_USE_IPv6;
390 mhd = MHD_start_daemon (flags,
395 GNUNET_assert (NULL != mhd);
401 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
405 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
411 rr = GNUNET_VPN_redirect_to_ip (vpn,
416 GNUNET_TIME_UNIT_FOREVER_ABS,
417 &allocation_cb, NULL);
418 ctrl_c_task_id = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
425 * Test if the given AF is supported by this system.
428 * @return GNUNET_OK if the AF is supported
435 s = socket (af, SOCK_STREAM, 0);
438 if (EAFNOSUPPORT == errno)
440 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
441 return GNUNET_SYSERR;
449 main (int argc, char *const *argv)
454 if (0 != ACCESS ("/dev/net/tun", R_OK))
456 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
460 "WARNING: System unable to run test, skipping.\n");
464 GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
466 GNUNET_OS_check_helper_binary ("gnunet-helper-exit")) )
469 "WARNING: gnunet-helper-{exit,vpn} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
471 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
474 GNUNET_CRYPTO_setup_hostkey ("test_gnunet_vpn.conf");
476 if (NULL != strstr (bin, "lt-"))
477 bin = strstr (bin, "lt-") + 4;
478 type = strstr (bin, "-");
481 fprintf (stderr, "invalid binary name\n");
485 if (0 == strcmp (type, "4_to_6"))
487 dest_ip = "FC5A:04E1:C2BA::1";
491 else if (0 == strcmp (type, "6_to_4"))
493 dest_ip = "169.254.86.1";
497 else if (0 == strcmp (type, "4_over"))
499 dest_ip = "169.254.86.1";
503 else if (0 == strcmp (type, "6_over"))
505 dest_ip = "FC5A:04E1:C2BA::1";
511 fprintf (stderr, "invalid binary suffix `%s'\n", type);
514 if ( (GNUNET_OK != test_af (src_af)) ||
515 (GNUNET_OK != test_af (dest_af)) )
518 "Required address families not supported by this system, skipping test.\n");
521 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
523 fprintf (stderr, "failed to initialize curl\n");
526 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-vpn",
527 "test_gnunet_vpn.conf",
530 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
534 /* end of test_gnunet_vpn.c */