-only trigger check config if we actually need it
[oweals/gnunet.git] / src / dht / gnunet-service-wdht_neighbours.c
index ba2d4eb631145c2811fca4a766f11cd6800c75d2..54c52507e1714a5553734ad97f324a2167ae4ddc 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009-2015 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2015 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
 
      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.
 */
 /**
  * @file dht/gnunet-service-wdht_neighbours.c
  * @brief GNUnet DHT service's finger and friend table management code
  * @author Supriti Singh
+ * @author Christian Grothoff
+ * @author Arthur Dewarumez
+ *
+ * TODO:
+ * - initiate finding of successors
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
@@ -39,9 +44,6 @@
 #include "gnunet-service-wdht_datacache.h"
 #include "gnunet-service-wdht_neighbours.h"
 #include "gnunet-service-wdht_nse.h"
-#include <fenv.h>
-#include <stdlib.h>
-#include <string.h>
 #include "dht.h"
 
 #define DEBUG(...)                                           \
  */
 struct FriendInfo;
 
+/**
+ *
+ */
+struct FingerTable;
 
 /**
  * Information we keep per trail.
@@ -83,6 +89,21 @@ struct FriendInfo;
 struct Trail
 {
 
+  /**
+   * Identifier of the trail with the predecessor.
+   */
+  struct GNUNET_HashCode pred_id;
+
+  /**
+   * Identifier of the trail with the successor.
+   */
+  struct GNUNET_HashCode succ_id;
+
+  /**
+   * When does this trail expire.
+   */
+  struct GNUNET_TIME_Absolute expiration_time;
+
   /**
    * MDLL entry in the list of all trails with the same predecessor.
    */
@@ -113,21 +134,6 @@ struct Trail
    */
   struct FriendInfo *succ;
 
-  /**
-   * Identifier of the trail with the predecessor.
-   */
-  struct GNUNET_HashCode pred_id;
-
-  /**
-   * Identifier of the trail with the successor.
-   */
-  struct GNUNET_HashCode succ_id;
-
-  /**
-   * When does this trail expire.
-   */
-  struct GNUNET_TIME_Absolute expiration_time;
-
   /**
    * Location of this trail in the heap.
    */
@@ -135,9 +141,17 @@ struct Trail
 
   /**
    * If this peer started the to create a Finger (and thus @e pred is
-   * NULL), this is the Finger we are trying to intialize.
+   * NULL), this is the finger table of the finger we are trying to
+   * intialize.
    */
-  struct Finger **finger;
+  struct FingerTable *ft;
+
+  /**
+   * If this peer started the trail to create a Finger (and thus @e
+   * pred is NULL), this is the offset of the finger we are trying to
+   * intialize in the unsorted array.
+   */
+  unsigned int finger_off;
 
 };
 
@@ -180,12 +194,6 @@ struct FriendInfo
 };
 
 
-/**
- *
- */
-struct FingerTable;
-
-
 /**
  *
  */
@@ -220,18 +228,13 @@ struct FingerTable
    */
   struct Finger **fingers;
 
-  /**
-   * Array of sorted fingers (sorted by destination, valid fingers first).
-   */
-  struct Finger **sorted_fingers;
-
   /**
    * Size of the finger array.
    */
   unsigned int finger_array_size;
 
   /**
-   * Number of valid entries in @e sorted_fingers (contiguous from offset 0)
+   * Number of valid entries in @e fingers
    */
   unsigned int number_valid_fingers;
 
@@ -341,7 +344,7 @@ struct TrailDestroyMessage
 struct FindSuccessorMessage
 {
   /**
-   * Type: #GNUNET_MESSAGE_TYPE_WDHT_FIND_SUCCESSOR
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_SUCCESSOR_FIND
    */
   struct GNUNET_MessageHeader header;
 
@@ -350,12 +353,6 @@ struct FindSuccessorMessage
    */
   uint32_t reserved GNUNET_PACKED;
 
-  /**
-   * Unique (random) identifier this peer will use to
-   * identify the finger (in future messages).
-   */
-  struct GNUNET_HashCode trail_id;
-
   /**
    * Key for which we would like close values returned.
    * identify the finger (in future messages).
@@ -510,7 +507,7 @@ struct PeerGetResultMessage
   struct GNUNET_MessageHeader header;
 
   /**
-   * The type for the data.
+   * The type for the data in NBO.
    */
   uint32_t type GNUNET_PACKED;
 
@@ -520,20 +517,10 @@ struct PeerGetResultMessage
    */
   uint32_t put_path_length GNUNET_PACKED;
 
-  /**
-   * Length of the GET path that follows (if tracked).
-   */
-  uint32_t get_path_length GNUNET_PACKED;
-
-  /**
-   * Peer which queried for get and should get the result.
-   */
-  struct GNUNET_PeerIdentity querying_peer;
-
   /**
    * When does the content expire?
    */
-  struct GNUNET_TIME_Absolute expiration_time;
+  struct GNUNET_TIME_AbsoluteNBO expiration_time;
 
   /**
    * The key of the corresponding GET request.
@@ -542,8 +529,6 @@ struct PeerGetResultMessage
 
   /* put path (if tracked) */
 
-  /* get path (if tracked) */
-
   /* Payload */
 
 };
@@ -653,6 +638,7 @@ GDS_NEIGHBOURS_handle_get (const struct GNUNET_HashCode *key,
 {
   // find closest finger(s) on all layers
   // use TrailRoute with PeerGetMessage embedded to contact peer
+  // NOTE: actually more complicated, see paper!
 }
 
 
@@ -707,20 +693,92 @@ delete_trail (struct Trail *trail,
   }
   GNUNET_break (trail ==
                 GNUNET_CONTAINER_heap_remove_node (trail->hn));
-  finger = *trail->finger;
+  finger = trail->ft->fingers[trail->finger_off];
   if (NULL != finger)
   {
-    *trail->finger = NULL;
+    trail->ft->fingers[trail->finger_off] = NULL;
+    trail->ft->number_valid_fingers--;
     GNUNET_free (finger);
   }
   GNUNET_free (trail);
 }
 
 
+/**
+ * Forward the given payload message along the trail.
+ *
+ * @param next_target which direction along the trail should we forward
+ * @param trail_id which trail should we forward along
+ * @param have_path do we track the forwarding path?
+ * @param predecessor which peer do we tack on to the path?
+ * @param path path the message has taken so far along the trail
+ * @param path_length number of entries in @a path
+ * @param payload payload of the message
+ */
+static void
+forward_message_on_trail (struct FriendInfo *next_target,
+                          const struct GNUNET_HashCode *trail_id,
+                          int have_path,
+                          const struct GNUNET_PeerIdentity *predecessor,
+                          const struct GNUNET_PeerIdentity *path,
+                          uint16_t path_length,
+                          const struct GNUNET_MessageHeader *payload)
+{
+  struct GNUNET_MQ_Envelope *env;
+  struct TrailRouteMessage *trm;
+  struct GNUNET_PeerIdentity *new_path;
+  unsigned int plen;
+  uint16_t payload_len;
+
+  payload_len = ntohs (payload->size);
+  if (have_path)
+  {
+    plen = path_length + 1;
+    if (plen >= (GNUNET_SERVER_MAX_MESSAGE_SIZE
+                 - payload_len
+                 - sizeof (struct TrailRouteMessage))
+        / sizeof (struct GNUNET_PeerIdentity))
+    {
+      /* Should really not have paths this long... */
+      GNUNET_break_op (0);
+      plen = 0;
+      have_path = 0;
+    }
+  }
+  else
+  {
+    GNUNET_break_op (0 == path_length);
+    path_length = 0;
+    plen = 0;
+  }
+  env = GNUNET_MQ_msg_extra (trm,
+                             payload_len +
+                             plen * sizeof (struct GNUNET_PeerIdentity),
+                             GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE);
+  trm->record_path = htons (have_path);
+  trm->path_length = htons (plen);
+  trm->trail_id = *trail_id;
+  new_path = (struct GNUNET_PeerIdentity *) &trm[1];
+  if (have_path)
+  {
+    memcpy (new_path,
+            path,
+            path_length * sizeof (struct GNUNET_PeerIdentity));
+    new_path[path_length] = *predecessor;
+  }
+  memcpy (&new_path[plen],
+          payload,
+          payload_len);
+  GNUNET_MQ_send (next_target->mq,
+                  env);
+}
+
+
 /**
  * Send the get result to requesting client.
  *
  * @param trail_id trail identifying where to send the result to, NULL for us
+ * @param options routing options (from GET request)
  * @param key Key of the requested data.
  * @param type Block type
  * @param put_path_length Number of peers in @a put_path
@@ -731,6 +789,7 @@ delete_trail (struct Trail *trail,
  */
 void
 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *trail_id,
+                                enum GNUNET_DHT_RouteOption options,
                                 const struct GNUNET_HashCode *key,
                                 enum GNUNET_BLOCK_Type type,
                                 unsigned int put_path_length,
@@ -739,8 +798,40 @@ GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *trail_id,
                                 const void *data,
                                 size_t data_size)
 {
-  // TRICKY: need to introduce some context to remember trail from
-  // the lookup...
+  struct GNUNET_MessageHeader *payload;
+  struct Trail *trail;
+
+  trail = GNUNET_CONTAINER_multihashmap_get (trail_map,
+                                             trail_id);
+  if (NULL == trail)
+  {
+    /* TODO: inform statistics */
+    return;
+  }
+  if (NULL == trail->pred)
+  {
+    /* result is for *us* (local client) */
+    GDS_CLIENTS_handle_reply (expiration,
+                              key,
+                              0, NULL,
+                              put_path_length, put_path,
+                              type,
+                              data_size,
+                              data);
+    return;
+  }
+
+  payload = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader) + data_size);
+  payload->size = data_size;
+  payload->type = GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT;
+
+  forward_message_on_trail (trail->pred,
+                            trail_id,
+                            0 != (options & GNUNET_DHT_RO_RECORD_ROUTE),
+                            &my_identity,
+                            NULL, 0,
+                            payload);
+  GNUNET_free (payload);
 }
 
 
@@ -794,15 +885,44 @@ handle_core_disconnect (void *cls,
 }
 
 
+/**
+ * Function called with a random friend to be returned.
+ *
+ * @param cls a `struct FriendInfo **` with where to store the result
+ * @param peer the peer identity of the friend (ignored)
+ * @param value the `struct FriendInfo *` that was selected at random
+ * @return #GNUNET_OK (all good)
+ */
+static int
+pick_random_helper (void *cls,
+                    const struct GNUNET_PeerIdentity *peer,
+                    void *value)
+{
+  struct FriendInfo **fi = cls;
+  struct FriendInfo *v = value;
+
+  *fi = v;
+  return GNUNET_OK;
+}
+
+
 /**
  * Pick random friend from friends for random walk.
+ *
+ * @return NULL if we have no friends
  */
 static struct FriendInfo *
 pick_random_friend ()
 {
-  // TODO: need to extend peermap API to return random entry...
-  // (Note: same extension exists for hashmap API).
-  return NULL; // FIXME...
+  struct FriendInfo *ret;
+
+  ret = NULL;
+  if (0 ==
+      GNUNET_CONTAINER_multipeermap_get_random (friends_peermap,
+                                                &pick_random_helper,
+                                                &ret))
+    return NULL;
+  return ret;
 }
 
 
@@ -811,11 +931,9 @@ pick_random_friend ()
  * possibly initiate cleanup.
  *
  * @param cls NULL
- * @param tc unused
  */
 static void
-trail_timeout_callback (void *cls,
-                        const struct GNUNET_SCHEDULER_TaskContext *tc)
+trail_timeout_callback (void *cls)
 {
   struct Trail *trail;
   struct GNUNET_TIME_Relative left;
@@ -837,15 +955,26 @@ trail_timeout_callback (void *cls,
 }
 
 
+/**
+ * Compute how big our finger arrays should be (at least).
+ *
+ * @return size of the finger array, never 0
+ */
+static unsigned int
+get_desired_finger_array_size ()
+{
+  /* FIXME: This is just a stub... */
+  return 64;
+}
+
+
 /**
  * Initiate a random walk.
  *
  * @param cls NULL
- * @param tc unused
  */
 static void
-do_random_walk (void *cls,
-                const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_random_walk (void *cls)
 {
   static unsigned int walk_layer;
   struct FriendInfo *friend;
@@ -854,6 +983,7 @@ do_random_walk (void *cls,
   struct FingerTable *ft;
   struct Finger *finger;
   struct Trail *trail;
+  unsigned int nsize;
 
   random_walk_task = NULL;
   friend = pick_random_friend ();
@@ -899,21 +1029,19 @@ do_random_walk (void *cls,
     delete_trail (finger->trail,
                   GNUNET_NO,
                   GNUNET_YES);
-  if (ft->finger_array_size < 42)
-  {
-    // FIXME: must have finger array of the right size here,
-    // FIXME: growing / shrinking are tricy -- with pointers
-    // from Trails!!!
-  }
-
+  if (ft->finger_array_size < (nsize = get_desired_finger_array_size()) )
+    GNUNET_array_grow (ft->fingers,
+                       ft->finger_array_size,
+                       nsize);
   GNUNET_assert (NULL == ft->fingers[ft->walk_offset]);
-
+  trail->ft = ft;
+  trail->finger_off = ft->walk_offset;
   finger = GNUNET_new (struct Finger);
   finger->trail = trail;
-  trail->finger = &ft->fingers[ft->walk_offset];
   finger->ft = ft;
   ft->fingers[ft->walk_offset] = finger;
   ft->is_sorted = GNUNET_NO;
+  ft->number_valid_fingers++;
   ft->walk_offset = (ft->walk_offset + 1) % ft->finger_array_size;
 
   walk_layer = (walk_layer + 1) % NUMBER_LAYERED_ID;
@@ -999,8 +1127,15 @@ handle_dht_p2p_random_walk (void *cls,
   const struct RandomWalkMessage *m;
   struct Trail *t;
   struct FriendInfo *pred;
+  uint16_t layer;
 
   m = (const struct RandomWalkMessage *) message;
+  layer = ntohs (m->layer);
+  if (layer > NUMBER_LAYERED_ID)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
   pred = GNUNET_CONTAINER_multipeermap_get (friends_peermap,
                                             peer);
   t = GNUNET_new (struct Trail);
@@ -1034,25 +1169,17 @@ handle_dht_p2p_random_walk (void *cls,
     /* We are the last hop, generate response */
     struct GNUNET_MQ_Envelope *env;
     struct RandomWalkResponseMessage *rwrm;
-    uint16_t layer;
 
     env = GNUNET_MQ_msg (rwrm,
                          GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE);
     rwrm->reserved = htonl (0);
     rwrm->trail_id = m->trail_id;
-    layer = ntohs (m->layer);
     if (0 == layer)
       (void) GDS_DATACACHE_get_random_key (&rwrm->location);
     else
     {
       struct FingerTable *ft;
 
-      if (layer > NUMBER_LAYERED_ID)
-      {
-        GNUNET_break_op (0);
-        // FIXME: clean up 't'...
-        return GNUNET_SYSERR;
-      }
       ft = &fingers[layer-1];
       if (0 == ft->number_valid_fingers)
       {
@@ -1062,9 +1189,13 @@ handle_dht_p2p_random_walk (void *cls,
       else
       {
         struct Finger *f;
+        unsigned int off;
+        unsigned int i;
 
-        f = ft->fingers[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
-                                                  ft->number_valid_fingers)];
+        off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
+                                        ft->number_valid_fingers);
+        for (i=0; (NULL == (f = ft->fingers[i])) || (off > 0); i++)
+          if (NULL != f) off--;
         rwrm->location = f->destination;
       }
     }
@@ -1113,8 +1244,7 @@ handle_dht_p2p_random_walk (void *cls,
 
 
 /**
- * Handle a `struct RandomWalkResponseMessage` from a GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE
- * message.
+ * Handle a `struct RandomWalkResponseMessage`.
  *
  * @param cls closure (NULL)
  * @param peer sender identity
@@ -1127,9 +1257,57 @@ handle_dht_p2p_random_walk_response (void *cls,
                                      const struct GNUNET_MessageHeader *message)
 {
   const struct RandomWalkResponseMessage *rwrm;
+  struct Trail *trail;
+  struct FriendInfo *pred;
+  struct FingerTable *ft;
+  struct Finger *finger;
 
   rwrm = (const struct RandomWalkResponseMessage *) message;
-  // 1) lookup trail => find Finger entry => fill in 'destination' and mark valid, move to end of sorted array, mark unsorted, update links from 'trails'
+  trail = GNUNET_CONTAINER_multihashmap_get (trail_map,
+                                             &rwrm->trail_id);
+  if (NULL == trail)
+  {
+    /* TODO: log/statistics: we didn't find the trail (can happen) */
+    return GNUNET_OK;
+  }
+  if (NULL != (pred = trail->pred))
+  {
+    /* We are not the first hop, keep forwarding */
+    struct GNUNET_MQ_Envelope *env;
+    struct RandomWalkResponseMessage *rwrm2;
+
+    env = GNUNET_MQ_msg (rwrm2,
+                         GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE);
+    rwrm2->reserved = htonl (0);
+    rwrm2->location = rwrm->location;
+    rwrm2->trail_id = trail->pred_id;
+    GNUNET_MQ_send (pred->mq,
+                    env);
+    return GNUNET_OK;
+  }
+  /* We are the first hop, complete finger */
+  if (NULL == (ft = trail->ft))
+  {
+    /* Eh, why did we create the trail if we have no FT? */
+    GNUNET_break (0);
+    delete_trail (trail,
+                  GNUNET_NO,
+                  GNUNET_YES);
+    return GNUNET_OK;
+  }
+  if (NULL == (finger = ft->fingers[trail->finger_off]))
+  {
+    /* Eh, finger got deleted, but why not the trail as well? */
+    GNUNET_break (0);
+    delete_trail (trail,
+                  GNUNET_NO,
+                  GNUNET_YES);
+    return GNUNET_OK;
+  }
+
+
+  // 1) lookup trail => find Finger entry => fill in 'destination' and mark valid, move to end of sorted array,
+  //mark unsorted, update links from 'trails'
   /*
    * Steps :
    *  1 check if we are the correct layer
@@ -1159,92 +1337,17 @@ handle_dht_p2p_trail_destroy (void *cls,
   struct Trail *trail;
 
   tdm = (const struct TrailDestroyMessage *) message;
-
-  /* Retreive the trail from the trail_map */
-  trail = GNUNET_CONTAINER_multihashmap_get(trail_map, tdm->trail_id);
-
-  if(peer == trail->pred_id){
-    delete(trail, GNUNET_NO, GNUNET_YES);
-  }
-  else{
-    delete(trail, GNUNET_YES, GNUNET_NO);
-  }
-
-  return GNUNET_OK;
-}
-
-
-/**
- * Handler for a message we received along some trail.
- *
- * @param cls closure
- * @param trail_id trail identifier
- * @param message the message we got
- * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
- */
-typedef int
-(*TrailHandlerCallback)(void *cls,
-                        const struct GNUNET_HashCode *trail_id,
-                        const struct GNUNET_MessageHandler *message);
-
-
-/**
- * Definition of a handler for a message received along some trail.
- */
-struct TrailHandler
-{
-  /**
-   * NULL for end-of-list.
-   */
-  TrailHandlerCallback callback;
-
-  /**
-   * Closure for @e callback.
-   */
-  void *cls;
-
-  /**
-   * Message type this handler addresses.
-   */
-  uint16_t message_type;
-
-  /**
-   * Use 0 for variable-size.
-   */
-  uint16_t message_size;
-};
-
-
-/**
- * Handle a `struct TrailRouteMessage`.
- *
- * @param cls closure (NULL)
- * @param peer sender identity
- * @param message the finger destroy message
- * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
- */
-static int
-handle_dht_p2p_trail_route (void *cls,
-                            const struct GNUNET_PeerIdentity *peer,
-                            const struct GNUNET_MessageHeader *message)
-{
-  const struct TrailRouteMessage *trm;
-  const void *payload;
-  size_t msize;
-  unsigned int start_payload;
-
-  /* msize = ntohs (message->size); */
-  /* if (msize < sizeof (struct TrailRouteMessage)) */
-  /* { */
-  /*   GNUNET_break_op (0); */
-  /*   return GNUNET_YES; */
-  /* } */
-
-  /* trm = (const struct TrailRouteMessage *) message; */
-  /* /\* Retreive payload *\/ */
-  /* start_payload = sizeof (struct PeerGetResultMessage); */
-  /* payload = message[start_payload]; */
-
+  trail = GNUNET_CONTAINER_multihashmap_get (trail_map,
+                                             &tdm->trail_id);
+  delete_trail (trail,
+                ( (NULL != trail->succ) &&
+                  (0 == memcmp (peer,
+                                &trail->succ->id,
+                                sizeof (struct GNUNET_PeerIdentity))) ),
+                ( (NULL != trail->pred) &&
+                  (0 == memcmp (peer,
+                                &trail->pred->id,
+                                sizeof (struct GNUNET_PeerIdentity))) ));
   return GNUNET_OK;
 }
 
@@ -1255,24 +1358,26 @@ handle_dht_p2p_trail_route (void *cls,
  *
  * @param cls closure (NULL)
  * @param trail_id path to the originator
+ * @param trail_path path the message took on the trail, if available
+ * @param trail_path_length number of entries on the @a trail_path
  * @param message the finger setup message
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
 handle_dht_p2p_successor_find (void *cls,
                                const struct GNUNET_HashCode *trail_id,
+                               const struct GNUNET_PeerIdentity *trail_path,
+                               unsigned int trail_path_length,
                                const struct GNUNET_MessageHeader *message)
 {
   const struct FindSuccessorMessage *fsm;
 
+  /* We do not expect to track trails for the forward-direction
+     of successor finding... */
+  GNUNET_break_op (0 == trail_path_length);
   fsm = (const struct FindSuccessorMessage *) message;
-  // locate trail (for sending reply), if not exists, fail nicely.
-  // otherwise, go to datacache and return 'top k' elements closest to 'key'
-  // as "PUT" messages via the trail (need to extend DB API!)
-#if 0
   GDS_DATACACHE_get_successors (trail_id,
-                                key);
-#endif
+                                &fsm->key);
   return GNUNET_OK;
 }
 
@@ -1282,12 +1387,16 @@ handle_dht_p2p_successor_find (void *cls,
  *
  * @param cls closure (NULL)
  * @param trail_id path to the originator
+ * @param trail_path path the message took on the trail, if available
+ * @param trail_path_length number of entries on the @a trail_path
  * @param message the peer get message
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
 handle_dht_p2p_peer_get (void *cls,
                          const struct GNUNET_HashCode *trail_id,
+                         const struct GNUNET_PeerIdentity *trail_path,
+                         unsigned int trail_path_length,
                          const struct GNUNET_MessageHeader *message)
 {
   const struct PeerGetMessage *pgm;
@@ -1313,12 +1422,16 @@ handle_dht_p2p_peer_get (void *cls,
  *
  * @param cls closure (NULL)
  * @param trail_id path to the originator
+ * @param trail_path path the message took on the trail, if available
+ * @param trail_path_length number of entries on the @a trail_path
  * @param message the peer get result message
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
 handle_dht_p2p_peer_get_result (void *cls,
                                 const struct GNUNET_HashCode *trail_id,
+                                const struct GNUNET_PeerIdentity *trail_path,
+                                unsigned int trail_path_length,
                                 const struct GNUNET_MessageHeader *message)
 {
   const struct PeerGetResultMessage *pgrm;
@@ -1349,12 +1462,16 @@ handle_dht_p2p_peer_get_result (void *cls,
  *
  * @param cls closure (NULL)
  * @param trail_id path to the originator
+ * @param trail_path path the message took on the trail, if available
+ * @param trail_path_length number of entries on the @a trail_path
  * @param message the peer put message
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
 handle_dht_p2p_peer_put (void *cls,
                          const struct GNUNET_HashCode *trail_id,
+                         const struct GNUNET_PeerIdentity *trail_path,
+                         unsigned int trail_path_length,
                          const struct GNUNET_MessageHeader *message)
 {
   const struct PeerGetResultMessage *pgrm;
@@ -1370,14 +1487,14 @@ handle_dht_p2p_peer_put (void *cls,
 #if 0
   GDS_DATACACHE_handle_put (expiration_time,
                             key,
-                            path_length, path,
+                            combined_path_length, combined_path,
                             block_type,
                             data_size,
                             data);
   GDS_CLIENTS_process_put (options,
                            block_type,
                            0, 0,
-                           path_length, path,
+                           combined_path_length, combined_path,
                            expiration_time,
                            key,
                            data,
@@ -1387,8 +1504,182 @@ handle_dht_p2p_peer_put (void *cls,
 }
 
 
+/**
+ * Handler for a message we received along some trail.
+ *
+ * @param cls closure
+ * @param trail_id trail identifier
+ * @param trail_path path the message took on the trail, if available
+ * @param trail_path_length number of entries on the @a trail_path
+ * @param message the message we got
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+typedef int
+(*TrailHandlerCallback)(void *cls,
+                        const struct GNUNET_HashCode *trail_id,
+                        const struct GNUNET_PeerIdentity *trail_path,
+                        unsigned int trail_path_length,
+                        const struct GNUNET_MessageHeader *message);
+
+
+/**
+ * Definition of a handler for a message received along some trail.
+ */
+struct TrailHandler
+{
+  /**
+   * NULL for end-of-list.
+   */
+  TrailHandlerCallback callback;
+
+  /**
+   * Closure for @e callback.
+   */
+  void *cls;
+
+  /**
+   * Message type this handler addresses.
+   */
+  uint16_t message_type;
+
+  /**
+   * Use 0 for variable-size.
+   */
+  uint16_t message_size;
+};
+
+
+/**
+ * Handle a `struct TrailRouteMessage`.
+ *
+ * @param cls closure (NULL)
+ * @param peer sender identity
+ * @param message the finger destroy message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
+ */
+static int
+handle_dht_p2p_trail_route (void *cls,
+                            const struct GNUNET_PeerIdentity *peer,
+                            const struct GNUNET_MessageHeader *message)
+{
+  static const struct TrailHandler handlers[] = {
+    { &handle_dht_p2p_successor_find, NULL,
+      GNUNET_MESSAGE_TYPE_WDHT_SUCCESSOR_FIND,
+      sizeof (struct FindSuccessorMessage) },
+    { &handle_dht_p2p_peer_get, NULL,
+      GNUNET_MESSAGE_TYPE_WDHT_GET,
+      0 },
+    { &handle_dht_p2p_peer_get_result, NULL,
+      GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT,
+      0 },
+    { &handle_dht_p2p_peer_put, NULL,
+      GNUNET_MESSAGE_TYPE_WDHT_PUT,
+      0 },
+    { NULL, NULL, 0, 0 }
+  };
+  unsigned int i;
+  const struct TrailRouteMessage *trm;
+  const struct GNUNET_PeerIdentity *path;
+  uint16_t path_length;
+  const struct GNUNET_MessageHeader *payload;
+  const struct TrailHandler *th;
+  struct Trail *trail;
+  size_t msize;
+
+  /* Parse and check message is well-formed */
+  msize = ntohs (message->size);
+  if (msize < sizeof (struct TrailRouteMessage))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_YES;
+  }
+  trm = (const struct TrailRouteMessage *) message;
+  path_length = ntohs (trm->path_length);
+  if (msize < sizeof (struct TrailRouteMessage) +
+      path_length * sizeof (struct GNUNET_PeerIdentity) +
+      sizeof (struct GNUNET_MessageHeader) )
+  {
+    GNUNET_break_op (0);
+    return GNUNET_YES;
+  }
+  path = (const struct GNUNET_PeerIdentity *) &trm[1];
+  payload = (const struct GNUNET_MessageHeader *) &path[path_length];
+  if (msize != (ntohs (payload->size) +
+                sizeof (struct TrailRouteMessage) +
+                path_length * sizeof (struct GNUNET_PeerIdentity)))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_YES;
+  }
+
+  /* Is this message for us? */
+  trail = GNUNET_CONTAINER_multihashmap_get (trail_map,
+                                             &trm->trail_id);
+  if ( (NULL != trail->pred) &&
+       (0 == memcmp (peer,
+                     &trail->pred->id,
+                     sizeof (struct GNUNET_PeerIdentity))) )
+  {
+    /* forward to 'successor' */
+    if (NULL != trail->succ)
+    {
+      forward_message_on_trail (trail->succ,
+                                &trail->succ_id,
+                                ntohs (trm->record_path),
+                                peer,
+                                path,
+                                path_length,
+                                payload);
+      return GNUNET_OK;
+    }
+  }
+  else
+  {
+    /* forward to 'predecessor' */
+    GNUNET_break_op ( (NULL != trail->succ) &&
+                      (0 == memcmp (peer,
+                                    &trail->succ->id,
+                                    sizeof (struct GNUNET_PeerIdentity))) );
+    if (NULL != trail->pred)
+    {
+      forward_message_on_trail (trail->pred,
+                                &trail->pred_id,
+                                ntohs (trm->record_path),
+                                peer,
+                                path,
+                                path_length,
+                                payload);
+      return GNUNET_OK;
+    }
+  }
+
+  /* Message is for us, dispatch to handler */
+  th = NULL;
+  for (i=0; NULL != handlers[i].callback; i++)
+  {
+    th = &handlers[i];
+    if (ntohs (payload->type) == th->message_type)
+    {
+      if ( (0 == th->message_size) ||
+           (ntohs (payload->size) == th->message_size) )
+        th->callback (th->cls,
+                      &trm->trail_id,
+                      path,
+                      path_length,
+                      payload);
+      else
+        GNUNET_break_op (0);
+      break;
+    }
+  }
+  GNUNET_break_op (NULL != th);
+  return GNUNET_OK;
+}
+
+
 /**
  * Initialize neighbours subsystem.
+ *
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int