indentation fixes
[oweals/gnunet.git] / src / dht / gnunet-service-dht_clients.c
index 8790d8fbbbd81b25a1c07e30528c5bc523b1d895..df56c010a57c7b1ed61c4fadb14f6a2805fd80e3 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2010, 2011, 2016 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.
 */
 
 /**
  */
 
 #include "platform.h"
-#include "gnunet_block_lib.h"
-#include "gnunet_util_lib.h"
+#include "gnunet_constants.h"
 #include "gnunet_protocols.h"
-#include "gnunet_nse_service.h"
-#include "gnunet_core_service.h"
-#include "gnunet_datacache_lib.h"
-#include "gnunet_transport_service.h"
-#include "gnunet_hello_lib.h"
-#include "gnunet_dht_service.h"
 #include "gnunet_statistics_service.h"
-#include "dht_new.h"
-#include <fenv.h>
-#include "gnunet-service-dht_clients.h"
+#include "gnunet-service-dht.h"
 #include "gnunet-service-dht_datacache.h"
 #include "gnunet-service-dht_neighbours.h"
+#include "dht.h"
 
 
 /**
- * Linked list of messages to send to clients.
+ * Should routing details be logged to stderr (for debugging)?
  */
-struct PendingMessage
-{
-  /**
-   * Pointer to next item in the list
-   */
-  struct PendingMessage *next;
+#define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
 
-  /**
-   * Pointer to previous item in the list
-   */
-  struct PendingMessage *prev;
-
-  /**
-   * Actual message to be sent, allocated at the end of the struct:
-   * // msg = (cast) &pm[1]; 
-   * // memcpy (&pm[1], data, len);
-   */
-  const struct GNUNET_MessageHeader *msg;
-
-};
+#define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
 
 
 /**
@@ -73,57 +48,34 @@ struct PendingMessage
  * handle to connect to it, and any pending messages
  * that need to be sent to it.
  */
-struct ClientList
-{
-  /**
-   * Linked list of active clients
-   */
-  struct ClientList *next;
+struct ClientHandle;
 
-  /**
-   * Linked list of active clients
-   */
-  struct ClientList *prev;
 
-  /**
-   * The handle to this client
-   */
-  struct GNUNET_SERVER_Client *client_handle;
-
-  /**
-   * Handle to the current transmission request, NULL
-   * if none pending.
-   */
-  struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
+/**
+ * Entry in the local forwarding map for a client's GET request.
+ */
+struct ClientQueryRecord
+{
 
   /**
-   * Linked list of pending messages for this client
+   * The key this request was about
    */
-  struct PendingMessage *pending_head;
+  struct GNUNET_HashCode key;
 
   /**
-   * Tail of linked list of pending messages for this client
+   * Kept in a DLL with @e client.
    */
-  struct PendingMessage *pending_tail;
-
-};
-
-
-/**
- * Entry in the DHT routing table for a client's GET request.
- */
-struct ClientQueryRecord
-{
+  struct ClientQueryRecord *next;
 
   /**
-   * The key this request was about
+   * Kept in a DLL with @e client.
    */
-  GNUNET_HashCode key;
+  struct ClientQueryRecord *prev;
 
   /**
    * Client responsible for the request.
    */
-  struct ClientList *client;
+  struct ClientHandle *ch;
 
   /**
    * Extended query (see gnunet_block_lib.h), allocated at the end of this struct.
@@ -133,7 +85,7 @@ struct ClientQueryRecord
   /**
    * Replies we have already seen for this request.
    */
-  GNUNET_HashCode *seen_replies;
+  struct GNUNET_HashCode *seen_replies;
 
   /**
    * Pointer to this nodes heap location in the retry-heap (for fast removal)
@@ -179,23 +131,120 @@ struct ClientQueryRecord
   /**
    * The type for the data for the GET request.
    */
-  enum GNUNET_BLOCK_Type msg_type;
+  enum GNUNET_BLOCK_Type type;
+
+};
+
+
+/**
+ * Struct containing paremeters of monitoring requests.
+ */
+struct ClientMonitorRecord
+{
+
+  /**
+   * Next element in DLL.
+   */
+  struct ClientMonitorRecord *next;
+
+  /**
+   * Previous element in DLL.
+   */
+  struct ClientMonitorRecord *prev;
+
+  /**
+   * Type of blocks that are of interest
+   */
+  enum GNUNET_BLOCK_Type type;
+
+  /**
+   * Key of data of interest, NULL for all.
+   */
+  struct GNUNET_HashCode *key;
+
+  /**
+   * Flag whether to notify about GET messages.
+   */
+  int16_t get;
+
+  /**
+   * Flag whether to notify about GET_REPONSE messages.
+   */
+  int16_t get_resp;
+
+  /**
+   * Flag whether to notify about PUT messages.
+   */
+  uint16_t put;
+
+  /**
+   * Client to notify of these requests.
+   */
+  struct ClientHandle *ch;
+};
+
+
+/**
+ * Struct containing information about a client,
+ * handle to connect to it, and any pending messages
+ * that need to be sent to it.
+ */
+struct ClientHandle
+{
+  /**
+   * Linked list of active queries of this client.
+   */
+  struct ClientQueryRecord *cqr_head;
+
+  /**
+   * Linked list of active queries of this client.
+   */
+  struct ClientQueryRecord *cqr_tail;
+
+  /**
+   * The handle to this client
+   */
+  struct GNUNET_SERVICE_Client *client;
+
+  /**
+   * The message queue to this client
+   */
+  struct GNUNET_MQ_Handle *mq;
 
 };
 
+/**
+ * Our handle to the BLOCK library.
+ */
+struct GNUNET_BLOCK_Context *GDS_block_context;
+
+/**
+ * Handle for the statistics service.
+ */
+struct GNUNET_STATISTICS_Handle *GDS_stats;
+
+/**
+ * Handle for the service.
+ */
+struct GNUNET_SERVICE_Handle *GDS_service;
+
+/**
+ * The configuration the DHT service is running with
+ */
+const struct GNUNET_CONFIGURATION_Handle *GDS_cfg;
 
 /**
- * List of active clients.
+ * List of active monitoring requests.
  */
-static struct ClientList *client_head;
+static struct ClientMonitorRecord *monitor_head;
 
 /**
- * List of active clients.
+ * List of active monitoring requests.
  */
-static struct ClientList *client_tail;
+static struct ClientMonitorRecord *monitor_tail;
 
 /**
- * Hashmap for fast key based lookup, maps keys to 'struct ClientQueryRecord' entries.
+ * Hashmap for fast key based lookup, maps keys to `struct ClientQueryRecord` entries.
  */
 static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
 
@@ -207,63 +256,55 @@ static struct GNUNET_CONTAINER_Heap *retry_heap;
 /**
  * Task that re-transmits requests (using retry_heap).
  */
-static GNUNET_SCHEDULER_TaskIdentifier retry_task;
+static struct GNUNET_SCHEDULER_Task *retry_task;
 
 
 /**
- * Find a client if it exists, add it otherwise.
- *
- * @param client the server handle to the client
+ * Free data structures associated with the given query.
  *
- * @return the client if found, a new client otherwise
+ * @param record record to remove
  */
-static struct ClientList *
-find_active_client (struct GNUNET_SERVER_Client *client)
+static void
+remove_client_record (struct ClientQueryRecord *record)
 {
-  struct ClientList *pos = client_list;
-  struct ClientList *ret;
+  struct ClientHandle *ch = record->ch;
 
-  while (pos != NULL)
-  {
-    if (pos->client_handle == client)
-      return pos;
-    pos = pos->next;
-  }
-  ret = GNUNET_malloc (sizeof (struct ClientList));
-  ret->client_handle = client;
-  GNUNET_CONTAINER_DLL_insert (client_head,
-                              client_tail,
-                              ret);
-  return ret;
+  GNUNET_CONTAINER_DLL_remove (ch->cqr_head,
+                               ch->cqr_tail,
+                               record);
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multihashmap_remove (forward_map,
+                                                       &record->key,
+                                                       record));
+  if (NULL != record->hnode)
+    GNUNET_CONTAINER_heap_remove_node (record->hnode);
+  GNUNET_array_grow (record->seen_replies,
+                     record->seen_replies_count,
+                     0);
+  GNUNET_free (record);
 }
 
 
 /**
- * Iterator over hash map entries that frees all entries 
- * associated with the given client.
+ * Functions with this signature are called whenever a local client is
+ * connects to us.
  *
- * @param cls client to search for in source routes
- * @param key current key code (ignored)
- * @param value value in the hash map, a ClientQueryRecord
- * @return GNUNET_YES (we should continue to iterate)
+ * @param cls closure (NULL for dht)
+ * @param client identification of the client
+ * @param mq message queue for talking to @a client
+ * @return our `struct ClientHandle` for @a client
  */
-static int
-remove_client_records (void *cls, const GNUNET_HashCode * key, void *value)
+static void *
+client_connect_cb (void *cls,
+                   struct GNUNET_SERVICE_Client *client,
+                   struct GNUNET_MQ_Handle *mq)
 {
-  struct ClientList *client = cls;
-  struct ClientQueryRecord *record = value;
+  struct ClientHandle *ch;
 
-  if (record->client != client)
-    return GNUNET_YES;
-  GNUNET_assert (GNUNET_YES ==
-                GNUNET_CONTAINER_multihashmap_remove (forward_map,
-                                                      key, record));
-  GNUNET_CONTAINER_heap_remove_node (record->hnode);
-  GNUNET_ARRAY_append (record->seen_replies,
-                      record->seen_replies_count,
-                      0);
-  GNUNET_free (record);
-  return GNUNET_YES;
+  ch = GNUNET_new (struct ClientHandle);
+  ch->client = client;
+  ch->mq = mq;
+  return ch;
 }
 
 
@@ -272,48 +313,49 @@ remove_client_records (void *cls, const GNUNET_HashCode * key, void *value)
  * is disconnected on the network level.
  *
  * @param cls closure (NULL for dht)
- * @param client identification of the client; NULL
- *        for the last call when the server is destroyed
+ * @param client identification of the client
+ * @param app_ctx our `struct ClientHandle` for @a client
  */
 static void
-handle_client_disconnect (void *cls, 
-                         struct GNUNET_SERVER_Client *client)
+client_disconnect_cb (void *cls,
+                      struct GNUNET_SERVICE_Client *client,
+                      void *app_ctx)
 {
-  struct ClientList *pos = client_list;
-  struct ClientList *found;
-  struct PendingMessage *reply;
+  struct ClientHandle *ch = app_ctx;
+  struct ClientQueryRecord *cqr;
+  struct ClientMonitorRecord *monitor;
 
-  found = NULL;
-  while (pos != NULL)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Local client %p disconnects\n",
+             ch);
+  monitor = monitor_head;
+  while (NULL != monitor)
   {
-    if (pos->client_handle == client)
+    if (monitor->ch == ch)
     {
-      GNUNET_CONTAINER_DLL_remove (client_head,
-                                  client_tail,
-                                  pos);
-      found = pos;
-      break;
+      struct ClientMonitorRecord *next;
+
+      next = monitor->next;
+      GNUNET_free_non_null (monitor->key);
+      GNUNET_CONTAINER_DLL_remove (monitor_head,
+                                   monitor_tail,
+                                   monitor);
+      GNUNET_free (monitor);
+      monitor = next;
     }
-    pos = pos->next;
-  }
-  if (found == NULL)
-    return;
-  if (found->transmit_handle != NULL)
-    GNUNET_CONNECTION_notify_transmit_ready_cancel (found->transmit_handle);
-  while (NULL != (reply = found->pending_head))
+    else
     {
-      GNUNET_CONTAINER_DLL_remove (found->pending_head, found->pending_tail,
-                                   reply);
-      GNUNET_free (reply);
+      monitor = monitor->next;
     }
-  GNUNET_CONTAINER_multihashmap_iterate (forward_list.hashmap,
-                                        &remove_client_records, found);
-  GNUNET_free (found);
+  }
+  while (NULL != (cqr = ch->cqr_head))
+    remove_client_record (cqr);
+  GNUNET_free (ch);
 }
 
 
 /**
- * Route the given request via the DHT.  This includes updating 
+ * Route the given request via the DHT.  This includes updating
  * the bloom filter and retransmission times, building the P2P
  * message and initiating the routing operation.
  */
@@ -321,347 +363,612 @@ static void
 transmit_request (struct ClientQueryRecord *cqr)
 {
   int32_t reply_bf_mutator;
-
-  reply_bf_mutator = (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
-                                                        UINT32_MAX);
-  reply_bf = GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator,
-                                                cqr->seen_replies,
-                                                cqr->seen_replies_count);
-  GST_NEIGHBOURS_handle_get (cqr->msg_type,
-                            cqr->msg_options,
-                            cqr->replication,
-                            &cqr->key,
-                            cqr->xquery,
-                            cqr->xquery_size,
-                            reply_bf,
-                            reply_bf_mutator,
-                            NULL /* no peers blocked initially */);
-  GNUNET_CONTAINER_bloomfilter_destroy (reply_bf);
-
-  /* exponential back-off for retries, max 1h */
-  cqr->retry_frequency = 
-    GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
-                             GNUNET_TIME_relative_multiply (cqr->retry_frequency, 2));
+  struct GNUNET_CONTAINER_BloomFilter *reply_bf;
+  struct GNUNET_CONTAINER_BloomFilter *peer_bf;
+
+  GNUNET_STATISTICS_update (GDS_stats,
+                            gettext_noop ("# GET requests from clients injected"),
+                            1,
+                            GNUNET_NO);
+  reply_bf_mutator =
+      (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                          UINT32_MAX);
+  reply_bf
+    = GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator,
+                                          cqr->seen_replies,
+                                          cqr->seen_replies_count);
+  peer_bf
+    = GNUNET_CONTAINER_bloomfilter_init (NULL,
+                                         DHT_BLOOM_SIZE,
+                                         GNUNET_CONSTANTS_BLOOMFILTER_K);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Initiating GET for %s, replication %u, already have %u replies\n",
+       GNUNET_h2s (&cqr->key),
+       cqr->replication,
+       cqr->seen_replies_count);
+  GDS_NEIGHBOURS_handle_get (cqr->type,
+                             cqr->msg_options,
+                             cqr->replication,
+                             0 /* hop count */ ,
+                             &cqr->key,
+                             cqr->xquery,
+                             cqr->xquery_size,
+                             reply_bf,
+                             reply_bf_mutator,
+                             peer_bf);
+  GNUNET_CONTAINER_bloomfilter_free (reply_bf);
+  GNUNET_CONTAINER_bloomfilter_free (peer_bf);
+
+  /* exponential back-off for retries.
+   * max GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD (15 min) */
+  cqr->retry_frequency = GNUNET_TIME_STD_BACKOFF (cqr->retry_frequency);
   cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
 }
 
 
 /**
- * Task that looks at the 'retry_heap' and transmits all of the requests
+ * Task that looks at the #retry_heap and transmits all of the requests
  * on the heap that are ready for transmission.  Then re-schedules
  * itself (unless the heap is empty).
  *
  * @param cls unused
- * @param tc scheduler context
  */
 static void
-transmit_next_request_task (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext *tc)
+transmit_next_request_task (void *cls)
 {
   struct ClientQueryRecord *cqr;
   struct GNUNET_TIME_Relative delay;
 
-  retry_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
+  retry_task = NULL;
   while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
+  {
+    cqr->hnode = NULL;
+    delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
+    if (delay.rel_value_us > 0)
     {
-      cqr->hnode = NULL;
-      delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
-      if (delay.value > 0)
-       {
-         cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
-                                                    cqr->retry_time.abs_value);
-         retry_task = GNUNET_SCHEDULER_add_delayed (delay,
-                                                    &transmit_next_request_task,
-                                                    NULL);
-         return;
-       }
-      transmit_request (cqr);
+      cqr->hnode =
+          GNUNET_CONTAINER_heap_insert (retry_heap,
+                                        cqr,
+                                        cqr->retry_time.abs_value_us);
+      retry_task =
+          GNUNET_SCHEDULER_add_delayed (delay,
+                                        &transmit_next_request_task,
+                                        NULL);
+      return;
     }
+    transmit_request (cqr);
+    cqr->hnode
+      = GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
+                                      cqr->retry_time.abs_value_us);
+  }
+}
+
+
+/**
+ * Check DHT PUT messages from the client.
+ *
+ * @param cls the client we received this message from
+ * @param dht_msg the actual message received
+ * @return #GNUNET_OK (always)
+ */
+static int
+check_dht_local_put (void *cls,
+                      const struct GNUNET_DHT_ClientPutMessage *dht_msg)
+{
+  /* always well-formed */
+  return GNUNET_OK;
 }
 
 
 /**
  * Handler for PUT messages.
  *
- * @param cls closure for the service
- * @param client the client we received this message from
- * @param message the actual message received
+ * @param cls the client we received this message from
+ * @param dht_msg the actual message received
  */
 static void
-handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
-                     const struct GNUNET_MessageHeader *message)
+handle_dht_local_put (void *cls,
+                      const struct GNUNET_DHT_ClientPutMessage *dht_msg)
 {
-  const struct GNUNET_DHT_ClientPutMessage *dht_msg;
+  struct ClientHandle *ch = cls;
+  struct GNUNET_CONTAINER_BloomFilter *peer_bf;
   uint16_t size;
-  
-  size = ntohs (message->size);
-  if (size < sizeof (struct GNUNET_DHT_ClientPutMessage))
-    {
-      GNUNET_break (0);
-      GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    }
-  dht_msg = (const struct GNUNET_DHT_ClientPutMessage *) message;
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_DHT_ClientPutConfirmationMessage *conf;
+
+  size = ntohs (dht_msg->header.size);
+  GNUNET_STATISTICS_update (GDS_stats,
+                            gettext_noop ("# PUT requests received from clients"),
+                            1,
+                            GNUNET_NO);
+  LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
+               "CLIENT-PUT %s\n",
+               GNUNET_h2s_full (&dht_msg->key));
   /* give to local clients */
-  GDS_CLIENT_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
-                          &dht_msg->key,
-                          0, NULL,
-                          0, NULL,
-                          ntohl (dht_msg->type),
-                          size - sizeof (struct GNUNET_DHT_ClientPutMessage),
-                          &dht_msg[1]);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Handling local PUT of %u-bytes for query %s\n",
+       size - sizeof (struct GNUNET_DHT_ClientPutMessage),
+       GNUNET_h2s (&dht_msg->key));
+  GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
+                            &dht_msg->key,
+                            0,
+                            NULL,
+                            0,
+                            NULL,
+                            ntohl (dht_msg->type),
+                            size - sizeof (struct GNUNET_DHT_ClientPutMessage),
+                            &dht_msg[1]);
   /* store locally */
-  GST_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
-                           &dht_msg->key,
-                           0, NULL,
-                           ntohl (dht_msg->type),
-                           size - sizeof (struct GNUNET_DHT_ClientPutMessage),
-                           &dht_msg[1]);
+  GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
+                            &dht_msg->key,
+                            0,
+                            NULL,
+                            ntohl (dht_msg->type),
+                            size - sizeof (struct GNUNET_DHT_ClientPutMessage),
+                            &dht_msg[1]);
   /* route to other peers */
-  GST_NEIGHBOURS_handle_put (ntohl (dht_msg->type),
-                            ntohl (dht_msg->options),
-                            ntohl (dht_msg->desired_replication_level),
-                            GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
-                            &dht_msg->key,
-                            &dht_msg[1],
-                            size - sizeof (struct GNUNET_DHT_ClientPutMessage));
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+  peer_bf
+    = GNUNET_CONTAINER_bloomfilter_init (NULL,
+                                         DHT_BLOOM_SIZE,
+                                         GNUNET_CONSTANTS_BLOOMFILTER_K);
+  GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type),
+                             ntohl (dht_msg->options),
+                             ntohl (dht_msg->desired_replication_level),
+                             GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
+                             0 /* hop count */,
+                             peer_bf,
+                             &dht_msg->key,
+                             0,
+                             NULL,
+                             &dht_msg[1],
+                             size - sizeof (struct GNUNET_DHT_ClientPutMessage));
+  GDS_CLIENTS_process_put (ntohl (dht_msg->options),
+                           ntohl (dht_msg->type),
+                           0,
+                           ntohl (dht_msg->desired_replication_level),
+                           1,
+                           GDS_NEIGHBOURS_get_id(),
+                           GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
+                           &dht_msg->key,
+                           &dht_msg[1],
+                           size - sizeof (struct GNUNET_DHT_ClientPutMessage));
+  GNUNET_CONTAINER_bloomfilter_free (peer_bf);
+  env = GNUNET_MQ_msg (conf,
+                       GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK);
+  conf->reserved = htonl (0);
+  conf->unique_id = dht_msg->unique_id;
+  GNUNET_MQ_send (ch->mq,
+                  env);
+  GNUNET_SERVICE_client_continue (ch->client);
 }
 
 
 /**
- * Handler for any generic DHT messages, calls the appropriate handler
- * depending on message type, sends confirmation if responses aren't otherwise
- * expected.
+ * Check DHT GET messages from the client.
  *
- * @param cls closure for the service
- * @param client the client we received this message from
+ * @param cls the client we received this message from
  * @param message the actual message received
+ * @return #GNUNET_OK (always)
+ */
+static int
+check_dht_local_get (void *cls,
+                      const struct GNUNET_DHT_ClientGetMessage *get)
+{
+  /* always well-formed */
+  return GNUNET_OK;
+}
+
+
+/**
+ * Handle a result from local datacache for a GET operation.
+ *
+ * @param cls the `struct ClientHandle` of the client doing the query
+ * @param type type of the block
+ * @param expiration_time when does the content expire
+ * @param key key for the content
+ * @param put_path_length number of entries in @a put_path
+ * @param put_path peers the original PUT traversed (if tracked)
+ * @param get_path_length number of entries in @a get_path
+ * @param get_path peers this reply has traversed so far (if tracked)
+ * @param data payload of the reply
+ * @param data_size number of bytes in @a data
  */
 static void
-handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
-                     const struct GNUNET_MessageHeader *message)
+handle_local_result (void *cls,
+                     enum GNUNET_BLOCK_Type type,
+                     struct GNUNET_TIME_Absolute expiration_time,
+                     const struct GNUNET_HashCode *key,
+                     unsigned int put_path_length,
+                     const struct GNUNET_PeerIdentity *put_path,
+                     unsigned int get_path_length,
+                     const struct GNUNET_PeerIdentity *get_path,
+                     const void *data,
+                     size_t data_size)
 {
-  const struct GNUNET_DHT_ClientGetMessage *get;
-  const struct GNUNET_MessageHeader *enc_msg;
+  // FIXME: this needs some clean up: inline the function,
+  // possibly avoid even looking up the client!
+  GDS_CLIENTS_handle_reply (expiration_time,
+                            key,
+                            0, NULL,
+                            put_path_length, put_path,
+                            type,
+                            data_size, data);
+}
+
 
+/**
+ * Handler for DHT GET messages from the client.
+ *
+ * @param cls the client we received this message from
+ * @param message the actual message received
+ */
+static void
+handle_dht_local_get (void *cls,
+                      const struct GNUNET_DHT_ClientGetMessage *get)
+{
+  struct ClientHandle *ch = cls;
   struct ClientQueryRecord *cqr;
   size_t xquery_size;
-  const charxquery;
+  const char *xquery;
   uint16_t size;
 
-  size = ntohs (message->size);
-  if (size < sizeof (struct GNUNET_DHT_ClientGetMessage))
-    {
-      GNUNET_break (0);
-      GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-      return;
-    }
+  size = ntohs (get->header.size);
   xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
-  get = (const struct GNUNET_DHT_ClientGetMessage *) message;
-  xquery = (const char*) &get[1];
+  xquery = (const char *) &get[1];
+  GNUNET_STATISTICS_update (GDS_stats,
+                            gettext_noop
+                            ("# GET requests received from clients"), 1,
+                            GNUNET_NO);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received GET request for %s from local client %p, xq: %.*s\n",
+       GNUNET_h2s (&get->key),
+       ch->client,
+       xquery_size,
+       xquery);
+  LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
+               "CLIENT-GET %s\n",
+               GNUNET_h2s_full (&get->key));
 
-  
   cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
   cqr->key = get->key;
-  cqr->client = find_active_client (client);
-  cqr->xquery = (void*) &cqr[1];
-  memcpy (&cqr[1], xquery, xquery_size);
-  cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0); 
-  cqr->retry_frequency = GNUNET_TIME_UNIT_MILLISECONDS;
+  cqr->ch = ch;
+  cqr->xquery = (void *) &cqr[1];
+  GNUNET_memcpy (&cqr[1], xquery, xquery_size);
+  cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
+  cqr->retry_frequency = GNUNET_TIME_UNIT_SECONDS;
   cqr->retry_time = GNUNET_TIME_absolute_get ();
   cqr->unique_id = get->unique_id;
   cqr->xquery_size = xquery_size;
   cqr->replication = ntohl (get->desired_replication_level);
   cqr->msg_options = ntohl (get->options);
-  cqr->msg_type = ntohl (get->type);  
-  GNUNET_CONTAINER_multihashmap_put (forward_map, KEY, cqr,
-                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+  cqr->type = ntohl (get->type);
+  GNUNET_CONTAINER_DLL_insert (ch->cqr_head,
+                               ch->cqr_tail,
+                               cqr);
+  GNUNET_CONTAINER_multihashmap_put (forward_map,
+                                     &cqr->key,
+                                     cqr,
+                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+  GDS_CLIENTS_process_get (ntohl (get->options),
+                           ntohl (get->type),
+                           0,
+                           ntohl (get->desired_replication_level),
+                           1,
+                           GDS_NEIGHBOURS_get_id(),
+                           &get->key);
   /* start remote requests */
-  if (GNUNET_SCHEDULER_NO_TASK != retry_task)
+  if (NULL != retry_task)
     GNUNET_SCHEDULER_cancel (retry_task);
-  retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL);
+  retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task,
+                                        NULL);
   /* perform local lookup */
   GDS_DATACACHE_handle_get (&get->key,
-                           cqr->msg_type,
+                           cqr->type,
                            cqr->xquery,
                            xquery_size,
-                           NULL, 0);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+                            NULL,
+                           0,
+                            &handle_local_result,
+                            ch);
+  GNUNET_SERVICE_client_continue (ch->client);
 }
 
 
 /**
- * Closure for 'remove_by_uid'.
+ * Closure for #find_by_unique_id().
  */
-struct RemoveByUidContext
+struct FindByUniqueIdContext
 {
   /**
-   * Client that issued the removal request.
+   * Where to store the result, if found.
    */
-  struct ClientList *client;
+  struct ClientQueryRecord *cqr;
 
-  /**
-   * Unique ID of the request.
-   */
-  uint64_t uid;
+  uint64_t unique_id;
 };
 
 
 /**
- * Iterator over hash map entries that frees all entries 
- * that match the given client and UID.
+ * Function called for each existing DHT record for the given
+ * query.  Checks if it matches the UID given in the closure
+ * and if so returns the entry as a result.
  *
- * @param cls UID and client to search for in source routes
- * @param key current key code
- * @param value value in the hash map, a ClientQueryRecord
- * @return GNUNET_YES (we should continue to iterate)
+ * @param cls the search context
+ * @param key query for the lookup (not used)
+ * @param value the `struct ClientQueryRecord`
+ * @return #GNUNET_YES to continue iteration (result not yet found)
  */
 static int
-remove_by_uid (void *cls, const GNUNET_HashCode * key, void *value)
+find_by_unique_id (void *cls,
+                  const struct GNUNET_HashCode *key,
+                  void *value)
 {
-  const struct RemoveByUidContext *ctx = cls;
-  struct ClientQueryRecord *record = value;
+  struct FindByUniqueIdContext *fui_ctx = cls;
+  struct ClientQueryRecord *cqr = value;
 
-  if (record->uid != ctx->uid)
+  if (cqr->unique_id != fui_ctx->unique_id)
     return GNUNET_YES;
-  return remove_client_records (ctx->client, key, record);
+  fui_ctx->cqr = cqr;
+  return GNUNET_NO;
 }
 
 
 /**
- * Handler for any generic DHT stop messages, calls the appropriate handler
- * depending on message type (if processed locally)
+ * Check "GET result seen" messages from the client.
  *
- * @param cls closure for the service
- * @param client the client we received this message from
+ * @param cls the client we received this message from
  * @param message the actual message received
+ * @return #GNUNET_OK if @a seen is well-formed
+ */
+static int
+check_dht_local_get_result_seen (void *cls,
+                                 const struct GNUNET_DHT_ClientGetResultSeenMessage *seen)
+{
+  uint16_t size;
+  unsigned int hash_count;
+
+  size = ntohs (seen->header.size);
+  hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
+  if (size != sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage) + hash_count * sizeof (struct GNUNET_HashCode))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
+}
+
+
+/**
+ * Handler for "GET result seen" messages from the client.
  *
+ * @param cls the client we received this message from
+ * @param message the actual message received
  */
 static void
-handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
-                          const struct GNUNET_MessageHeader *message)
+handle_dht_local_get_result_seen (void *cls,
+                                 const struct GNUNET_DHT_ClientGetResultSeenMessage *seen)
 {
-  const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg =
-    (const struct GNUNET_DHT_ClientGetStopMessage *) message;
-  
-  ctx.client = find_active_client (client);
-  ctx.uid = &dht_stop_msg.unique_id);
+  struct ClientHandle *ch = cls;
+  uint16_t size;
+  unsigned int hash_count;
+  unsigned int old_count;
+  const struct GNUNET_HashCode *hc;
+  struct FindByUniqueIdContext fui_ctx;
+  struct ClientQueryRecord *cqr;
+
+  size = ntohs (seen->header.size);
+  hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
+  hc = (const struct GNUNET_HashCode*) &seen[1];
+  fui_ctx.unique_id = seen->unique_id;
+  fui_ctx.cqr = NULL;
   GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
-                                             &dht_stop_msg->key,
-                                             &remove_by_uid,
-                                             &ctx);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+                                             &seen->key,
+                                             &find_by_unique_id,
+                                             &fui_ctx);
+  if (NULL == (cqr = fui_ctx.cqr))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVICE_client_drop (ch->client);
+    return;
+  }
+  /* finally, update 'seen' list */
+  old_count = cqr->seen_replies_count;
+  GNUNET_array_grow (cqr->seen_replies,
+                    cqr->seen_replies_count,
+                    cqr->seen_replies_count + hash_count);
+  GNUNET_memcpy (&cqr->seen_replies[old_count],
+                 hc,
+                 sizeof (struct GNUNET_HashCode) * hash_count);
 }
 
 
 /**
- * Task run to check for messages that need to be sent to a client.
+ * Closure for #remove_by_unique_id().
+ */
+struct RemoveByUniqueIdContext
+{
+  /**
+   * Client that issued the removal request.
+   */
+  struct ClientHandle *ch;
+
+  /**
+   * Unique ID of the request.
+   */
+  uint64_t unique_id;
+};
+
+
+/**
+ * Iterator over hash map entries that frees all entries
+ * that match the given client and unique ID.
  *
- * @param client a ClientList, containing the client and any messages to be sent to it
+ * @param cls unique ID and client to search for in source routes
+ * @param key current key code
+ * @param value value in the hash map, a ClientQueryRecord
+ * @return #GNUNET_YES (we should continue to iterate)
  */
-static void
-process_pending_messages (struct ClientList *client);
+static int
+remove_by_unique_id (void *cls,
+                     const struct GNUNET_HashCode *key,
+                     void *value)
+{
+  const struct RemoveByUniqueIdContext *ctx = cls;
+  struct ClientQueryRecord *cqr = value;
+
+  if (cqr->unique_id != ctx->unique_id)
+    return GNUNET_YES;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Removing client %p's record for key %s (by unique id)\n",
+              ctx->ch->client,
+              GNUNET_h2s (key));
+  remove_client_record (cqr);
+  return GNUNET_YES;
+}
 
 
 /**
- * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
- * request.  A ClientList is passed as closure, take the head of the list
- * and copy it into buf, which has the result of sending the message to the
- * client.
+ * Handler for any generic DHT stop messages, calls the appropriate handler
+ * depending on message type (if processed locally)
  *
- * @param cls closure to this call
- * @param size maximum number of bytes available to send
- * @param buf where to copy the actual message to
+ * @param cls client we received this message from
+ * @param message the actual message received
  *
- * @return the number of bytes actually copied, 0 indicates failure
  */
-static size_t
-send_reply_to_client (void *cls, size_t size, void *buf)
+static void
+handle_dht_local_get_stop (void *cls,
+                           const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg)
 {
-  struct ClientList *client = cls;
-  char *cbuf = buf;
-  struct PendingMessage *reply;
-  size_t off;
-  size_t msize;
-
-  client->transmit_handle = NULL;
-  if (buf == NULL)
-  {
-    /* client disconnected */
-    return 0;
-  }
-  off = 0;
-  while ((NULL != (reply = client->pending_head)) &&
-         (size >= off + (msize = ntohs (reply->msg->size))))
-  {
-    GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
-                                 reply);
-    memcpy (&cbuf[off], reply->msg, msize);
-    GNUNET_free (reply);
-    off += msize;
-  }
-  process_pending_messages (client);
-  return off;
+  struct ClientHandle *ch = cls;
+  struct RemoveByUniqueIdContext ctx;
+
+  GNUNET_STATISTICS_update (GDS_stats,
+                            gettext_noop
+                            ("# GET STOP requests received from clients"), 1,
+                            GNUNET_NO);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received GET STOP request for %s from local client %p\n",
+       GNUNET_h2s (&dht_stop_msg->key),
+       ch->client);
+  ctx.ch = ch;
+  ctx.unique_id = dht_stop_msg->unique_id;
+  GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
+                                              &dht_stop_msg->key,
+                                              &remove_by_unique_id,
+                                              &ctx);
+  GNUNET_SERVICE_client_continue (ch->client);
 }
 
 
 /**
- * Task run to check for messages that need to be sent to a client.
+ * Handler for monitor start messages
+ *
+ * @param cls the client we received this message from
+ * @param msg the actual message received
  *
- * @param client a ClientList, containing the client and any messages to be sent to it
  */
 static void
-process_pending_messages (struct ClientList *client)
+handle_dht_local_monitor (void *cls,
+                          const struct GNUNET_DHT_MonitorStartStopMessage *msg)
 {
-  if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
-    return;
-  client->transmit_handle =
-      GNUNET_SERVER_notify_transmit_ready (client->client_handle,
-                                           ntohs (client->pending_head->
-                                                  msg->size),
-                                           GNUNET_TIME_UNIT_FOREVER_REL,
-                                           &send_reply_to_client, client);
+  struct ClientHandle *ch = cls;
+  struct ClientMonitorRecord *r;
+
+  r = GNUNET_new (struct ClientMonitorRecord);
+  r->ch = ch;
+  r->type = ntohl (msg->type);
+  r->get = ntohs (msg->get);
+  r->get_resp = ntohs (msg->get_resp);
+  r->put = ntohs (msg->put);
+  if (0 == ntohs (msg->filter_key))
+  {
+    r->key = NULL;
+  }
+  else
+  {
+    r->key = GNUNET_new (struct GNUNET_HashCode);
+    GNUNET_memcpy (r->key,
+                   &msg->key,
+                   sizeof (struct GNUNET_HashCode));
+  }
+  GNUNET_CONTAINER_DLL_insert (monitor_head,
+                               monitor_tail,
+                               r);
+  GNUNET_SERVICE_client_continue (ch->client);
 }
 
 
 /**
- * Add a PendingMessage to the clients list of messages to be sent
+ * Handler for monitor stop messages
  *
- * @param client the active client to send the message to
- * @param pending_message the actual message to send
+ * @param cls the client we received this message from
+ * @param msg the actual message received
  */
 static void
-add_pending_message (struct ClientList *client,
-                     struct PendingMessage *pending_message)
+handle_dht_local_monitor_stop (void *cls,
+                               const struct GNUNET_DHT_MonitorStartStopMessage *msg)
 {
-  GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
-                                   pending_message);
-  process_pending_messages (client);
+  struct ClientHandle *ch = cls;
+  struct ClientMonitorRecord *r;
+  int keys_match;
+
+  GNUNET_SERVICE_client_continue (ch->client);
+  for (r = monitor_head; NULL != r; r = r->next)
+  {
+    if (NULL == r->key)
+    {
+      keys_match = (0 == ntohs(msg->filter_key));
+    }
+    else
+    {
+      keys_match = ( (0 != ntohs(msg->filter_key)) &&
+                     (! memcmp (r->key,
+                                &msg->key,
+                                sizeof(struct GNUNET_HashCode))) );
+    }
+    if ( (ch == r->ch) &&
+         (ntohl(msg->type) == r->type) &&
+         (r->get == msg->get) &&
+         (r->get_resp == msg->get_resp) &&
+         (r->put == msg->put) &&
+         keys_match )
+    {
+      GNUNET_CONTAINER_DLL_remove (monitor_head,
+                                   monitor_tail,
+                                   r);
+      GNUNET_free_non_null (r->key);
+      GNUNET_free (r);
+      return; /* Delete only ONE entry */
+    }
+  }
 }
 
 
 /**
- * Closure for 'forward_reply'
+ * Closure for #forward_reply()
  */
 struct ForwardReplyContext
 {
 
   /**
-   * Actual message to send to matching clients. 
+   * Expiration time of the reply.
    */
-  struct PendingMessage *pm;
+  struct GNUNET_TIME_Absolute expiration;
 
   /**
-   * Embedded payload.
+   * GET path taken.
    */
-  const void *data;
+  const struct GNUNET_PeerIdentity *get_path;
 
   /**
-   * Type of the data.
+   * PUT path taken.
    */
-  enum GNUNET_BLOCK_Type type;
+  const struct GNUNET_PeerIdentity *put_path;
+
+  /**
+   * Embedded payload.
+   */
+  const void *data;
 
   /**
    * Number of bytes in data.
@@ -669,9 +976,19 @@ struct ForwardReplyContext
   size_t data_size;
 
   /**
-   * Do we need to copy 'pm' because it was already used?
+   * Number of entries in @e get_path.
    */
-  int do_copy;
+  unsigned int get_path_length;
+
+  /**
+   * Number of entries in @e put_path.
+   */
+  unsigned int put_path_length;
+
+  /**
+   * Type of the data.
+   */
+  enum GNUNET_BLOCK_Type type;
 
 };
 
@@ -684,49 +1001,77 @@ struct ForwardReplyContext
  * @param cls the 'struct ForwardReplyContext'
  * @param key current key
  * @param value value in the hash map, a ClientQueryRecord
- * @return GNUNET_YES (we should continue to iterate),
- *         if the result is mal-formed, GNUNET_NO
+ * @return #GNUNET_YES (we should continue to iterate),
+ *         if the result is mal-formed, #GNUNET_NO
  */
 static int
-forward_reply (void *cls, const GNUNET_HashCode * key, void *value)
+forward_reply (void *cls,
+               const struct GNUNET_HashCode *key,
+               void *value)
 {
   struct ForwardReplyContext *frc = cls;
   struct ClientQueryRecord *record = value;
-  struct PendingMessage *pm;
-  struct ReplyMessage *reply;
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_DHT_ClientResultMessage *reply;
   enum GNUNET_BLOCK_EvaluationResult eval;
   int do_free;
-  GNUNET_HashCode ch;
-  unsigned int i;
-  
+  struct GNUNET_HashCode ch;
+  struct GNUNET_PeerIdentity *paths;
+
+  LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
+              "CLIENT-RESULT %s\n",
+               GNUNET_h2s_full (key));
   if ( (record->type != GNUNET_BLOCK_TYPE_ANY) &&
-       (record->type != frc->type) )
-    return GNUNET_YES; /* type mismatch */
-  GNUNET_CRYPTO_hash (frc->data,
-                     frc->data_size,
-                     &ch);
-  for (i=0;i<record->seen_replies_count;i++)
+       (record->type != frc->type))
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Record type missmatch, not passing request for key %s to local client\n",
+         GNUNET_h2s (key));
+    GNUNET_STATISTICS_update (GDS_stats,
+                              gettext_noop
+                              ("# Key match, type mismatches in REPLY to CLIENT"),
+                              1, GNUNET_NO);
+    return GNUNET_YES;          /* type mismatch */
+  }
+  GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch);
+  for (unsigned int i = 0; i < record->seen_replies_count; i++)
     if (0 == memcmp (&record->seen_replies[i],
-                    &ch,
-                    sizeof (GNUNET_HashCode)))
-      return GNUNET_YES; /* duplicate */             
-  eval =
-    GNUNET_BLOCK_evaluate (GDS_block_context, 
-                          record->type, key, 
-                          NULL, 0,
-                          record->xquery,
-                          record->xquery_size, 
-                          frc->data,
-                          frc->data_size);
+                     &ch,
+                     sizeof (struct GNUNET_HashCode)))
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Duplicate reply, not passing request for key %s to local client\n",
+           GNUNET_h2s (key));
+      GNUNET_STATISTICS_update (GDS_stats,
+                                gettext_noop
+                                ("# Duplicate REPLIES to CLIENT request dropped"),
+                                1, GNUNET_NO);
+      return GNUNET_YES;        /* duplicate */
+    }
+  eval
+    = GNUNET_BLOCK_evaluate (GDS_block_context,
+                             record->type,
+                             GNUNET_BLOCK_EO_NONE,
+                             key,
+                             NULL,
+                             0,
+                             record->xquery,
+                             record->xquery_size,
+                             frc->data,
+                             frc->data_size);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Evaluation result is %d for key %s for local client's query\n",
+       (int) eval,
+       GNUNET_h2s (key));
   switch (eval)
   {
   case GNUNET_BLOCK_EVALUATION_OK_LAST:
     do_free = GNUNET_YES;
     break;
   case GNUNET_BLOCK_EVALUATION_OK_MORE:
-    GNUNET_ARRAY_append (record->seen_replies,
-                        record->seen_replies_count,
-                        ch);
+    GNUNET_array_append (record->seen_replies,
+                         record->seen_replies_count,
+                         ch);
     do_free = GNUNET_NO;
     break;
   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
@@ -742,32 +1087,48 @@ forward_reply (void *cls, const GNUNET_HashCode * key, void *value)
   case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
     GNUNET_break (0);
     return GNUNET_NO;
+  case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
+    return GNUNET_YES;
   case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Unsupported block type (%u) in request!\n",
-                record->type);
+                _("Unsupported block type (%u) in request!\n"), record->type);
+    return GNUNET_NO;
+  default:
+    GNUNET_break (0);
     return GNUNET_NO;
   }
-  if (GNUNET_NO == frc->do_copy)
-    {
-      /* first time, we can use the original data */
-      pm = frc->pm; 
-      frc->do_copy = GNUNET_YES;
-    }
-  else
-    {
-      /* two clients waiting for same reply, must copy for queueing */
-      pm = GNUNET_malloc (sizeof (struct PendingMessage) +
-                         ntohs (frc->pm->msg->size));
-      memcpy (pm, frc->pm, 
-             sizeof (struct PendingMessage) + ntohs (frc->pm->msg->size));
-      pm->next = pm->prev = NULL;
-    }
-  reply = (struct ReplyMessage*) &pm[1];  
+  GNUNET_STATISTICS_update (GDS_stats,
+                            gettext_noop ("# RESULTS queued for clients"),
+                            1,
+                            GNUNET_NO);
+  env = GNUNET_MQ_msg_extra (reply,
+                             frc->data_size +
+                             (frc->get_path_length + frc->put_path_length) * sizeof (struct GNUNET_PeerIdentity),
+                             GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
+  reply->type = htonl (frc->type);
+  reply->get_path_length = htonl (frc->get_path_length);
+  reply->put_path_length = htonl (frc->put_path_length);
   reply->unique_id = record->unique_id;
-  add_pending_message (record->client, pm);
+  reply->expiration = GNUNET_TIME_absolute_hton (frc->expiration);
+  reply->key = *key;
+  paths = (struct GNUNET_PeerIdentity *) &reply[1];
+  GNUNET_memcpy (paths,
+                 frc->put_path,
+                 sizeof (struct GNUNET_PeerIdentity) * frc->put_path_length);
+  GNUNET_memcpy (&paths[frc->put_path_length],
+                 frc->get_path,
+                 sizeof (struct GNUNET_PeerIdentity) * frc->get_path_length);
+  GNUNET_memcpy (&paths[frc->get_path_length + frc->put_path_length],
+                 frc->data,
+                 frc->data_size);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Sending reply to query %s for client %p\n",
+       GNUNET_h2s (key),
+       record->ch->client);
+  GNUNET_MQ_send (record->ch->mq,
+                  env);
   if (GNUNET_YES == do_free)
-    remove_client_records (record->client, key, record);
+    remove_client_record (record);
   return GNUNET_YES;
 }
 
@@ -779,122 +1140,393 @@ forward_reply (void *cls, const GNUNET_HashCode * key, void *value)
  *
  * @param expiration when will the reply expire
  * @param key the query this reply is for
- * @param get_path_length number of peers in 'get_path'
+ * @param get_path_length number of peers in @a get_path
  * @param get_path path the reply took on get
- * @param put_path_length number of peers in 'put_path'
+ * @param put_path_length number of peers in @a put_path
  * @param put_path path the reply took on put
  * @param type type of the reply
- * @param data_size number of bytes in 'data'
+ * @param data_size number of bytes in @a data
  * @param data application payload data
  */
 void
-GDS_CLIENT_handle_reply (struct GNUNET_TIME_Absolute expiration,
-                        const GNUNET_HashCode *key,
-                        unsigned int get_path_length,
-                        const struct GNUNET_PeerIdentity *get_path,
-                        unsigned int put_path_length,
-                        const struct GNUNET_PeerIdentity *put_path,
-                        enum GNUNET_BLOCK_Type type,
-                        size_t data_size,
-                        const void *data)
+GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
+                          const struct GNUNET_HashCode *key,
+                          unsigned int get_path_length,
+                          const struct GNUNET_PeerIdentity *get_path,
+                          unsigned int put_path_length,
+                          const struct GNUNET_PeerIdentity *put_path,
+                          enum GNUNET_BLOCK_Type type,
+                          size_t data_size,
+                          const void *data)
 {
   struct ForwardReplyContext frc;
-  struct PendingMessage *pm;
-  struct ReplyMessage *reply;
-  struct GNUNET_PeerIdentity *paths;
   size_t msize;
 
-  if (NULL ==
-      GNUNET_CONTAINER_multihashmap_get (foward_map, key))
-    return; /* no matching request, fast exit! */
-  msize = sizeof(struct ReplyMessage) + data_size + 
+  msize = sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size +
     (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Could not pass reply to client, message too big!\n"));
-      return;
-    }
-  pm = (struct PendingMessage *) GNUNET_malloc (msize + sizeof (struct PendingMessage));
-  reply = (struct ReplyMessage*) &pm[1];
-  pm->msg = &reply->header;
-  reply->header.size = htons ((uint16_t) msize);
-  reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
-  reply->type = htonl (type);
-  reply->get_path_length = htonl (get_path_length);
-  reply->put_path_length = htonl (put_path_length);
-  reply->unique_id = 0; /* filled in later */
-  reply->expiration = GNUNET_TIME_absolute_hton (expiration);
-  reply->key = *key;
-  paths = (struct GNUNET_PeerIdentity*) &reply[1];
-  mempcy (paths, get_path, 
-         sizeof (struct GNUNET_PeerIdentity) * get_path_length);
-  mempcy (&paths[get_path_length], 
-         put_path, sizeof (struct GNUNET_PeerIdentity) * put_path_length);
-  memcpy (&paths[get_path_length + put_path_length],
-         data, 
-         data_size);
-  frc.do_copy = GNUNET_NO;
-  frc.pm = pm;
+  {
+    GNUNET_break (0);
+    return;
+  }
+  if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map,
+                                                 key))
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "No matching client for reply for key %s\n",
+         GNUNET_h2s (key));
+    GNUNET_STATISTICS_update (GDS_stats,
+                              gettext_noop ("# REPLIES ignored for CLIENTS (no match)"),
+                              1,
+                              GNUNET_NO);
+    return;                     /* no matching request, fast exit! */
+  }
+  frc.expiration = expiration;
+  frc.get_path = get_path;
+  frc.put_path = put_path;
   frc.data = data;
   frc.data_size = data_size;
+  frc.get_path_length = get_path_length;
+  frc.put_path_length = put_path_length;
   frc.type = type;
-  GNUNET_CONTAINER_multihashmap_get_multiple (foward_map, key,
-                                             &forward_reply,
-                                             &frc);
-  if (GNUNET_NO == frc.do_copy)
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Forwarding reply for key %s to client\n",
+       GNUNET_h2s (key));
+  GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
+                                              key,
+                                              &forward_reply,
+                                              &frc);
+
+}
+
+
+/**
+ * Check if some client is monitoring GET messages and notify
+ * them in that case.
+ *
+ * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
+ * @param type The type of data in the request.
+ * @param hop_count Hop count so far.
+ * @param path_length number of entries in path (or 0 if not recorded).
+ * @param path peers on the GET path (or NULL if not recorded).
+ * @param desired_replication_level Desired replication level.
+ * @param key Key of the requested data.
+ */
+void
+GDS_CLIENTS_process_get (uint32_t options,
+                         enum GNUNET_BLOCK_Type type,
+                         uint32_t hop_count,
+                         uint32_t desired_replication_level,
+                         unsigned int path_length,
+                         const struct GNUNET_PeerIdentity *path,
+                         const struct GNUNET_HashCode * key)
+{
+  struct ClientMonitorRecord *m;
+  struct ClientHandle **cl;
+  unsigned int cl_size;
+
+  cl = NULL;
+  cl_size = 0;
+  for (m = monitor_head; NULL != m; m = m->next)
+  {
+    if ( ( (GNUNET_BLOCK_TYPE_ANY == m->type) ||
+           (m->type == type) ) &&
+         ( (NULL == m->key) ||
+           (0 == memcmp (key,
+                         m->key,
+                         sizeof(struct GNUNET_HashCode))) ) )
     {
-      /* did not match any of the requests, free! */
-      GNUNET_free (buf);
+      struct GNUNET_MQ_Envelope *env;
+      struct GNUNET_DHT_MonitorGetMessage *mmsg;
+      struct GNUNET_PeerIdentity *msg_path;
+      size_t msize;
+      unsigned int i;
+
+      /* Don't send duplicates */
+      for (i = 0; i < cl_size; i++)
+        if (cl[i] == m->ch)
+          break;
+      if (i < cl_size)
+        continue;
+      GNUNET_array_append (cl,
+                           cl_size,
+                           m->ch);
+
+      msize = path_length * sizeof (struct GNUNET_PeerIdentity);
+      env = GNUNET_MQ_msg_extra (mmsg,
+                                 msize,
+                                 GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET);
+      mmsg->options = htonl(options);
+      mmsg->type = htonl(type);
+      mmsg->hop_count = htonl(hop_count);
+      mmsg->desired_replication_level = htonl(desired_replication_level);
+      mmsg->get_path_length = htonl(path_length);
+      mmsg->key = *key;
+      msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
+      GNUNET_memcpy (msg_path,
+                     path,
+                     path_length * sizeof (struct GNUNET_PeerIdentity));
+      GNUNET_MQ_send (m->ch->mq,
+                      env);
     }
+  }
+  GNUNET_free_non_null (cl);
 }
 
 
 /**
- * Initialize client subsystem.
+ * Check if some client is monitoring GET RESP messages and notify
+ * them in that case.
  *
- * @param server the initialized server
+ * @param type The type of data in the result.
+ * @param get_path Peers on GET path (or NULL if not recorded).
+ * @param get_path_length number of entries in get_path.
+ * @param put_path peers on the PUT path (or NULL if not recorded).
+ * @param put_path_length number of entries in get_path.
+ * @param exp Expiration time of the data.
+ * @param key Key of the data.
+ * @param data Pointer to the result data.
+ * @param size Number of bytes in @a data.
  */
-void 
-GDS_CLIENT_init (struct GNUNET_SERVER_Handle *server)
+void
+GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type,
+                              const struct GNUNET_PeerIdentity *get_path,
+                              unsigned int get_path_length,
+                              const struct GNUNET_PeerIdentity *put_path,
+                              unsigned int put_path_length,
+                              struct GNUNET_TIME_Absolute exp,
+                              const struct GNUNET_HashCode * key,
+                              const void *data,
+                              size_t size)
 {
-  static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
-    {&handle_dht_local_put, NULL, 
-     GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, 0},
-    {&handle_dht_local_get, NULL, 
-     GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, 0},
-    {&handle_dht_local_get_stop, NULL,
-     GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP, 
-     sizeof (struct GNUNET_DHT_StopMessage) },
-    {NULL, NULL, 0, 0}
-  };
-  forward_map = GNUNET_CONTAINER_multihashmap_create (1024);
-  retry_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
-  GNUNET_SERVER_add_handlers (server, plugin_handlers);
-  GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
+  struct ClientMonitorRecord *m;
+  struct ClientHandle **cl;
+  unsigned int cl_size;
+
+  cl = NULL;
+  cl_size = 0;
+  for (m = monitor_head; NULL != m; m = m->next)
+  {
+    if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
+        (NULL == m->key ||
+         memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
+    {
+      struct GNUNET_MQ_Envelope *env;
+      struct GNUNET_DHT_MonitorGetRespMessage *mmsg;
+      struct GNUNET_PeerIdentity *path;
+      size_t msize;
+      unsigned int i;
+
+      /* Don't send duplicates */
+      for (i = 0; i < cl_size; i++)
+        if (cl[i] == m->ch)
+          break;
+      if (i < cl_size)
+        continue;
+      GNUNET_array_append (cl,
+                           cl_size,
+                           m->ch);
+
+      msize = size;
+      msize += (get_path_length + put_path_length)
+               * sizeof (struct GNUNET_PeerIdentity);
+      env = GNUNET_MQ_msg_extra (mmsg,
+                                 msize,
+                                 GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP);
+      mmsg->type = htonl(type);
+      mmsg->put_path_length = htonl(put_path_length);
+      mmsg->get_path_length = htonl(get_path_length);
+      mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
+      mmsg->key = *key;
+      path = (struct GNUNET_PeerIdentity *) &mmsg[1];
+      GNUNET_memcpy (path,
+                     put_path,
+                     put_path_length * sizeof (struct GNUNET_PeerIdentity));
+      GNUNET_memcpy (path,
+                     get_path,
+                     get_path_length * sizeof (struct GNUNET_PeerIdentity));
+      GNUNET_memcpy (&path[get_path_length],
+                     data,
+                     size);
+      GNUNET_MQ_send (m->ch->mq,
+                      env);
+    }
+  }
+  GNUNET_free_non_null (cl);
 }
 
 
 /**
- * Shutdown client subsystem.
+ * Check if some client is monitoring PUT messages and notify
+ * them in that case.
+ *
+ * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
+ * @param type The type of data in the request.
+ * @param hop_count Hop count so far.
+ * @param path_length number of entries in path (or 0 if not recorded).
+ * @param path peers on the PUT path (or NULL if not recorded).
+ * @param desired_replication_level Desired replication level.
+ * @param exp Expiration time of the data.
+ * @param key Key under which data is to be stored.
+ * @param data Pointer to the data carried.
+ * @param size Number of bytes in data.
  */
 void
-GDS_CLIENT_done ()
+GDS_CLIENTS_process_put (uint32_t options,
+                         enum GNUNET_BLOCK_Type type,
+                         uint32_t hop_count,
+                         uint32_t desired_replication_level,
+                         unsigned int path_length,
+                         const struct GNUNET_PeerIdentity *path,
+                         struct GNUNET_TIME_Absolute exp,
+                         const struct GNUNET_HashCode *key,
+                         const void *data,
+                         size_t size)
 {
-  GNUNET_assert (client_head == NULL);
-  GNUNET_assert (client_tail == NULL);
-  if (GNUNET_SCHEDULER_NO_TASK != retry_task)
+  struct ClientMonitorRecord *m;
+  struct ClientHandle **cl;
+  unsigned int cl_size;
+
+  cl = NULL;
+  cl_size = 0;
+  for (m = monitor_head; NULL != m; m = m->next)
+  {
+    if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
+        (NULL == m->key ||
+         memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
     {
-      GNUNET_SCHEDULER_cancel (retry_task);
-      retry_task = GNUNET_SCHEDULER_NO_TASK;
+      struct GNUNET_MQ_Envelope *env;
+      struct GNUNET_DHT_MonitorPutMessage *mmsg;
+      struct GNUNET_PeerIdentity *msg_path;
+      size_t msize;
+      unsigned int i;
+
+      /* Don't send duplicates */
+      for (i = 0; i < cl_size; i++)
+        if (cl[i] == m->ch)
+          break;
+      if (i < cl_size)
+        continue;
+      GNUNET_array_append (cl,
+                           cl_size,
+                           m->ch);
+
+      msize = size;
+      msize += path_length * sizeof (struct GNUNET_PeerIdentity);
+      env = GNUNET_MQ_msg_extra (mmsg,
+                                 msize,
+                                 GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT);
+      mmsg->options = htonl(options);
+      mmsg->type = htonl(type);
+      mmsg->hop_count = htonl(hop_count);
+      mmsg->desired_replication_level = htonl (desired_replication_level);
+      mmsg->put_path_length = htonl (path_length);
+      mmsg->key = *key;
+      mmsg->expiration_time = GNUNET_TIME_absolute_hton (exp);
+      msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
+      GNUNET_memcpy (msg_path,
+                     path,
+                     path_length * sizeof (struct GNUNET_PeerIdentity));
+      GNUNET_memcpy (&msg_path[path_length],
+                     data,
+                     size);
+      GNUNET_MQ_send (m->ch->mq,
+                      env);
     }
-  GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
-  GNUNET_CONTAINER_heap_destroy (retry_heap);
-  retry_heap = NULL;
-  GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
-  GNUNET_CONTAINER_multihashmap_destroy (forward_map);
-  forward_map = NULL;
+  }
+  GNUNET_free_non_null (cl);
 }
 
-/* end of gnunet-service-dht_clients.c */
 
+/**
+ * Initialize client subsystem.
+ *
+ * @param server the initialized server
+ */
+static void
+GDS_CLIENTS_init ()
+{
+  forward_map
+    = GNUNET_CONTAINER_multihashmap_create (1024,
+                                            GNUNET_YES);
+  retry_heap
+    = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
+}
+
+
+/**
+ * Shutdown client subsystem.
+ */
+static void
+GDS_CLIENTS_stop ()
+{
+  if (NULL != retry_task)
+  {
+    GNUNET_SCHEDULER_cancel (retry_task);
+    retry_task = NULL;
+  }
+}
+
+
+/**
+ * Define "main" method using service macro.
+ *
+ * @param name name of the service, i.e. "dht" or "xdht"
+ * @param run name of the initializaton method for the service
+ */
+#define GDS_DHT_SERVICE_INIT(name,run)          \
+ GNUNET_SERVICE_MAIN \
+  (name, \
+  GNUNET_SERVICE_OPTION_NONE, \
+  run, \
+  &client_connect_cb, \
+  &client_disconnect_cb, \
+  NULL, \
+  GNUNET_MQ_hd_var_size (dht_local_put, \
+                         GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, \
+                         struct GNUNET_DHT_ClientPutMessage, \
+                         NULL), \
+  GNUNET_MQ_hd_var_size (dht_local_get, \
+                         GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, \
+                         struct GNUNET_DHT_ClientGetMessage, \
+                         NULL), \
+  GNUNET_MQ_hd_fixed_size (dht_local_get_stop, \
+                          GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP, \
+                          struct GNUNET_DHT_ClientGetStopMessage, \
+                          NULL), \
+  GNUNET_MQ_hd_fixed_size (dht_local_monitor, \
+                           GNUNET_MESSAGE_TYPE_DHT_MONITOR_START, \
+                           struct GNUNET_DHT_MonitorStartStopMessage, \
+                           NULL), \
+  GNUNET_MQ_hd_fixed_size (dht_local_monitor_stop, \
+                           GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP, \
+                           struct GNUNET_DHT_MonitorStartStopMessage, \
+                           NULL), \
+  GNUNET_MQ_hd_var_size (dht_local_get_result_seen, \
+                         GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN, \
+                         struct GNUNET_DHT_ClientGetResultSeenMessage , \
+                         NULL), \
+  GNUNET_MQ_handler_end ())
+
+
+/**
+ * MINIMIZE heap size (way below 128k) since this process doesn't need much.
+ */
+void __attribute__ ((destructor))
+GDS_CLIENTS_done ()
+{
+  if (NULL != retry_heap)
+  {
+    GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
+    GNUNET_CONTAINER_heap_destroy (retry_heap);
+    retry_heap = NULL;
+  }
+  if (NULL != forward_map)
+  {
+    GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
+    GNUNET_CONTAINER_multihashmap_destroy (forward_map);
+    forward_map = NULL;
+  }
+}
+
+/* end of gnunet-service-dht_clients.c */