plugin datastore mysql
[oweals/gnunet.git] / src / hostlist / gnunet-daemon-hostlist_server.c
index 8a7e747c6158187dead01d426849a22670fbcdfe..5c7b8887ec414b6616ea7420c119b8ce40e5afe6 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2008, 2009, 2010, 2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2008, 2009, 2010, 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.
 */
 
 /**
@@ -74,12 +74,12 @@ static struct GNUNET_PEERINFO_NotifyContext *notify;
 /**
  * Our primary task for IPv4.
  */
-static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
+static struct GNUNET_SCHEDULER_Task *hostlist_task_v4;
 
 /**
  * Our primary task for IPv6.
  */
-static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
+static struct GNUNET_SCHEDULER_Task *hostlist_task_v6;
 
 /**
  * Our canonical response.
@@ -101,17 +101,33 @@ static int advertising;
  */
 static char *hostlist_uri;
 
+/**
+ * Map of peer identities to `struct GNUNET_CORE_TransmitHandle *` for
+ * pending hostlist server advertisements.
+ */
+static struct GNUNET_CONTAINER_MultiPeerMap *advertisements;
+
 
 /**
- * Context for host processor.
+ * Context for #host_processor().
  */
 struct HostSet
 {
-  unsigned int size;
+  /**
+   * Iterator used to build @e data (NULL when done).
+   */
+  struct GNUNET_PEERINFO_IteratorContext *pitr;
 
+  /**
+   * Place where we accumulate all of the HELLO messages.
+   */
   char *data;
 
-  struct GNUNET_PEERINFO_IteratorContext *pitr;
+  /**
+   * Number of bytes in @e data.
+   */
+  unsigned int size;
+
 };
 
 
@@ -121,6 +137,27 @@ struct HostSet
 static struct HostSet *builder;
 
 
+/**
+ * Add headers to a request indicating that we allow Cross-Origin Resource
+ * Sharing.
+ *
+ * @param response response to add headers to
+ */
+static void
+add_cors_headers (struct MHD_Response *response)
+{
+  MHD_add_response_header (response,
+                           "Access-Control-Allow-Origin",
+                           "*");
+  MHD_add_response_header (response,
+                           "Access-Control-Allow-Methods",
+                           "GET, OPTIONS");
+  MHD_add_response_header (response,
+                           "Access-Control-Max-Age",
+                           "86400");
+}
+
+
 /**
  * Function that assembles our response.
  */
@@ -133,8 +170,10 @@ finish_response ()
               "Creating hostlist response with %u bytes\n",
               (unsigned int) builder->size);
   response =
-      MHD_create_response_from_data (builder->size, builder->data, MHD_YES,
-                                     MHD_NO);
+      MHD_create_response_from_buffer (builder->size,
+                                       builder->data,
+                                       MHD_RESPMEM_MUST_FREE);
+  add_cors_headers (response);
   if ((NULL == daemon_handle_v4) && (NULL == daemon_handle_v6))
   {
     MHD_destroy_response (response);
@@ -181,7 +220,7 @@ check_has_addr (void *cls,
  * @param cls closure, NULL
  * @param peer id of the peer, NULL for last call
  * @param hello hello message for the peer (can be NULL)
- * @param error message
+ * @param err_msg message
  */
 static void
 host_processor (void *cls,
@@ -199,6 +238,7 @@ host_processor (void *cls,
     builder->pitr = NULL;
     GNUNET_free_non_null (builder->data);
     GNUNET_free (builder);
+    builder = NULL;
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 _("Error in communication with PEERINFO service: %s\n"),
                 err_msg);
@@ -235,21 +275,26 @@ host_processor (void *cls,
               (unsigned int) s,
               "HELLO",
               GNUNET_i2s (peer));
-  if ((old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
-      (old + s >= MAX_BYTES_PER_HOSTLISTS))
+  if ( (old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
+       (old + s >= MAX_BYTES_PER_HOSTLISTS) )
   {
+    /* too large, skip! */
     GNUNET_STATISTICS_update (stats,
                               gettext_noop
                               ("bytes not included in hostlist (size limit)"),
                               s, GNUNET_NO);
-    return;                     /* too large, skip! */
+    return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Adding peer `%s' to hostlist (%u bytes)\n",
               GNUNET_i2s (peer),
               (unsigned int) s);
-  GNUNET_array_grow (builder->data, builder->size, old + s);
-  memcpy (&builder->data[old], hello, s);
+  GNUNET_array_grow (builder->data,
+                     builder->size,
+                     old + s);
+  memcpy (&builder->data[old],
+          hello,
+          s);
 }
 
 
@@ -277,32 +322,12 @@ accept_policy_callback (void *cls,
 }
 
 
-/**
- * Add headers to a request indicating that we allow Cross-Origin Resource
- * Sharing.
- *
- * @param response response to add headers to
- */
-static void
-add_cors_headers (struct MHD_Response *response)
-{
-  MHD_add_response_header (response,
-                           "Access-Control-Allow-Origin",
-                           "*");
-  MHD_add_response_header (response,
-                           "Access-Control-Allow-Methods",
-                           "GET, OPTIONS");
-  MHD_add_response_header (response,
-                           "Access-Control-Max-Age",
-                           "86400");
-}
-
-
 /**
  * Main request handler.
  *
  * @param cls argument given together with the function
  *        pointer when the handler was registered with MHD
+ * @param connection
  * @param url the requested url
  * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
  *        #MHD_HTTP_METHOD_PUT, etc.)
@@ -370,9 +395,7 @@ access_handler_callback (void *cls,
   if (NULL == *con_cls)
   {
     (*con_cls) = &dummy;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Sending 100 CONTINUE reply\n");
-    return MHD_YES;             /* send 100 continue */
+    return MHD_YES;
   }
   if (0 != *upload_data_size)
   {
@@ -400,7 +423,6 @@ access_handler_callback (void *cls,
   GNUNET_STATISTICS_update (stats,
                             gettext_noop ("hostlist requests processed"),
                             1, GNUNET_YES);
-  add_cors_headers (response);
   return MHD_queue_response (connection, MHD_HTTP_OK, response);
 }
 
@@ -408,7 +430,8 @@ access_handler_callback (void *cls,
 /**
  * Handler called by CORE when CORE is ready to transmit message
  *
- * @param cls closure
+ * @param cls closure with the `const struct GNUNET_PeerIdentity *` of
+ *            the peer we are sending to
  * @param size size of buffer to copy message to
  * @param buf buffer to copy message to
  * @return number of bytes copied to @a buf
@@ -418,12 +441,21 @@ adv_transmit_ready (void *cls,
                     size_t size,
                     void *buf)
 {
+  const struct GNUNET_PeerIdentity *peer = cls;
   static uint64_t hostlist_adv_count;
   size_t transmission_size;
   size_t uri_size;              /* Including \0 termination! */
   struct GNUNET_MessageHeader header;
   char *cbuf;
-
+  struct GNUNET_CORE_TransmitHandle *th;
+
+  th = GNUNET_CONTAINER_multipeermap_get (advertisements,
+                                          peer);
+  GNUNET_assert (NULL != th);
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multipeermap_remove (advertisements,
+                                                       peer,
+                                                       th));
   if (NULL == buf)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -435,15 +467,20 @@ adv_transmit_ready (void *cls,
   header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
   header.size = htons (transmission_size);
   GNUNET_assert (size >= transmission_size);
-  memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
+  memcpy (buf,
+          &header,
+          sizeof (struct GNUNET_MessageHeader));
   cbuf = buf;
-  memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)], hostlist_uri, uri_size);
+  memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)],
+          hostlist_uri,
+          uri_size);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Sent advertisement message: Copied %u bytes into buffer!\n",
               (unsigned int) transmission_size);
   hostlist_adv_count++;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " # Sent advertisement message: %u\n",
-              hostlist_adv_count);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              " # Sent advertisement message: %llu\n",
+              (unsigned long long) hostlist_adv_count);
   GNUNET_STATISTICS_update (stats,
                             gettext_noop ("# hostlist advertisements send"), 1,
                             GNUNET_NO);
@@ -462,8 +499,9 @@ connect_handler (void *cls,
                  const struct GNUNET_PeerIdentity *peer)
 {
   size_t size;
+  struct GNUNET_CORE_TransmitHandle *th;
 
-  if (!advertising)
+  if (! advertising)
     return;
   if (NULL == hostlist_uri)
     return;
@@ -482,19 +520,25 @@ connect_handler (void *cls,
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n",
-              size,
+              (unsigned int) size,
               GNUNET_i2s (peer));
   if (NULL ==
-      GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES,
-                                         GNUNET_CORE_PRIO_BEST_EFFORT,
-                                         GNUNET_ADV_TIMEOUT,
-                                         peer,
-                                         size,
-                                         &adv_transmit_ready, NULL))
+      (th = GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES,
+                                               GNUNET_CORE_PRIO_BEST_EFFORT,
+                                               GNUNET_ADV_TIMEOUT,
+                                               peer,
+                                               size,
+                                               &adv_transmit_ready,
+                                               (void *) peer)) )
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 _("Advertisement message could not be queued by core\n"));
   }
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multipeermap_put (advertisements,
+                                                    peer,
+                                                    th,
+                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
 }
 
 
@@ -508,7 +552,19 @@ static void
 disconnect_handler (void *cls,
                     const struct GNUNET_PeerIdentity *peer)
 {
-  /* nothing to do */
+  struct GNUNET_CORE_TransmitHandle *th;
+
+  if (! advertising)
+    return;
+  th = GNUNET_CONTAINER_multipeermap_get (advertisements,
+                                          peer);
+  if (NULL == th)
+    return;
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multipeermap_remove (advertisements,
+                                                       peer,
+                                                       th));
+  GNUNET_CORE_notify_transmit_ready_cancel (th);
 }
 
 
@@ -536,7 +592,11 @@ process_notify (void *cls,
   if (NULL != builder)
   {
     /* restart re-build already in progress ... */
-    GNUNET_PEERINFO_iterate_cancel (builder->pitr);
+    if (NULL != builder->pitr)
+    {
+      GNUNET_PEERINFO_iterate_cancel (builder->pitr);
+      builder->pitr = NULL;
+    }
     GNUNET_free_non_null (builder->data);
     builder->size = 0;
     builder->data = NULL;
@@ -546,8 +606,10 @@ process_notify (void *cls,
     builder = GNUNET_new (struct HostSet);
   }
   GNUNET_assert (NULL != peerinfo);
-  builder->pitr =
-      GNUNET_PEERINFO_iterate (peerinfo, GNUNET_NO, NULL, GNUNET_TIME_UNIT_MINUTES,
+  builder->pitr
+    = GNUNET_PEERINFO_iterate (peerinfo,
+                               GNUNET_NO, NULL,
+                               GNUNET_TIME_UNIT_MINUTES,
                                &host_processor, NULL);
 }
 
@@ -556,7 +618,7 @@ process_notify (void *cls,
  * Function that queries MHD's select sets and
  * starts the task waiting for them.
  */
-static GNUNET_SCHEDULER_TaskIdentifier
+static struct GNUNET_SCHEDULER_Task *
 prepare_daemon (struct MHD_Daemon *daemon_handle);
 
 
@@ -565,21 +627,16 @@ prepare_daemon (struct MHD_Daemon *daemon_handle);
  * and schedule the next run.
  *
  * @param cls the `struct MHD_Daemon` of the HTTP server to run
- * @param tc scheduler context
  */
 static void
-run_daemon (void *cls,
-            const struct GNUNET_SCHEDULER_TaskContext *tc)
+run_daemon (void *cls)
 {
   struct MHD_Daemon *daemon_handle = cls;
 
   if (daemon_handle == daemon_handle_v4)
-    hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
+    hostlist_task_v4 = NULL;
   else
-    hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
+    hostlist_task_v6 = NULL;
   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
   if (daemon_handle == daemon_handle_v4)
     hostlist_task_v4 = prepare_daemon (daemon_handle);
@@ -594,10 +651,10 @@ run_daemon (void *cls,
  *
  * @param daemon_handle HTTP server to prepare to run
  */
-static GNUNET_SCHEDULER_TaskIdentifier
+static struct GNUNET_SCHEDULER_Task *
 prepare_daemon (struct MHD_Daemon *daemon_handle)
 {
-  GNUNET_SCHEDULER_TaskIdentifier ret;
+  struct GNUNET_SCHEDULER_Task * ret;
   fd_set rs;
   fd_set ws;
   fd_set es;
@@ -614,7 +671,9 @@ prepare_daemon (struct MHD_Daemon *daemon_handle)
   wrs = GNUNET_NETWORK_fdset_create ();
   wws = GNUNET_NETWORK_fdset_create ();
   max = -1;
-  GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
+  GNUNET_assert (MHD_YES ==
+                MHD_get_fdset (daemon_handle,
+                               &rs, &ws, &es, &max));
   haveto = MHD_get_timeout (daemon_handle, &timeout);
   if (haveto == MHD_YES)
     tv.rel_value_us = (uint64_t) timeout * 1000LL;
@@ -622,10 +681,9 @@ prepare_daemon (struct MHD_Daemon *daemon_handle)
     tv = GNUNET_TIME_UNIT_FOREVER_REL;
   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
-  ret =
-      GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
-                                  tv, wrs, wws,
-                                   &run_daemon, daemon_handle);
+  ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
+                                    tv, wrs, wws,
+                                    &run_daemon, daemon_handle);
   GNUNET_NETWORK_fdset_destroy (wrs);
   GNUNET_NETWORK_fdset_destroy (wws);
   return ret;
@@ -638,8 +696,8 @@ prepare_daemon (struct MHD_Daemon *daemon_handle)
  * @param c configuration to use
  * @param st statistics handle to use
  * @param co core handle to use
- * @param server_ch[OUT] set to handler for CORE connect events
- * @param server_dh[OUT] set to handler for CORE disconnect events
+ * @param[out] server_ch set to handler for CORE connect events
+ * @param[out] server_dh set to handler for CORE disconnect events
  * @param advertise #GNUNET_YES if we should advertise our hostlist
  * @return #GNUNET_OK on success
  */
@@ -665,11 +723,17 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
 
   advertising = advertise;
   if (! advertising)
+  {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Advertising not enabled on this hostlist server\n");
+  }
   else
+  {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Advertising enabled on this hostlist server\n");
+    advertisements = GNUNET_CONTAINER_multipeermap_create (8,
+                                                           GNUNET_NO);
+  }
   cfg = c;
   stats = st;
   peerinfo = GNUNET_PEERINFO_connect (cfg);
@@ -723,17 +787,26 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
 
   if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV4"))
   {
-    GNUNET_break (GNUNET_OK ==
-                  GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
-                                                         "BINDTOIP", &ipv4));
+    if (GNUNET_OK !=
+        GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
+                                               "BINDTOIP", &ipv4))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV4.\n"));
+    }
+
   }
   else
     ipv4 = NULL;
   if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV6"))
   {
-    GNUNET_break (GNUNET_OK ==
-                  GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
-                                                         "BINDTOIP", &ipv6));
+    if (GNUNET_OK !=
+        GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
+                                               "BINDTOIP", &ipv6))
+      {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+          _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n"));
+    }
   }
   else
     ipv6 = NULL;
@@ -783,9 +856,9 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
                                        &accept_policy_callback, NULL,
                                        &access_handler_callback, NULL,
                                        MHD_OPTION_CONNECTION_LIMIT,
-                                       (unsigned int) 16,
+                                       (unsigned int) 128,
                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT,
-                                       (unsigned int) 1,
+                                       (unsigned int) 32,
                                        MHD_OPTION_CONNECTION_TIMEOUT,
                                        (unsigned int) 16,
                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
@@ -798,9 +871,9 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
                                        &accept_policy_callback, NULL,
                                        &access_handler_callback, NULL,
                                        MHD_OPTION_CONNECTION_LIMIT,
-                                       (unsigned int) 16,
+                                       (unsigned int) 128,
                                        MHD_OPTION_PER_IP_CONNECTION_LIMIT,
-                                       (unsigned int) 1,
+                                       (unsigned int) 32,
                                        MHD_OPTION_CONNECTION_TIMEOUT,
                                        (unsigned int) 16,
                                        MHD_OPTION_CONNECTION_MEMORY_LIMIT,
@@ -825,7 +898,8 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
     hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
   if (NULL != daemon_handle_v6)
     hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
-  notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO,
+  notify = GNUNET_PEERINFO_notify (cfg,
+                                   GNUNET_NO,
                                    &process_notify, NULL);
   return GNUNET_OK;
 }
@@ -837,16 +911,17 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
 void
 GNUNET_HOSTLIST_server_stop ()
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist server shutdown\n");
-  if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Hostlist server shutdown\n");
+  if (NULL != hostlist_task_v6)
   {
     GNUNET_SCHEDULER_cancel (hostlist_task_v6);
-    hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
+    hostlist_task_v6 = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
+  if (NULL != hostlist_task_v4)
   {
     GNUNET_SCHEDULER_cancel (hostlist_task_v4);
-    hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
+    hostlist_task_v4 = NULL;
   }
   if (NULL != daemon_handle_v4)
   {
@@ -870,16 +945,27 @@ GNUNET_HOSTLIST_server_stop ()
   }
   if (NULL != builder)
   {
-    GNUNET_PEERINFO_iterate_cancel (builder->pitr);
-    builder->pitr = NULL;
+    if (NULL != builder->pitr)
+    {
+      GNUNET_PEERINFO_iterate_cancel (builder->pitr);
+      builder->pitr = NULL;
+    }
     GNUNET_free_non_null (builder->data);
     GNUNET_free (builder);
+    builder = NULL;
   }
   if (NULL != peerinfo)
   {
     GNUNET_PEERINFO_disconnect (peerinfo);
     peerinfo = NULL;
   }
+  if (NULL != advertisements)
+  {
+    GNUNET_break (0 ==
+                  GNUNET_CONTAINER_multipeermap_size (advertisements));
+    GNUNET_CONTAINER_multipeermap_destroy (advertisements);
+    advertisements = NULL;
+  }
   cfg = NULL;
   stats = NULL;
   core = NULL;