(no commit message)
[oweals/gnunet.git] / src / transport / plugin_transport_http.c
index d754c7e67b58ca16e355f8ba17c176441fd83c96..5cd611d1f1b5783c1434a90b94b9c1f7960f4180 100644 (file)
@@ -40,7 +40,7 @@
 #include "microhttpd.h"
 #include <curl/curl.h>
 
-#define DEBUG_HTTP GNUNET_YES
+#define DEBUG_HTTP GNUNET_NO
 #define DEBUG_CURL GNUNET_NO
 #define DEBUG_CONNECTIONS GNUNET_NO
 #define DEBUG_SESSION_SELECTION GNUNET_NO
@@ -356,7 +356,11 @@ struct Plugin
    */
   struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
 
-  struct in_addr * bind_address;
+  struct sockaddr_in * bind4_address;
+  struct sockaddr_in6 * bind6_address;
+  char * bind_hostname;
+  int use_ipv6;
+  int use_ipv4;
 };
 
 
@@ -538,12 +542,13 @@ process_interfaces (void *cls,
   struct IPv4HttpAddress * t4;
   struct IPv6HttpAddress * t6;
   int af;
-  struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
+
 
   GNUNET_assert(cls !=NULL);
   af = addr->sa_family;
-  if (af == AF_INET)
+  if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
     {
+         struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
       t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
       /* Not skipping loopback addresses
       if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
@@ -554,39 +559,49 @@ process_interfaces (void *cls,
       */
       t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
       t4->u_port = htons (plugin->port_inbound);
-      if (plugin->bind_address != NULL)
+      if (plugin->bind4_address != NULL)
       {
-         if (0 == memcmp(plugin->bind_address, &bnd_cmp, sizeof (struct in_addr)))
+         if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
+         {
                  plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
+         }
       }
       else
       {
          plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
       }
-
+      GNUNET_free (t4);
     }
-  else if ((af == AF_INET6) && (plugin->bind_address == NULL))
+  else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES)  && (plugin->bind4_address == NULL))
     {
+         struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
       t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
       if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
         {
-          /* skip link local addresses */
           return GNUNET_OK;
         }
-      /* Not skipping loopback addresses
-      if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
-        {
 
-          return GNUNET_OK;
-        }
-      */
-      memcpy (&t6->ipv6_addr,
-              &((struct sockaddr_in6 *) addr)->sin6_addr,
-              sizeof (struct in6_addr));
-      t6->u6_port = htons (plugin->port_inbound);
-      plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
+      if (plugin->bind6_address != NULL)
+      {
+         if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
+         {
+             memcpy (&t6->ipv6_addr,
+                     &((struct sockaddr_in6 *) addr)->sin6_addr,
+                     sizeof (struct in6_addr));
+             t6->u6_port = htons (plugin->port_inbound);
+             plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
+         }
+      }
+      else
+      {
+          memcpy (&t6->ipv6_addr,
+                  &((struct sockaddr_in6 *) addr)->sin6_addr,
+                  sizeof (struct in6_addr));
+          t6->u6_port = htons (plugin->port_inbound);
+          plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
+      }
+      GNUNET_free (t6);
     }
-  //return GNUNET_NO;
   return GNUNET_OK;
 }
 
@@ -1795,10 +1810,8 @@ http_plugin_send (void *cls,
 {
   struct Plugin *plugin = cls;
   struct HTTP_Message *msg;
-
   struct HTTP_PeerContext * pc;
   struct Session * ps = NULL;
-  //struct Session * ps_tmp = NULL;
 
   GNUNET_assert(cls !=NULL);
 
@@ -2031,8 +2044,8 @@ http_plugin_address_pretty_printer (void *cls,
   res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
   GNUNET_free (address);
   GNUNET_assert(res != 0);
-
   asc (asc_cls, ret);
+  GNUNET_free_non_null (ret);
 }
 
 
@@ -2209,7 +2222,9 @@ libgnunet_plugin_transport_http_done (void *cls)
     plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
   }
 
-  GNUNET_free_non_null (plugin->bind_address);
+  GNUNET_free_non_null (plugin->bind4_address);
+  GNUNET_free_non_null (plugin->bind6_address);
+  GNUNET_free_non_null(plugin->bind_hostname);
   GNUNET_free (plugin);
   GNUNET_free (api);
 #if DEBUG_HTTP
@@ -2230,7 +2245,6 @@ libgnunet_plugin_transport_http_init (void *cls)
   struct GNUNET_TRANSPORT_PluginFunctions *api;
   struct GNUNET_TIME_Relative gn_timeout;
   long long unsigned int port;
-  char * hostname;
 
   GNUNET_assert(cls !=NULL);
 #if DEBUG_HTTP
@@ -2241,7 +2255,9 @@ libgnunet_plugin_transport_http_init (void *cls)
   plugin->stats = env->stats;
   plugin->env = env;
   plugin->peers = NULL;
-  plugin->bind_address = GNUNET_malloc(sizeof(struct in_addr*));
+  plugin->bind4_address = NULL;
+  plugin->use_ipv6  = GNUNET_YES;
+  plugin->use_ipv4  = GNUNET_YES;
 
   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
   api->cls = plugin;
@@ -2254,6 +2270,22 @@ libgnunet_plugin_transport_http_init (void *cls)
   /* Hashing our identity to use it in URLs */
   GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
 
+  /* Reading port number from config file */
+  if (GNUNET_CONFIGURATION_have_value (env->cfg,
+                                                                  "transport-http", "USE_IPv6"))
+    {
+         plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
+                                                                                                          "transport-http",
+                                                                                                          "USE_IPv6");
+    }
+  /* Reading port number from config file */
+  if (GNUNET_CONFIGURATION_have_value (env->cfg,
+                                                                  "transport-http", "USE_IPv4"))
+    {
+         plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
+                                                                                                          "transport-http",
+                                                                                                          "USE_IPv4");
+    }
   /* Reading port number from config file */
   if ((GNUNET_OK !=
        GNUNET_CONFIGURATION_get_value_number (env->cfg,
@@ -2270,31 +2302,66 @@ libgnunet_plugin_transport_http_init (void *cls)
       return NULL;
     }
 
-  /* Reading port number from config file */
-  if (GNUNET_CONFIGURATION_have_value (env->cfg,
-                                                                  "transport-http", "BINDTO"))
-    {
-      GNUNET_break (GNUNET_OK ==
-                    GNUNET_CONFIGURATION_get_value_string (env->cfg,
-                                                                                          "transport-http",
-                                                           "BINDTO",
-                                                           &hostname));
-      if (inet_aton(hostname, plugin->bind_address)==0)
-      {
-          GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
-                           "http",
-                           _("Misconfigured address to bind to in configuration!\n"),
-                           "transport-http");
-         GNUNET_free(plugin->bind_address);
-         plugin->bind_address = NULL;
-      }
-    }
+  /* Reading ipv4 addresse to bind to from config file */
+  if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
+                                                                  "transport-http", "BINDTO4")))
+  {
+         GNUNET_break (GNUNET_OK ==
+                                       GNUNET_CONFIGURATION_get_value_string (env->cfg,
+                                                                                                                  "transport-http",
+                                                                                                                  "BINDTO4",
+                                                                                                                  &plugin->bind_hostname));
+         plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
+         plugin->bind4_address->sin_family = AF_INET;
+         plugin->bind4_address->sin_port = htons (port);
+
+         if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
+         {
+                 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+                                                  "http",
+                                                  _("Misconfigured address to bind to in configuration!\n"),
+                                                  "transport-http");
+                 GNUNET_free(plugin->bind4_address);
+                 GNUNET_free(plugin->bind_hostname);
+                 plugin->bind_hostname = NULL;
+                 plugin->bind4_address = NULL;
+         }
+  }
+
+  /* Reading ipv4 addresse to bind to from config file */
+  if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
+                                                                  "transport-http", "BINDTO6")))
+  {
+         GNUNET_break (GNUNET_OK ==
+                                       GNUNET_CONFIGURATION_get_value_string (env->cfg,
+                                                                                                                  "transport-http",
+                                                                                                                  "BINDTO6",
+                                                                                                                  &plugin->bind_hostname));
+
+         plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
+         plugin->bind6_address->sin6_family = AF_INET6;
+         plugin->bind6_address->sin6_port = htons (port);
+
+      if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
+         {
+                 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+                                                  "http",
+                                                  _("Misconfigured address to bind to in configuration!\n"),
+                                                  "transport-http");
+                 GNUNET_free(plugin->bind6_address);
+                 GNUNET_free(plugin->bind_hostname);
+                 plugin->bind_hostname = NULL;
+                 plugin->bind6_address = NULL;
+         }
+  }
+
   GNUNET_assert ((port > 0) && (port <= 65535));
   plugin->port_inbound = port;
   gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
   unsigned int timeout = (gn_timeout.value) / 1000;
-  if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
-    {
+  if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
+  {
+       struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
     plugin->http_server_daemon_v6 = MHD_start_daemon (
 #if DEBUG_CONNECTIONS
                                                                   MHD_USE_DEBUG |
@@ -2303,14 +2370,17 @@ libgnunet_plugin_transport_http_init (void *cls)
                                        port,
                                        &mhd_accept_cb,
                                        plugin , &mdh_access_cb, plugin,
+                                       MHD_OPTION_SOCK_ADDR, tmp,
                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
                                        MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
                                        MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
                                        MHD_OPTION_END);
-
-    plugin->http_server_daemon_v4 = MHD_start_daemon (
+  }
+  if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
+  {
+  plugin->http_server_daemon_v4 = MHD_start_daemon (
 #if DEBUG_CONNECTIONS
                                                                   MHD_USE_DEBUG |
 #endif
@@ -2318,14 +2388,14 @@ libgnunet_plugin_transport_http_init (void *cls)
                                        port,
                                        &mhd_accept_cb,
                                        plugin , &mdh_access_cb, plugin,
-                                       //MHD_OPTION_SOCK_ADDR, (struct sockaddr *) &in4,
+                                       MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
                                        MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
                                        MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
                                        MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
                                        MHD_OPTION_END);
-    }
+  }
   if (plugin->http_server_daemon_v4 != NULL)
     plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
   if (plugin->http_server_daemon_v6 != NULL)
@@ -2335,13 +2405,19 @@ libgnunet_plugin_transport_http_init (void *cls)
   if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
   {
 #if DEBUG_HTTP
-         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
+#endif
+  }
+  else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
+  {
+#if DEBUG_HTTP
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
 #endif
   }
-  else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
+  else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
   {
 #if DEBUG_HTTP
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
 #endif
   }
   else