-improve indentation, reduce duplication of PIDs in core's neighbour map
[oweals/gnunet.git] / src / nat / nat_auto.c
index 184f23538a2b5a3eda179d5065736542c72990f1..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?
@@ -141,6 +141,10 @@ struct GNUNET_NAT_AutoHandle
    */
   int have_v6;
 
+  /**
+   * Error code for better debugging and user feedback
+   */
+  enum GNUNET_NAT_StatusCode ret;
 };
 
 
@@ -154,34 +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;
-
-  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
@@ -189,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);
 }
 
@@ -219,18 +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)
-  {
-    next_phase (ah);
-    return;
-  }
-  ah->task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, ah);
 }
 
 
@@ -243,6 +214,10 @@ static void
 test_online (struct GNUNET_NAT_AutoHandle *ah)
 {
   // FIXME: not implemented
+  /*
+   * if (failure)
+   *  ah->ret = GNUNET_NAT_ERROR_NOT_ONLINE;
+   */
   next_phase (ah);
 }
 
@@ -257,13 +232,14 @@ 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];
 
   ah->eh = NULL;
-  if (NULL == addr)
+  ah->ret = ret;
+  if (GNUNET_NAT_ERROR_SUCCESS != ret)
   {
     next_phase (ah);
     return;
@@ -281,6 +257,11 @@ set_external_ipv4 (void *cls,
   if (NULL == inet_ntop (AF_INET, addr, buf, sizeof (buf)))
   {
     GNUNET_break (0);
+    /* actually, this should never happen, as the caller already executed just
+     * this check, but for consistency (eg: future changes in the caller) 
+     * we still need to report this error...
+     */
+    ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;
     next_phase (ah);
     return;
   }
@@ -298,6 +279,9 @@ set_external_ipv4 (void *cls,
 static void
 test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
 {
+  if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
+    next_phase (ah);
+  
   // FIXME: CPS?
   /* try to detect external IP */
   ah->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT,
@@ -319,7 +303,7 @@ test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
  * @return GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
  */
 static int
-nipo (void *cls,
+process_if (void *cls,
       const char *name,
       int isDefault,
       const struct sockaddr *addr,
@@ -358,6 +342,7 @@ nipo (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              _("Detected internal network address `%s'.\n"),
              buf);
+  ah->ret = GNUNET_NAT_ERROR_SUCCESS;
   /* no need to continue iteration */
   return GNUNET_SYSERR;
 }
@@ -372,7 +357,9 @@ static void
 test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
 {
   ah->have_v6 = GNUNET_NO;
-  GNUNET_OS_network_interfaces_list (&nipo, ah);
+  ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP
+  GNUNET_OS_network_interfaces_list (&process_if, ah);
+  
   GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "DISABLEV6",
                                         (GNUNET_YES == ah->have_v6) ? "NO" : "YES");
   next_phase (ah);
@@ -387,7 +374,11 @@ test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
 static void
 test_nat_punched (struct GNUNET_NAT_AutoHandle *ah)
 {
+  if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
+    next_phase (ah);
+  
   // FIXME: not implemented
+  
   next_phase (ah);
 }
 
@@ -402,6 +393,9 @@ test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
 {
   int have_upnpc;
 
+  if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
+    next_phase (ah);
+  
   /* test if upnpc is available */
   have_upnpc = (GNUNET_SYSERR !=
                GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL));
@@ -425,27 +419,44 @@ test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
 static void
 test_icmp_server (struct GNUNET_NAT_AutoHandle *ah)
 {
-  int hns;
+  int ext_ip;
+  int nated;
+  int binary;
   char *tmp;
-  char *binary;
-
+  char *helper;
+  ext_ip = GNUNET_NO;
+  nated = GNUNET_NO;
+  binary = GNUNET_NO;
+  
   tmp = NULL;
-  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
-  hns =
-      ((GNUNET_OK ==
+  helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
+  if ((GNUNET_OK ==
         GNUNET_CONFIGURATION_get_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
-                                               &tmp)) && (0 < strlen (tmp)) &&
-       (GNUNET_YES ==
-        GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, "nat", "BEHIND_NAT")) &&
-       (GNUNET_YES ==
-        GNUNET_OS_check_helper_binary (binary, GNUNET_YES, "-d 127.0.0.1" ))); // use localhost as source for that one udp-port, ok for testing
+                                               &tmp)) && (0 < strlen (tmp))){
+    ext_ip = GNUNET_OK;
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we have no public IPv4 address\n"));
+  }
+  else
+    goto err;
+    
+  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
+    goto err;
+  
+  if (GNUNET_YES ==
+        GNUNET_OS_check_helper_binary (helper, GNUNET_YES, "-d 127.0.0.1" )){
+    binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("No working gnunet-helper-nat-server found\n"));
+  }
+err:
   GNUNET_free_non_null (tmp);
-  GNUNET_free (binary);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             (hns)
-             ? _("gnunet-helper-nat-server found, testing it\n")
-             : _("No working gnunet-helper-nat-server found\n"));
-  if (hns)
+  GNUNET_free (helper);
+
+  if (GNUNET_OK == ext_ip && GNUNET_YES == nated && GNUNET_OK == binary)
     ah->task = GNUNET_SCHEDULER_add_now (&reversal_test, ah);
   else
     next_phase (ah);
@@ -460,26 +471,36 @@ test_icmp_server (struct GNUNET_NAT_AutoHandle *ah)
 static void
 test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
 {
-  int hnc;
   char *tmp;
-  char *binary;
+  char *helper;
 
   tmp = NULL;
-  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
-  hnc =
-      ((GNUNET_OK ==
+  helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
+  if ((GNUNET_OK ==
         GNUNET_CONFIGURATION_get_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
-                                               &tmp)) && (0 < strlen (tmp)) &&
-       (GNUNET_YES !=
-        GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, "nat", "BEHIND_NAT")) &&
-       (GNUNET_YES ==
-        GNUNET_OS_check_helper_binary (binary, GNUNET_YES, "-d 127.0.0.1 127.0.0.2 42"))); // none of these parameters are actually used in privilege testing mode
+                                               &tmp)) && (0 < strlen (tmp)))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_client not possible, as we have no internal IPv4 address\n"));
+  }
+  else
+    goto err;
+  
+  if (GNUNET_YES !=
+      GNUNET_CONFIGURATION_get_value_yesno (ah->cfg, "nat", "BEHIND_NAT")){
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("test_icmp_server not possible, as we are not behind NAT\n"));
+  }
+  else
+    goto err;
+  
+  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
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("No working gnunet-helper-nat-server found\n"));
+  }
+err:
   GNUNET_free_non_null (tmp);
-  GNUNET_free (binary);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             (hnc)
-             ? _("gnunet-helper-nat-client found, enabling it\n")
-             : _("gnunet-helper-nat-client not found or behind NAT, disabling it\n"));
+  GNUNET_free (helper);
+
   next_phase (ah);
 }
 
@@ -524,7 +545,7 @@ next_phase (struct GNUNET_NAT_AutoHandle *ah)
                                          ah->cfg);
     ah->fin_cb (ah->fin_cb_cls,
                diff,
-                GNUNET_NAT_ERROR_SUCCESS);
+                ah->ret);
     GNUNET_CONFIGURATION_destroy (diff);
     GNUNET_NAT_autoconfig_cancel (ah);
     return;
@@ -551,6 +572,7 @@ GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
   ah->fin_cb = cb;
   ah->fin_cb_cls = cb_cls;
+  ah->ret = GNUNET_NAT_ERROR_SUCCESS;
   ah->cfg = GNUNET_CONFIGURATION_dup (cfg);
   ah->initial_cfg = GNUNET_CONFIGURATION_dup (cfg);
 
@@ -581,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);