WiP
[oweals/gnunet.git] / src / nat / natpmp.c
index 335228767203cca997ca5822620395cc94dafb08..c6a5604d8ce61e2ce5fdc6c6f64c882098f50c85 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  *
  * @author Milan Bouchet-Valat
  */
-
-#ifdef __cplusplus
-extern "C"
-{
-#if 0                           /* keep Emacsens' auto-indent happy */
-}
-#endif
-#endif
-
 #include <assert.h>
 #include <errno.h>
 #include <time.h>
@@ -50,6 +41,7 @@ extern "C"
 #include "platform.h"
 #include "gnunet_common.h"
 #include "gnunet_nat_lib.h"
+#include "nat.h"
 #include "natpmp.h"
 
 #define LIFETIME_SECS 3600
@@ -57,7 +49,7 @@ extern "C"
 /* Component name for logging */
 #define COMP_NAT_NATPMP _("NAT (NAT-PMP))")
 
-typedef enum
+enum NATPMP_state
 {
   NATPMP_IDLE,
   NATPMP_ERR,
@@ -68,40 +60,38 @@ typedef enum
   NATPMP_SEND_UNMAP,
   NATPMP_RECV_UNMAP
 }
-NATPMP_state;
+ ;
 
 struct GNUNET_NAT_NATPMP_Handle
 {
   const struct sockaddr *addr;
   socklen_t addrlen;
+  struct sockaddr *ext_addr;
   int is_mapped;
   int has_discovered;
   int port;
   time_t renew_time;
   time_t command_time;
-  NATPMP_state state;
-  natpmp_t natpmp;
+  enum NATPMP_state state;
+  struct natpmp_t natpmp;
 };
 
-/**
-***
-**/
 
 static void
 log_val (const char *func, int ret)
 {
 #ifdef DEBUG
   if (ret == NATPMP_TRYAGAIN)
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP, _("%s retry (%d)\n"), func, ret);
-//    return;
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                     COMP_NAT_NATPMP, _("%s retry (%d)\n"), func, ret);
   if (ret >= 0)
-    GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP, _("%s succeeded (%d)\n"), func, ret);
+    GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
+                     COMP_NAT_NATPMP, _("%s succeeded (%d)\n"), func, ret);
   else
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
-                "%s failed.  natpmp returned %d (%s); errno is %d (%s)\n",
-                func, ret, strnatpmperr (ret), errno, strerror (errno));
-#else
-  return;
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                     COMP_NAT_NATPMP,
+                     "%s failed.  natpmp returned %d (%s); errno is %d (%s)\n",
+                     func, ret, strnatpmperr (ret), errno, strerror (errno));
 #endif
 }
 
@@ -120,7 +110,7 @@ GNUNET_NAT_NATPMP_init (const struct sockaddr *addr, socklen_t addrlen,
 }
 
 void
-GNUNET_NAT_NATPMP_close (GNUNET_NAT_NATPMP_Handle * nat)
+GNUNET_NAT_NATPMP_close (struct GNUNET_NAT_NATPMP_Handle *nat)
 {
   if (nat)
     {
@@ -145,6 +135,11 @@ int
 GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
                          struct sockaddr **ext_addr)
 {
+#if DEBUG
+  char buf[INET6_ADDRSTRLEN];
+#endif
+  struct sockaddr_in *v4;
+  struct sockaddr_in6 *v6;
   int ret;
 
   /* Keep to NULL if address could not be found */
@@ -163,21 +158,52 @@ GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
 
   if ((nat->state == NATPMP_RECV_PUB) && can_send_command (nat))
     {
-      natpmpresp_t response;
+      struct natpmpresp_t response;
       const int val = readnatpmpresponseorretry (&nat->natpmp,
                                                  &response);
       log_val ("readnatpmpresponseorretry", val);
       if (val >= 0)
         {
-          *ext_addr =
-            GNUNET_malloc (sizeof (response.pnu.publicaddress.addr));
-          memcpy (*ext_addr, &response.pnu.publicaddress.addr,
-                  (sizeof (response.pnu.publicaddress.addr)));
+          if (NULL != nat->ext_addr)
+            {
+              GNUNET_free (nat->ext_addr);
+              nat->ext_addr = NULL;
+            }
+
+          if (response.pnu.publicaddress.family == AF_INET)
+            {
+              v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
+              nat->ext_addr = (struct sockaddr *) v4;
+              v4->sin_family = AF_INET;
+              v4->sin_port = response.pnu.newportmapping.mappedpublicport;
+              memcpy (&v4->sin_addr, &response.pnu.publicaddress.addr,
+                      sizeof (struct in_addr));
 #ifdef DEBUG
-          GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
-                      _("Found public IP address %s\n"),
-                      GNUNET_a2s (*ext_addr, sizeof (*ext_addr)));
+              GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
+                               _("Found public IP address %s\n"),
+                               inet_ntop (AF_INET,
+                                          &response.pnu.publicaddress.addr,
+                                          buf, sizeof (buf)));
 #endif
+            }
+          else
+            {
+              v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
+              nat->ext_addr = (struct sockaddr *) v6;
+              v6->sin6_family = AF_INET6;
+              v6->sin6_port = response.pnu.newportmapping.mappedpublicport;
+              memcpy (&v6->sin6_addr,
+                      &response.pnu.publicaddress.addr6,
+                      (sizeof (struct in6_addr)));
+#ifdef DEBUG
+              GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, COMP_NAT_NATPMP,
+                               _("Found public IP address %s\n"),
+                               inet_ntop (AF_INET6,
+                                          &response.pnu.publicaddress.addr6,
+                                          buf, sizeof (buf)));
+#endif
+            }
+          *ext_addr = nat->ext_addr;
           nat->state = NATPMP_IDLE;
         }
       else if (val != NATPMP_TRYAGAIN)
@@ -205,14 +231,14 @@ GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
 
   if (nat->state == NATPMP_RECV_UNMAP)
     {
-      natpmpresp_t resp;
+      struct natpmpresp_t resp;
       const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
       log_val ("readnatpmpresponseorretry", val);
       if (val >= 0)
         {
           const int p = resp.pnu.newportmapping.privateport;
           GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
-                      _("No longer forwarding port %d\n"), p);
+                           _("No longer forwarding port %d\n"), p);
           if (nat->port == p)
             {
               nat->port = -1;
@@ -249,7 +275,7 @@ GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
 
   if (nat->state == NATPMP_RECV_MAP)
     {
-      natpmpresp_t resp;
+      struct natpmpresp_t resp;
       const int val = readnatpmpresponseorretry (&nat->natpmp, &resp);
       log_val ("readnatpmpresponseorretry", val);
       if (val >= 0)
@@ -259,7 +285,7 @@ GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
           nat->renew_time = time (NULL) + LIFETIME_SECS;
           nat->port = resp.pnu.newportmapping.privateport;
           GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, COMP_NAT_NATPMP,
-                      _("Port %d forwarded successfully\n"), nat->port);
+                           _("Port %d forwarded successfully\n"), nat->port);
         }
       else if (val != NATPMP_TRYAGAIN)
         {
@@ -295,3 +321,5 @@ GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
     }
   return ret;
 }
+
+/* end of natpmp.c */