preparations for proper manual hole punching support in new NAT API
[oweals/gnunet.git] / src / nat / gnunet-nat.c
index 0574f43559a593d781b58bf85249a34a50cdbcd8..81e4549b5d2763c07e6c566a6ff8d200543a5895 100644 (file)
@@ -39,9 +39,10 @@ static int global_ret;
 static struct GNUNET_NAT_AutoHandle *ah;
 
 /**
- * Port we advertise.
+ * External hostname and port, if user manually punched
+ * the NAT.  
  */ 
-static unsigned int adv_port;
+static char *hole_external;
 
 /**
  * Flag set to 1 if we use IPPROTO_UDP.
@@ -58,6 +59,22 @@ static int listen_reversal;
  */
 static int use_tcp;
 
+/**
+ * If we do auto-configuration, should we write the result
+ * to a file?
+ */
+static int write_cfg;
+
+/**
+ * Configuration filename.
+ */ 
+static const char *cfg_file;
+
+/**
+ * Original configuration.
+ */
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
+
 /**
  * Protocol to use.
  */
@@ -149,9 +166,16 @@ auto_conf_iter (void *cls,
                 const char *option,
                 const char *value)
 {
+  struct GNUNET_CONFIGURATION_Handle *new_cfg = cls;
+  
   PRINTF ("%s: %s\n",
          option,
          value);
+  if (NULL != new_cfg)
+    GNUNET_CONFIGURATION_set_value_string (new_cfg,
+                                          section,
+                                          option,
+                                          value);
 }
 
 
@@ -172,39 +196,94 @@ auto_config_cb (void *cls,
 {
   const char *nat_type;
   char unknown_type[64];
+  struct GNUNET_CONFIGURATION_Handle *new_cfg;
 
   ah = NULL;
   switch (type)
   {
-    case GNUNET_NAT_TYPE_NO_NAT:
-      nat_type = "NO NAT";
-      break;
-    case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
-      nat_type = "NAT but we can traverse";
-      break;
-    case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
-      nat_type = "NAT but STUN is able to identify the correct information";
-      break;
-    case GNUNET_NAT_TYPE_UPNP_NAT:
-      nat_type = "NAT but UPNP opened the ports";
-      break;
-    default:
-      SPRINTF (unknown_type,
-              "NAT unknown, type %u",
-              type);
-      nat_type = unknown_type;
+  case GNUNET_NAT_TYPE_NO_NAT:
+    nat_type = "NO NAT";
+    break;
+  case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
+    nat_type = "NAT but we can traverse";
+    break;
+  case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
+    nat_type = "NAT but STUN is able to identify the correct information";
+    break;
+  case GNUNET_NAT_TYPE_UPNP_NAT:
+    nat_type = "NAT but UPNP opened the ports";
+    break;
+  default:
+    SPRINTF (unknown_type,
+            "NAT unknown, type %u",
+            type);
+    nat_type = unknown_type;
+    break;
+  }
+
+  GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+             "NAT status: %s/%s\n",
+             GNUNET_NAT_status2string (result),
+             nat_type);
+
+  /* Shortcut: if there are no changes suggested, bail out early. */
+  if (GNUNET_NO ==
+      GNUNET_CONFIGURATION_is_dirty (diff))
+  {
+    test_finished ();
+    return;
   }
 
-  PRINTF ("NAT status: %s/%s\n",
-         GNUNET_NAT_status2string (result),
-         nat_type);
+  /* Apply diff to original configuration and show changes
+     to the user */
+  new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;
   
-  PRINTF ("SUGGESTED CHANGES:\n");
-  GNUNET_CONFIGURATION_iterate_section_values (diff,
-                                               "nat",
-                                               &auto_conf_iter,
-                                               NULL);
-  // FIXME: have option to save config
+  if (NULL != diff)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+               _("Suggested configuration changes:\n"));
+    GNUNET_CONFIGURATION_iterate_section_values (diff,
+                                                "nat",
+                                                &auto_conf_iter,
+                                                new_cfg);
+  }
+
+  /* If desired, write configuration to file; we write only the
+     changes to the defaults to keep things compact. */
+  if ( (write_cfg) &&
+       (NULL != diff) )
+  {
+    struct GNUNET_CONFIGURATION_Handle *def_cfg;
+
+    GNUNET_CONFIGURATION_set_value_string (new_cfg,
+                                          "ARM",
+                                          "CONFIG",
+                                          NULL);
+    def_cfg = GNUNET_CONFIGURATION_create ();
+    GNUNET_break (GNUNET_OK ==
+                 GNUNET_CONFIGURATION_load (def_cfg,
+                                            NULL));
+    if (GNUNET_OK !=
+       GNUNET_CONFIGURATION_write_diffs (def_cfg,
+                                         new_cfg,
+                                         cfg_file))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                 _("Failed to write configuration to `%s'\n"),
+                 cfg_file);
+      global_ret = 1;
+    }
+    else
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                 _("Wrote updated configuration to `%s'\n"),
+                 cfg_file);
+    }
+    GNUNET_CONFIGURATION_destroy (def_cfg);
+  }
+
+  if (NULL != new_cfg)
+    GNUNET_CONFIGURATION_destroy (new_cfg);
   test_finished ();
 }
 
@@ -260,15 +339,11 @@ address_cb (void *cls,
  * reversal.
  *
  * @param cls closure, NULL
- * @param local_addr address where we received the request
- * @param local_addrlen actual length of the @a local_addr
  * @param remote_addr public IP address of the other peer
  * @param remote_addrlen actual length of the @a remote_addr
  */
 static void
 reversal_cb (void *cls,
-            const struct sockaddr *local_addr,
-            socklen_t local_addrlen,
             const struct sockaddr *remote_addr,
             socklen_t remote_addrlen)
 {
@@ -381,8 +456,11 @@ run (void *cls,
   struct sockaddr_in extern_sa;
   struct sockaddr *local_sa;
   struct sockaddr *remote_sa;
-  size_t local_len;
+  socklen_t local_len;
   size_t remote_len;
+
+  cfg_file = cfgfile;
+  cfg = c;
   
   if (use_tcp && use_udp)
   {
@@ -396,8 +474,21 @@ run (void *cls,
     proto = IPPROTO_TCP;
   if (use_udp)
     proto = IPPROTO_UDP;
+
+  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                NULL);
+
+  if (do_auto)
+  {
+    ah = GNUNET_NAT_autoconfig_start (c,
+                                     &auto_config_cb,
+                                     NULL);
+  }
+
   if (0 == proto)
   {
+    if (do_auto)
+      return; /* all good, we just run auto config */
     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
                "Must specify either TCP or UDP\n");
     global_ret = 1;
@@ -433,9 +524,9 @@ run (void *cls,
   }
   if (NULL != local_addr)
   {
-    local_len = GNUNET_STRINGS_parse_socket_addr (local_addr,
-                                                 &af,
-                                                 &local_sa);
+    local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr,
+                                                             &af,
+                                                             &local_sa);
     if (0 == local_len)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
@@ -478,7 +569,7 @@ run (void *cls,
   {
     nh = GNUNET_NAT_register (c,
                              proto,
-                             (uint16_t) adv_port,
+                             hole_external,
                              1,
                              (const struct sockaddr **) &local_sa,
                              &local_len,
@@ -486,9 +577,14 @@ run (void *cls,
                              (listen_reversal) ? &reversal_cb : NULL,
                              NULL);
   }
-
-  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
-                                NULL);
+  else if (listen_reversal)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+               "Use of `-W` only effective in combination with `-i`\n");    
+    global_ret = 1;
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
 
   if (NULL != remote_addr)
   {
@@ -530,13 +626,6 @@ run (void *cls,
     }
   }
   
-  if (do_auto)
-  {
-    ah = GNUNET_NAT_autoconfig_start (c,
-                                     &auto_config_cb,
-                                     NULL);
-  }
-
   if (do_stun)
   {
     if (NULL == local_addr)
@@ -603,18 +692,15 @@ main (int argc,
     {'e', "external", "ADDRESS",
      gettext_noop ("which external IP and port should be used to test"),
      GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
-    {'l', "local", "ADDRESS",
-     gettext_noop ("which IP and port are we locally using to listen to for connection reversals"),
+    {'i', "in", "ADDRESS",
+     gettext_noop ("which IP and port are we locally using to bind/listen to"),
      GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
     {'r', "remote", "ADDRESS",
      gettext_noop ("which remote IP and port should be asked for connection reversal"),
      GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
-    {'L', "listen", NULL,
-     gettext_noop ("listen for connection reversal requests"),
-     GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
-    {'p', "port", NULL,
-     gettext_noop ("port to use to advertise"),
-     GNUNET_YES, &GNUNET_GETOPT_set_uint, &adv_port },
+    {'p', "punched", NULL,
+     gettext_noop ("external hostname and port of NAT, if punched manually; use AUTO for hostname for automatic determination of the external IP"),
+     GNUNET_YES, &GNUNET_GETOPT_set_string, &hole_external },
     {'s', "stun", NULL,
      gettext_noop ("enable STUN processing"),
      GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },
@@ -624,6 +710,12 @@ main (int argc,
     {'u', "udp", NULL,
      gettext_noop ("use UDP"),
      GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp },
+    {'w', "write", NULL,
+     gettext_noop ("write configuration file (for autoconfiguration)"),
+     GNUNET_NO, &GNUNET_GETOPT_set_one, &write_cfg },
+    {'W', "watch", NULL,
+     gettext_noop ("watch for connection reversal requests"),
+     GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
    GNUNET_GETOPT_OPTION_END
   };