2 This file is part of GNUnet
3 (C) 2007, 2009, 2011, 2012 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 2, 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_namestore_service.h"
30 #include "gnunet_gns_service.h"
31 #include "gnunet_testing_lib-new.h"
34 #define TEST_DOMAIN "www.gnunet"
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
39 * Return value for 'main'.
41 static int global_ret;
43 static struct GNUNET_NAMESTORE_Handle *namestore;
45 static struct MHD_Daemon *mhd;
47 static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
49 static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
58 * IP address of the ultimate destination.
60 static const char *dest_ip;
63 * Address family of the dest_ip.
68 * 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 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
92 cbc->pos += size * nmemb;
99 struct MHD_Connection *connection,
103 const char *upload_data, size_t *upload_data_size,
107 struct MHD_Response *response;
110 if (0 != strcmp ("GET", method))
111 return MHD_NO; /* unexpected method */
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
119 response = MHD_create_response_from_buffer (strlen (url),
121 MHD_RESPMEM_MUST_COPY);
122 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
123 MHD_destroy_response (response);
133 if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
135 GNUNET_SCHEDULER_cancel (mhd_task_id);
136 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
138 if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
140 GNUNET_SCHEDULER_cancel (curl_task_id);
141 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
145 MHD_stop_daemon (mhd);
148 GNUNET_free_non_null (url);
154 * Function to run the HTTP client.
161 curl_task (void *cls,
162 const struct GNUNET_SCHEDULER_TaskContext *tc)
164 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
176 struct GNUNET_NETWORK_FDSet nrs;
177 struct GNUNET_NETWORK_FDSet nws;
178 struct GNUNET_TIME_Relative delay;
187 curl_multi_perform (multi, &running);
190 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
191 if (msg->msg == CURLMSG_DONE)
193 if (msg->data.result != CURLE_OK)
196 "%s failed at %s:%d: `%s'\n",
197 "curl_multi_perform",
199 __LINE__, curl_easy_strerror (msg->data.result));
203 curl_multi_remove_handle (multi, curl);
204 curl_multi_cleanup (multi);
205 curl_easy_cleanup (curl);
208 if (cbc.pos != strlen ("/hello_world"))
213 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
222 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
223 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
225 delay = GNUNET_TIME_UNIT_SECONDS;
227 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
228 GNUNET_NETWORK_fdset_copy_native (&nrs,
231 GNUNET_NETWORK_fdset_copy_native (&nws,
234 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
244 start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 GNUNET_asprintf (&url,
247 "http://%s/hello_world",
249 curl = curl_easy_init ();
250 curl_easy_setopt (curl, CURLOPT_URL, url);
251 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
252 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
253 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
254 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
255 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
256 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
258 multi = curl_multi_init ();
259 GNUNET_assert (multi != NULL);
260 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
267 disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
269 GNUNET_NAMESTORE_disconnect (namestore);
274 * Callback invoked from the namestore service once record is
278 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
279 * will match 'result_af' from the request
280 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
281 * that the VPN allocated for the redirection;
282 * traffic to this IP will now be redirected to the
283 * specified target peer; NULL on error
286 commence_testing (void *cls, int32_t success, const char *emsg)
288 GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
290 if ((emsg != NULL) && (GNUNET_YES != success))
293 "NS failed to create record %s\n", emsg);
294 GNUNET_SCHEDULER_shutdown ();
298 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), start_curl, NULL);
306 * Function to keep the HTTP server running.
314 const struct GNUNET_SCHEDULER_TaskContext *tc)
316 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
325 struct GNUNET_NETWORK_FDSet nrs;
326 struct GNUNET_NETWORK_FDSet nws;
331 unsigned MHD_LONG_LONG timeout;
332 struct GNUNET_TIME_Relative delay;
334 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
339 GNUNET_assert (MHD_YES ==
340 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
341 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
342 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
343 (unsigned int) timeout);
345 delay = GNUNET_TIME_UNIT_FOREVER_REL;
346 GNUNET_NETWORK_fdset_copy_native (&nrs,
349 GNUNET_NETWORK_fdset_copy_native (&nws,
352 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
363 const struct GNUNET_CONFIGURATION_Handle *cfg,
364 struct GNUNET_TESTING_Peer *peer)
367 struct GNUNET_PeerIdentity id;
368 struct GNUNET_CRYPTO_HashAsciiEncoded peername;
369 struct GNUNET_CRYPTO_RsaPrivateKey *host_key;
370 struct GNUNET_NAMESTORE_RecordData rd;
374 GNUNET_TESTING_peer_get_identity (peer, &id);
375 GNUNET_CRYPTO_hash_to_enc ((struct GNUNET_HashCode*)&id, &peername);
377 namestore = GNUNET_NAMESTORE_connect (cfg);
378 GNUNET_assert (NULL != namestore);
379 flags = MHD_USE_DEBUG;
380 //if (GNUNET_YES == use_v6)
381 // flags |= MHD_USE_IPv6;
382 mhd = MHD_start_daemon (flags,
387 GNUNET_assert (NULL != mhd);
390 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
394 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
398 host_key = GNUNET_CRYPTO_rsa_key_create_from_file (zone_keyfile);
399 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
400 GNUNET_asprintf (&rd_string, "6 %s %s", (char*)&peername, "www.gnunet.");
401 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_string_to_value (GNUNET_GNS_RECORD_VPN,
405 rd.record_type = GNUNET_GNS_RECORD_VPN;
407 GNUNET_NAMESTORE_record_create (namestore,
413 GNUNET_free ((void**)rd.data);
414 GNUNET_free (rd_string);
415 GNUNET_free (zone_keyfile);
416 GNUNET_CRYPTO_rsa_key_free (host_key);
421 * Test if the given AF is supported by this system.
424 * @return GNUNET_OK if the AF is supported
431 s = socket (af, SOCK_STREAM, 0);
434 if (EAFNOSUPPORT == errno)
436 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
437 return GNUNET_SYSERR;
445 * Open '/dev/null' and make the result the given
448 * @param target_fd desired FD to point to /dev/null
449 * @param flags open flags (O_RDONLY, O_WRONLY)
452 open_dev_null (int target_fd,
457 fd = open ("/dev/null", flags);
462 if (-1 == dup2 (fd, target_fd))
472 * Run the given command and wait for it to complete.
474 * @param file name of the binary to run
475 * @param cmd command line arguments (as given to 'execv')
476 * @return 0 on success, 1 on any error
479 fork_and_exec (const char *file,
496 /* we are the child process */
497 /* close stdin/stdout to not cause interference
498 with the helper's main protocol! */
500 open_dev_null (0, O_RDONLY);
502 open_dev_null (1, O_WRONLY);
503 (void) execv (file, cmd);
504 /* can only get here on error */
506 "exec `%s' failed: %s\n",
511 /* keep running waitpid as long as the only error we get is 'EINTR' */
512 while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
517 "waitpid failed: %s\n",
521 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
523 /* child process completed and returned success, we're happy */
528 main (int argc, char *const *argv)
531 char *const iptables_args[] =
533 "iptables", "-t", "mangle", "-L", "-v", NULL
536 if (0 == access ("/sbin/iptables", X_OK))
537 sbin_iptables = "/sbin/iptables";
538 else if (0 == access ("/usr/sbin/iptables", X_OK))
539 sbin_iptables = "/usr/sbin/iptables";
543 "Executable iptables not found in approved directories: %s, skipping\n",
548 if (0 != fork_and_exec (sbin_iptables, iptables_args))
551 "Failed to run `iptables -t mangle -L -v'. Skipping test.\n");
555 if (0 != ACCESS ("/dev/net/tun", R_OK))
557 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
561 "WARNING: System unable to run test, skipping.\n");
565 if ( (0 != geteuid ()) &&
567 GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
569 GNUNET_OS_check_helper_binary ("gnunet-helper-exit")) ||
571 GNUNET_OS_check_helper_binary ("gnunet-helper-dns"))) )
574 "WARNING: gnunet-helper-{exit,vpn,dns} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
576 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
579 GNUNET_CRYPTO_setup_hostkey ("test_gns_vpn.conf");
581 dest_ip = "169.254.86.1";
585 if (GNUNET_OK == test_af (AF_INET6))
590 if ( (GNUNET_OK != test_af (src_af)) ||
591 (GNUNET_OK != test_af (dest_af)) )
594 "Required address families not supported by this system, skipping test.\n");
597 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
599 fprintf (stderr, "failed to initialize curl\n");
602 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-vpn",
606 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
610 /* end of test_gns_vpn.c */