2 This file is part of GNUnet
3 Copyright (C) 2007, 2009, 2011, 2012, 2015, 2017 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_gns_vpn.c
23 * @brief testcase for accessing VPN services via GNS
24 * @author Martin Schanzenbach
25 * @author Christian Grothoff
27 * This test requires libcurl/libgnurl *with* support for C-ARES.
28 * This is NOT the default on most platforms, which means the test
29 * will be skipped in many cases. Compile libcurl/libgnurl with
30 * "--enable-ares" to get this test to pass.
32 * Furthermore, the test relies on gnunet-dns2gns being able to bind
33 * to port 53. This means that 'setcap' has to have worked during
34 * 'make install'. If this failed, but everything else is OK, the
35 * test may FAIL hard even though it is just an installation issue (we
36 * cannot conveniently test for the setcap to have worked). However,
37 * you should get a warning that gnunet-dns2gns failed to 'bind'.
40 /* Just included for the right curl.h */
41 #include "gnunet_curl_lib.h"
42 #include <microhttpd.h>
43 #include "gnunet_identity_service.h"
44 #include "gnunet_namestore_service.h"
45 #include "gnunet_gnsrecord_lib.h"
46 #include "gnunet_gns_service.h"
47 #include "gnunet_testing_lib.h"
50 #define TEST_DOMAIN "www.gnu"
52 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
55 * Return value for #main().
57 static int global_ret;
59 static struct GNUNET_NAMESTORE_Handle *namestore;
61 static struct MHD_Daemon *mhd;
63 static struct GNUNET_SCHEDULER_Task *mhd_task_id;
65 static struct GNUNET_SCHEDULER_Task *curl_task_id;
67 static struct GNUNET_SCHEDULER_Task *timeout_task;
69 static struct GNUNET_IDENTITY_Handle *identity;
71 static struct GNUNET_NAMESTORE_QueueEntry *qe;
79 static struct GNUNET_PeerIdentity id;
82 * IP address of the ultimate destination.
84 static const char *dest_ip;
87 * Address family of the dest_ip.
92 * Address family to use by the curl client.
105 static struct CBC cbc;
109 copy_buffer (void *ptr,
114 struct CBC *cbc = ctx;
116 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
117 return 0; /* overflow */
118 GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
119 cbc->pos += size * nmemb;
126 struct MHD_Connection *connection,
130 const char *upload_data, size_t *upload_data_size,
134 struct MHD_Response *response;
137 if (0 != strcmp ("GET", method))
138 return MHD_NO; /* unexpected method */
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
146 response = MHD_create_response_from_buffer (strlen (url),
148 MHD_RESPMEM_MUST_COPY);
149 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
150 MHD_destroy_response (response);
158 do_shutdown (void *cls)
160 if (NULL != mhd_task_id)
162 GNUNET_SCHEDULER_cancel (mhd_task_id);
165 if (NULL != curl_task_id)
167 GNUNET_SCHEDULER_cancel (curl_task_id);
170 if (NULL != timeout_task)
172 GNUNET_SCHEDULER_cancel (timeout_task);
177 MHD_stop_daemon (mhd);
180 if (NULL != identity)
182 GNUNET_IDENTITY_disconnect (identity);
187 GNUNET_NAMESTORE_cancel (qe);
190 if (NULL != namestore)
192 GNUNET_NAMESTORE_disconnect (namestore);
195 GNUNET_free_non_null (url);
201 do_timeout (void *cls)
204 GNUNET_SCHEDULER_shutdown ();
209 * Function to run the HTTP client.
216 curl_task (void *cls)
230 struct GNUNET_NETWORK_FDSet nrs;
231 struct GNUNET_NETWORK_FDSet nws;
232 struct GNUNET_TIME_Relative delay;
241 curl_multi_perform (multi, &running);
244 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
245 if (msg->msg == CURLMSG_DONE)
247 if (msg->data.result != CURLE_OK)
250 "%s failed at %s:%d: `%s'\n",
251 "curl_multi_perform",
253 __LINE__, curl_easy_strerror (msg->data.result));
257 curl_multi_remove_handle (multi, curl);
258 curl_multi_cleanup (multi);
259 curl_easy_cleanup (curl);
262 if (cbc.pos != strlen ("/hello_world"))
267 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Download complete, shutting down!\n");
274 GNUNET_SCHEDULER_shutdown ();
277 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
278 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
280 delay = GNUNET_TIME_UNIT_SECONDS;
282 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
283 GNUNET_NETWORK_fdset_copy_native (&nrs,
286 GNUNET_NETWORK_fdset_copy_native (&nws,
289 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
299 start_curl (void *cls)
304 GNUNET_asprintf (&url,
305 "http://%s/hello_world",
307 curl = curl_easy_init ();
308 curl_easy_setopt (curl, CURLOPT_URL, url);
309 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
310 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
311 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
312 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
313 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 150L);
314 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
316 (ec = curl_easy_setopt (curl,
320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
321 "curl build without support for CURLOPT_DNS_SERVERS (%s), cannot run test\n",
322 curl_easy_strerror (ec));
324 GNUNET_SCHEDULER_shutdown ();
327 multi = curl_multi_init ();
328 GNUNET_assert (multi != NULL);
329 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Beginning HTTP download from `%s'\n",
338 * Callback invoked from the namestore service once record is
342 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
343 * will match 'result_af' from the request
344 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
345 * that the VPN allocated for the redirection;
346 * traffic to this IP will now be redirected to the
347 * specified target peer; NULL on error
350 commence_testing (void *cls,
355 if ( (NULL != emsg) &&
356 (GNUNET_YES != success) )
359 "NS failed to create record %s\n",
361 GNUNET_SCHEDULER_shutdown ();
365 /* wait a little bit before downloading, as we just created the record */
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Launching cURL request\n");
369 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
376 * Function to keep the HTTP server running.
394 struct GNUNET_NETWORK_FDSet nrs;
395 struct GNUNET_NETWORK_FDSet nws;
400 unsigned MHD_LONG_LONG timeout;
401 struct GNUNET_TIME_Relative delay;
403 GNUNET_assert (NULL == mhd_task_id);
408 GNUNET_assert (MHD_YES ==
409 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
410 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
411 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
412 (unsigned int) timeout);
414 delay = GNUNET_TIME_UNIT_FOREVER_REL;
415 GNUNET_NETWORK_fdset_copy_native (&nrs,
418 GNUNET_NETWORK_fdset_copy_native (&nws,
421 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
432 * Open '/dev/null' and make the result the given
435 * @param target_fd desired FD to point to /dev/null
436 * @param flags open flags (O_RDONLY, O_WRONLY)
439 open_dev_null (int target_fd,
444 fd = open ("/dev/null", flags);
449 if (-1 == dup2 (fd, target_fd))
459 * Run the given command and wait for it to complete.
461 * @param file name of the binary to run
462 * @param cmd command line arguments (as given to 'execv')
463 * @return 0 on success, 1 on any error
466 fork_and_exec (const char *file,
476 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
482 /* we are the child process */
483 /* close stdin/stdout to not cause interference
484 with the helper's main protocol! */
486 open_dev_null (0, O_RDONLY);
488 open_dev_null (1, O_WRONLY);
489 (void) execv (file, cmd);
490 /* can only get here on error */
491 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
496 /* keep running waitpid as long as the only error we get is 'EINTR' */
497 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
501 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
505 if (! (WIFEXITED (status) &&
506 (0 == WEXITSTATUS (status))) )
508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
509 "Process `%s` returned status code %d/%d.\n",
512 WEXITSTATUS (status));
515 /* child process completed and returned success, we're happy */
522 * Method called to inform about the egos of this peer.
524 * When used with #GNUNET_IDENTITY_connect, this function is
525 * initially called for all egos and then again whenever a
526 * ego's name changes or if it is deleted. At the end of
527 * the initial pass over all egos, the function is once called
528 * with 'NULL' for @a ego. That does NOT mean that the callback won't
529 * be invoked in the future or that there was an error.
531 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
532 * this function is only called ONCE, and 'NULL' being passed in
533 * @a ego does indicate an error (i.e. name is taken or no default
534 * value is known). If @a ego is non-NULL and if '*ctx'
535 * is set in those callbacks, the value WILL be passed to a subsequent
536 * call to the identity callback of #GNUNET_IDENTITY_connect (if
537 * that one was not NULL).
539 * When an identity is renamed, this function is called with the
540 * (known) @a ego but the NEW @a name.
542 * When an identity is deleted, this function is called with the
543 * (known) ego and "NULL" for the @a name. In this case,
544 * the @a ego is henceforth invalid (and the @a ctx should also be
548 * @param ego ego handle
549 * @param ctx context for application to store data for this ego
550 * (during the lifetime of this process, initially NULL)
551 * @param name name assigned by the user for this ego,
552 * NULL if the user just deleted the ego and it
553 * must thus no longer be used
556 identity_cb (void *cls,
557 struct GNUNET_IDENTITY_Ego *ego,
561 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key;
562 struct GNUNET_GNSRECORD_Data rd;
573 "Failed to find master-zone ego\n");
574 GNUNET_SCHEDULER_shutdown ();
577 GNUNET_IDENTITY_disconnect (identity);
581 GNUNET_assert (NULL != name);
582 if (0 != strcmp (name,
586 "Unexpected name %s\n",
590 zone_key = GNUNET_IDENTITY_ego_get_private_key (ego);
591 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
592 peername = GNUNET_strdup (GNUNET_i2s_full (&id));
593 GNUNET_asprintf (&rd_string,
597 GNUNET_free (peername);
598 GNUNET_assert (GNUNET_OK ==
599 GNUNET_GNSRECORD_string_to_value (GNUNET_GNSRECORD_TYPE_VPN,
603 rd.record_type = GNUNET_GNSRECORD_TYPE_VPN;
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Creating `www` record\n");
607 qe = GNUNET_NAMESTORE_records_store (namestore,
613 GNUNET_free ((void**)rd.data);
614 GNUNET_free (rd_string);
620 const struct GNUNET_CONFIGURATION_Handle *cfg,
621 struct GNUNET_TESTING_Peer *peer)
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Test logic starting...\n");
634 GNUNET_CONFIGURATION_get_value_string (cfg,
639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
640 "Failed to locate configuration file. Skipping test.\n");
641 GNUNET_SCHEDULER_shutdown ();
645 char *const identity_args[] =
652 char *const identity2_args[] =
660 char *const identity3_args[] =
668 char *const arm_args[] =
675 char *const gns_args[] =
683 GNUNET_TESTING_peer_get_identity (peer,
685 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
687 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
690 bin = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
691 GNUNET_asprintf (&bin_identity,
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "Creating `master-zone` ego\n");
697 if (0 != fork_and_exec (bin_identity, identity_args))
699 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
700 "Failed to run `gnunet-identity -C`. Skipping test.\n");
701 GNUNET_SCHEDULER_shutdown ();
702 GNUNET_free (bin_identity);
703 GNUNET_free (config);
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Setting `master-zone` ego as default for `gns-master` and `dns2gns`\n");
709 if (0 != fork_and_exec (bin_identity, identity2_args))
711 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
712 "Failed to run `gnunet-identity -e`. Skipping test.\n");
713 GNUNET_SCHEDULER_shutdown ();
714 GNUNET_free (bin_identity);
715 GNUNET_free (config);
719 if (0 != fork_and_exec (bin_identity, identity3_args))
721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
722 "Failed to run `gnunet-identity -e`. Skipping test.\n");
723 GNUNET_SCHEDULER_shutdown ();
724 GNUNET_free (bin_identity);
725 GNUNET_free (config);
729 GNUNET_free (bin_identity);
731 /* do lookup just to launch GNS service */
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Resolving `www.gnu` zone entry to launch GNS (will yield no answer yet)\n");
734 GNUNET_asprintf (&bin_gns,
738 if (0 != fork_and_exec (bin_gns,
741 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
742 "Failed to run `gnunet-gns -u. Skipping test.\n");
743 GNUNET_SCHEDULER_shutdown ();
744 GNUNET_free (bin_gns);
745 GNUNET_free (config);
749 GNUNET_free (bin_gns);
751 GNUNET_asprintf (&bin_arm,
755 if (0 != fork_and_exec (bin_arm,
758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
759 "Failed to run `gnunet-arm -i dns2gns. Skipping test.\n");
760 GNUNET_SCHEDULER_shutdown ();
761 GNUNET_free (bin_arm);
762 GNUNET_free (config);
766 GNUNET_free (bin_arm);
768 GNUNET_free (config);
770 sleep (1); /* give dns2gns chance to really run */
772 namestore = GNUNET_NAMESTORE_connect (cfg);
773 GNUNET_assert (NULL != namestore);
774 flags = MHD_USE_DEBUG;
775 if (GNUNET_YES == use_v6)
776 flags |= MHD_USE_DUAL_STACK;
777 mhd = MHD_start_daemon (flags,
782 GNUNET_assert (NULL != mhd);
785 identity = GNUNET_IDENTITY_connect (cfg,
798 GNUNET_log_setup ("test-gns-vpn",
801 if (0 != ACCESS ("/dev/net/tun", R_OK))
803 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
807 "WARNING: System unable to run test, skipping.\n");
811 bin_vpn = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
812 bin_exit = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
813 if ( (0 != geteuid ()) &&
815 GNUNET_OS_check_helper_binary (bin_vpn,
817 "-d gnunet-vpn - - 169.1.3.3.7 255.255.255.0")) || //ipv4 only please!
819 GNUNET_OS_check_helper_binary (bin_exit,
821 "-d gnunet-vpn - - - 169.1.3.3.7 255.255.255.0")) ) ) //no nat, ipv4 only
824 "WARNING: gnunet-helper-{exit,vpn} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
826 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
827 GNUNET_free (bin_vpn);
828 GNUNET_free (bin_exit);
831 GNUNET_free (bin_vpn);
832 GNUNET_free (bin_exit);
834 dest_ip = "169.254.86.1";
838 if (GNUNET_OK == GNUNET_NETWORK_test_pf (PF_INET6))
843 if ( (GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
844 (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)) )
847 "Required address families not supported by this system, skipping test.\n");
850 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
852 fprintf (stderr, "failed to initialize curl\n");
858 GNUNET_TESTING_peer_run ("test_gns_vpn",
863 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
867 /* end of test_gns_vpn.c */