-improve indentation, reduce duplication of PIDs in core's neighbour map
[oweals/gnunet.git] / src / transport / plugin_transport_http_server.c
index c7c1453282a66db46476f2295ffa7810253414fd..0000a604db099eaeea162c10aff3d3525739e8e6 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     (C) 2002-2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2002-2014 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -255,9 +255,9 @@ struct Session
   uint32_t tag;
 
   /**
-   * ATS network type in NBO
+   * ATS network type.
    */
-  uint32_t ats_address_network_type;
+  enum GNUNET_ATS_Network_Type scope;
 
   /**
    * #GNUNET_YES if this session is known to the service.
@@ -604,6 +604,7 @@ server_delete_session (struct Session *s)
     plugin->env->session_end (plugin->env->cls,
                               s->address,
                               s);
+    s->known_to_service = GNUNET_NO;
   }
   if (NULL != s->msg_tk)
   {
@@ -1362,13 +1363,13 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
   struct ServerRequest *sc = NULL;
   const union MHD_ConnectionInfo *conn_info;
   struct HttpAddress *addr;
-  struct GNUNET_ATS_Information ats;
   struct GNUNET_PeerIdentity target;
   size_t addr_len;
   struct SessionTagContext stc;
   uint32_t options;
   int direction = GNUNET_SYSERR;
   unsigned int to;
+  enum GNUNET_ATS_Network_Type scope;
 
   conn_info = MHD_get_connection_info (mhd_connection,
                                        MHD_CONNECTION_INFO_CLIENT_ADDRESS);
@@ -1380,6 +1381,7 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
        method,
        url);
   stc.tag = 0;
+  options = 0; /* make gcc happy */
   if (GNUNET_SYSERR ==
       server_parse_url (plugin, url, &target, &stc.tag, &options))
   {
@@ -1423,36 +1425,32 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
                                               conn_info->client_addr,
                                               sizeof (struct sockaddr_in));
       addr_len = http_common_address_get_size (addr);
-      ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
-      ats.value = htonl (plugin->env->get_address_type (plugin->env->cls,
-                                                        conn_info->client_addr,
-                                                        sizeof (struct sockaddr_in)));
+      scope = plugin->env->get_address_type (plugin->env->cls,
+                                             conn_info->client_addr,
+                                             sizeof (struct sockaddr_in));
       break;
     case (AF_INET6):
       addr = http_common_address_from_socket (plugin->protocol,
                                               conn_info->client_addr,
                                               sizeof (struct sockaddr_in6));
       addr_len = http_common_address_get_size (addr);
-      ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
-      ats.value = htonl (plugin->env->get_address_type (plugin->env->cls,
-                                                        conn_info->client_addr,
-                                                        sizeof (struct sockaddr_in6)));
+      scope = plugin->env->get_address_type (plugin->env->cls,
+                                             conn_info->client_addr,
+                                             sizeof (struct sockaddr_in6));
       break;
     default:
       /* external host name */
-      ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
-      ats.value = htonl (GNUNET_ATS_NET_WAN);
       return NULL;
     }
     s = GNUNET_new (struct Session);
     s->target = target;
     s->plugin = plugin;
+    s->scope = scope;
     s->address = GNUNET_HELLO_address_allocate (&s->target,
                                                 PLUGIN_NAME,
                                                 addr,
                                                 addr_len,
                                                 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
-    s->ats_address_network_type = ats.value;
     s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
     s->tag = stc.tag;
     s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
@@ -1522,30 +1520,14 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
     notify_session_monitor (plugin,
                             s,
                             GNUNET_TRANSPORT_SS_UP);
-    plugin->env->session_start (NULL,
+    plugin->env->session_start (plugin->env->cls,
                                 s->address,
                                 s,
-                                NULL, 0);
+                                s->scope);
   }
 
-  if ( (NULL == s->server_recv) ||
-       (NULL == s->server_send) )
-  {
-    to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
-    MHD_set_connection_option (mhd_connection,
-                              MHD_CONNECTION_OPTION_TIMEOUT, to);
-    server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
-  }
-  else
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Session %p for peer `%s' fully connected\n",
-         s, GNUNET_i2s (&target));
-    to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
-    server_mhd_connection_timeout (plugin, s, to);
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Setting timeout for %p to %u sec.\n", sc, to);
+  to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
+  server_mhd_connection_timeout (plugin, s, to);
   return sc;
 }
 
@@ -1658,23 +1640,16 @@ server_receive_mst_cb (void *cls,
 {
   struct Session *s = cls;
   struct HTTP_Server_Plugin *plugin = s->plugin;
-  struct GNUNET_ATS_Information atsi;
   struct GNUNET_TIME_Relative delay;
   char *stat_txt;
 
-  atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
-  atsi.value = s->ats_address_network_type;
-  GNUNET_break (s->ats_address_network_type !=
-                ntohl (GNUNET_ATS_NET_UNSPECIFIED));
-
   if (GNUNET_NO == s->known_to_service)
   {
     s->known_to_service = GNUNET_YES;
-    plugin->env->session_start (NULL,
+    plugin->env->session_start (plugin->env->cls,
                                 s->address,
                                 s,
-                                NULL,
-                                0);
+                                s->scope);
     notify_session_monitor (plugin,
                             s,
                             GNUNET_TRANSPORT_SS_UP);
@@ -1683,9 +1658,6 @@ server_receive_mst_cb (void *cls,
                                 s->address,
                                 s,
                                 message);
-  plugin->env->update_address_metrics (plugin->env->cls,
-                                       s->address, s,
-                                       &atsi, 1);
   GNUNET_asprintf (&stat_txt,
                    "# bytes received via %s_server",
                    plugin->protocol);
@@ -1790,10 +1762,9 @@ server_access_cb (void *cls,
     else
     {
       /* existing session already has matching connection, refuse */
-      response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
-                                                HTTP_ERROR_RESPONSE,
-                                                MHD_NO,
-                                                MHD_NO);
+      response = MHD_create_response_from_buffer (strlen (HTTP_ERROR_RESPONSE),
+                                                  HTTP_ERROR_RESPONSE,
+                                                  MHD_RESPMEM_PERSISTENT);
       MHD_add_response_header (response,
                               MHD_HTTP_HEADER_CONTENT_TYPE,
                               "text/html");
@@ -1808,8 +1779,9 @@ server_access_cb (void *cls,
   {
     /* Session was already disconnected;
        sent HTTP/1.1: 200 OK as response */
-    response = MHD_create_response_from_data (strlen ("Thank you!"),
-        "Thank you!", MHD_NO, MHD_NO);
+    response = MHD_create_response_from_buffer (strlen ("Thank you!"),
+                                                "Thank you!",
+                                                MHD_RESPMEM_PERSISTENT);
     add_cors_headers(response);
     MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
     MHD_destroy_response (response);
@@ -1852,8 +1824,9 @@ server_access_cb (void *cls,
                                                  s->address->address_length));
       sc->connected = GNUNET_NO;
       /* Sent HTTP/1.1: 200 OK as PUT Response\ */
-      response = MHD_create_response_from_data (strlen ("Thank you!"),
-          "Thank you!", MHD_NO, MHD_NO);
+      response = MHD_create_response_from_buffer (strlen ("Thank you!"),
+                                                  "Thank you!",
+                                                  MHD_RESPMEM_PERSISTENT);
       add_cors_headers(response);
       MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
       MHD_destroy_response (response);
@@ -2788,7 +2761,7 @@ server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
                            plugin->port,
                            (unsigned int) res,
                            (const struct sockaddr **) addrs, addrlens,
-                           &server_nat_port_map_callback, NULL, plugin);
+                           &server_nat_port_map_callback, NULL, plugin, NULL);
   while (res > 0)
   {
     res--;
@@ -3286,7 +3259,7 @@ static enum GNUNET_ATS_Network_Type
 http_server_plugin_get_network (void *cls,
                                 struct Session *session)
 {
-  return ntohl (session->ats_address_network_type);
+  return session->scope;
 }