Renamed GNUNET_OS_process_close to GNUNET_OS_process_destroy
[oweals/gnunet.git] / src / transport / plugin_transport_http_server.c
index c0153e15abb6c63edfa93b9c1b2952c87bd0f0ce..0ec26c88b76dd633d40616726de77128ce351d03 100644 (file)
@@ -30,6 +30,8 @@
 #define _RECEIVE 0
 #define _SEND 1
 
+static struct Plugin * p;
+
 struct ServerConnection
 {
   /* _RECV or _SEND */
@@ -47,10 +49,12 @@ struct ServerConnection
  * starts the task waiting for them.
  * @param plugin plugin
  * @param daemon_handle the MHD daemon handle
+ * @param now schedule now or with MHD delay
  * @return gnunet task identifier
  */
 static GNUNET_SCHEDULER_TaskIdentifier
-server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
+server_schedule (struct Plugin *plugin,
+                 struct MHD_Daemon *daemon_handle,
                  int now);
 
 static void
@@ -60,7 +64,7 @@ server_log (void *arg, const char *fmt, va_list ap)
 
   vsnprintf (text, sizeof (text), fmt, ap);
   va_end (ap);
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: %s\n", text);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
 }
 
 /**
@@ -93,12 +97,13 @@ static char *
 server_load_file (const char *file)
 {
   struct GNUNET_DISK_FileHandle *gn_file;
-  struct stat fstat;
+  uint64_t fsize;
   char *text = NULL;
 
-  if (0 != STAT (file, &fstat))
+  if (GNUNET_OK != GNUNET_DISK_file_size (file,
+      &fsize, GNUNET_NO, GNUNET_YES))
     return NULL;
-  text = GNUNET_malloc (fstat.st_size + 1);
+  text = GNUNET_malloc (fsize + 1);
   gn_file =
       GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
                              GNUNET_DISK_PERM_USER_READ);
@@ -107,13 +112,13 @@ server_load_file (const char *file)
     GNUNET_free (text);
     return NULL;
   }
-  if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
+  if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
   {
     GNUNET_free (text);
     GNUNET_DISK_file_close (gn_file);
     return NULL;
   }
-  text[fstat.st_size] = '\0';
+  text[fsize] = '\0';
   GNUNET_DISK_file_close (gn_file);
   return text;
 }
@@ -154,11 +159,9 @@ server_load_certificate (struct Plugin *plugin)
   }
 
   /* read key & certificates from file */
-#if VERBOSE_SERVER
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
               key_file, cert_file);
-#endif
 
   plugin->key = server_load_file (key_file);
   plugin->cert = server_load_file (cert_file);
@@ -178,7 +181,7 @@ server_load_certificate (struct Plugin *plugin)
 #endif
     errno = 0;
     cert_creation =
-        GNUNET_OS_start_process (NULL, NULL,
+        GNUNET_OS_start_process (GNUNET_NO, NULL, NULL,
                                  "gnunet-transport-certificate-creation",
                                  "gnunet-transport-certificate-creation",
                                  key_file, cert_file, NULL);
@@ -200,7 +203,7 @@ server_load_certificate (struct Plugin *plugin)
       return GNUNET_SYSERR;
     }
     GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
-    GNUNET_OS_process_close (cert_creation);
+    GNUNET_OS_process_destroy (cert_creation);
 
     plugin->key = server_load_file (key_file);
     plugin->cert = server_load_file (cert_file);
@@ -226,10 +229,7 @@ server_load_certificate (struct Plugin *plugin)
   }
   GNUNET_free (key_file);
   GNUNET_free (cert_file);
-#if DEBUG_HTTP
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
-#endif
-
   return res;
 }
 #endif
@@ -238,14 +238,15 @@ server_load_certificate (struct Plugin *plugin)
 /**
  * Reschedule the execution of both IPv4 and IPv6 server
  * @param plugin the plugin
+ * @param server which server to schedule v4 or v6?
  * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
  * until timeout
  */
 
 static void
-server_reschedule (struct Plugin *plugin, int now)
+server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now)
 {
-  if (plugin->server_v4 != NULL)
+  if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
   {
     if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
     {
@@ -255,7 +256,7 @@ server_reschedule (struct Plugin *plugin, int now)
     plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
   }
 
-  if (plugin->server_v6 != NULL)
+  if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
   {
     if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
     {
@@ -278,9 +279,11 @@ server_receive_mst_cb (void *cls, void *client,
 {
   struct Session *s = cls;
 
-#if VERBOSE_SERVER
+  GNUNET_assert (NULL != p);
+  if (GNUNET_NO == exist_session(p, s))
+    return;
+
   struct Plugin *plugin = s->plugin;
-#endif
   struct GNUNET_TIME_Relative delay;
 
   delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
@@ -290,13 +293,11 @@ server_receive_mst_cb (void *cls, void *client,
 
   if (delay.rel_value > 0)
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
                      GNUNET_i2s (&s->target),
                      http_plugin_address_to_string (NULL, s->addr, s->addrlen),
                      delay);
-#endif
   }
 }
 
@@ -312,10 +313,10 @@ static ssize_t
 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
 {
   struct Session *s = cls;
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
-                   "Server: %X can sent maximum  %u \n", s, max);
-#endif
+  GNUNET_assert (NULL != p);
+  if (GNUNET_NO == exist_session(p, s))
+    return 0;
+
   struct HTTP_Message *msg;
   int bytes_read = 0;
 
@@ -347,11 +348,10 @@ server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
     }
   }
 
-#if VERBOSE_CLIENT
   struct Plugin *plugin = s->plugin;
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: %X: sent %u bytes\n", s, bytes_read);
-#endif
+
   return bytes_read;
 }
 
@@ -364,7 +364,7 @@ server_lookup_session (struct Plugin *plugin,
   struct Session *t;
   struct ServerConnection *sc = NULL;
   const union MHD_ConnectionInfo *conn_info;
-
+  struct GNUNET_ATS_Information ats;
   struct IPv4HttpAddress a4;
   struct IPv6HttpAddress a6;
   struct sockaddr_in *s4;
@@ -374,7 +374,7 @@ server_lookup_session (struct Plugin *plugin,
   struct GNUNET_PeerIdentity target;
   int check = GNUNET_NO;
   uint32_t tag = 0;
-  int direction;
+  int direction = GNUNET_SYSERR;
 
   conn_info =
       MHD_get_connection_info (mhd_connection,
@@ -402,23 +402,26 @@ server_lookup_session (struct Plugin *plugin,
 
   if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
     direction = _RECEIVE;
-  if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
+  else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
     direction = _SEND;
+  else
+  {
+    GNUNET_break_op (0);
+    goto error;
+  }
+
 
   if (check == GNUNET_NO)
     goto error;
 
   plugin->cur_connections++;
-
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   "Server: New inbound connection from %s with tag %u\n",
+                   "Server: New %s connection from %s with tag %u\n",
+                   method,
                    GNUNET_i2s (&target), tag);
-#endif
-  /* find duplicate session */
 
+  /* find duplicate session */
   t = plugin->head;
-
   while (t != NULL)
   {
     if ((t->inbound) &&
@@ -431,11 +434,9 @@ server_lookup_session (struct Plugin *plugin,
   }
   if (t != NULL)
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Duplicate session, dismissing new connection from peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
     goto error;
   }
 
@@ -455,20 +456,15 @@ server_lookup_session (struct Plugin *plugin,
 
   if (t == NULL)
     goto create;
-
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: Found existing semi-session for `%s'\n",
                    GNUNET_i2s (&target));
-#endif
 
   if ((direction == _SEND) && (t->server_send != NULL))
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
     goto error;
   }
   else
@@ -477,21 +473,23 @@ server_lookup_session (struct Plugin *plugin,
     GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
                                  plugin->server_semi_tail, s);
     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Found matching semi-session, merging session for peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
+
+    plugin->inbound_sessions ++;
+    GNUNET_STATISTICS_set (plugin->env->stats,
+        "# HTTP inbound sessions",
+        plugin->inbound_sessions,
+        GNUNET_NO);
 
     goto found;
   }
   if ((direction == _RECEIVE) && (t->server_recv != NULL))
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
     goto error;
   }
   else
@@ -500,22 +498,23 @@ server_lookup_session (struct Plugin *plugin,
     GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
                                  plugin->server_semi_tail, s);
     GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: Found matching semi-session, merging session for peer `%s'\n",
                      GNUNET_i2s (&target));
-#endif
+    plugin->inbound_sessions ++;
+    GNUNET_STATISTICS_set (plugin->env->stats,
+        "# HTTP inbound sessions",
+        plugin->inbound_sessions,
+        GNUNET_NO);
+
     goto found;
   }
 
 create:
 /* create new session */
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: Creating new session for peer `%s' \n",
                    GNUNET_i2s (&target));
-#endif
-
   switch (conn_info->client_addr->sa_family)
   {
   case (AF_INET):
@@ -524,6 +523,7 @@ create:
     memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
     a = &a4;
     a_len = sizeof (struct IPv4HttpAddress);
+    ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s4, sizeof (struct sockaddr_in));
     break;
   case (AF_INET6):
     s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
@@ -531,13 +531,14 @@ create:
     memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
     a = &a6;
     a_len = sizeof (struct IPv6HttpAddress);
+    ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s6, sizeof (struct sockaddr_in6));
     break;
   default:
     GNUNET_break (0);
     goto error;
   }
-
   s = create_session (plugin, &target, a, a_len, NULL, NULL);
+  s->ats_address_network_type = ats.value;
 
   s->inbound = GNUNET_YES;
   s->next_receive = GNUNET_TIME_absolute_get_zero ();
@@ -551,10 +552,8 @@ create:
   goto found;
 
 error:
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: Invalid connection request\n");
-#endif
   return NULL;
 
 found:
@@ -570,12 +569,18 @@ found:
 #if MHD_VERSION >= 0x00090E00
   int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
 
-#if VERBOSE_SERVER
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Server: Setting timeout for %X to %u sec.\n", sc, to);
-#endif
   MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
-  server_reschedule (plugin, GNUNET_NO);
+
+  struct MHD_Daemon *d = NULL;
+
+  if (s->addrlen == sizeof (struct IPv6HttpAddress))
+    d = plugin->server_v6;
+  if (s->addrlen == sizeof (struct IPv4HttpAddress))
+    d = plugin->server_v4;
+
+  server_reschedule (plugin, d, GNUNET_NO);
 #endif
   return sc;
 }
@@ -626,14 +631,11 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
   /* connection is to be disconnected */
   if (sc->disconnect == GNUNET_YES)
   {
+    /* Sent HTTP/1.1: 200 OK as PUT Response\ */
     response =
         MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
                                        MHD_NO, MHD_NO);
     res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
-#if VERBOSE_SERVER
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Sent HTTP/1.1: 200 OK as PUT Response\n");
-#endif
     MHD_destroy_response (response);
     return MHD_YES;
   }
@@ -642,7 +644,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
   /* Check if both directions are connected */
   if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
   {
-    (*upload_data_size) = 0;
+    /* Delayed read from since not both semi-connections are connected */
     return MHD_YES;
   }
 
@@ -651,7 +653,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
     response =
         MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
                                            s, NULL);
-    res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
+    MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
     MHD_destroy_response (response);
     return MHD_YES;
   }
@@ -659,39 +661,34 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
   {
     if (*upload_data_size == 0)
     {
-#if VERBOSE_SERVER
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                        "Server: Peer `%s' PUT on address `%s' connected\n",
                        GNUNET_i2s (&s->target),
-                       http_plugin_address_to_string (NULL, s->addr, s->addrlen));
-#endif
+                       http_plugin_address_to_string (NULL, s->addr,
+                                                      s->addrlen));
       return MHD_YES;
     }
 
-    /* Recieving data */
+    /* Receiving data */
     if ((*upload_data_size > 0))
     {
-#if VERBOSE_SERVER
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                        "Server: peer `%s' PUT on address `%s' received %u bytes\n",
                        GNUNET_i2s (&s->target),
-                       http_plugin_address_to_string (NULL,s->addr, s->addrlen),
+                       http_plugin_address_to_string (NULL, s->addr,
+                                                      s->addrlen),
                        *upload_data_size);
-#endif
       struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
 
       if ((s->next_receive.abs_value <= now.abs_value))
       {
-#if VERBOSE_SERVER
         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                          "Server: %X: PUT with %u bytes forwarded to MST\n", s,
                          *upload_data_size);
-#endif
         if (s->msg_tk == NULL)
         {
           s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
         }
-        res =
             GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
                                        *upload_data_size, GNUNET_NO, GNUNET_NO);
 
@@ -699,45 +696,42 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
         int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
         struct ServerConnection *t = NULL;
 
-#if VERBOSE_SERVER
         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                          "Server: Received %u bytes\n", *upload_data_size);
-#endif
-
         /* Setting timeouts for other connections */
         if (s->server_recv != NULL)
         {
           t = s->server_recv;
-#if VERBOSE_SERVER
           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                            "Server: Setting timeout for %X to %u sec.\n", t,
                            to);
-#endif
           MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
                                      to);
         }
         if (s->server_send != NULL)
         {
           t = s->server_send;
-#if VERBOSE_SERVER
           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                            "Server: Setting timeout for %X to %u sec.\n", t,
                            to);
-#endif
           MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
                                      to);
         }
-        server_reschedule (plugin, GNUNET_NO);
+        struct MHD_Daemon *d = NULL;
+
+        if (s->addrlen == sizeof (struct IPv6HttpAddress))
+          d = plugin->server_v6;
+        if (s->addrlen == sizeof (struct IPv4HttpAddress))
+          d = plugin->server_v4;
+        server_reschedule (plugin, d, GNUNET_NO);
 #endif
         (*upload_data_size) = 0;
       }
       else
       {
-#if DEBUG_HTTP
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                     "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
                     s, now.abs_value - s->next_receive.abs_value);
-#endif
       }
       return MHD_YES;
     }
@@ -752,7 +746,7 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
                       void **httpSessionCache)
 {
   struct ServerConnection *sc = *httpSessionCache;
-  struct ServerConnection *tc = *httpSessionCache;
+  struct ServerConnection *tc = NULL;
   struct Session *s = NULL;
   struct Session *t = NULL;
   struct Plugin *plugin = NULL;
@@ -761,15 +755,19 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
     return;
 
   s = sc->session;
+  GNUNET_assert (NULL != p);
+  if (GNUNET_NO == exist_session(p, s))
+    return;
+
   plugin = s->plugin;
   if (sc->direction == _SEND)
   {
-#if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: %X peer `%s' GET on address `%s' disconnected\n",
                      s->server_send, GNUNET_i2s (&s->target),
                      http_plugin_address_to_string (NULL, s->addr, s->addrlen));
-#endif
+
     s->server_send = NULL;
 
     if (s->server_recv != NULL)
@@ -784,12 +782,10 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
   }
   if (sc->direction == _RECEIVE)
   {
-#if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: %X peer `%s' PUT on address `%s' disconnected\n",
                      s->server_recv, GNUNET_i2s (&s->target),
                      http_plugin_address_to_string (NULL, s->addr, s->addrlen));
-#endif
     s->server_recv = NULL;
     if (s->server_send != NULL)
     {
@@ -822,22 +818,32 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
   }
   plugin->cur_connections--;
 
-  server_reschedule (plugin, GNUNET_NO);
+  struct MHD_Daemon *d = NULL;
+
+  if (s->addrlen == sizeof (struct IPv6HttpAddress))
+    d = plugin->server_v6;
+  if (s->addrlen == sizeof (struct IPv4HttpAddress))
+    d = plugin->server_v4;
+  server_reschedule (plugin, d, GNUNET_NO);
 
   if ((s->server_send == NULL) && (s->server_recv == NULL))
   {
-#if VERBOSE_SERVER
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Server: peer `%s' on address `%s' disconnected\n",
                      GNUNET_i2s (&s->target),
                      http_plugin_address_to_string (NULL, s->addr, s->addrlen));
-#endif
     if (s->msg_tk != NULL)
     {
       GNUNET_SERVER_mst_destroy (s->msg_tk);
       s->msg_tk = NULL;
     }
 
+    GNUNET_assert (plugin->inbound_sessions > 0);
+    plugin->inbound_sessions --;
+    GNUNET_STATISTICS_set (plugin->env->stats,
+        "# HTTP inbound sessions",
+        plugin->inbound_sessions, GNUNET_NO);
+
     notify_session_end (s->plugin, &s->target, s);
   }
 }
@@ -870,7 +876,17 @@ int
 server_send (struct Session *s, struct HTTP_Message *msg)
 {
   GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
-  server_reschedule (s->plugin, GNUNET_YES);
+
+  if (s->addrlen == sizeof (struct IPv4HttpAddress))
+  {
+    server_reschedule (s->plugin, s->plugin->server_v4, GNUNET_YES);
+  }
+  else if (s->addrlen == sizeof (struct IPv6HttpAddress))
+  {
+    server_reschedule (s->plugin, s->plugin->server_v6, GNUNET_YES);
+  }
+  else
+    return GNUNET_SYSERR;
   return GNUNET_OK;
 }
 
@@ -893,9 +909,9 @@ server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                       "Running IPv4 server\n");
+#if 0
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                   "Running IPv4 server\n");
 #endif
   GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
   if (plugin->server_v4 != NULL)
@@ -921,9 +937,9 @@ server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                       "Running IPv6 server\n");
+#if 0
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                   "Running IPv6 server\n");
 #endif
   GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
   if (plugin->server_v6 != NULL)
@@ -995,13 +1011,13 @@ server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
       GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
       plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
     }
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                       "Scheduling IPv4 server task in %llu ms\n", tv);
+#if 0
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                     "Scheduling IPv4 server task in %llu ms\n", tv);
 #endif
     ret =
         GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                     GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
+                                     tv, wrs, wws,
                                      &server_v4_run, plugin);
   }
   if (daemon_handle == plugin->server_v6)
@@ -1011,13 +1027,13 @@ server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
       GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
       plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
     }
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                       "Scheduling IPv6 server task in %llu ms\n", tv);
+#if 0
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                     "Scheduling IPv6 server task in %llu ms\n", tv);
 #endif
     ret =
         GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                     GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
+                                     tv, wrs, wws,
                                      &server_v6_run, plugin);
   }
   GNUNET_NETWORK_fdset_destroy (wrs);
@@ -1031,6 +1047,8 @@ server_start (struct Plugin *plugin)
 {
   int res = GNUNET_OK;
   unsigned int timeout;
+  p = plugin;
+  GNUNET_assert (NULL != plugin);
 
 #if BUILD_HTTPS
   res = server_load_certificate (plugin);
@@ -1044,7 +1062,7 @@ server_start (struct Plugin *plugin)
 
 
 #if MHD_VERSION >= 0x00090E00
-  timeout = GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT.rel_value / 1000;
+  timeout = HTTP_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);
@@ -1137,6 +1155,8 @@ server_start (struct Plugin *plugin)
                      plugin->name, plugin->port);
     return GNUNET_SYSERR;
   }
+  server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
+
   if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
   {
     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
@@ -1144,14 +1164,10 @@ server_start (struct Plugin *plugin)
                      plugin->name, plugin->port);
     return GNUNET_SYSERR;
   }
-
-  server_reschedule (plugin, GNUNET_NO);
-
-#if DEBUG_HTTP
+  server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "%s server component started on port %u\n", plugin->name,
                    plugin->port);
-#endif
   return res;
 }
 
@@ -1161,10 +1177,7 @@ server_stop (struct Plugin *plugin)
   struct Session *s = NULL;
   struct Session *t = NULL;
 
-#if VERBOSE_SERVER
-  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                   "server_stop\n");
-#endif
+  p = NULL;
 
   struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
 
@@ -1199,20 +1212,21 @@ server_stop (struct Plugin *plugin)
   while (s != NULL)
   {
 #if VERBOSE_SERVER
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Deleting semi-sessions %p\n", s);
 #endif
     t = s->next;
-    struct HTTP_Message * msg = s->msg_head;
-    struct HTTP_Message * tmp = s->msg_head;
+    struct HTTP_Message *msg = s->msg_head;
+    struct HTTP_Message *tmp = NULL;
+
     while (msg != NULL)
     {
       tmp = msg->next;
 
-      GNUNET_CONTAINER_DLL_remove(s->msg_head,s->msg_tail, msg);
+      GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
       if (msg->transmit_cont != NULL)
       {
-        msg->transmit_cont(msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
+        msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
       }
       GNUNET_free (msg);
       msg = tmp;
@@ -1228,10 +1242,8 @@ server_stop (struct Plugin *plugin)
   GNUNET_free_non_null (plugin->key);
 #endif
 
-#if DEBUG_HTTP
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "%s server component stopped\n", plugin->name);
-#endif
 }