2 This file is part of GNUnet
3 Copyright (C) 2007, 2009, 2011, 2012, 2015 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file test_gns_vpn.c
23 * @brief testcase for accessing VPN services via GNS
24 * @author Martin Schanzenbach
27 #include <curl/curl.h>
28 #include <microhttpd.h>
29 #include "gnunet_identity_service.h"
30 #include "gnunet_namestore_service.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_gns_service.h"
33 #include "gnunet_testing_lib.h"
36 #define TEST_DOMAIN "www.gnu"
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
41 * Return value for #main().
43 static int global_ret;
45 static struct GNUNET_NAMESTORE_Handle *namestore;
47 static struct MHD_Daemon *mhd;
49 static struct GNUNET_SCHEDULER_Task *mhd_task_id;
51 static struct GNUNET_SCHEDULER_Task *curl_task_id;
53 static struct GNUNET_IDENTITY_Handle *identity;
55 static struct GNUNET_NAMESTORE_QueueEntry *qe;
63 static struct GNUNET_PeerIdentity id;
66 * IP address of the ultimate destination.
68 static const char *dest_ip;
71 * Address family of the dest_ip.
76 * Address family to use by the curl client.
89 static struct CBC cbc;
93 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
95 struct CBC *cbc = ctx;
97 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
98 return 0; /* overflow */
99 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
100 cbc->pos += size * nmemb;
107 struct MHD_Connection *connection,
111 const char *upload_data, size_t *upload_data_size,
115 struct MHD_Response *response;
118 if (0 != strcmp ("GET", method))
119 return MHD_NO; /* unexpected method */
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
127 response = MHD_create_response_from_buffer (strlen (url),
129 MHD_RESPMEM_MUST_COPY);
130 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
131 MHD_destroy_response (response);
139 do_shutdown (void *cls,
140 const struct GNUNET_SCHEDULER_TaskContext *c)
142 if (mhd_task_id != NULL)
144 GNUNET_SCHEDULER_cancel (mhd_task_id);
147 if (curl_task_id != NULL)
149 GNUNET_SCHEDULER_cancel (curl_task_id);
154 MHD_stop_daemon (mhd);
157 if (NULL != identity)
159 GNUNET_IDENTITY_disconnect (identity);
164 GNUNET_NAMESTORE_cancel (qe);
167 GNUNET_free_non_null (url);
173 * Function to run the HTTP client.
180 curl_task (void *cls,
181 const struct GNUNET_SCHEDULER_TaskContext *tc)
195 struct GNUNET_NETWORK_FDSet nrs;
196 struct GNUNET_NETWORK_FDSet nws;
197 struct GNUNET_TIME_Relative delay;
206 curl_multi_perform (multi, &running);
209 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
210 if (msg->msg == CURLMSG_DONE)
212 if (msg->data.result != CURLE_OK)
215 "%s failed at %s:%d: `%s'\n",
216 "curl_multi_perform",
218 __LINE__, curl_easy_strerror (msg->data.result));
222 curl_multi_remove_handle (multi, curl);
223 curl_multi_cleanup (multi);
224 curl_easy_cleanup (curl);
227 if (cbc.pos != strlen ("/hello_world"))
232 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
238 GNUNET_SCHEDULER_shutdown ();
241 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
242 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
244 delay = GNUNET_TIME_UNIT_SECONDS;
246 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
247 GNUNET_NETWORK_fdset_copy_native (&nrs,
250 GNUNET_NETWORK_fdset_copy_native (&nws,
253 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
263 start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
265 GNUNET_asprintf (&url,
266 "http://%s/hello_world",
268 curl = curl_easy_init ();
269 curl_easy_setopt (curl, CURLOPT_URL, url);
270 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
271 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
272 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
273 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
274 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 150L);
275 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
277 multi = curl_multi_init ();
278 GNUNET_assert (multi != NULL);
279 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Beginning HTTP download from `%s'\n",
288 disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 GNUNET_NAMESTORE_disconnect (namestore);
296 * Callback invoked from the namestore service once record is
300 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
301 * will match 'result_af' from the request
302 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
303 * that the VPN allocated for the redirection;
304 * traffic to this IP will now be redirected to the
305 * specified target peer; NULL on error
308 commence_testing (void *cls, int32_t success, const char *emsg)
310 GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
312 if ((emsg != NULL) && (GNUNET_YES != success))
315 "NS failed to create record %s\n",
317 GNUNET_SCHEDULER_shutdown ();
320 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
321 (GNUNET_TIME_UNIT_SECONDS, 10),
328 * Function to keep the HTTP server running.
336 const struct GNUNET_SCHEDULER_TaskContext *tc)
347 struct GNUNET_NETWORK_FDSet nrs;
348 struct GNUNET_NETWORK_FDSet nws;
353 unsigned MHD_LONG_LONG timeout;
354 struct GNUNET_TIME_Relative delay;
356 GNUNET_assert (NULL == mhd_task_id);
361 GNUNET_assert (MHD_YES ==
362 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
363 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
364 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
365 (unsigned int) timeout);
367 delay = GNUNET_TIME_UNIT_FOREVER_REL;
368 GNUNET_NETWORK_fdset_copy_native (&nrs,
371 GNUNET_NETWORK_fdset_copy_native (&nws,
374 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
386 * Open '/dev/null' and make the result the given
389 * @param target_fd desired FD to point to /dev/null
390 * @param flags open flags (O_RDONLY, O_WRONLY)
393 open_dev_null (int target_fd,
398 fd = open ("/dev/null", flags);
403 if (-1 == dup2 (fd, target_fd))
413 * Run the given command and wait for it to complete.
415 * @param file name of the binary to run
416 * @param cmd command line arguments (as given to 'execv')
417 * @return 0 on success, 1 on any error
420 fork_and_exec (const char *file,
437 /* we are the child process */
438 /* close stdin/stdout to not cause interference
439 with the helper's main protocol! */
441 open_dev_null (0, O_RDONLY);
443 open_dev_null (1, O_WRONLY);
444 (void) execv (file, cmd);
445 /* can only get here on error */
447 "exec `%s' failed: %s\n",
452 /* keep running waitpid as long as the only error we get is 'EINTR' */
453 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
458 "waitpid failed: %s\n",
462 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
464 /* child process completed and returned success, we're happy */
471 * Method called to inform about the egos of this peer.
473 * When used with #GNUNET_IDENTITY_connect, this function is
474 * initially called for all egos and then again whenever a
475 * ego's name changes or if it is deleted. At the end of
476 * the initial pass over all egos, the function is once called
477 * with 'NULL' for @a ego. That does NOT mean that the callback won't
478 * be invoked in the future or that there was an error.
480 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
481 * this function is only called ONCE, and 'NULL' being passed in
482 * @a ego does indicate an error (i.e. name is taken or no default
483 * value is known). If @a ego is non-NULL and if '*ctx'
484 * is set in those callbacks, the value WILL be passed to a subsequent
485 * call to the identity callback of #GNUNET_IDENTITY_connect (if
486 * that one was not NULL).
488 * When an identity is renamed, this function is called with the
489 * (known) @a ego but the NEW @a name.
491 * When an identity is deleted, this function is called with the
492 * (known) ego and "NULL" for the @a name. In this case,
493 * the @a ego is henceforth invalid (and the @a ctx should also be
497 * @param ego ego handle
498 * @param ctx context for application to store data for this ego
499 * (during the lifetime of this process, initially NULL)
500 * @param name name assigned by the user for this ego,
501 * NULL if the user just deleted the ego and it
502 * must thus no longer be used
505 identity_cb (void *cls,
506 struct GNUNET_IDENTITY_Ego *ego,
510 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key;
511 struct GNUNET_GNSRECORD_Data rd;
522 "Failed to find master-zone ego\n");
523 GNUNET_SCHEDULER_shutdown ();
526 GNUNET_IDENTITY_disconnect (identity);
530 GNUNET_assert (NULL != name);
531 if (0 != strcmp (name,
535 "Unexpected name %s\n",
539 zone_key = GNUNET_IDENTITY_ego_get_private_key (ego);
540 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
541 peername = GNUNET_strdup (GNUNET_i2s_full (&id));
542 GNUNET_asprintf (&rd_string,
546 GNUNET_free (peername);
547 GNUNET_assert (GNUNET_OK ==
548 GNUNET_GNSRECORD_string_to_value (GNUNET_GNSRECORD_TYPE_VPN,
552 rd.record_type = GNUNET_GNSRECORD_TYPE_VPN;
554 qe = GNUNET_NAMESTORE_records_store (namestore,
560 GNUNET_free ((void**)rd.data);
561 GNUNET_free (rd_string);
567 const struct GNUNET_CONFIGURATION_Handle *cfg,
568 struct GNUNET_TESTING_Peer *peer)
577 GNUNET_CONFIGURATION_get_value_string (cfg,
583 "Failed to locate configuration file. Skipping test.\n");
584 GNUNET_SCHEDULER_shutdown ();
588 char *const identity_args[] =
595 char *const identity2_args[] =
603 char *const identity3_args[] =
607 "-s", "gns-intercept",
611 GNUNET_TESTING_peer_get_identity (peer, &id);
612 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
615 bin = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
616 GNUNET_asprintf (&bin_identity,
621 if (0 != fork_and_exec (bin_identity, identity_args))
624 "Failed to run `gnunet-identity -C. Skipping test.\n");
625 GNUNET_SCHEDULER_shutdown ();
626 GNUNET_free (bin_identity);
627 GNUNET_free (config);
630 if (0 != fork_and_exec (bin_identity, identity2_args))
633 "Failed to run `gnunet-identity -e. Skipping test.\n");
634 GNUNET_SCHEDULER_shutdown ();
635 GNUNET_free (bin_identity);
636 GNUNET_free (config);
639 if (0 != fork_and_exec (bin_identity, identity3_args))
642 "Failed to run `gnunet-identity -e. Skipping test.\n");
643 GNUNET_SCHEDULER_shutdown ();
644 GNUNET_free (bin_identity);
645 GNUNET_free (config);
648 GNUNET_free (bin_identity);
649 GNUNET_free (config);
651 namestore = GNUNET_NAMESTORE_connect (cfg);
652 GNUNET_assert (NULL != namestore);
653 flags = MHD_USE_DEBUG;
654 if (GNUNET_YES == use_v6)
655 flags |= MHD_USE_IPv6;
656 mhd = MHD_start_daemon (flags,
661 GNUNET_assert (NULL != mhd);
664 identity = GNUNET_IDENTITY_connect (cfg,
671 main (int argc, char *const *argv)
677 char *const iptables_args[] =
679 "iptables", "-t", "mangle", "-L", "-v", NULL
682 if (0 == access ("/sbin/iptables", X_OK))
683 sbin_iptables = "/sbin/iptables";
684 else if (0 == access ("/usr/sbin/iptables", X_OK))
685 sbin_iptables = "/usr/sbin/iptables";
689 "Executable iptables not found in approved directories: %s, skipping\n",
694 if (0 != fork_and_exec (sbin_iptables, iptables_args))
697 "Failed to run `iptables -t mangle -L -v'. Skipping test.\n");
701 if (0 != ACCESS ("/dev/net/tun", R_OK))
703 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
707 "WARNING: System unable to run test, skipping.\n");
711 bin_vpn = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
712 bin_exit = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
713 bin_dns = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
714 if ( (0 != geteuid ()) &&
716 GNUNET_OS_check_helper_binary (bin_vpn, GNUNET_YES, "-d gnunet-vpn - - 169.1.3.3.7 255.255.255.0")) || //ipv4 only please!
718 GNUNET_OS_check_helper_binary (bin_exit, GNUNET_YES, "-d gnunet-vpn - - - 169.1.3.3.7 255.255.255.0")) || //no nat, ipv4 only
720 GNUNET_OS_check_helper_binary (bin_dns, GNUNET_YES, NULL))) ) // TODO: once we have a windows-testcase, add test parameters here
723 "WARNING: gnunet-helper-{exit,vpn,dns} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
725 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
726 GNUNET_free (bin_vpn);
727 GNUNET_free (bin_exit);
728 GNUNET_free (bin_dns);
731 GNUNET_free (bin_vpn);
732 GNUNET_free (bin_exit);
733 GNUNET_free (bin_dns);
735 dest_ip = "169.254.86.1";
739 if (GNUNET_OK == GNUNET_NETWORK_test_pf (PF_INET6))
744 if ( (GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
745 (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)) )
748 "Required address families not supported by this system, skipping test.\n");
751 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
753 fprintf (stderr, "failed to initialize curl\n");
758 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-vpn",
762 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
766 /* end of test_gns_vpn.c */