memcmp() -> GNUNET_memcmp(), first take
[oweals/gnunet.git] / src / pt / test_gnunet_vpn.c
index 29c087106272207d7362bae7751fca3c385ac1e5..0afd0f94b22aee630b660a882db1fa95f5e46517 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet
      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 3, 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.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     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.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -47,11 +47,11 @@ static struct GNUNET_VPN_Handle *vpn;
 
 static struct MHD_Daemon *mhd;
 
-static struct GNUNET_SCHEDULER_Task * mhd_task_id;
+static struct GNUNET_SCHEDULER_Task *mhd_task_id;
 
-static struct GNUNET_SCHEDULER_Task * curl_task_id;
+static struct GNUNET_SCHEDULER_Task *curl_task_id;
 
-static struct GNUNET_SCHEDULER_Task * ctrl_c_task_id;
+static struct GNUNET_SCHEDULER_Task *timeout_task_id;
 
 static struct GNUNET_VPN_RedirectionRequest *rr;
 
@@ -93,16 +93,21 @@ 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;
 }
 
 
 static int
-mhd_ahc (void *cls, struct MHD_Connection *connection, const char *url,
-         const char *method, const char *version, const char *upload_data,
-         size_t * upload_data_size, void **unused)
+mhd_ahc (void *cls,
+         struct MHD_Connection *connection,
+         const char *url,
+         const char *method,
+         const char *version,
+         const char *upload_data,
+         size_t * upload_data_size,
+         void **unused)
 {
   static int ptr;
   struct MHD_Response *response;
@@ -130,22 +135,22 @@ mhd_ahc (void *cls, struct MHD_Connection *connection, const char *url,
 
 
 static void
-do_shutdown ()
+do_shutdown (void *cls)
 {
-  if (mhd_task_id != NULL)
+  if (NULL != mhd_task_id)
   {
     GNUNET_SCHEDULER_cancel (mhd_task_id);
     mhd_task_id = NULL;
   }
-  if (curl_task_id != NULL)
+  if (NULL != curl_task_id)
   {
     GNUNET_SCHEDULER_cancel (curl_task_id);
     curl_task_id = NULL;
   }
-  if (ctrl_c_task_id != NULL)
+  if (NULL != timeout_task_id)
   {
-    GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
-    ctrl_c_task_id = NULL;
+    GNUNET_SCHEDULER_cancel (timeout_task_id);
+    timeout_task_id = NULL;
   }
   if (NULL != mhd)
   {
@@ -171,8 +176,7 @@ do_shutdown ()
  * Function to run the HTTP client.
  */
 static void
-curl_main (void *cls,
-           const struct GNUNET_SCHEDULER_TaskContext *tc)
+curl_main (void *cls)
 {
   fd_set rs;
   fd_set ws;
@@ -221,7 +225,7 @@ curl_main (void *cls,
       global_ret = 3;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
-    do_shutdown ();
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
   GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
@@ -260,19 +264,26 @@ allocation_cb (void *cls, int af, const void *address)
   rr = NULL;
   if (src_af != af)
   {
-    fprintf (stderr, "VPN failed to allocate appropriate address\n");
+    fprintf (stderr,
+             "VPN failed to allocate appropriate address\n");
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
   if (AF_INET6 == af)
     GNUNET_asprintf (&url,
                      "http://[%s]:%u/hello_world",
-                     inet_ntop (af, address, ips, sizeof (ips)),
+                     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)),
+                     inet_ntop (af,
+                                address,
+                                ips,
+                                sizeof (ips)),
                      (unsigned int) PORT);
   curl = curl_easy_init ();
   curl_easy_setopt (curl, CURLOPT_URL, url);
@@ -304,7 +315,7 @@ mhd_main (void);
 
 
 static void
-mhd_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+mhd_task (void *cls)
 {
   mhd_task_id = NULL;
   MHD_run (mhd);
@@ -313,10 +324,10 @@ mhd_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 
 static void
-ctrl_c_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_timeout (void *cls)
 {
-  ctrl_c_task_id = NULL;
-  do_shutdown ();
+  timeout_task_id = NULL;
+  GNUNET_SCHEDULER_shutdown ();
   GNUNET_break (0);
   global_ret = 1;
 }
@@ -355,7 +366,8 @@ mhd_main ()
 
 
 static void
-run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
+run (void *cls,
+     const struct GNUNET_CONFIGURATION_Handle *cfg,
      struct GNUNET_TESTING_Peer *peer)
 {
   struct in_addr v4;
@@ -392,8 +404,12 @@ run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
   rr = GNUNET_VPN_redirect_to_ip (vpn, src_af, dest_af, addr,
                                   GNUNET_TIME_UNIT_FOREVER_ABS, &allocation_cb,
                                   NULL);
-  ctrl_c_task_id =
-      GNUNET_SCHEDULER_add_delayed (TIMEOUT, &ctrl_c_shutdown, NULL);
+  timeout_task_id =
+      GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                    &do_timeout,
+                                    NULL);
+  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                 NULL);
 }
 
 
@@ -404,16 +420,17 @@ main (int argc, char *const *argv)
   const char *bin;
   char *vpn_binary;
   char *exit_binary;
-  int ret=0;
+  int ret = 0;
 
 #ifndef MINGW
   if (0 != ACCESS ("/dev/net/tun", R_OK))
   {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access",
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+                              "access",
                               "/dev/net/tun");
     fprintf (stderr,
              "WARNING: System unable to run test, skipping.\n");
-    return 0;
+    return 77;
   }
 #endif
   vpn_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
@@ -425,7 +442,7 @@ main (int argc, char *const *argv)
     GNUNET_free (exit_binary);
     fprintf (stderr,
              "WARNING: gnunet-helper-{exit,vpn} binaries are not SUID, refusing to run test (as it would have to fail). %d\n", ret);
-    return 0;
+    return 77;
   }
 
   GNUNET_free (vpn_binary);