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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file test_gns_vpn.c
23 * @brief testcase for accessing VPN services via GNS
24 * @author Martin Schanzenbach
28 #include <curl/curl.h>
29 #elif HAVE_GNURL_CURL_H
30 #include <gnurl/curl.h>
32 #include <microhttpd.h>
33 #include "gnunet_identity_service.h"
34 #include "gnunet_namestore_service.h"
35 #include "gnunet_gnsrecord_lib.h"
36 #include "gnunet_gns_service.h"
37 #include "gnunet_testing_lib.h"
40 #define TEST_DOMAIN "www.gnu"
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
45 * Return value for #main().
47 static int global_ret;
49 static struct GNUNET_NAMESTORE_Handle *namestore;
51 static struct MHD_Daemon *mhd;
53 static struct GNUNET_SCHEDULER_Task *mhd_task_id;
55 static struct GNUNET_SCHEDULER_Task *curl_task_id;
57 static struct GNUNET_IDENTITY_Handle *identity;
59 static struct GNUNET_NAMESTORE_QueueEntry *qe;
67 static struct GNUNET_PeerIdentity id;
70 * IP address of the ultimate destination.
72 static const char *dest_ip;
75 * Address family of the dest_ip.
80 * Address family to use by the curl client.
93 static struct CBC cbc;
97 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
99 struct CBC *cbc = ctx;
101 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
102 return 0; /* overflow */
103 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
104 cbc->pos += size * nmemb;
111 struct MHD_Connection *connection,
115 const char *upload_data, size_t *upload_data_size,
119 struct MHD_Response *response;
122 if (0 != strcmp ("GET", method))
123 return MHD_NO; /* unexpected method */
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
131 response = MHD_create_response_from_buffer (strlen (url),
133 MHD_RESPMEM_MUST_COPY);
134 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
135 MHD_destroy_response (response);
143 do_shutdown (void *cls,
144 const struct GNUNET_SCHEDULER_TaskContext *c)
146 if (mhd_task_id != NULL)
148 GNUNET_SCHEDULER_cancel (mhd_task_id);
151 if (curl_task_id != NULL)
153 GNUNET_SCHEDULER_cancel (curl_task_id);
158 MHD_stop_daemon (mhd);
161 if (NULL != identity)
163 GNUNET_IDENTITY_disconnect (identity);
168 GNUNET_NAMESTORE_cancel (qe);
171 GNUNET_free_non_null (url);
177 * Function to run the HTTP client.
184 curl_task (void *cls,
185 const struct GNUNET_SCHEDULER_TaskContext *tc)
199 struct GNUNET_NETWORK_FDSet nrs;
200 struct GNUNET_NETWORK_FDSet nws;
201 struct GNUNET_TIME_Relative delay;
210 curl_multi_perform (multi, &running);
213 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
214 if (msg->msg == CURLMSG_DONE)
216 if (msg->data.result != CURLE_OK)
219 "%s failed at %s:%d: `%s'\n",
220 "curl_multi_perform",
222 __LINE__, curl_easy_strerror (msg->data.result));
226 curl_multi_remove_handle (multi, curl);
227 curl_multi_cleanup (multi);
228 curl_easy_cleanup (curl);
231 if (cbc.pos != strlen ("/hello_world"))
236 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
242 GNUNET_SCHEDULER_shutdown ();
245 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
246 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
248 delay = GNUNET_TIME_UNIT_SECONDS;
250 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
251 GNUNET_NETWORK_fdset_copy_native (&nrs,
254 GNUNET_NETWORK_fdset_copy_native (&nws,
257 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
267 start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
269 GNUNET_asprintf (&url,
270 "http://%s/hello_world",
272 curl = curl_easy_init ();
273 curl_easy_setopt (curl, CURLOPT_URL, url);
274 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
275 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
276 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
277 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
278 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 150L);
279 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
281 multi = curl_multi_init ();
282 GNUNET_assert (multi != NULL);
283 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Beginning HTTP download from `%s'\n",
292 disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
294 GNUNET_NAMESTORE_disconnect (namestore);
300 * Callback invoked from the namestore service once record is
304 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
305 * will match 'result_af' from the request
306 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
307 * that the VPN allocated for the redirection;
308 * traffic to this IP will now be redirected to the
309 * specified target peer; NULL on error
312 commence_testing (void *cls,
317 GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
319 if ((emsg != NULL) && (GNUNET_YES != success))
322 "NS failed to create record %s\n",
324 GNUNET_SCHEDULER_shutdown ();
327 /* wait a little bit before downloading, as we just created the record */
328 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
329 (GNUNET_TIME_UNIT_SECONDS, 1),
336 * Function to keep the HTTP server running.
344 const struct GNUNET_SCHEDULER_TaskContext *tc)
355 struct GNUNET_NETWORK_FDSet nrs;
356 struct GNUNET_NETWORK_FDSet nws;
361 unsigned MHD_LONG_LONG timeout;
362 struct GNUNET_TIME_Relative delay;
364 GNUNET_assert (NULL == mhd_task_id);
369 GNUNET_assert (MHD_YES ==
370 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
371 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
372 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
373 (unsigned int) timeout);
375 delay = GNUNET_TIME_UNIT_FOREVER_REL;
376 GNUNET_NETWORK_fdset_copy_native (&nrs,
379 GNUNET_NETWORK_fdset_copy_native (&nws,
382 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
394 * Open '/dev/null' and make the result the given
397 * @param target_fd desired FD to point to /dev/null
398 * @param flags open flags (O_RDONLY, O_WRONLY)
401 open_dev_null (int target_fd,
406 fd = open ("/dev/null", flags);
411 if (-1 == dup2 (fd, target_fd))
421 * Run the given command and wait for it to complete.
423 * @param file name of the binary to run
424 * @param cmd command line arguments (as given to 'execv')
425 * @return 0 on success, 1 on any error
428 fork_and_exec (const char *file,
445 /* we are the child process */
446 /* close stdin/stdout to not cause interference
447 with the helper's main protocol! */
449 open_dev_null (0, O_RDONLY);
451 open_dev_null (1, O_WRONLY);
452 (void) execv (file, cmd);
453 /* can only get here on error */
455 "exec `%s' failed: %s\n",
460 /* keep running waitpid as long as the only error we get is 'EINTR' */
461 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
466 "waitpid failed: %s\n",
470 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
472 /* child process completed and returned success, we're happy */
479 * Method called to inform about the egos of this peer.
481 * When used with #GNUNET_IDENTITY_connect, this function is
482 * initially called for all egos and then again whenever a
483 * ego's name changes or if it is deleted. At the end of
484 * the initial pass over all egos, the function is once called
485 * with 'NULL' for @a ego. That does NOT mean that the callback won't
486 * be invoked in the future or that there was an error.
488 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
489 * this function is only called ONCE, and 'NULL' being passed in
490 * @a ego does indicate an error (i.e. name is taken or no default
491 * value is known). If @a ego is non-NULL and if '*ctx'
492 * is set in those callbacks, the value WILL be passed to a subsequent
493 * call to the identity callback of #GNUNET_IDENTITY_connect (if
494 * that one was not NULL).
496 * When an identity is renamed, this function is called with the
497 * (known) @a ego but the NEW @a name.
499 * When an identity is deleted, this function is called with the
500 * (known) ego and "NULL" for the @a name. In this case,
501 * the @a ego is henceforth invalid (and the @a ctx should also be
505 * @param ego ego handle
506 * @param ctx context for application to store data for this ego
507 * (during the lifetime of this process, initially NULL)
508 * @param name name assigned by the user for this ego,
509 * NULL if the user just deleted the ego and it
510 * must thus no longer be used
513 identity_cb (void *cls,
514 struct GNUNET_IDENTITY_Ego *ego,
518 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key;
519 struct GNUNET_GNSRECORD_Data rd;
530 "Failed to find master-zone ego\n");
531 GNUNET_SCHEDULER_shutdown ();
534 GNUNET_IDENTITY_disconnect (identity);
538 GNUNET_assert (NULL != name);
539 if (0 != strcmp (name,
543 "Unexpected name %s\n",
547 zone_key = GNUNET_IDENTITY_ego_get_private_key (ego);
548 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
549 peername = GNUNET_strdup (GNUNET_i2s_full (&id));
550 GNUNET_asprintf (&rd_string,
554 GNUNET_free (peername);
555 GNUNET_assert (GNUNET_OK ==
556 GNUNET_GNSRECORD_string_to_value (GNUNET_GNSRECORD_TYPE_VPN,
560 rd.record_type = GNUNET_GNSRECORD_TYPE_VPN;
562 qe = GNUNET_NAMESTORE_records_store (namestore,
568 GNUNET_free ((void**)rd.data);
569 GNUNET_free (rd_string);
575 const struct GNUNET_CONFIGURATION_Handle *cfg,
576 struct GNUNET_TESTING_Peer *peer)
586 GNUNET_CONFIGURATION_get_value_string (cfg,
592 "Failed to locate configuration file. Skipping test.\n");
593 GNUNET_SCHEDULER_shutdown ();
597 char *const identity_args[] =
604 char *const identity2_args[] =
612 char *const identity3_args[] =
616 "-s", "gns-intercept",
620 char *const gns_args[] =
627 GNUNET_TESTING_peer_get_identity (peer, &id);
628 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
631 bin = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
632 GNUNET_asprintf (&bin_identity,
636 if (0 != fork_and_exec (bin_identity, identity_args))
639 "Failed to run `gnunet-identity -C. Skipping test.\n");
640 GNUNET_SCHEDULER_shutdown ();
641 GNUNET_free (bin_identity);
642 GNUNET_free (config);
646 if (0 != fork_and_exec (bin_identity, identity2_args))
649 "Failed to run `gnunet-identity -e. Skipping test.\n");
650 GNUNET_SCHEDULER_shutdown ();
651 GNUNET_free (bin_identity);
652 GNUNET_free (config);
656 if (0 != fork_and_exec (bin_identity, identity3_args))
659 "Failed to run `gnunet-identity -e. Skipping test.\n");
660 GNUNET_SCHEDULER_shutdown ();
661 GNUNET_free (bin_identity);
662 GNUNET_free (config);
666 GNUNET_free (bin_identity);
668 /* do lookup just to launch GNS service */
669 GNUNET_asprintf (&bin_gns,
673 if (0 != fork_and_exec (bin_gns, gns_args))
676 "Failed to run `gnunet-gns -u. Skipping test.\n");
677 GNUNET_SCHEDULER_shutdown ();
678 GNUNET_free (bin_gns);
679 GNUNET_free (config);
683 GNUNET_free (bin_gns);
684 GNUNET_free (config);
688 namestore = GNUNET_NAMESTORE_connect (cfg);
689 GNUNET_assert (NULL != namestore);
690 flags = MHD_USE_DEBUG;
691 if (GNUNET_YES == use_v6)
692 flags |= MHD_USE_DUAL_STACK;
693 mhd = MHD_start_daemon (flags,
698 GNUNET_assert (NULL != mhd);
701 identity = GNUNET_IDENTITY_connect (cfg,
708 main (int argc, char *const *argv)
717 char *const iptables_args[] =
719 "iptables", "-t", "mangle", "-L", "-v", NULL
722 if (0 == access ("/sbin/iptables", X_OK))
723 sbin_iptables = "/sbin/iptables";
724 else if (0 == access ("/usr/sbin/iptables", X_OK))
725 sbin_iptables = "/usr/sbin/iptables";
729 "Executable iptables not found in approved directories: %s, skipping\n",
734 if (0 != fork_and_exec (sbin_iptables, iptables_args))
737 "Failed to run `iptables -t mangle -L -v'. Skipping test.\n");
741 if (0 != ACCESS ("/dev/net/tun", R_OK))
743 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
747 "WARNING: System unable to run test, skipping.\n");
751 bin_vpn = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
752 bin_exit = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
753 bin_dns = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
754 srv_dns = GNUNET_OS_get_libexec_binary_path ("gnunet-service-dns");
755 if ( (0 != geteuid ()) &&
757 GNUNET_OS_check_helper_binary (bin_vpn, GNUNET_YES, "-d gnunet-vpn - - 169.1.3.3.7 255.255.255.0")) || //ipv4 only please!
759 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
761 GNUNET_OS_check_helper_binary (bin_dns, GNUNET_YES, NULL))) ) // TODO: once we have a windows-testcase, add test parameters here
764 "WARNING: gnunet-helper-{exit,vpn,dns} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
766 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
767 GNUNET_free (bin_vpn);
768 GNUNET_free (bin_exit);
769 GNUNET_free (bin_dns);
770 GNUNET_free (srv_dns);
773 GNUNET_free (bin_vpn);
774 GNUNET_free (bin_exit);
776 if ( (0 != stat (bin_dns, &s)) ||
777 (my_gid == s.st_gid) ||
778 ( (0 == (S_ISUID & s.st_mode)) && (0 != getuid()) ) )
781 "WARNING: %s has wrong permissions (%d, %d, %d), refusing to run test (as it would have to fail).\n",
783 (0 != stat (bin_dns, &s)),
784 (my_gid == s.st_gid),
785 (0 == (S_ISUID & s.st_mode)) || (0 != getuid()) );
786 GNUNET_free (bin_dns);
787 GNUNET_free (srv_dns);
790 if ( (0 != stat (srv_dns, &s)) ||
791 (my_gid == s.st_gid) ||
792 (0 == (S_ISGID & s.st_mode)) )
795 "WARNING: %s has wrong permissions (%d, %d, %d), refusing to run test (as it would have to fail).\n",
797 (0 != stat (bin_dns, &s)),
798 (my_gid == s.st_gid),
799 (0 == (S_ISGID & s.st_mode)) );
800 GNUNET_free (bin_dns);
801 GNUNET_free (srv_dns);
804 GNUNET_free (bin_dns);
805 GNUNET_free (srv_dns);
807 dest_ip = "169.254.86.1";
811 if (GNUNET_OK == GNUNET_NETWORK_test_pf (PF_INET6))
816 if ( (GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
817 (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)) )
820 "Required address families not supported by this system, skipping test.\n");
823 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
825 fprintf (stderr, "failed to initialize curl\n");
830 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-vpn",
834 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
838 /* end of test_gns_vpn.c */