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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 *timeout_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 GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
97 cbc->pos += size * nmemb;
104 struct MHD_Connection *connection,
108 const char *upload_data,
109 size_t * upload_data_size,
113 struct MHD_Response *response;
116 if (0 != strcmp ("GET", method))
117 return MHD_NO; /* unexpected method */
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "MHD sends respose for request to URL `%s'\n", url);
127 MHD_create_response_from_buffer (strlen (url), (void *) url,
128 MHD_RESPMEM_MUST_COPY);
129 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
130 MHD_destroy_response (response);
138 do_shutdown (void *cls)
140 if (NULL != mhd_task_id)
142 GNUNET_SCHEDULER_cancel (mhd_task_id);
145 if (NULL != curl_task_id)
147 GNUNET_SCHEDULER_cancel (curl_task_id);
150 if (NULL != timeout_task_id)
152 GNUNET_SCHEDULER_cancel (timeout_task_id);
153 timeout_task_id = NULL;
157 MHD_stop_daemon (mhd);
162 GNUNET_VPN_cancel_request (rr);
167 GNUNET_VPN_disconnect (vpn);
170 GNUNET_free_non_null (url);
176 * Function to run the HTTP client.
179 curl_main (void *cls)
185 struct GNUNET_NETWORK_FDSet nrs;
186 struct GNUNET_NETWORK_FDSet nws;
187 struct GNUNET_TIME_Relative delay;
197 curl_multi_perform (multi, &running);
200 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
201 if (msg->msg == CURLMSG_DONE)
203 if (msg->data.result != CURLE_OK)
205 fprintf (stderr, "%s failed at %s:%d: `%s'\n", "curl_multi_perform",
206 __FILE__, __LINE__, curl_easy_strerror (msg->data.result));
210 curl_multi_remove_handle (multi, curl);
211 curl_multi_cleanup (multi);
212 curl_easy_cleanup (curl);
215 if (cbc.pos != strlen ("/hello_world"))
220 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
224 "You might want to check if your host-based firewall is blocking the connections.\n");
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
228 GNUNET_SCHEDULER_shutdown ();
231 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
232 if ((CURLM_OK != curl_multi_timeout (multi, &timeout)) || (-1 == timeout))
233 delay = GNUNET_TIME_UNIT_SECONDS;
236 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
237 (unsigned int) timeout);
238 GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max + 1);
239 GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max + 1);
241 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
242 &nrs, &nws, &curl_main, NULL);
247 * Callback invoked from the VPN service once a redirection is
248 * available. Provides the IP address that can now be used to
249 * reach the requested destination (in our case, the MHD server)
252 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
253 * will match 'result_af' from the request
254 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
255 * that the VPN allocated for the redirection;
256 * traffic to this IP will now be redirected to the
257 * specified target peer; NULL on error
260 allocation_cb (void *cls, int af, const void *address)
262 char ips[INET6_ADDRSTRLEN];
268 "VPN failed to allocate appropriate address\n");
269 GNUNET_SCHEDULER_shutdown ();
273 GNUNET_asprintf (&url,
274 "http://[%s]:%u/hello_world",
279 (unsigned int) PORT);
281 GNUNET_asprintf (&url,
282 "http://%s:%u/hello_world",
287 (unsigned int) PORT);
288 curl = curl_easy_init ();
289 curl_easy_setopt (curl, CURLOPT_URL, url);
290 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
291 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
292 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
293 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
294 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
295 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
296 curl_easy_setopt (curl, CURLOPT_VERBOSE, 0);
298 multi = curl_multi_init ();
299 GNUNET_assert (multi != NULL);
300 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Beginning HTTP download from `%s'\n",
304 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
311 * Function to keep the HTTP server running.
327 do_timeout (void *cls)
329 timeout_task_id = NULL;
330 GNUNET_SCHEDULER_shutdown ();
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 (NULL == mhd_task_id);
353 GNUNET_assert (MHD_YES == MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
354 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
356 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, &rs, max_fd + 1);
361 GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max_fd + 1);
363 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
364 &nrs, &nws, &mhd_task, NULL);
370 const struct GNUNET_CONFIGURATION_Handle *cfg,
371 struct GNUNET_TESTING_Peer *peer)
378 vpn = GNUNET_VPN_connect (cfg);
379 GNUNET_assert (NULL != vpn);
380 flags = MHD_USE_DEBUG;
381 if (AF_INET6 == dest_af)
382 flags |= MHD_USE_IPv6;
384 MHD_start_daemon (flags, PORT, NULL, NULL, &mhd_ahc, NULL,
388 GNUNET_assert (NULL != mhd);
394 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
398 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
404 rr = GNUNET_VPN_redirect_to_ip (vpn, src_af, dest_af, addr,
405 GNUNET_TIME_UNIT_FOREVER_ABS, &allocation_cb,
408 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
411 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
417 main (int argc, char *const *argv)
426 if (0 != ACCESS ("/dev/net/tun", R_OK))
428 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
432 "WARNING: System unable to run test, skipping.\n");
436 vpn_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
437 exit_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
438 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!
439 (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
441 GNUNET_free (vpn_binary);
442 GNUNET_free (exit_binary);
444 "WARNING: gnunet-helper-{exit,vpn} binaries are not SUID, refusing to run test (as it would have to fail). %d\n", ret);
448 GNUNET_free (vpn_binary);
449 GNUNET_free (exit_binary);
451 if (NULL != strstr (bin, "lt-"))
452 bin = strstr (bin, "lt-") + 4;
453 type = strstr (bin, "-");
457 "invalid binary name\n");
461 /* on Windows, .exe is suffixed to these binaries,
462 * thus cease comparison after the 6th char.
464 if (0 == strncmp (type, "4_to_6",6))
466 dest_ip = "FC5A:04E1:C2BA::1";
470 else if (0 == strncmp (type, "6_to_4",6))
472 dest_ip = "169.254.86.1";
476 else if (0 == strncmp (type, "4_over",6))
478 dest_ip = "169.254.86.1";
482 else if (0 == strncmp (type, "6_over",6))
484 dest_ip = "FC5A:04E1:C2BA::1";
490 fprintf (stderr, "invalid binary suffix `%s'\n", type);
493 if ((GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
494 (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)))
497 "Required address families not supported by this system, skipping test.\n");
500 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
502 fprintf (stderr, "failed to initialize curl\n");
506 GNUNET_TESTING_peer_run ("test-gnunet-vpn", "test_gnunet_vpn.conf", &run,
509 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
513 /* end of test_gnunet_vpn.c */