fix
[oweals/gnunet.git] / src / transport / plugin_transport_http_server.c
index 8161df21d24ddbc3f9c456c32c586bdc07ed6f90..2ce844307eb1e14c5d026866055984270579dd04 100644 (file)
@@ -814,34 +814,44 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
 
   if (url_len < 105)
   {
+    GNUNET_break (0);
     goto error; /* too short */
   }
   hash_start = strrchr (url, '/');
   if (NULL == hash_start)
   {
+      GNUNET_break (0);
     goto error; /* '/' delimiter not found */
   }
   if (hash_start >= url_end)
   {
+      GNUNET_break (0);
     goto error; /* mal formed */
   }
   hash_start++;
 
   hash_end = strrchr (hash_start, ';');
   if (NULL == hash_end)
+  {
+    GNUNET_break (0);
     goto error; /* ';' delimiter not found */
+  }
+
   if (hash_end >= url_end)
   {
+      GNUNET_break (0);
     goto error; /* mal formed */
   }
 
   if (hash_start >= hash_end)
   {
+      GNUNET_break (0);
     goto error; /* mal formed */
   }
 
   if ((strlen(hash_start) - strlen(hash_end)) != 103)
   {
+      GNUNET_break (0);
     goto error; /* invalid hash length */
   }
 
@@ -850,11 +860,13 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
   hash[103] = '\0';
   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target.hashPubKey)))
   {
+      GNUNET_break (0);
     goto error; /* mal formed */
   }
 
   if (hash_end >= url_end)
   {
+      GNUNET_break (0);
     goto error; /* mal formed */
   }
 
@@ -864,14 +876,17 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
   tag = strtoul (tag_start, &tag_end, 10);
   if (tag == 0)
   {
+      GNUNET_break (0);
     goto error; /* mal formed */
   }
   if (tag_end == NULL)
   {
+      GNUNET_break (0);
     goto error; /* mal formed */
   }
   if (tag_end != url_end)
   {
+      GNUNET_break (0);
     goto error; /* mal formed */
   }
 
@@ -975,7 +990,7 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
 #if MHD_VERSION >= 0x00090E00
   if ((NULL == s->server_recv) || (NULL == s->server_send))
   {
-    to = (HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
+    to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
     MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
     server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
   }
@@ -984,7 +999,7 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Session %p for peer `%s' fully connected\n",
                      s, GNUNET_i2s (&target));
-    to = (TIMEOUT.rel_value / 1000);
+    to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
     server_mhd_connection_timeout (plugin, s, to);
   }
 
@@ -1066,7 +1081,7 @@ server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
     }
   }
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
-                   "Sent %u bytes\n", s, bytes_read);
+                   "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
 
 
 
@@ -1219,7 +1234,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
       if ((s->next_receive.abs_value <= now.abs_value))
       {
         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                         "PUT with %u bytes forwarded to MST\n", s,
+                         "PUT with %u bytes forwarded to MST\n",
                          *upload_data_size);
         if (s->msg_tk == NULL)
         {
@@ -1674,12 +1689,12 @@ server_start (struct HTTP_Server_Plugin *plugin)
 
 
 #if MHD_VERSION >= 0x00090E00
-  timeout = HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
+  timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "MHD can set timeout per connection! Default time out %u sec.\n",
                    timeout);
 #else
-  timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
+  timeout = SERVER_SESSION_TIMEOUT.rel_value / 1000;
   GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
                    "MHD cannot set timeout per connection! Default time out %u sec.\n",
                    timeout);
@@ -1787,12 +1802,6 @@ server_start (struct HTTP_Server_Plugin *plugin)
 void
 server_stop (struct HTTP_Server_Plugin *plugin)
 {
-  struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
-  plugin->server_v4 = NULL;
-
-  struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
-  plugin->server_v6 = NULL;
-
   if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
   {
     GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
@@ -1805,13 +1814,15 @@ server_stop (struct HTTP_Server_Plugin *plugin)
     plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
   }
 
-  if (server_v6_tmp != NULL)
+  if (plugin->server_v4 != NULL)
   {
-    MHD_stop_daemon (server_v4_tmp);
+    MHD_stop_daemon (plugin->server_v4);
+    plugin->server_v4 = NULL;
   }
-  if (server_v6_tmp != NULL)
+  if ( plugin->server_v6 != NULL)
   {
-    MHD_stop_daemon (server_v6_tmp);
+    MHD_stop_daemon (plugin->server_v6);
+    plugin->server_v6 = NULL;
   }
 
   p = NULL;
@@ -1833,12 +1844,6 @@ server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
   struct HTTP_Server_Plugin *plugin = cls;
   struct HttpAddressWrapper *w = NULL;
 
-  if ((AF_INET == addr->sa_family) && (GNUNET_NO == plugin->use_ipv4))
-    return;
-
-  if ((AF_INET6 == addr->sa_family) && (GNUNET_NO == plugin->use_ipv6))
-    return;
-
   w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
   w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
   if (NULL == w->addr)
@@ -1908,10 +1913,45 @@ server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *
   struct HTTP_Server_Plugin *plugin = cls;
 
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   "NPMC called %s to address `%s'\n",
+                   "NAT called to %s address `%s'\n",
                    (add_remove == GNUNET_NO) ? "remove" : "add",
                    GNUNET_a2s (addr, addrlen));
 
+  if (AF_INET == addr->sa_family)
+  {
+    struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
+
+    if (GNUNET_NO == plugin->use_ipv4)
+      return;
+
+    if ((NULL != plugin->server_addr_v4) &&
+        (0 != memcmp (&plugin->server_addr_v4->sin_addr,
+                      &s4->sin_addr, sizeof (struct in_addr))))
+    {
+        GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                         "Skipping address `%s' (not bindto address)\n",
+                         GNUNET_a2s (addr, addrlen));
+      return;
+    }
+  }
+
+  if (AF_INET6 == addr->sa_family)
+  {
+    struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
+    if (GNUNET_NO == plugin->use_ipv6)
+      return;
+
+    if ((NULL != plugin->server_addr_v6) &&
+        (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
+                      &s6->sin6_addr, sizeof (struct in6_addr))))
+    {
+        GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                         "Skipping address `%s' (not bindto address)\n",
+                         GNUNET_a2s (addr, addrlen));
+        return;
+    }
+  }
+
   switch (add_remove)
   {
   case GNUNET_YES:
@@ -2378,7 +2418,7 @@ server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
   s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_log (TIMEOUT_LOG,
               "Session %p was idle for %llu ms, disconnecting\n",
-              s, (unsigned long long) TIMEOUT.rel_value);
+              s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
 
   /* call session destroy function */
  GNUNET_assert (GNUNET_OK == server_disconnect (s));
@@ -2392,12 +2432,12 @@ server_start_session_timeout (struct Session *s)
 {
  GNUNET_assert (NULL != s);
  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
- s->timeout_task =  GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+ s->timeout_task =  GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
                                                   &server_session_timeout,
                                                   s);
  GNUNET_log (TIMEOUT_LOG,
              "Timeout for session %p set to %llu ms\n",
-             s,  (unsigned long long) TIMEOUT.rel_value);
+             s,  (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
 }
 
 
@@ -2411,12 +2451,12 @@ server_reschedule_session_timeout (struct Session *s)
  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
 
  GNUNET_SCHEDULER_cancel (s->timeout_task);
- s->timeout_task =  GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+ s->timeout_task =  GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
                                                   &server_session_timeout,
                                                   s);
  GNUNET_log (TIMEOUT_LOG,
              "Timeout rescheduled for session %p set to %llu ms\n",
-             s, (unsigned long long) TIMEOUT.rel_value);
+             s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
 }
 
 /**
@@ -2432,10 +2472,15 @@ LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
 
   if (NULL == api->cls)
   {
+    /* Free for stub mode */
     GNUNET_free (api);
     return NULL;
   }
 
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                   _("Shutting down plugin `%s'\n"),
+                   plugin->name);
+
   if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
   {
       GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
@@ -2475,6 +2520,10 @@ LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
   GNUNET_free_non_null (plugin->server_addr_v4);
   GNUNET_free_non_null (plugin->server_addr_v6);
 
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                   _("Shutdown for plugin `%s' complete\n"),
+                   plugin->name);
+
   GNUNET_free (plugin);
   GNUNET_free (api);
   return NULL;