preparations for proper manual hole punching support in new NAT API
[oweals/gnunet.git] / src / nat / gnunet-nat.c
index c833337554e38697878d243a769fdefc2ce459eb..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.
  */
@@ -105,6 +122,16 @@ static struct GNUNET_NAT_Test *nt;
  */
 static struct GNUNET_NAT_Handle *nh;
 
+/**
+ * Listen socket for STUN processing.
+ */ 
+static struct GNUNET_NETWORK_Handle *ls;
+
+/**
+ * Task for reading STUN packets.
+ */
+static struct GNUNET_SCHEDULER_Task *rtask;
+
 
 /**
  * Test if all activities have finished, and if so,
@@ -119,6 +146,8 @@ test_finished ()
     return;
   if (NULL != nh)
     return;
+  if (NULL != rtask)
+    return;
   GNUNET_SCHEDULER_shutdown ();
 }
 
@@ -137,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);
 }
 
 
@@ -160,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;
   }
 
-  PRINTF ("NAT status: %s/%s\n",
-         GNUNET_NAT_status2string (result),
-         nat_type);
+  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;
+  }
+
+  /* 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);
-  // 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 ();
 }
 
@@ -219,7 +310,7 @@ test_report_cb (void *cls,
  * Signature of the callback passed to #GNUNET_NAT_register() for
  * a function to call whenever our set of 'valid' addresses changes.
  *
- * @param cls closure
+ * @param cls closure, NULL
  * @param add_remove #GNUNET_YES to add a new public IP address, 
  *                   #GNUNET_NO to remove a previous (now invalid) one
  * @param ac address class the address belongs to
@@ -233,7 +324,12 @@ address_cb (void *cls,
            const struct sockaddr *addr,
            socklen_t addrlen)
 {
-  // FIXME: print!
+  GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+             "%s %s (%d)\n",
+             add_remove ? "+" : "-",
+             GNUNET_a2s (addr,
+                         addrlen),
+             (int) ac);
 }
 
 
@@ -242,20 +338,19 @@ address_cb (void *cls,
  * for a function to call whenever someone asks us to do connection
  * reversal.
  *
- * @param cls closure
- * @param local_addr address where we received the request
- * @param local_addrlen actual length of the @a local_addr
+ * @param cls closure, NULL
  * @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)
 {
-  // FIXME: print!
+  GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+             "Connection reversal requested by %s\n",
+             GNUNET_a2s (remote_addr,
+                         remote_addrlen));
 }
 
 
@@ -282,6 +377,63 @@ do_shutdown (void *cls)
     GNUNET_NAT_unregister (nh);
     nh = NULL;
   }
+  if (NULL != ls)
+  {
+    GNUNET_NETWORK_socket_close (ls);
+    ls = NULL;
+  }
+  if (NULL != rtask)
+  {
+    GNUNET_SCHEDULER_cancel (rtask);
+    rtask = NULL;
+  }
+}
+
+
+/**
+ * Task to receive incoming packets for STUN processing.
+ */
+static void
+stun_read_task (void *cls)
+{
+  ssize_t size;
+  
+  rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                                        ls,
+                                        &stun_read_task,
+                                        NULL);
+  size = GNUNET_NETWORK_socket_recvfrom_amount (ls);
+  if (size > 0)
+  {
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_shutdown ();
+    global_ret = 1;
+    return;
+  }
+  {
+    char buf[size + 1];
+    struct sockaddr_storage sa;
+    socklen_t salen = sizeof (sa);
+    ssize_t ret;
+    
+    ret = GNUNET_NETWORK_socket_recvfrom (ls,
+                                         buf,
+                                         size + 1,
+                                         (struct sockaddr *) &sa,
+                                         &salen);
+    if (ret != size)
+    {
+      GNUNET_break (0);
+      GNUNET_SCHEDULER_shutdown ();
+      global_ret = 1;
+      return;
+    }
+    (void) GNUNET_NAT_stun_handle_packet (nh,
+                                         (const struct sockaddr *) &sa,
+                                         salen,
+                                         buf,
+                                         ret);
+  }
 }
 
 
@@ -304,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)
   {
@@ -319,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;
@@ -356,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,
@@ -401,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,
@@ -409,6 +577,14 @@ run (void *cls,
                              (listen_reversal) ? &reversal_cb : NULL,
                              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)
   {
@@ -450,14 +626,47 @@ run (void *cls,
     }
   }
   
-  if (do_auto)
+  if (do_stun)
   {
-    ah = GNUNET_NAT_autoconfig_start (c,
-                                     &auto_config_cb,
-                                     NULL);
+    if (NULL == local_addr)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                 "Require local address to support STUN requests\n");
+      global_ret = 1;
+      GNUNET_SCHEDULER_shutdown ();
+      return;
+    }
+    if (IPPROTO_UDP != proto)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                 "STUN only supported over UDP\n");
+      global_ret = 1;
+      GNUNET_SCHEDULER_shutdown ();
+      return;
+    }
+    ls = GNUNET_NETWORK_socket_create (af,
+                                      SOCK_DGRAM,
+                                      IPPROTO_UDP);
+    if (GNUNET_OK !=
+       GNUNET_NETWORK_socket_bind (ls,
+                                   local_sa,
+                                   local_len))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 "Failed to bind to %s: %s\n",
+                 GNUNET_a2s (local_sa,
+                             local_len),
+                 STRERROR (errno));
+      global_ret = 1;
+      GNUNET_SCHEDULER_shutdown ();
+      return;
+    }
+    rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                                          ls,
+                                          &stun_read_task,
+                                          NULL);
   }
-  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
-                                NULL);
+
   test_finished ();
 }
 
@@ -483,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 },
@@ -504,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
   };