glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / gns / test_gns_proxy.c
index 35350e8bb64fe49c0333cb4862469d4f0df82bce..ee4d19884f9b7fa21d18ad1726b0036f4c551b18 100644 (file)
@@ -1,21 +1,16 @@
 /*
      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
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
+     Affero General Public License for more details.
 */
 
 /**
  * @author Martin Schanzenbach
  */
 #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_namestore_service.h"
 #include "gnunet_gns_service.h"
-#include "gnunet_testing_lib-new.h"
+#include "gnunet_testing_lib.h"
 #include "gnunet_os_lib.h"
 
 #define PORT 8080
-#define TEST_DOMAIN "www.gnunet"
+#define TEST_DOMAIN "www.gnu"
 
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
 
@@ -45,9 +44,9 @@ static struct GNUNET_NAMESTORE_Handle *namestore;
 
 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 CURL *curl;
 
@@ -75,7 +74,7 @@ copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
 
   if (cbc->pos + size * nmemb > sizeof(cbc->buf))
     return 0;                   /* overflow */
-  memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
+  GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
   cbc->pos += size * nmemb;
   return size * nmemb;
 }
@@ -117,15 +116,15 @@ mhd_ahc (void *cls,
 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 (NULL != mhd)
   {
@@ -135,11 +134,21 @@ do_shutdown ()
   GNUNET_free_non_null (url);
 
   if (NULL != tmp_cfgfile)
-    remove (tmp_cfgfile);
-
+    {
+      if (0 != remove (tmp_cfgfile))
+       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", tmp_cfgfile);
+      GNUNET_free (tmp_cfgfile);
+      tmp_cfgfile = NULL;
+    }
   if (NULL != proxy_proc)
-    GNUNET_OS_process_kill (proxy_proc, 9);
+    {
+      (void) GNUNET_OS_process_kill (proxy_proc, SIGKILL);
+      GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proxy_proc));
+      GNUNET_OS_process_destroy (proxy_proc);
+      proxy_proc = NULL;
+    }
   url = NULL;
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -151,10 +160,9 @@ curl_main (void);
 
 
 static void
-curl_task (void *cls,
-         const struct GNUNET_SCHEDULER_TaskContext *tc)
+curl_task (void *cls)
 {
-  curl_task_id = GNUNET_SCHEDULER_NO_TASK;
+  curl_task_id = NULL;
   curl_main ();
 }
 
@@ -210,9 +218,9 @@ curl_main ()
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
     do_shutdown ();
-    return;    
+    return;
   }
-  GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max)); 
+  GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
   if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
        (-1 == timeout) )
     delay = GNUNET_TIME_UNIT_SECONDS;
@@ -229,14 +237,15 @@ curl_main ()
                                              &nrs,
                                              &nws,
                                              &curl_task,
-                                             NULL);  
+                                             NULL);
 }
 
+
 static void
-start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+start_curl (void *cls)
 {
-  GNUNET_asprintf (&url, 
-                  "http://%s:%d/hello_world",  
+  GNUNET_asprintf (&url,
+                  "http://%s:%d/hello_world",
                   TEST_DOMAIN, PORT);
   curl = curl_easy_init ();
   curl_easy_setopt (curl, CURLOPT_URL, url);
@@ -255,12 +264,14 @@ start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   curl_main ();
 }
 
+
 static void
-disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+disco_ns (void* cls)
 {
   GNUNET_NAMESTORE_disconnect (namestore);
 }
 
+
 /**
  * Callback invoked from the namestore service once record is
  * created.
@@ -270,47 +281,48 @@ disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *                will match 'result_af' from the request
  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
  *                that the VPN allocated for the redirection;
- *                traffic to this IP will now be redirected to the 
+ *                traffic to this IP will now be redirected to the
  *                specified target peer; NULL on error
  */
 static void
-commence_testing (void *cls, int32_t success, const char *emsg)
+commence_testing (void *cls,
+                 int32_t success,
+                 const char *emsg)
 {
   GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
 
-  if ((emsg != NULL) && (GNUNET_YES != success))
+  if ( (emsg != NULL) && (GNUNET_YES != success) )
   {
-    fprintf (stderr, 
-            "NS failed to create record %s\n", emsg);
+    fprintf (stderr,
+            "NS failed to create record %s\n",
+            emsg);
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1), start_curl, NULL);
 
+  curl_task_id =
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 
+                                 &start_curl, NULL);
 }
 
 
-
-
 /**
  * Function to keep the HTTP server running.
  */
-static void 
+static void
 mhd_main (void);
 
 
 static void
-mhd_task (void *cls,
-         const struct GNUNET_SCHEDULER_TaskContext *tc)
+mhd_task (void *cls)
 {
-  mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
+  mhd_task_id = NULL;
   MHD_run (mhd);
   mhd_main ();
 }
 
 
-static void 
+static void
 mhd_main ()
 {
   struct GNUNET_NETWORK_FDSet nrs;
@@ -322,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);
@@ -345,7 +357,7 @@ mhd_main ()
                                             &nrs,
                                             &nws,
                                             &mhd_task,
-                                            NULL);  
+                                            NULL);
 }
 
 static void
@@ -354,10 +366,10 @@ run (void *cls,
      struct GNUNET_TESTING_Peer *peer)
 {
   enum MHD_FLAG flags;
-  struct GNUNET_CRYPTO_RsaPrivateKey *host_key;
-  struct GNUNET_NAMESTORE_RecordData rd;
+  struct GNUNET_CRYPTO_EcdsaPrivateKey *host_key;
+  struct GNUNET_GNSRECORD_Data rd;
   char *zone_keyfile;
-  
+
   namestore = GNUNET_NAMESTORE_connect (cfg);
   GNUNET_assert (NULL != namestore);
   flags = MHD_USE_DEBUG;
@@ -386,11 +398,12 @@ run (void *cls,
     do_shutdown ();
     return;
   }
-  
+
   proxy_proc = GNUNET_OS_start_process (GNUNET_NO,
                                         GNUNET_OS_INHERIT_STD_ALL,
                                         NULL,
                                         NULL,
+                                        NULL,
                                         "gnunet-gns-proxy",
                                         "gnunet-gns-proxy",
                                         "-c", tmp_cfgfile, NULL);
@@ -402,7 +415,7 @@ run (void *cls,
     do_shutdown ();
     return;
   }
-  
+
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
                                                             "ZONEKEY",
                                                             &zone_keyfile))
@@ -411,13 +424,13 @@ run (void *cls,
     return;
   }
 
-  host_key = GNUNET_CRYPTO_rsa_key_create_from_file (zone_keyfile);
-  rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
-  GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_string_to_value (GNUNET_GNS_RECORD_A,
+  host_key = GNUNET_CRYPTO_ecdsa_key_create_from_file (zone_keyfile);
+  rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
+  GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (GNUNET_DNSPARSER_TYPE_A,
                                                                "127.0.0.1",
                                                                (void**)&rd.data,
                                                                &rd.data_size));
-  rd.record_type = GNUNET_GNS_RECORD_A;
+  rd.record_type = GNUNET_DNSPARSER_TYPE_A;
 
   GNUNET_NAMESTORE_record_create (namestore,
                                   host_key,
@@ -428,21 +441,28 @@ run (void *cls,
 
   GNUNET_free ((void**)rd.data);
   GNUNET_free (zone_keyfile);
-  GNUNET_CRYPTO_rsa_key_free (host_key);
+  GNUNET_free (host_key);
 }
 
 int
 main (int argc, char *const *argv)
 {
+  char *binary;
 
-  if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("gnunet-gns-proxy"))
+  if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("gnunet-gns-proxy", GNUNET_NO, NULL))
   {
     fprintf (stderr, "Proxy binary not in PATH... skipping!\n");
     return 0;
   }
+  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
+  if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) // TODO: once we have a windows-testcase, add test parameters here
+  {
+    fprintf (stderr, "DNS helper binary has wrong permissions... skipping!\n");
+    GNUNET_free (binary);
+    return 0;
+  }
+    GNUNET_free (binary);
 
-  GNUNET_CRYPTO_setup_hostkey ("test_gns_proxy.conf");
-  
   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
   {
     fprintf (stderr, "failed to initialize curl\n");
@@ -457,4 +477,3 @@ main (int argc, char *const *argv)
 }
 
 /* end of test_gns_vpn.c */
-