bunch of fixes to bugs found by grothoff
authorMoon <moon@140774ce-b5e7-0310-ab8b-a85725594a96>
Fri, 30 Oct 2009 16:18:34 +0000 (16:18 +0000)
committerMoon <moon@140774ce-b5e7-0310-ab8b-a85725594a96>
Fri, 30 Oct 2009 16:18:34 +0000 (16:18 +0000)
src/nat/libnatpmp/natpmp.c
src/nat/libnatpmp/natpmp.h
src/nat/miniupnp/miniupnpc.c
src/nat/miniupnp/upnpcommands.c
src/nat/nat.c
src/nat/natpmp.c
src/nat/upnp.c

index 17191197907113ff76ac4851e25b17d683d2479c..12526df8ea97f19d2fbd9f19f6a089ee525f75d2 100644 (file)
@@ -267,9 +267,13 @@ readnatpmpresponse (natpmp_t * p, natpmpresp_t * response)
         {
           response->type = buf[1] & 0x7f;
           if (buf[1] == 128)
-            //response->publicaddress.addr = *((uint32_t *)(buf + 8));
-            response->pnu.publicaddress.addr.s_addr =
-              *((uint32_t *) (buf + 8));
+            {
+              response->pnu.publicaddress.family = AF_INET;
+              memset (&response->pnu.publicaddress.addr6.s6_addr, 0, sizeof (struct in6_addr));
+              response->pnu.publicaddress.addr.s_addr =
+                *((uint32_t *) (buf + 8));
+              /* FIXME: support IPv6 address */
+            }
           else
             {
               response->pnu.newportmapping.privateport =
index d8b23a74d9e6678acf1fdb0285572ba9c856b18d..976bad06f73e790b51b32c8d6495eb00a8b02223 100644 (file)
@@ -56,8 +56,9 @@ typedef struct
   {
     struct
     {
-      //in_addr_t addr;
+      int family;
       struct in_addr addr;
+      struct in6_addr addr6;
     } publicaddress;
     struct
     {
index 412c8ec2502049133cbaf2e5a8e76525a0193ed6..34bae0ad85a12ab0146f8122381d61a80e3064e5 100644 (file)
@@ -238,6 +238,7 @@ simpleUPnPcommand (int s, const char *url, const char *service,
       /* Test IPv4 address, else use IPv6 */
       if (inet_pton (AF_INET, hostname, &dest.sin_addr) == 1)
         {
+          memset (&dest, 0, sizeof (dest));
           dest.sin_family = AF_INET;
           dest.sin_port = htons (port);
 #ifdef HAVE_SOCKADDR_IN_SIN_LEN
@@ -253,9 +254,9 @@ simpleUPnPcommand (int s, const char *url, const char *service,
         }
       else if (inet_pton (AF_INET6, hostname, &dest6.sin6_addr) == 1)
         {
+          memset (&dest6, 0, sizeof (dest6));
           dest6.sin6_family = AF_INET6;
           dest6.sin6_port = htons (port);
-          dest6.sin6_flowinfo = 0;
 #ifdef HAVE_SOCKADDR_IN_SIN_LEN
           dest6.sin6_len = sizeof (dest6);
 #endif
@@ -270,7 +271,9 @@ simpleUPnPcommand (int s, const char *url, const char *service,
       else
         {
           PRINT_SOCKET_ERROR ("inet_pton");
-          closesocket (s);
+          if (s > 0)
+            closesocket (s);
+
           *bufsize = 0;
           return -1;
         }
@@ -495,7 +498,11 @@ upnpDiscover (int delay, const char *multicastif,
       memset (&sockudp6_w, 0, sizeof (struct sockaddr_in6));
       sockudp6_w.sin6_family = AF_INET6;
       sockudp6_w.sin6_port = htons (PORT);
-      inet_pton (AF_INET6, UPNP_MCAST_ADDR6, &sockudp6_w.sin6_addr);
+      if (inet_pton (AF_INET6, UPNP_MCAST_ADDR6, &sockudp6_w.sin6_addr) != 1)
+        {
+          PRINT_SOCKET_ERROR ("inet_pton");
+          return NULL;
+        }
 #ifdef HAVE_SOCKADDR_IN_SIN_LEN
       sockudp6_w.sin6_len = sizeof (struct sockaddr_in6);
 #endif
@@ -535,13 +542,17 @@ upnpDiscover (int delay, const char *multicastif,
         }
       else
         {
-          if (multicastif && !(if_index = if_nametoindex (multicastif)))
-              PRINT_SOCKET_ERROR ("if_nametoindex");
-
-          if (setsockopt
-              (sudp, IPPROTO_IPV6, IPV6_MULTICAST_IF, &if_index, sizeof (if_index)) < 0)
+          if (multicastif)
             {
-              PRINT_SOCKET_ERROR ("setsockopt");
+              if_index = if_nametoindex (multicastif);
+              if (!if_index)
+                PRINT_SOCKET_ERROR ("if_nametoindex");
+
+              if (setsockopt
+                  (sudp, IPPROTO_IPV6, IPV6_MULTICAST_IF, &if_index, sizeof (if_index)) < 0)
+                {
+                  PRINT_SOCKET_ERROR ("setsockopt");
+                }
             }
 
           /* Bind to receive response before sending packet */
index 7a342540a04cbc873081eb0974294ef4aae1d190..8b59bc7cc08efc87bc090c4548317c7d119fa82d 100644 (file)
@@ -435,8 +435,7 @@ UPNP_GetGenericPortMappingEntry (const char *controlURL,
   int r = UPNPCOMMAND_UNKNOWN_ERROR;
   if (!index)
     return UPNPCOMMAND_INVALID_ARGS;
-  intClient[0] = '\0';
-  intPort[0] = '\0';
+
   GetPortMappingArgs = calloc (2, sizeof (struct UPNParg));
   GetPortMappingArgs[0].elt = "NewPortMappingIndex";
   GetPortMappingArgs[0].val = index;
index e725220abd8fbffbf648f3638b2bac7ac34af16b..2a93d31a7b8e4a74bb1a3a085025557a2f019f00 100644 (file)
@@ -359,6 +359,9 @@ GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nat)
 
   GNUNET_NAT_NATPMP_close (nat->natpmp);
   GNUNET_NAT_UPNP_close (nat->upnp);
+
+  if (nat->local_addr)
+    GNUNET_free (nat->local_addr);
   if (nat->ext_addr)
     GNUNET_free (nat->ext_addr);
   GNUNET_free (nat);
index 16d68c702c6615d38d472dcb1ad0677f0e4f5c22..2a9187295d0b2f1e09c102c10d30fe6f78edf551 100644 (file)
@@ -65,6 +65,7 @@ struct GNUNET_NAT_NATPMP_Handle
 {
   const struct sockaddr *addr;
   socklen_t addrlen;
+  struct sockaddr *ext_addr;
   int is_mapped;
   int has_discovered;
   int port;
@@ -107,6 +108,7 @@ 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;
 }
 
@@ -160,10 +162,28 @@ GNUNET_NAT_NATPMP_pulse (struct GNUNET_NAT_NATPMP_Handle *nat, int is_enabled,
       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 (nat->ext_addr)
+            {
+              GNUNET_free (nat->ext_addr);
+              nat->ext_addr = NULL;
+            }
+
+          if (response.pnu.publicaddress.family == AF_INET)
+            {
+              nat->ext_addr =
+                GNUNET_malloc (sizeof (struct in_addr));
+              memcpy (nat->ext_addr, &response.pnu.publicaddress.addr,
+                      sizeof (struct in_addr));
+            }
+          else
+            {
+              nat->ext_addr =
+                GNUNET_malloc (sizeof (struct in6_addr));
+              memcpy (nat->ext_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"),
index 62ecd7ff9a057eecb8377c19c77f15162b3b350a..e8a5f1afc7eefffba5f3744c9b67dcd8051cae53 100644 (file)
@@ -223,7 +223,7 @@ GNUNET_NAT_UPNP_pulse (GNUNET_NAT_UPNP_Handle * handle, int is_enabled,
       int err = -1;
       errno = 0;
 
-      if (!handle->urls.controlURL || !handle->data.servicetype)
+      if (!handle->urls.controlURL)
         handle->is_mapped = 0;
       else
         {