WiP
[oweals/gnunet.git] / src / nat / upnp.c
index e383055b5b5eb80cab23fdff15751bb0c88c64e8..45c7df41994220a3914802684b7400e3e727bb8d 100644 (file)
  *
  * @author Milan Bouchet-Valat
  */
+#include "platform.h"
+#include "gnunet_common.h"
+
 #include <stdlib.h>
 #include <assert.h>
 #include <errno.h>
 #include <string.h>
 
-#include "platform.h"
-#include "gnunet_common.h"
 #include "gnunet_nat_lib.h"
 #include "nat.h"
 #include "upnp-discover.h"
@@ -57,7 +58,6 @@ enum UPNP_State
 
 struct GNUNET_NAT_UPNP_Handle
 {
-  struct GNUNET_SCHEDULER_Handle *sched;
   int hasDiscovered;
   char *control_url;
   char *service_type;
@@ -67,7 +67,7 @@ struct GNUNET_NAT_UPNP_Handle
   unsigned int is_mapped;
   enum UPNP_State state;
   struct sockaddr *ext_addr;
-  const char *iface;
+  char *iface;
   int processing;
   GNUNET_NAT_UPNP_pulse_cb pulse_cb;
   void *pulse_cls;
@@ -79,10 +79,10 @@ process_if (void *cls,
             int isDefault, const struct sockaddr *addr, socklen_t addrlen)
 {
   struct GNUNET_NAT_UPNP_Handle *upnp = cls;
-
+  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "UPNP found if `%s'\n", name);
   if (addr && GNUNET_NAT_cmp_addr (upnp->addr, addr) == 0)
     {
-      upnp->iface = name;       // BADNESS!
+      upnp->iface = GNUNET_strdup(name);       // BADNESS!
       return GNUNET_SYSERR;
     }
 
@@ -91,8 +91,7 @@ process_if (void *cls,
 
 
 struct GNUNET_NAT_UPNP_Handle *
-GNUNET_NAT_UPNP_init (struct GNUNET_SCHEDULER_Handle *sched,
-                      const struct sockaddr *addr,
+GNUNET_NAT_UPNP_init (const struct sockaddr *addr,
                       socklen_t addrlen,
                       u_short port,
                       GNUNET_NAT_UPNP_pulse_cb pulse_cb, void *pulse_cls)
@@ -100,7 +99,6 @@ GNUNET_NAT_UPNP_init (struct GNUNET_SCHEDULER_Handle *sched,
   struct GNUNET_NAT_UPNP_Handle *handle;
 
   handle = GNUNET_malloc (sizeof (struct GNUNET_NAT_UPNP_Handle));
-  handle->sched = sched;
   handle->processing = GNUNET_NO;
   handle->state = UPNP_DISCOVER;
   handle->addr = addr;
@@ -305,7 +303,12 @@ get_ip_address_cb (int error, char *ext_addr, void *cls)
         }
 
       /* Try IPv4 and IPv6 as we don't know what's the format */
+#ifndef MINGW
       if (inet_aton (ext_addr, &addr) != 0)
+#else
+      addr.S_un.S_addr = inet_addr(ext_addr);
+      if (addr.S_un.S_addr == INADDR_NONE)
+#endif
         {
           handle->ext_addr = GNUNET_malloc (sizeof (struct sockaddr_in));
           handle->ext_addr->sa_family = AF_INET;
@@ -335,9 +338,7 @@ get_ip_address_cb (int error, char *ext_addr, void *cls)
  * 
  * @param handle the handle for UPnP object
  * @param is_enabled whether enable port redirection
- * @param doPortCheck FIXME
- * @param ext_addr pointer for returning external IP address.
- *     Will be set to NULL if address could not be found. Don't free the sockaddr.
+ * @param doPortCheck do port check
  */
 void
 GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *handle,
@@ -350,7 +351,7 @@ GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *handle,
   if (is_enabled && (handle->state == UPNP_DISCOVER))
     {
       handle->processing = GNUNET_YES;
-      UPNP_discover_ (handle->sched, handle->iface, handle->addr, discover_cb,
+      UPNP_discover_ (handle->iface, handle->addr, discover_cb,
                       handle);
     }
 
@@ -367,8 +368,7 @@ GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *handle,
       GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port);
 
       handle->processing = GNUNET_YES;
-      UPNP_get_specific_port_mapping_entry_ (handle->sched,
-                                             handle->control_url,
+      UPNP_get_specific_port_mapping_entry_ (handle->control_url,
                                              handle->service_type, portStr,
                                              "TCP", check_port_mapping_cb,
                                              handle);
@@ -380,7 +380,7 @@ GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *handle,
       GNUNET_snprintf (portStr, sizeof (portStr), "%d", handle->port);
 
       handle->processing = GNUNET_YES;
-      UPNP_delete_port_mapping_ (handle->sched, handle->control_url,
+      UPNP_delete_port_mapping_ (handle->control_url,
                                  handle->service_type, portStr, "TCP", NULL,
                                  delete_port_mapping_cb, handle);
     }
@@ -403,7 +403,7 @@ GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *handle,
           GNUNET_snprintf (desc, sizeof (desc), "GNUnet at %d", handle->port);
 
           handle->processing = GNUNET_YES;
-          UPNP_add_port_mapping_ (handle->sched, handle->control_url,
+          UPNP_add_port_mapping_ (handle->control_url,
                                   handle->service_type,
                                   portStr, portStr, GNUNET_a2s (handle->addr,
                                                                 handle->addrlen),
@@ -415,7 +415,7 @@ GNUNET_NAT_UPNP_pulse (struct GNUNET_NAT_UPNP_Handle *handle,
   if (handle->state != UPNP_DISCOVER)
     {
       handle->processing = GNUNET_YES;
-      UPNP_get_external_ip_address_ (handle->sched, handle->control_url,
+      UPNP_get_external_ip_address_ (handle->control_url,
                                      handle->service_type,
                                      get_ip_address_cb, handle);
     }