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/>.
20 * @file test_gnunet_vpn.c
21 * @brief testcase for tunneling HTTP over the GNUnet VPN
22 * @author Christian Grothoff
26 #include <curl/curl.h>
27 #elif HAVE_GNURL_CURL_H
28 #include <gnurl/curl.h>
30 #include <microhttpd.h>
31 #include "gnunet_vpn_service.h"
32 #include "gnunet_testing_lib.h"
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
40 * Return value for 'main'.
42 static int global_ret;
44 static struct GNUNET_VPN_Handle *vpn;
46 static struct MHD_Daemon *mhd;
48 static struct GNUNET_SCHEDULER_Task *mhd_task_id;
50 static struct GNUNET_SCHEDULER_Task *curl_task_id;
52 static struct GNUNET_SCHEDULER_Task *timeout_task_id;
54 static struct GNUNET_VPN_RedirectionRequest *rr;
63 * IP address of the ultimate destination.
65 static const char *dest_ip;
68 * Address family of the dest_ip.
73 * Address family to use by the curl client.
84 static struct CBC cbc;
88 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
90 struct CBC *cbc = ctx;
92 if (cbc->pos + size * nmemb > sizeof (cbc->buf))
93 return 0; /* overflow */
94 GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
95 cbc->pos += size * nmemb;
102 struct MHD_Connection *connection,
106 const char *upload_data,
107 size_t * upload_data_size,
111 struct MHD_Response *response;
114 if (0 != strcmp ("GET", method))
115 return MHD_NO; /* unexpected method */
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 "MHD sends respose for request to URL `%s'\n", url);
125 MHD_create_response_from_buffer (strlen (url), (void *) url,
126 MHD_RESPMEM_MUST_COPY);
127 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
128 MHD_destroy_response (response);
136 do_shutdown (void *cls)
138 if (NULL != mhd_task_id)
140 GNUNET_SCHEDULER_cancel (mhd_task_id);
143 if (NULL != curl_task_id)
145 GNUNET_SCHEDULER_cancel (curl_task_id);
148 if (NULL != timeout_task_id)
150 GNUNET_SCHEDULER_cancel (timeout_task_id);
151 timeout_task_id = NULL;
155 MHD_stop_daemon (mhd);
160 GNUNET_VPN_cancel_request (rr);
165 GNUNET_VPN_disconnect (vpn);
168 GNUNET_free_non_null (url);
174 * Function to run the HTTP client.
177 curl_main (void *cls)
183 struct GNUNET_NETWORK_FDSet nrs;
184 struct GNUNET_NETWORK_FDSet nws;
185 struct GNUNET_TIME_Relative delay;
195 curl_multi_perform (multi, &running);
198 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
199 if (msg->msg == CURLMSG_DONE)
201 if (msg->data.result != CURLE_OK)
203 fprintf (stderr, "%s failed at %s:%d: `%s'\n", "curl_multi_perform",
204 __FILE__, __LINE__, curl_easy_strerror (msg->data.result));
208 curl_multi_remove_handle (multi, curl);
209 curl_multi_cleanup (multi);
210 curl_easy_cleanup (curl);
213 if (cbc.pos != strlen ("/hello_world"))
218 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
221 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
222 "You might want to check if your host-based firewall is blocking the connections.\n");
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
226 GNUNET_SCHEDULER_shutdown ();
229 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
230 if ((CURLM_OK != curl_multi_timeout (multi, &timeout)) || (-1 == timeout))
231 delay = GNUNET_TIME_UNIT_SECONDS;
234 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
235 (unsigned int) timeout);
236 GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max + 1);
237 GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max + 1);
239 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
240 &nrs, &nws, &curl_main, NULL);
245 * Callback invoked from the VPN service once a redirection is
246 * available. Provides the IP address that can now be used to
247 * reach the requested destination (in our case, the MHD server)
250 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
251 * will match 'result_af' from the request
252 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
253 * that the VPN allocated for the redirection;
254 * traffic to this IP will now be redirected to the
255 * specified target peer; NULL on error
258 allocation_cb (void *cls, int af, const void *address)
260 char ips[INET6_ADDRSTRLEN];
266 "VPN failed to allocate appropriate address\n");
267 GNUNET_SCHEDULER_shutdown ();
271 GNUNET_asprintf (&url,
272 "http://[%s]:%u/hello_world",
277 (unsigned int) PORT);
279 GNUNET_asprintf (&url,
280 "http://%s:%u/hello_world",
285 (unsigned int) PORT);
286 curl = curl_easy_init ();
287 curl_easy_setopt (curl, CURLOPT_URL, url);
288 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
289 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
290 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
291 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
292 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
293 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
294 curl_easy_setopt (curl, CURLOPT_VERBOSE, 0);
296 multi = curl_multi_init ();
297 GNUNET_assert (multi != NULL);
298 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Beginning HTTP download from `%s'\n",
302 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
309 * Function to keep the HTTP server running.
325 do_timeout (void *cls)
327 timeout_task_id = NULL;
328 GNUNET_SCHEDULER_shutdown ();
337 struct GNUNET_NETWORK_FDSet nrs;
338 struct GNUNET_NETWORK_FDSet nws;
343 unsigned MHD_LONG_LONG timeout;
344 struct GNUNET_TIME_Relative delay;
346 GNUNET_assert (NULL == mhd_task_id);
351 GNUNET_assert (MHD_YES == MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
352 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
354 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
355 (unsigned int) timeout);
357 delay = GNUNET_TIME_UNIT_FOREVER_REL;
358 GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max_fd + 1);
359 GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max_fd + 1);
361 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
362 &nrs, &nws, &mhd_task, NULL);
368 const struct GNUNET_CONFIGURATION_Handle *cfg,
369 struct GNUNET_TESTING_Peer *peer)
376 vpn = GNUNET_VPN_connect (cfg);
377 GNUNET_assert (NULL != vpn);
378 flags = MHD_USE_DEBUG;
379 if (AF_INET6 == dest_af)
380 flags |= MHD_USE_IPv6;
382 MHD_start_daemon (flags, PORT, NULL, NULL, &mhd_ahc, NULL,
386 GNUNET_assert (NULL != mhd);
392 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
396 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
402 rr = GNUNET_VPN_redirect_to_ip (vpn, src_af, dest_af, addr,
403 GNUNET_TIME_UNIT_FOREVER_ABS, &allocation_cb,
406 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
409 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
415 main (int argc, char *const *argv)
424 if (0 != ACCESS ("/dev/net/tun", R_OK))
426 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
430 "WARNING: System unable to run test, skipping.\n");
434 vpn_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
435 exit_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
436 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!
437 (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
439 GNUNET_free (vpn_binary);
440 GNUNET_free (exit_binary);
442 "WARNING: gnunet-helper-{exit,vpn} binaries are not SUID, refusing to run test (as it would have to fail). %d\n", ret);
446 GNUNET_free (vpn_binary);
447 GNUNET_free (exit_binary);
449 if (NULL != strstr (bin, "lt-"))
450 bin = strstr (bin, "lt-") + 4;
451 type = strstr (bin, "-");
455 "invalid binary name\n");
459 /* on Windows, .exe is suffixed to these binaries,
460 * thus cease comparison after the 6th char.
462 if (0 == strncmp (type, "4_to_6",6))
464 dest_ip = "FC5A:04E1:C2BA::1";
468 else if (0 == strncmp (type, "6_to_4",6))
470 dest_ip = "169.254.86.1";
474 else if (0 == strncmp (type, "4_over",6))
476 dest_ip = "169.254.86.1";
480 else if (0 == strncmp (type, "6_over",6))
482 dest_ip = "FC5A:04E1:C2BA::1";
488 fprintf (stderr, "invalid binary suffix `%s'\n", type);
491 if ((GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
492 (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)))
495 "Required address families not supported by this system, skipping test.\n");
498 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
500 fprintf (stderr, "failed to initialize curl\n");
504 GNUNET_TESTING_peer_run ("test-gnunet-vpn", "test_gnunet_vpn.conf", &run,
507 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
511 /* end of test_gnunet_vpn.c */