2 This file is part of GNUnet
3 Copyright (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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file test_gnunet_vpn.c
23 * @brief testcase for tunneling HTTP over the GNUnet VPN
24 * @author Christian Grothoff
28 #include <curl/curl.h>
29 #elif HAVE_GNURL_CURL_H
30 #include <gnurl/curl.h>
32 #include <microhttpd.h>
33 #include "gnunet_vpn_service.h"
34 #include "gnunet_testing_lib.h"
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
42 * Return value for 'main'.
44 static int global_ret;
46 static struct GNUNET_VPN_Handle *vpn;
48 static struct MHD_Daemon *mhd;
50 static struct GNUNET_SCHEDULER_Task * mhd_task_id;
52 static struct GNUNET_SCHEDULER_Task * curl_task_id;
54 static struct GNUNET_SCHEDULER_Task * ctrl_c_task_id;
56 static struct GNUNET_VPN_RedirectionRequest *rr;
65 * IP address of the ultimate destination.
67 static const char *dest_ip;
70 * Address family of the dest_ip.
75 * Address family to use by the curl client.
86 static struct CBC cbc;
90 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
92 struct CBC *cbc = ctx;
94 if (cbc->pos + size * nmemb > sizeof (cbc->buf))
95 return 0; /* overflow */
96 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
97 cbc->pos += size * nmemb;
103 mhd_ahc (void *cls, struct MHD_Connection *connection, const char *url,
104 const char *method, const char *version, const char *upload_data,
105 size_t * upload_data_size, void **unused)
108 struct MHD_Response *response;
111 if (0 != strcmp ("GET", method))
112 return MHD_NO; /* unexpected method */
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 "MHD sends respose for request to URL `%s'\n", url);
122 MHD_create_response_from_buffer (strlen (url), (void *) url,
123 MHD_RESPMEM_MUST_COPY);
124 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
125 MHD_destroy_response (response);
135 if (mhd_task_id != NULL)
137 GNUNET_SCHEDULER_cancel (mhd_task_id);
140 if (curl_task_id != NULL)
142 GNUNET_SCHEDULER_cancel (curl_task_id);
145 if (ctrl_c_task_id != NULL)
147 GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
148 ctrl_c_task_id = NULL;
152 MHD_stop_daemon (mhd);
157 GNUNET_VPN_cancel_request (rr);
162 GNUNET_VPN_disconnect (vpn);
165 GNUNET_free_non_null (url);
171 * Function to run the HTTP client.
174 curl_main (void *cls,
175 const struct GNUNET_SCHEDULER_TaskContext *tc)
181 struct GNUNET_NETWORK_FDSet nrs;
182 struct GNUNET_NETWORK_FDSet nws;
183 struct GNUNET_TIME_Relative delay;
193 curl_multi_perform (multi, &running);
196 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
197 if (msg->msg == CURLMSG_DONE)
199 if (msg->data.result != CURLE_OK)
201 fprintf (stderr, "%s failed at %s:%d: `%s'\n", "curl_multi_perform",
202 __FILE__, __LINE__, curl_easy_strerror (msg->data.result));
206 curl_multi_remove_handle (multi, curl);
207 curl_multi_cleanup (multi);
208 curl_easy_cleanup (curl);
211 if (cbc.pos != strlen ("/hello_world"))
216 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
220 "You might want to check if your host-based firewall is blocking the connections.\n");
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
227 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
228 if ((CURLM_OK != curl_multi_timeout (multi, &timeout)) || (-1 == timeout))
229 delay = GNUNET_TIME_UNIT_SECONDS;
232 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
233 (unsigned int) timeout);
234 GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max + 1);
235 GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max + 1);
237 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
238 &nrs, &nws, &curl_main, NULL);
243 * Callback invoked from the VPN service once a redirection is
244 * available. Provides the IP address that can now be used to
245 * reach the requested destination (in our case, the MHD server)
248 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
249 * will match 'result_af' from the request
250 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
251 * that the VPN allocated for the redirection;
252 * traffic to this IP will now be redirected to the
253 * specified target peer; NULL on error
256 allocation_cb (void *cls, int af, const void *address)
258 char ips[INET6_ADDRSTRLEN];
263 fprintf (stderr, "VPN failed to allocate appropriate address\n");
264 GNUNET_SCHEDULER_shutdown ();
268 GNUNET_asprintf (&url,
269 "http://[%s]:%u/hello_world",
270 inet_ntop (af, address, ips, sizeof (ips)),
271 (unsigned int) PORT);
273 GNUNET_asprintf (&url,
274 "http://%s:%u/hello_world",
275 inet_ntop (af, address, ips, sizeof (ips)),
276 (unsigned int) PORT);
277 curl = curl_easy_init ();
278 curl_easy_setopt (curl, CURLOPT_URL, url);
279 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
280 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
281 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
282 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
283 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
284 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
285 curl_easy_setopt (curl, CURLOPT_VERBOSE, 0);
287 multi = curl_multi_init ();
288 GNUNET_assert (multi != NULL);
289 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Beginning HTTP download from `%s'\n",
293 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
300 * Function to keep the HTTP server running.
307 mhd_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
316 ctrl_c_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
318 ctrl_c_task_id = NULL;
328 struct GNUNET_NETWORK_FDSet nrs;
329 struct GNUNET_NETWORK_FDSet nws;
334 unsigned MHD_LONG_LONG timeout;
335 struct GNUNET_TIME_Relative delay;
337 GNUNET_assert (NULL == mhd_task_id);
342 GNUNET_assert (MHD_YES == MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
343 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
345 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
346 (unsigned int) timeout);
348 delay = GNUNET_TIME_UNIT_FOREVER_REL;
349 GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max_fd + 1);
350 GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max_fd + 1);
352 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
353 &nrs, &nws, &mhd_task, NULL);
358 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
359 struct GNUNET_TESTING_Peer *peer)
366 vpn = GNUNET_VPN_connect (cfg);
367 GNUNET_assert (NULL != vpn);
368 flags = MHD_USE_DEBUG;
369 if (AF_INET6 == dest_af)
370 flags |= MHD_USE_IPv6;
372 MHD_start_daemon (flags, PORT, NULL, NULL, &mhd_ahc, NULL,
376 GNUNET_assert (NULL != mhd);
382 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
386 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
392 rr = GNUNET_VPN_redirect_to_ip (vpn, src_af, dest_af, addr,
393 GNUNET_TIME_UNIT_FOREVER_ABS, &allocation_cb,
396 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &ctrl_c_shutdown, NULL);
401 main (int argc, char *const *argv)
410 if (0 != ACCESS ("/dev/net/tun", R_OK))
412 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access",
415 "WARNING: System unable to run test, skipping.\n");
419 vpn_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
420 exit_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
421 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!
422 (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
424 GNUNET_free (vpn_binary);
425 GNUNET_free (exit_binary);
427 "WARNING: gnunet-helper-{exit,vpn} binaries are not SUID, refusing to run test (as it would have to fail). %d\n", ret);
431 GNUNET_free (vpn_binary);
432 GNUNET_free (exit_binary);
434 if (NULL != strstr (bin, "lt-"))
435 bin = strstr (bin, "lt-") + 4;
436 type = strstr (bin, "-");
440 "invalid binary name\n");
444 /* on Windows, .exe is suffixed to these binaries,
445 * thus cease comparison after the 6th char.
447 if (0 == strncmp (type, "4_to_6",6))
449 dest_ip = "FC5A:04E1:C2BA::1";
453 else if (0 == strncmp (type, "6_to_4",6))
455 dest_ip = "169.254.86.1";
459 else if (0 == strncmp (type, "4_over",6))
461 dest_ip = "169.254.86.1";
465 else if (0 == strncmp (type, "6_over",6))
467 dest_ip = "FC5A:04E1:C2BA::1";
473 fprintf (stderr, "invalid binary suffix `%s'\n", type);
476 if ((GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
477 (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)))
480 "Required address families not supported by this system, skipping test.\n");
483 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
485 fprintf (stderr, "failed to initialize curl\n");
489 GNUNET_TESTING_peer_run ("test-gnunet-vpn", "test_gnunet_vpn.conf", &run,
492 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
496 /* end of test_gnunet_vpn.c */