WiP
[oweals/gnunet.git] / src / nat / natpmp.c
index 2a9187295d0b2f1e09c102c10d30fe6f78edf551..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
@@ -41,6 +41,7 @@
 #include "platform.h"
 #include "gnunet_common.h"
 #include "gnunet_nat_lib.h"
+#include "nat.h"
 #include "natpmp.h"
 
 #define LIFETIME_SECS 3600
@@ -48,7 +49,7 @@
 /* Component name for logging */
 #define COMP_NAT_NATPMP _("NAT (NAT-PMP))")
 
-typedef enum
+enum NATPMP_state
 {
   NATPMP_IDLE,
   NATPMP_ERR,
@@ -59,7 +60,7 @@ typedef enum
   NATPMP_SEND_UNMAP,
   NATPMP_RECV_UNMAP
 }
-NATPMP_state;
+ ;
 
 struct GNUNET_NAT_NATPMP_Handle
 {
@@ -71,29 +72,26 @@ struct GNUNET_NAT_NATPMP_Handle
   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
 }
 
@@ -108,12 +106,11 @@ GNUNET_NAT_NATPMP_init (const struct sockaddr *addr, socklen_t addrlen,
   nat->port = port;
   nat->addr = addr;
   nat->addrlen = addrlen;
-  nat->ext_addr = NULL;
   return nat;
 }
 
 void
-GNUNET_NAT_NATPMP_close (GNUNET_NAT_NATPMP_Handle * nat)
+GNUNET_NAT_NATPMP_close (struct GNUNET_NAT_NATPMP_Handle *nat)
 {
   if (nat)
     {
@@ -138,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 */
@@ -156,13 +158,13 @@ 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)
         {
-          if (nat->ext_addr)
+          if (NULL != nat->ext_addr)
             {
               GNUNET_free (nat->ext_addr);
               nat->ext_addr = NULL;
@@ -170,25 +172,38 @@ GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
 
           if (response.pnu.publicaddress.family == AF_INET)
             {
-              nat->ext_addr =
-                GNUNET_malloc (sizeof (struct in_addr));
-              memcpy (nat->ext_addr, &response.pnu.publicaddress.addr,
+              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"),
+                               inet_ntop (AF_INET,
+                                          &response.pnu.publicaddress.addr,
+                                          buf, sizeof (buf)));
+#endif
             }
           else
             {
-              nat->ext_addr =
-                GNUNET_malloc (sizeof (struct in6_addr));
-              memcpy (nat->ext_addr, &response.pnu.publicaddress.addr6,
+              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)));
-            }
-
-            *ext_addr = nat->ext_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_INET6,
+                                          &response.pnu.publicaddress.addr6,
+                                          buf, sizeof (buf)));
 #endif
+            }
+          *ext_addr = nat->ext_addr;
           nat->state = NATPMP_IDLE;
         }
       else if (val != NATPMP_TRYAGAIN)
@@ -216,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;
@@ -260,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)
@@ -270,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)
         {
@@ -306,3 +321,5 @@ GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
     }
   return ret;
 }
+
+/* end of natpmp.c */