-improve indentation, reduce duplication of PIDs in core's neighbour map
[oweals/gnunet.git] / src / nat / nat_auto.c
index 3ee520d9b59d3632aaf4191083c5c3e50adfa4de..327970e0afae473c807360fca28832eaf838f2fe 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012 Christian Grothoff (and other contributing authors)
 
      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.
 */
 
 /**
@@ -129,7 +129,7 @@ struct GNUNET_NAT_AutoHandle
   /**
    * Task identifier for the timeout.
    */
-  GNUNET_SCHEDULER_TaskIdentifier task;
+  struct GNUNET_SCHEDULER_Task * task;
 
   /**
    * Where are we in the test?
@@ -144,7 +144,7 @@ struct GNUNET_NAT_AutoHandle
   /**
    * Error code for better debugging and user feedback
    */
-  enum GNUNET_NAT_FailureCode ret;
+  enum GNUNET_NAT_StatusCode ret;
 };
 
 
@@ -158,35 +158,8 @@ next_phase (struct GNUNET_NAT_AutoHandle *ah);
 
 
 /**
- * Function called if NAT failed to confirm success.
- * Clean up and update GUI (with failure).
- *
- * @param cls closure with setup context
- * @param tc scheduler callback
- */
-static void
-fail_timeout (void *cls,
-              const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_NAT_AutoHandle *ah = cls;
-
-  ah->ret = GNUNET_NAT_ERROR_NAT_TEST_TIMEOUT; 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             _("NAT traversal with ICMP Server timed out.\n"));
-  GNUNET_assert (NULL != ah->tst);
-  ah->task = GNUNET_SCHEDULER_NO_TASK;
-  GNUNET_NAT_test_stop (ah->tst);
-  ah->tst = NULL;
-  GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat",
-                                        "ENABLE_ICMP_SERVER",
-                                        "NO");
-  next_phase (ah);
-}
-
-
-/**
- * Function called by NAT on success.
- * Clean up and update GUI (with success).
+ * Function called by NAT to report the outcome of the nat-test.
+ * Clean up and update GUI.
  *
  * @param cls the auto handle
  * @param success currently always #GNUNET_OK
@@ -194,20 +167,19 @@ fail_timeout (void *cls,
  */
 static void
 result_callback (void *cls,
-                 enum GNUNET_NAT_FailureCode ret)
+                 enum GNUNET_NAT_StatusCode ret)
 {
   struct GNUNET_NAT_AutoHandle *ah = cls;
-
-  GNUNET_SCHEDULER_cancel (ah->task);
-  ah->task = GNUNET_SCHEDULER_NO_TASK;
-  GNUNET_NAT_test_stop (ah->tst);
+  if (GNUNET_NAT_ERROR_SUCCESS == ret)
+    GNUNET_NAT_test_stop (ah->tst);
   ah->tst = NULL;
+  ah->ret = ret;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              ret
+              GNUNET_NAT_ERROR_SUCCESS == ret
              ? _("NAT traversal with ICMP Server succeeded.\n")
              : _("NAT traversal with ICMP Server failed.\n"));
   GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER",
-                                        ret ? "NO" : "YES");
+                                        GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES");
   next_phase (ah);
 }
 
@@ -224,19 +196,12 @@ reversal_test (void *cls,
 {
   struct GNUNET_NAT_AutoHandle *ah = cls;
 
-  ah->task = GNUNET_SCHEDULER_NO_TASK;
+  ah->task = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              _("Testing connection reversal with ICMP server.\n"));
   GNUNET_RESOLVER_connect (ah->cfg);
-  ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0,
+  ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0, TIMEOUT,
                                   &result_callback, ah);
-  if (NULL == ah->tst)
-  {
-    ah->ret = GNUNET_NAT_ERROR_NAT_TEST_START_FAILED;
-    next_phase (ah);
-    return;
-  }
-  ah->task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, ah);
 }
 
 
@@ -267,7 +232,7 @@ test_online (struct GNUNET_NAT_AutoHandle *ah)
 static void
 set_external_ipv4 (void *cls,
                    const struct in_addr *addr,
-                   enum GNUNET_NAT_FailureCode ret)
+                   enum GNUNET_NAT_StatusCode ret)
 {
   struct GNUNET_NAT_AutoHandle *ah = cls;
   char buf[INET_ADDRSTRLEN];
@@ -506,15 +471,8 @@ err:
 static void
 test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
 {
-  int ext_ip;
-  int nated;
-  int binary;
   char *tmp;
   char *helper;
-  
-  ext_ip = GNUNET_NO;
-  nated = GNUNET_NO;
-  binary = GNUNET_NO;
 
   tmp = NULL;
   helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
@@ -522,7 +480,6 @@ test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
         GNUNET_CONFIGURATION_get_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
                                                &tmp)) && (0 < strlen (tmp)))
   {
-    ext_ip = GNUNET_OK;
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_client not possible, as we have no internal IPv4 address\n"));
   }
   else
@@ -530,7 +487,6 @@ test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
   
   if (GNUNET_YES !=
       GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, "nat", "BEHIND_NAT")){
-        nated = GNUNET_YES;
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we are not behind NAT\n"));
   }
   else
@@ -539,7 +495,6 @@ test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
   if (GNUNET_YES ==
       GNUNET_OS_check_helper_binary (helper, GNUNET_YES, "-d 127.0.0.1 127.0.0.2 42")){
           // none of these parameters are actually used in privilege testing mode
-    binary = GNUNET_OK;
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("No working gnunet-helper-nat-server found\n"));
   }
 err:
@@ -648,10 +603,10 @@ GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
     GNUNET_NAT_mini_get_external_ipv4_cancel (ah->eh);
     ah->eh = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != ah->task)
+  if (NULL != ah->task)
   {
     GNUNET_SCHEDULER_cancel (ah->task);
-    ah->task = GNUNET_SCHEDULER_NO_TASK;
+    ah->task = NULL;
   }
   GNUNET_CONFIGURATION_destroy (ah->cfg);
   GNUNET_CONFIGURATION_destroy (ah->initial_cfg);