-fix NPE
[oweals/gnunet.git] / src / transport / plugin_transport_http_server.c
index fc35b08fb6a6dbf565cb9bbb3485c67f835108d0..07137e0fba18da7601a52c8a253262ed8c768bc6 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.
 */
 
 /**
@@ -63,7 +63,7 @@ struct ServerRequest
    * The session this server request belongs to
    * Can be NULL, when session was disconnected and freed
    */
-  struct Session *session;
+  struct GNUNET_ATS_Session *session;
 
   /**
    * The MHD connection
@@ -174,7 +174,7 @@ struct HTTP_Message
 /**
  * Session handle for connections.
  */
-struct Session
+struct GNUNET_ATS_Session
 {
 
   /**
@@ -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.
@@ -466,7 +466,7 @@ struct HTTP_Server_Plugin
  */
 static void
 notify_session_monitor (struct HTTP_Server_Plugin *plugin,
-                        struct Session *session,
+                        struct GNUNET_ATS_Session *session,
                         enum GNUNET_TRANSPORT_SessionState state)
 {
   struct GNUNET_TRANSPORT_SessionInfo info;
@@ -497,7 +497,7 @@ static void
 server_wake_up (void *cls,
                 const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct Session *s = cls;
+  struct GNUNET_ATS_Session *s = cls;
 
   s->recv_wakeup_task = NULL;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
@@ -529,7 +529,7 @@ server_reschedule (struct HTTP_Server_Plugin *plugin,
  * @param s the session to delete
  */
 static void
-server_delete_session (struct Session *s)
+server_delete_session (struct GNUNET_ATS_Session *s)
 {
   struct HTTP_Server_Plugin *plugin = s->plugin;
   struct HTTP_Message *msg;
@@ -630,7 +630,7 @@ server_delete_session (struct Session *s)
  */
 static int
 http_server_plugin_disconnect_session (void *cls,
-                                       struct Session *s)
+                                       struct GNUNET_ATS_Session *s)
 {
   server_delete_session (s);
   return GNUNET_OK;
@@ -647,7 +647,7 @@ static void
 server_session_timeout (void *cls,
                         const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct Session *s = cls;
+  struct GNUNET_ATS_Session *s = cls;
   struct GNUNET_TIME_Relative left;
 
   s->timeout_task = NULL;
@@ -679,7 +679,7 @@ server_session_timeout (void *cls,
  * @param s the session
  */
 static void
-server_reschedule_session_timeout (struct Session *s)
+server_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
 {
  GNUNET_assert (NULL != s->timeout_task);
   s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
@@ -715,7 +715,7 @@ server_reschedule_session_timeout (struct Session *s)
  */
 static ssize_t
 http_server_plugin_send (void *cls,
-                         struct Session *session,
+                         struct GNUNET_ATS_Session *session,
                          const char *msgbuf,
                          size_t msgbuf_size,
                          unsigned int priority,
@@ -775,7 +775,7 @@ http_server_plugin_send (void *cls,
  *
  * @param cls the `struct HTTP_Server_Plugin *`
  * @param peer for which this is a session
- * @param value the `struct Session` to clean up
+ * @param value the `struct GNUNET_ATS_Session` to clean up
  * @return #GNUNET_OK (continue to iterate)
  */
 static int
@@ -783,7 +783,7 @@ destroy_session_shutdown_cb (void *cls,
                     const struct GNUNET_PeerIdentity *peer,
                     void *value)
 {
-  struct Session *s = value;
+  struct GNUNET_ATS_Session *s = value;
   struct ServerRequest *sc_send;
   struct ServerRequest *sc_recv;
 
@@ -802,7 +802,7 @@ destroy_session_shutdown_cb (void *cls,
  *
  * @param cls the `struct HTTP_Server_Plugin *`
  * @param peer for which this is a session
- * @param value the `struct Session` to clean up
+ * @param value the `struct GNUNET_ATS_Session` to clean up
  * @return #GNUNET_OK (continue to iterate)
  */
 static int
@@ -810,7 +810,7 @@ destroy_session_cb (void *cls,
                     const struct GNUNET_PeerIdentity *peer,
                     void *value)
 {
-  struct Session *s = value;
+  struct GNUNET_ATS_Session *s = value;
 
   server_delete_session (s);
   return GNUNET_OK;
@@ -897,7 +897,7 @@ http_server_plugin_address_suggested (void *cls,
  * @param address the address
  * @return always NULL
  */
-static struct Session *
+static struct GNUNET_ATS_Session *
 http_server_plugin_get_session (void *cls,
                                 const struct GNUNET_HELLO_Address *address)
 {
@@ -1127,7 +1127,7 @@ http_server_query_keepalive_factor (void *cls)
 static void
 http_server_plugin_update_session_timeout (void *cls,
                                            const struct GNUNET_PeerIdentity *peer,
-                                           struct Session *session)
+                                           struct GNUNET_ATS_Session *session)
 {
   server_reschedule_session_timeout (session);
 }
@@ -1142,7 +1142,7 @@ http_server_plugin_update_session_timeout (void *cls,
  */
 static void
 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
-                              struct Session *s,
+                              struct GNUNET_ATS_Session *s,
                               unsigned int to)
 {
   /* Setting timeouts for other connections */
@@ -1305,12 +1305,12 @@ server_parse_url (struct HTTP_Server_Plugin *plugin,
 /**
  * Closure for #session_tag_it().
  */
-struct SessionTagContext
+struct GNUNET_ATS_SessionTagContext
 {
   /**
    * Set to session matching the tag.
    */
-  struct Session *res;
+  struct GNUNET_ATS_Session *res;
 
   /**
    * Tag we are looking for.
@@ -1322,9 +1322,9 @@ struct SessionTagContext
 /**
  * Find a session with a matching tag.
  *
- * @param cls the `struct SessionTagContext *`
+ * @param cls the `struct GNUNET_ATS_SessionTagContext *`
  * @param key peer identity (unused)
- * @param value the `struct Session *`
+ * @param value the `struct GNUNET_ATS_Session *`
  * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
  */
 static int
@@ -1332,8 +1332,8 @@ session_tag_it (void *cls,
                 const struct GNUNET_PeerIdentity *key,
                 void *value)
 {
-  struct SessionTagContext *stc = cls;
-  struct Session *s = value;
+  struct GNUNET_ATS_SessionTagContext *stc = cls;
+  struct GNUNET_ATS_Session *s = value;
 
   if (s->tag == stc->tag)
   {
@@ -1359,17 +1359,17 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
                           const char *url,
                           const char *method)
 {
-  struct Session *s = NULL;
+  struct GNUNET_ATS_Session *s = NULL;
   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;
+  struct GNUNET_ATS_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);
@@ -1381,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))
   {
@@ -1424,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 = GNUNET_new (struct GNUNET_ATS_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);
@@ -1526,27 +1523,11 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
     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;
 }
 
@@ -1567,7 +1548,7 @@ server_send_callback (void *cls,
                       size_t max)
 {
   struct ServerRequest *sc = cls;
-  struct Session *s = sc->session;
+  struct GNUNET_ATS_Session *s = sc->session;
   ssize_t bytes_read = 0;
   struct HTTP_Message *msg;
   char *stat_txt;
@@ -1657,25 +1638,18 @@ server_receive_mst_cb (void *cls,
                        void *client,
                        const struct GNUNET_MessageHeader *message)
 {
-  struct Session *s = cls;
+  struct GNUNET_ATS_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 (plugin->env->cls,
                                 s->address,
                                 s,
-                                NULL,
-                                0);
+                                s->scope);
     notify_session_monitor (plugin,
                             s,
                             GNUNET_TRANSPORT_SS_UP);
@@ -1684,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);
@@ -1756,7 +1727,7 @@ server_access_cb (void *cls,
 {
   struct HTTP_Server_Plugin *plugin = cls;
   struct ServerRequest *sc = *httpSessionCache;
-  struct Session *s;
+  struct GNUNET_ATS_Session *s;
   struct MHD_Response *response;
   int res = MHD_YES;
 
@@ -1791,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");
@@ -1809,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);
@@ -1853,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);
@@ -2789,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--;
@@ -3279,15 +3251,33 @@ http_server_plugin_address_to_string (void *cls,
 /**
  * Function obtain the network type for a session
  *
- * @param cls closure ('struct HTTP_Server_Plugin*')
+ * @param cls closure (`struct HTTP_Server_Plugin *`)
  * @param session the session
  * @return the network type in HBO or #GNUNET_SYSERR
  */
 static enum GNUNET_ATS_Network_Type
 http_server_plugin_get_network (void *cls,
-                                struct Session *session)
+                                struct GNUNET_ATS_Session *session)
 {
-  return ntohl (session->ats_address_network_type);
+  return session->scope;
+}
+
+
+/**
+ * Function obtain the network type for an address.
+ *
+ * @param cls closure (`struct Plugin *`)
+ * @param address the address
+ * @return the network type
+ */
+static enum GNUNET_ATS_Network_Type
+http_server_plugin_get_network_for_address (void *cls,
+                                            const struct GNUNET_HELLO_Address *address)
+{
+  struct HTTP_Server_Plugin *plugin = cls;
+
+  return http_common_get_network_for_address (plugin->env,
+                                              address);
 }
 
 
@@ -3304,7 +3294,7 @@ http_server_plugin_get_network (void *cls,
 static void
 http_server_plugin_update_inbound_delay (void *cls,
                                          const struct GNUNET_PeerIdentity *peer,
-                                         struct Session *session,
+                                         struct GNUNET_ATS_Session *session,
                                          struct GNUNET_TIME_Relative delay)
 {
   session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
@@ -3329,7 +3319,7 @@ http_server_plugin_update_inbound_delay (void *cls,
  *
  * @param cls the `struct Plugin` with the monitor callback (`sic`)
  * @param peer peer we send information about
- * @param value our `struct Session` to send information about
+ * @param value our `struct GNUNET_ATS_Session` to send information about
  * @return #GNUNET_OK (continue to iterate)
  */
 static int
@@ -3338,7 +3328,7 @@ send_session_info_iter (void *cls,
                         void *value)
 {
   struct HTTP_Server_Plugin *plugin = cls;
-  struct Session *session = value;
+  struct GNUNET_ATS_Session *session = value;
 
   notify_session_monitor (plugin,
                           session,
@@ -3421,6 +3411,7 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
   api->string_to_address = &http_common_plugin_string_to_address;
   api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
   api->get_network = &http_server_plugin_get_network;
+  api->get_network_for_address = &http_server_plugin_get_network_for_address;
   api->update_session_timeout = &http_server_plugin_update_session_timeout;
   api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
   api->setup_monitor = &http_server_plugin_setup_monitor;