psyc/social: local join flag; social service: leave place, save _file
[oweals/gnunet.git] / src / pt / test_gnunet_vpn.c
index 8b9bb2b2046bd8cfc80a7f3b6938deb2d7457aba..b5bc1395ba6018bb9be4c22019888b4a9d4b2188 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet
-     (C) 2007, 2009, 2011, 2012 Christian Grothoff
+     Copyright (C) 2007, 2009, 2011, 2012 Christian Grothoff
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
  * @author Christian Grothoff
  */
 #include "platform.h"
+#if HAVE_CURL_CURL_H
 #include <curl/curl.h>
+#elif HAVE_GNURL_CURL_H
+#include <gnurl/curl.h>
+#endif
 #include <microhttpd.h>
 #include "gnunet_vpn_service.h"
-#include "gnunet_testing_lib-new.h"
+#include "gnunet_testing_lib.h"
 
 #define PORT 48080
 
@@ -43,11 +47,11 @@ static struct GNUNET_VPN_Handle *vpn;
 
 static struct MHD_Daemon *mhd;
 
-static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
+static struct GNUNET_SCHEDULER_Task * mhd_task_id;
 
-static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
+static struct GNUNET_SCHEDULER_Task * curl_task_id;
 
-static GNUNET_SCHEDULER_TaskIdentifier ctrl_c_task_id;
+static struct GNUNET_SCHEDULER_Task * ctrl_c_task_id;
 
 static struct GNUNET_VPN_RedirectionRequest *rr;
 
@@ -128,20 +132,20 @@ mhd_ahc (void *cls, struct MHD_Connection *connection, const char *url,
 static void
 do_shutdown ()
 {
-  if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
+  if (mhd_task_id != NULL)
   {
     GNUNET_SCHEDULER_cancel (mhd_task_id);
-    mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
+    mhd_task_id = NULL;
   }
-  if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
+  if (curl_task_id != NULL)
   {
     GNUNET_SCHEDULER_cancel (curl_task_id);
-    curl_task_id = GNUNET_SCHEDULER_NO_TASK;
+    curl_task_id = NULL;
   }
-  if (ctrl_c_task_id != GNUNET_SCHEDULER_NO_TASK)
+  if (ctrl_c_task_id != NULL)
   {
     GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
-    ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
+    ctrl_c_task_id = NULL;
   }
   if (NULL != mhd)
   {
@@ -167,19 +171,8 @@ do_shutdown ()
  * Function to run the HTTP client.
  */
 static void
-curl_main (void);
-
-
-static void
-curl_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  curl_task_id = GNUNET_SCHEDULER_NO_TASK;
-  curl_main ();
-}
-
-
-static void
-curl_main ()
+curl_main (void *cls,
+           const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   fd_set rs;
   fd_set ws;
@@ -192,6 +185,7 @@ curl_main ()
   int running;
   struct CURLMsg *msg;
 
+  curl_task_id = NULL;
   max = 0;
   FD_ZERO (&rs);
   FD_ZERO (&ws);
@@ -241,7 +235,7 @@ curl_main ()
   GNUNET_NETWORK_fdset_copy_native (&nws, &ws, max + 1);
   curl_task_id =
       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
-                                   &nrs, &nws, &curl_task, NULL);
+                                   &nrs, &nws, &curl_main, NULL);
 }
 
 
@@ -270,9 +264,16 @@ allocation_cb (void *cls, int af, const void *address)
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_asprintf (&url, "http://%s:%u/hello_world",
-                   inet_ntop (af, address, ips, sizeof (ips)),
-                   (unsigned int) PORT);
+  if (AF_INET6 == af)
+    GNUNET_asprintf (&url,
+                     "http://[%s]:%u/hello_world",
+                     inet_ntop (af, address, ips, sizeof (ips)),
+                     (unsigned int) PORT);
+  else
+    GNUNET_asprintf (&url,
+                     "http://%s:%u/hello_world",
+                     inet_ntop (af, address, ips, sizeof (ips)),
+                     (unsigned int) PORT);
   curl = curl_easy_init ();
   curl_easy_setopt (curl, CURLOPT_URL, url);
   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copy_buffer);
@@ -281,13 +282,17 @@ allocation_cb (void *cls, int af, const void *address)
   curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
   curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
+  curl_easy_setopt (curl, CURLOPT_VERBOSE, 0);
 
   multi = curl_multi_init ();
   GNUNET_assert (multi != NULL);
   GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Beginning HTTP download from `%s'\n",
               url);
-  curl_main ();
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                &curl_main,
+                                NULL);
 }
 
 
@@ -301,7 +306,7 @@ mhd_main (void);
 static void
 mhd_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
+  mhd_task_id = NULL;
   MHD_run (mhd);
   mhd_main ();
 }
@@ -310,7 +315,7 @@ mhd_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 ctrl_c_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
+  ctrl_c_task_id = NULL;
   do_shutdown ();
   GNUNET_break (0);
   global_ret = 1;
@@ -329,7 +334,7 @@ mhd_main ()
   unsigned MHD_LONG_LONG timeout;
   struct GNUNET_TIME_Relative delay;
 
-  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
+  GNUNET_assert (NULL == mhd_task_id);
   FD_ZERO (&rs);
   FD_ZERO (&ws);
   FD_ZERO (&es);
@@ -366,6 +371,8 @@ run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
   mhd =
       MHD_start_daemon (flags, PORT, NULL, NULL, &mhd_ahc, NULL,
                         MHD_OPTION_END);
+
+
   GNUNET_assert (NULL != mhd);
   mhd_main ();
   addr = NULL;
@@ -382,7 +389,7 @@ run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
   default:
     GNUNET_assert (0);
   }
-  rr = GNUNET_VPN_redirect_to_ip (vpn, src_af, dest_af, addr, GNUNET_YES,
+  rr = GNUNET_VPN_redirect_to_ip (vpn, src_af, dest_af, addr,
                                   GNUNET_TIME_UNIT_FOREVER_ABS, &allocation_cb,
                                   NULL);
   ctrl_c_task_id =
@@ -395,53 +402,67 @@ main (int argc, char *const *argv)
 {
   const char *type;
   const char *bin;
+  char *vpn_binary;
+  char *exit_binary;
+  int ret=0;
 
+#ifndef MINGW
   if (0 != ACCESS ("/dev/net/tun", R_OK))
   {
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access",
                               "/dev/net/tun");
-    fprintf (stderr, "WARNING: System unable to run test, skipping.\n");
+    fprintf (stderr,
+             "WARNING: System unable to run test, skipping.\n");
     return 0;
   }
-  if ((GNUNET_YES != GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
-      (GNUNET_YES != GNUNET_OS_check_helper_binary ("gnunet-helper-exit")))
+#endif
+  vpn_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
+  exit_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
+  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!
+      (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
   {
+    GNUNET_free (vpn_binary);
+    GNUNET_free (exit_binary);
     fprintf (stderr,
-             "WARNING: gnunet-helper-{exit,vpn} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
-    fprintf (stderr,
-             "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
+             "WARNING: gnunet-helper-{exit,vpn} binaries are not SUID, refusing to run test (as it would have to fail). %d\n", ret);
     return 0;
   }
-  GNUNET_CRYPTO_rsa_setup_hostkey ("test_gnunet_vpn.conf");
+
+  GNUNET_free (vpn_binary);
+  GNUNET_free (exit_binary);
   bin = argv[0];
   if (NULL != strstr (bin, "lt-"))
     bin = strstr (bin, "lt-") + 4;
   type = strstr (bin, "-");
   if (NULL == type)
   {
-    fprintf (stderr, "invalid binary name\n");
+    fprintf (stderr,
+             "invalid binary name\n");
     return 1;
   }
   type++;
-  if (0 == strcmp (type, "4_to_6"))
+  /* on Windows, .exe is suffixed to these binaries,
+   * thus cease comparison after the 6th char.
+   */
+  if (0 == strncmp (type, "4_to_6",6))
   {
     dest_ip = "FC5A:04E1:C2BA::1";
     dest_af = AF_INET6;
     src_af = AF_INET;
   }
-  else if (0 == strcmp (type, "6_to_4"))
+  else if (0 == strncmp (type, "6_to_4",6))
   {
     dest_ip = "169.254.86.1";
     dest_af = AF_INET;
     src_af = AF_INET6;
   }
-  else if (0 == strcmp (type, "4_over"))
+  else if (0 == strncmp (type, "4_over",6))
   {
     dest_ip = "169.254.86.1";
     dest_af = AF_INET;
     src_af = AF_INET;
   }
-  else if (0 == strcmp (type, "6_over"))
+  else if (0 == strncmp (type, "6_over",6))
   {
     dest_ip = "FC5A:04E1:C2BA::1";
     dest_af = AF_INET6;
@@ -452,7 +473,7 @@ main (int argc, char *const *argv)
     fprintf (stderr, "invalid binary suffix `%s'\n", type);
     return 1;
   }
-  if ((GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) || 
+  if ((GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
       (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)))
   {
     fprintf (stderr,