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.
17 * @file test_gnunet_vpn.c
18 * @brief testcase for tunneling HTTP over the GNUnet VPN
19 * @author Christian Grothoff
23 #include <curl/curl.h>
24 #elif HAVE_GNURL_CURL_H
25 #include <gnurl/curl.h>
27 #include <microhttpd.h>
28 #include "gnunet_vpn_service.h"
29 #include "gnunet_testing_lib.h"
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
37 * Return value for 'main'.
39 static int global_ret;
41 static struct GNUNET_VPN_Handle *vpn;
43 static struct MHD_Daemon *mhd;
45 static struct GNUNET_SCHEDULER_Task *mhd_task_id;
47 static struct GNUNET_SCHEDULER_Task *curl_task_id;
49 static struct GNUNET_SCHEDULER_Task *timeout_task_id;
51 static struct GNUNET_VPN_RedirectionRequest *rr;
60 * IP address of the ultimate destination.
62 static const char *dest_ip;
65 * Address family of the dest_ip.
70 * Address family to use by the curl client.
81 static struct CBC cbc;
85 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
87 struct CBC *cbc = ctx;
89 if (cbc->pos + size * nmemb > sizeof (cbc->buf))
90 return 0; /* overflow */
91 GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
92 cbc->pos += size * nmemb;
99 struct MHD_Connection *connection,
103 const char *upload_data,
104 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,
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);
133 do_shutdown (void *cls)
135 if (NULL != mhd_task_id)
137 GNUNET_SCHEDULER_cancel (mhd_task_id);
140 if (NULL != curl_task_id)
142 GNUNET_SCHEDULER_cancel (curl_task_id);
145 if (NULL != timeout_task_id)
147 GNUNET_SCHEDULER_cancel (timeout_task_id);
148 timeout_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)
180 struct GNUNET_NETWORK_FDSet nrs;
181 struct GNUNET_NETWORK_FDSet nws;
182 struct GNUNET_TIME_Relative delay;
192 curl_multi_perform (multi, &running);
195 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
196 if (msg->msg == CURLMSG_DONE)
198 if (msg->data.result != CURLE_OK)
200 fprintf (stderr, "%s failed at %s:%d: `%s'\n", "curl_multi_perform",
201 __FILE__, __LINE__, curl_easy_strerror (msg->data.result));
205 curl_multi_remove_handle (multi, curl);
206 curl_multi_cleanup (multi);
207 curl_easy_cleanup (curl);
210 if (cbc.pos != strlen ("/hello_world"))
215 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
219 "You might want to check if your host-based firewall is blocking the connections.\n");
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
223 GNUNET_SCHEDULER_shutdown ();
226 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
227 if ((CURLM_OK != curl_multi_timeout (multi, &timeout)) || (-1 == timeout))
228 delay = GNUNET_TIME_UNIT_SECONDS;
231 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
232 (unsigned int) timeout);
233 GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max + 1);
234 GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max + 1);
236 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
237 &nrs, &nws, &curl_main, NULL);
242 * Callback invoked from the VPN service once a redirection is
243 * available. Provides the IP address that can now be used to
244 * reach the requested destination (in our case, the MHD server)
247 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
248 * will match 'result_af' from the request
249 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
250 * that the VPN allocated for the redirection;
251 * traffic to this IP will now be redirected to the
252 * specified target peer; NULL on error
255 allocation_cb (void *cls, int af, const void *address)
257 char ips[INET6_ADDRSTRLEN];
263 "VPN failed to allocate appropriate address\n");
264 GNUNET_SCHEDULER_shutdown ();
268 GNUNET_asprintf (&url,
269 "http://[%s]:%u/hello_world",
274 (unsigned int) PORT);
276 GNUNET_asprintf (&url,
277 "http://%s:%u/hello_world",
282 (unsigned int) PORT);
283 curl = curl_easy_init ();
284 curl_easy_setopt (curl, CURLOPT_URL, url);
285 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
286 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
287 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
288 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
289 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
290 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
291 curl_easy_setopt (curl, CURLOPT_VERBOSE, 0);
293 multi = curl_multi_init ();
294 GNUNET_assert (multi != NULL);
295 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "Beginning HTTP download from `%s'\n",
299 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
306 * Function to keep the HTTP server running.
322 do_timeout (void *cls)
324 timeout_task_id = NULL;
325 GNUNET_SCHEDULER_shutdown ();
334 struct GNUNET_NETWORK_FDSet nrs;
335 struct GNUNET_NETWORK_FDSet nws;
340 unsigned MHD_LONG_LONG timeout;
341 struct GNUNET_TIME_Relative delay;
343 GNUNET_assert (NULL == mhd_task_id);
348 GNUNET_assert (MHD_YES == MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
349 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
351 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
352 (unsigned int) timeout);
354 delay = GNUNET_TIME_UNIT_FOREVER_REL;
355 GNUNET_NETWORK_fdset_copy_native (&nrs, &rs, max_fd + 1);
356 GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max_fd + 1);
358 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
359 &nrs, &nws, &mhd_task, NULL);
365 const struct GNUNET_CONFIGURATION_Handle *cfg,
366 struct GNUNET_TESTING_Peer *peer)
373 vpn = GNUNET_VPN_connect (cfg);
374 GNUNET_assert (NULL != vpn);
375 flags = MHD_USE_DEBUG;
376 if (AF_INET6 == dest_af)
377 flags |= MHD_USE_IPv6;
379 MHD_start_daemon (flags, PORT, NULL, NULL, &mhd_ahc, NULL,
383 GNUNET_assert (NULL != mhd);
389 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
393 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
399 rr = GNUNET_VPN_redirect_to_ip (vpn, src_af, dest_af, addr,
400 GNUNET_TIME_UNIT_FOREVER_ABS, &allocation_cb,
403 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
406 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
412 main (int argc, char *const *argv)
421 if (0 != ACCESS ("/dev/net/tun", R_OK))
423 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
427 "WARNING: System unable to run test, skipping.\n");
431 vpn_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
432 exit_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
433 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!
434 (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
436 GNUNET_free (vpn_binary);
437 GNUNET_free (exit_binary);
439 "WARNING: gnunet-helper-{exit,vpn} binaries are not SUID, refusing to run test (as it would have to fail). %d\n", ret);
443 GNUNET_free (vpn_binary);
444 GNUNET_free (exit_binary);
446 if (NULL != strstr (bin, "lt-"))
447 bin = strstr (bin, "lt-") + 4;
448 type = strstr (bin, "-");
452 "invalid binary name\n");
456 /* on Windows, .exe is suffixed to these binaries,
457 * thus cease comparison after the 6th char.
459 if (0 == strncmp (type, "4_to_6",6))
461 dest_ip = "FC5A:04E1:C2BA::1";
465 else if (0 == strncmp (type, "6_to_4",6))
467 dest_ip = "169.254.86.1";
471 else if (0 == strncmp (type, "4_over",6))
473 dest_ip = "169.254.86.1";
477 else if (0 == strncmp (type, "6_over",6))
479 dest_ip = "FC5A:04E1:C2BA::1";
485 fprintf (stderr, "invalid binary suffix `%s'\n", type);
488 if ((GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
489 (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)))
492 "Required address families not supported by this system, skipping test.\n");
495 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
497 fprintf (stderr, "failed to initialize curl\n");
501 GNUNET_TESTING_peer_run ("test-gnunet-vpn", "test_gnunet_vpn.conf", &run,
504 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
508 /* end of test_gnunet_vpn.c */