fix #4546
[oweals/gnunet.git] / src / nat / nat_test.c
index 2836730caafcaf02eafecf674d09afd6e97ae8df..b2f22781ecf67d61779ddbdc0d8250ba7bb948de 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -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.
 */
 
 /**
@@ -60,7 +60,7 @@ struct NatActivity
   /**
    * Task reading from the incoming connection.
    */
-  GNUNET_SCHEDULER_TaskIdentifier rtask;
+  struct GNUNET_SCHEDULER_Task * rtask;
 };
 
 
@@ -141,12 +141,12 @@ struct GNUNET_NAT_Test
   /**
    * Identity of task for the listen socket (if any)
    */
-  GNUNET_SCHEDULER_TaskIdentifier ltask;
-  
+  struct GNUNET_SCHEDULER_Task * ltask;
+
   /**
    * Task identifier for the timeout (if any)
    */
-  GNUNET_SCHEDULER_TaskIdentifier ttask;
+  struct GNUNET_SCHEDULER_Task * ttask;
 
   /**
    * GNUNET_YES if we're testing TCP
@@ -163,6 +163,10 @@ struct GNUNET_NAT_Test
    */
   uint16_t adv_port;
 
+  /**
+   * Status code to be reported to the timeout/status call
+   */
+  enum GNUNET_NAT_StatusCode status;
 };
 
 
@@ -201,15 +205,15 @@ reversal_cb (void *cls,
  * incoming connection.
  *
  * @param cls the `struct GNUNET_NAT_Test`
- * @param tc scheduler context
  */
 static void
-do_udp_read (void *cls,
-             const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_udp_read (void *cls)
 {
   struct GNUNET_NAT_Test *tst = cls;
   uint16_t data;
+  const struct GNUNET_SCHEDULER_TaskContext *tc;
 
+  tc = GNUNET_SCHEDULER_get_task_context ();
   tst->ltask =
       GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                      tst->lsock,
@@ -236,19 +240,21 @@ do_udp_read (void *cls,
  * incoming connection.
  *
  * @param cls the `struct NatActivity`
- * @param tc scheduler context
  */
 static void
-do_read (void *cls,
-         const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_read (void *cls)
 {
   struct NatActivity *na = cls;
   struct GNUNET_NAT_Test *tst;
   uint16_t data;
+  const struct GNUNET_SCHEDULER_TaskContext *tc;
 
-  na->rtask = GNUNET_SCHEDULER_NO_TASK;
+  tc = GNUNET_SCHEDULER_get_task_context ();
+  na->rtask = NULL;
   tst = na->h;
-  GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
+  GNUNET_CONTAINER_DLL_remove (tst->na_head,
+                              tst->na_tail,
+                              na);
   if ((NULL != tc->write_ready) &&
       (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
       (sizeof (data) ==
@@ -273,21 +279,17 @@ do_read (void *cls,
  * incoming connection.
  *
  * @param cls the `struct GNUNET_NAT_Test`
- * @param tc scheduler context
  */
 static void
-do_accept (void *cls,
-           const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_accept (void *cls)
 {
   struct GNUNET_NAT_Test *tst = cls;
   struct GNUNET_NETWORK_Handle *s;
   struct NatActivity *wl;
 
-  tst->ltask = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   tst->ltask =
-      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
+      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                                    tst->lsock,
                                      &do_accept, tst);
   s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
   if (NULL == s)
@@ -304,7 +306,9 @@ do_accept (void *cls,
     GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                    wl->sock,
                                    &do_read, wl);
-  GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
+  GNUNET_CONTAINER_DLL_insert (tst->na_head,
+                              tst->na_tail,
+                              wl);
 }
 
 
@@ -368,29 +372,30 @@ addr_cb (void *cls,
 
 
 /**
- * Timeout task for a nat test. 
+ * Timeout task for a nat test.
  * Calls the report-callback with a timeout return value
- * 
+ *
+ * Destroys the nat handle after the callback has been processed.
+ *
  * @param cls handle to the timed out NAT test
- * @param tc not used
  */
 static void
-do_timeout (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext * tc)
+do_timeout (void *cls)
 {
-  struct GNUNET_NAT_Test *nh = (struct GNUNET_NAT_Test *) cls;
-  
-  nh->ttask = GNUNET_SCHEDULER_NO_TASK;
-  nh->report (nh->report_cls, GNUNET_NAT_ERROR_TIMEOUT);
-  
-  GNUNET_NAT_test_stop(nh);
+  struct GNUNET_NAT_Test *nh = cls;
+
+  nh->ttask = NULL;
+  nh->report (nh->report_cls,
+              (GNUNET_NAT_ERROR_SUCCESS == nh->status)
+              ? GNUNET_NAT_ERROR_TIMEOUT
+              : nh->status);
 }
 
 
 /**
  * Start testing if NAT traversal works using the
  * given configuration (IPv4-only).
- * 
+ *
  * ALL failures are reported directly to the report callback
  *
  * @param cfg configuration for the NAT traversal
@@ -400,7 +405,7 @@ do_timeout (void *cls,
  * @param timeout delay after which the test should be aborted
  * @param report function to call with the result of the test
  * @param report_cls closure for @a report
- * @return handle to cancel NAT test
+ * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback
  */
 struct GNUNET_NAT_Test *
 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -430,12 +435,14 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   nh->adv_port = adv_port;
   nh->report = report;
   nh->report_cls = report_cls;
-  nh->ttask = GNUNET_SCHEDULER_NO_TASK;
+  nh->status = GNUNET_NAT_ERROR_SUCCESS;
   if (0 == bnd_port)
   {
-    nh->nat =
-        GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
-                             &reversal_cb, nh);
+    nh->nat
+      = GNUNET_NAT_register (cfg, is_tcp, 0, 0,
+                            NULL, NULL,
+                            &addr_cb,
+                             &reversal_cb, nh, NULL);
   }
   else
   {
@@ -454,42 +461,58 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
                   GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
                   STRERROR (errno));
       if (NULL != nh->lsock)
+      {
         GNUNET_NETWORK_socket_close (nh->lsock);
-      GNUNET_free (nh);
-      return NULL;
+        nh->lsock = NULL;
+      }
+      nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
+      nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
+      return nh;
     }
     if (GNUNET_YES == is_tcp)
     {
       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5));
       nh->ltask =
           GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                                         nh->lsock, &do_accept, nh);
+                                         nh->lsock,
+                                        &do_accept, nh);
     }
     else
     {
       nh->ltask =
           GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                                         nh->lsock, &do_udp_read, nh);
+                                         nh->lsock,
+                                        &do_udp_read, nh);
     }
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
+    LOG (GNUNET_ERROR_TYPE_INFO,
         "NAT test listens on port %u (%s)\n",
         bnd_port,
         (GNUNET_YES == is_tcp) ? "tcp" : "udp");
-    nh->nat = GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, addrs, addrlens,
-                             &addr_cb, NULL, nh);
+    nh->nat = GNUNET_NAT_register (cfg, is_tcp, adv_port, 1,
+                                   addrs, addrlens,
+                                   &addr_cb, NULL, nh, NULL);
     if (NULL == nh->nat)
     {
-      LOG (GNUNET_ERROR_TYPE_ERROR,
+      LOG (GNUNET_ERROR_TYPE_INFO,
           _("NAT test failed to start NAT library\n"));
-      if (GNUNET_SCHEDULER_NO_TASK != nh->ltask)
+      if (NULL != nh->ltask)
+      {
         GNUNET_SCHEDULER_cancel (nh->ltask);
+        nh->ltask = NULL;
+      }
       if (NULL != nh->lsock)
+      {
         GNUNET_NETWORK_socket_close (nh->lsock);
-      GNUNET_free (nh);
-      return NULL;
+        nh->lsock = NULL;
+      }
+      nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
+      nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
+      return nh;
     }
   }
-  nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
+  nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout,
+                                           &do_timeout,
+                                           nh);
   return nh;
 }
 
@@ -509,24 +532,29 @@ GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
        "Stopping NAT test\n");
   while (NULL != (cpos = tst->ca_head))
   {
-    GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
+    GNUNET_CONTAINER_DLL_remove (tst->ca_head,
+                                tst->ca_tail,
+                                cpos);
     GNUNET_CLIENT_disconnect (cpos->client);
     GNUNET_free (cpos);
   }
   while (NULL != (pos = tst->na_head))
   {
-    GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
+    GNUNET_CONTAINER_DLL_remove (tst->na_head,
+                                tst->na_tail,
+                                pos);
     GNUNET_SCHEDULER_cancel (pos->rtask);
     GNUNET_NETWORK_socket_close (pos->sock);
     GNUNET_free (pos);
   }
-  if (GNUNET_SCHEDULER_NO_TASK != tst->ttask)
+  if (NULL != tst->ttask)
     GNUNET_SCHEDULER_cancel (tst->ttask);
-  if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
+  if (NULL != tst->ltask)
     GNUNET_SCHEDULER_cancel (tst->ltask);
   if (NULL != tst->lsock)
     GNUNET_NETWORK_socket_close (tst->lsock);
-  GNUNET_NAT_unregister (tst->nat);
+  if (NULL != tst->nat)
+    GNUNET_NAT_unregister (tst->nat);
   GNUNET_free (tst);
 }