allow empty/NULL context message
[oweals/gnunet.git] / src / transport / plugin_transport_http_server.c
index 3ba651cb7ee93c627d65ca6913e8af0bd529f06d..c43124c2e86652112a7c896603f0a61704b30c80 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     Copyright (C) 2002-2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2002-2014 GNUnet e.V.
 
      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
 {
 
   /**
@@ -374,17 +374,17 @@ struct HTTP_Server_Plugin
   /**
    * MHD IPv4 task
    */
-  struct GNUNET_SCHEDULER_Task * server_v4_task;
+  struct GNUNET_SCHEDULER_Task *server_v4_task;
 
   /**
    * MHD IPv6 task
    */
-  struct GNUNET_SCHEDULER_Task * server_v6_task;
+  struct GNUNET_SCHEDULER_Task *server_v6_task;
 
   /**
    * Task calling transport service about external address
    */
-  struct GNUNET_SCHEDULER_Task * notify_ext_task;
+  struct GNUNET_SCHEDULER_Task *notify_ext_task;
 
   /**
    * Notify transport only about external address
@@ -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;
@@ -491,17 +491,13 @@ notify_session_monitor (struct HTTP_Server_Plugin *plugin,
  * Wake up an MHD connection which was suspended
  *
  * @param cls the session
- * @param tc task context
  */
 static void
-server_wake_up (void *cls,
-                const struct GNUNET_SCHEDULER_TaskContext *tc)
+server_wake_up (void *cls)
 {
-  struct Session *s = cls;
+  struct GNUNET_ATS_Session *s = cls;
 
   s->recv_wakeup_task = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Session %p: Waking up PUT handle\n",
        s);
@@ -529,7 +525,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 +626,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;
@@ -641,13 +637,11 @@ http_server_plugin_disconnect_session (void *cls,
  * Session was idle, so disconnect it
  *
  * @param cls the session
- * @param tc task context
  */
 static void
-server_session_timeout (void *cls,
-                        const struct GNUNET_SCHEDULER_TaskContext *tc)
+server_session_timeout (void *cls)
 {
-  struct Session *s = cls;
+  struct GNUNET_ATS_Session *s = cls;
   struct GNUNET_TIME_Relative left;
 
   s->timeout_task = NULL;
@@ -679,7 +673,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 +709,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 +769,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 +777,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 +796,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 +804,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 +891,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)
 {
@@ -910,17 +904,13 @@ http_server_plugin_get_session (void *cls,
  * and schedule the next run.
  *
  * @param cls plugin as closure
- * @param tc task context
  */
 static void
-server_v4_run (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+server_v4_run (void *cls)
 {
   struct HTTP_Server_Plugin *plugin = cls;
 
   plugin->server_v4_task = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   plugin->server_v4_immediately = GNUNET_NO;
   GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
   server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
@@ -932,17 +922,13 @@ server_v4_run (void *cls,
  * and schedule the next run.
  *
  * @param cls plugin as closure
- * @param tc task context
  */
 static void
-server_v6_run (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+server_v6_run (void *cls)
 {
   struct HTTP_Server_Plugin *plugin = cls;
 
   plugin->server_v6_task = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   plugin->server_v6_immediately = GNUNET_NO;
   GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
   server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
@@ -1127,7 +1113,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 +1128,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 +1291,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 +1308,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 +1318,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,13 +1345,13 @@ 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_PeerIdentity target;
   size_t addr_len;
-  struct SessionTagContext stc;
+  struct GNUNET_ATS_SessionTagContext stc;
   uint32_t options;
   int direction = GNUNET_SYSERR;
   unsigned int to;
@@ -1381,6 +1367,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))
   {
@@ -1441,7 +1428,7 @@ server_lookup_connection (struct HTTP_Server_Plugin *plugin,
       /* external host name */
       return NULL;
     }
-    s = GNUNET_new (struct Session);
+    s = GNUNET_new (struct GNUNET_ATS_Session);
     s->target = target;
     s->plugin = plugin;
     s->scope = scope;
@@ -1547,7 +1534,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;
@@ -1637,7 +1624,7 @@ 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_TIME_Relative delay;
   char *stat_txt;
@@ -1726,7 +1713,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;
 
@@ -1870,8 +1857,10 @@ server_access_cb (void *cls,
         GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
         MHD_suspend_connection (s->server_recv->mhd_conn);
         if (NULL == s->recv_wakeup_task)
-          s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delay,
-              &server_wake_up, s);
+          s->recv_wakeup_task
+           = GNUNET_SCHEDULER_add_delayed (delay,
+                                           &server_wake_up,
+                                           s);
       }
       return MHD_YES;
     }
@@ -2760,7 +2749,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--;
@@ -2843,11 +2832,9 @@ server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
  * Notify server about our external hostname
  *
  * @param cls plugin
- * @param tc task context (unused)
  */
 static void
-server_notify_external_hostname (void *cls,
-                                 const struct GNUNET_SCHEDULER_TaskContext *tc)
+server_notify_external_hostname (void *cls)
 {
   struct HTTP_Server_Plugin *plugin = cls;
   struct HttpAddress *ext_addr;
@@ -2856,14 +2843,10 @@ server_notify_external_hostname (void *cls,
   char *url;
 
   plugin->notify_ext_task = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-
-  GNUNET_asprintf(&url,
-                  "%s://%s",
-                  plugin->protocol,
-                  plugin->external_hostname);
-
+  GNUNET_asprintf (&url,
+                  "%s://%s",
+                  plugin->protocol,
+                  plugin->external_hostname);
   urlen = strlen (url) + 1;
   ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
   ext_addr->options = htonl (plugin->options);
@@ -2882,13 +2865,23 @@ server_notify_external_hostname (void *cls,
          "Enabling SSL verification for external hostname address `%s'\n",
          plugin->external_hostname);
   plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
-      "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
-  plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
+                                                   "https_client",
+                                                   ext_addr,
+                                                   ext_addr_len,
+                                                   GNUNET_HELLO_ADDRESS_INFO_NONE);
+  plugin->env->notify_address (plugin->env->cls,
+                              GNUNET_YES,
+                              plugin->ext_addr);
   GNUNET_free (ext_addr);
 #else
   plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
-      "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
-  plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
+                                                   "http_client",
+                                                   ext_addr,
+                                                   ext_addr_len,
+                                                   GNUNET_HELLO_ADDRESS_INFO_NONE);
+  plugin->env->notify_address (plugin->env->cls,
+                              GNUNET_YES,
+                              plugin->ext_addr);
   GNUNET_free (ext_addr);
 #endif
 }
@@ -3250,18 +3243,36 @@ 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 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);
+}
+
+
 /**
  * Function that will be called whenever the transport service wants to
  * notify the plugin that the inbound quota changed and that the plugin
@@ -3275,7 +3286,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);
@@ -3300,7 +3311,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
@@ -3309,7 +3320,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,
@@ -3392,6 +3403,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;