Adding a function pick_random_friend ()
[oweals/gnunet.git] / src / dht / gnunet-service-wdht_neighbours.c
index 858c0457dd826782e90246a57ca2de8db9e0d7b2..4e6ccf4324b0c94beb77b40a383e1d0572a411cb 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009-2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2015 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA 02111-1307, USA.
 */
-
 /**
- * @file dht/gnunet-service-xdht_neighbours.c
+ * @file dht/gnunet-service-wdht_neighbours.c
  * @brief GNUnet DHT service's finger and friend table management code
  * @author Supriti Singh
  */
-
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_block_lib.h"
 #include "gnunet_transport_service.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_statistics_service.h"
-#include "gnunet-service-xdht.h"
+#include "gnunet-service-wdht.h"
 #include "gnunet-service-wdht_clients.h"
 #include "gnunet-service-wdht_datacache.h"
 #include "gnunet-service-wdht_neighbours.h"
-#include "gnunet-service-wdht_routing.h"
+#include "gnunet-service-wdht_nse.h"
 #include <fenv.h>
+#include <stdlib.h>
+#include <string.h>
 #include "dht.h"
 
 #define DEBUG(...)                                           \
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 
 /**
- * FIXME
+ * Trail timeout. After what time do trails always die?
+ */
+#define TRAIL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 42)
+
+/**
+ * Random walk delay. How often do we walk the overlay?
+ */
+#define RANDOM_WALK_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 42)
+
+/**
+ * The number of layered ID to use.
+ */
+#define NUMBER_LAYERED_ID 8
+
+/**
+ * The number of random walk to launch at the beginning of the initialization
+ */
+/* FIXME: find a better value */
+#define NUMBER_RANDOM_WALK 20
+
+
+/******************* The db structure and related functions *******************/
+
+/**
+ * Entry in #friends_peermap.
+ */
+struct FriendInfo;
+
+
+/**
+ * Information we keep per trail.
+ */
+struct Trail
+{
+
+  /**
+   * MDLL entry in the list of all trails with the same predecessor.
+   */
+  struct Trail *prev_succ;
+
+  /**
+   * MDLL entry in the list of all trails with the same predecessor.
+   */
+  struct Trail *next_succ;
+
+  /**
+   * MDLL entry in the list of all trails with the same predecessor.
+   */
+  struct Trail *prev_pred;
+
+  /**
+   * MDLL entry in the list of all trails with the same predecessor.
+   */
+  struct Trail *next_pred;
+
+  /**
+   * Our predecessor in the trail, NULL if we are initiator (?).
+   */
+  struct FriendInfo *pred;
+
+  /**
+   * Our successor in the trail, NULL if we are the last peer.
+   */
+  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.
+   */
+  struct GNUNET_CONTAINER_HeapNode *hn;
+
+  /**
+   * If this peer started the to create a Finger (and thus @e pred is
+   * NULL), this is the Finger we are trying to intialize.
+   */
+  struct Finger **finger;
+
+};
+
+
+/**
+ *  Entry in #friends_peermap.
+ */
+struct FriendInfo
+{
+  /**
+   * Friend Identity
+   */
+  struct GNUNET_PeerIdentity id;
+
+  /**
+   *
+   */
+  struct Trail *pred_head;
+
+  /**
+   *
+   */
+  struct Trail *pred_tail;
+
+  /**
+   *
+   */
+  struct Trail *succ_head;
+
+  /**
+   *
+   */
+  struct Trail *succ_tail;
+
+  /**
+   * Core handle for sending messages to this friend.
+   */
+  struct GNUNET_MQ_Handle *mq;
+
+};
+
+
+/**
+ *
+ */
+struct FingerTable;
+
+
+/**
+ *
  */
-#define FOO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
+struct Finger
+{
+  /**
+   *
+   */
+  struct Trail *trail;
+
+  /**
+   *
+   */
+  struct FingerTable *ft;
+
+  /**
+   *
+   */
+  struct GNUNET_HashCode destination;
+
+  /**
+   * #GNUNET_YES if a response has been received. Otherwise #GNUNET_NO.
+   */
+  int valid;
+};
+
+
+struct FingerTable
+{
+  /**
+   * Array of our fingers, unsorted.
+   */
+  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)
+   */
+  unsigned int number_valid_fingers;
+
+  /**
+   * Which offset in @e fingers will we redo next.
+   */
+  unsigned int walk_offset;
+
+  /**
+   * Is the finger array sorted?
+   */
+  int is_sorted;
+
+};
+
+
+/***********************  end of the db structure part  ***********************/
 
 
 GNUNET_NETWORK_STRUCT_BEGIN
@@ -58,10 +256,10 @@ GNUNET_NETWORK_STRUCT_BEGIN
 /**
  * Setup a finger using the underlay topology ("social network").
  */
-struct FingerSetupMessage
+struct RandomWalkMessage
 {
   /**
-   * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK
    */
   struct GNUNET_MessageHeader header;
 
@@ -78,20 +276,19 @@ struct FingerSetupMessage
 
   /**
    * Unique (random) identifier this peer will use to
-   * identify the finger (in future messages).
+   * identify the trail (in future messages).
    */
-  struct GNUNET_HashCode finger_id;
+  struct GNUNET_HashCode trail_id;
 
 };
 
-
 /**
- * Response to a `struct FingerSetupMessage`.
+ * Response to a `struct RandomWalkMessage`.
  */
-struct FingerSetupResponseMessage
+struct RandomWalkResponseMessage
 {
   /**
-   * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP_RESPONSE
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE
    */
   struct GNUNET_MessageHeader header;
 
@@ -101,10 +298,10 @@ struct FingerSetupResponseMessage
   uint32_t reserved GNUNET_PACKED;
 
   /**
-   * Unique (random) identifier this peer will use to
-   * identify the finger (in future messages).
+   * Unique (random) identifier from the
+   * `struct RandomWalkMessage`.
    */
-  struct GNUNET_HashCode finger_id;
+  struct GNUNET_HashCode trail_id;
 
   /**
    * Random location in the respective layer where the
@@ -114,14 +311,13 @@ struct FingerSetupResponseMessage
 
 };
 
-
 /**
- * Response to an event that causes a finger to die.
+ * Response to an event that causes a trail to die.
  */
-struct FingerDestroyMessage
+struct TrailDestroyMessage
 {
   /**
-   * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_DESTROY
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY
    */
   struct GNUNET_MessageHeader header;
 
@@ -134,18 +330,18 @@ struct FingerDestroyMessage
    * Unique (random) identifier this peer will use to
    * identify the finger (in future messages).
    */
-  struct GNUNET_HashCode finger_id;
+  struct GNUNET_HashCode trail_id;
 
 };
 
 
 /**
- * Send a message along a finger.
+ * Send a message along a trail.
  */
-struct FingerRouteMessage
+struct FindSuccessorMessage
 {
   /**
-   * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_ROUTE
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_FIND_SUCCESSOR
    */
   struct GNUNET_MessageHeader header;
 
@@ -158,9 +354,50 @@ struct FingerRouteMessage
    * Unique (random) identifier this peer will use to
    * identify the finger (in future messages).
    */
-  struct GNUNET_HashCode finger_id;
+  struct GNUNET_HashCode trail_id;
+
+  /**
+   * Key for which we would like close values returned.
+   * identify the finger (in future messages).
+   */
+  struct GNUNET_HashCode key;
+
+};
+
+
+/**
+ * Send a message along a trail.
+ */
+struct TrailRouteMessage
+{
+  /**
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * #GNUNET_YES if the path should be recorded, #GNUNET_NO if not; in NBO.
+   */
+  uint16_t record_path GNUNET_PACKED;
+
+  /**
+   * Length of the recorded trail, 0 if @e record_path is #GNUNET_NO; in NBO.
+   */
+  uint16_t path_length GNUNET_PACKED;
+
+  /**
+   * Unique (random) identifier this peer will use to
+   * identify the finger (in future messages).
+   */
+  struct GNUNET_HashCode trail_id;
+
+  /**
+   * Path the message has taken so far (excluding sender).
+   */
+  /* struct GNUNET_PeerIdentity path[path_length]; */
 
-  /* followed by payload to send along the finger */
+  /* followed by payload (another `struct GNUNET_MessageHeader`) to
+     send along the trail */
 };
 
 
@@ -170,7 +407,7 @@ struct FingerRouteMessage
 struct PeerPutMessage
 {
   /**
-   * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_PUT
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_PUT
    */
   struct GNUNET_MessageHeader header;
 
@@ -222,7 +459,7 @@ struct PeerPutMessage
 struct PeerGetMessage
 {
   /**
-   * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_GET
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_GET
    */
   struct GNUNET_MessageHeader header;
 
@@ -268,7 +505,7 @@ struct PeerGetMessage
 struct PeerGetResultMessage
 {
   /**
-   * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_GET_RESULT
+   * Type: #GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT
    */
   struct GNUNET_MessageHeader header;
 
@@ -313,98 +550,21 @@ struct PeerGetResultMessage
 
 GNUNET_NETWORK_STRUCT_END
 
-/**
- * Entry in friend_peermap.
- */
-struct FriendInfo;
-
 
 /**
- * Information we keep per trail.
+ * Contains all the layered IDs of this peer.
  */
-struct Trail
-{
-
-  /**
-   * MDLL entry in the list of all trails with the same predecessor.
-   */
-  struct Tail *prev_succ;
-
-  /**
-   * MDLL entry in the list of all trails with the same predecessor.
-   */
-  struct Tail *next_succ;
-
-  /**
-   * MDLL entry in the list of all trails with the same predecessor.
-   */
-  struct Tail *prev_pred;
-
-  /**
-   * MDLL entry in the list of all trails with the same predecessor.
-   */
-  struct Tail *next_pred;
-
-  /**
-   * Our predecessor in the trail, NULL if we are initiator (?).
-   */
-  struct FriendInfo *pred;
-
-  /**
-   * Our successor in the trail, NULL if we are the last peer.
-   */
-  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;
-
-};
-
+struct GNUNET_PeerIdentity layered_id[NUMBER_LAYERED_ID];
 
 /**
- *  Entry in friend_peermap.
+ * Task to timeout trails that have expired.
  */
-struct FriendInfo
-{
-  /**
-   * Friend Identity
-   */
-  struct GNUNET_PeerIdentity id;
-
-  struct Tail *pred_head;
-
-  struct Tail *pred_tail;
-
-  struct Tail *succ_head;
-
-  struct Tail *succ_tail;
-
-  /**
-   * Core handle for sending messages to this friend.
-   * FIXME: use MQ?
-   */
-  struct GNUNET_CORE_TransmitHandle *th;
-
-};
-
-
+static struct GNUNET_SCHEDULER_Task *trail_timeout_task;
 
 /**
- * Task to timeout trails that have expired.
+ * Task to perform random walks.
  */
-static struct GNUNET_SCHEDULER_Task *trail_timeout_task;
+static struct GNUNET_SCHEDULER_Task *random_walk_task;
 
 /**
  * Identity of this peer.
@@ -414,17 +574,22 @@ static struct GNUNET_PeerIdentity my_identity;
 /**
  * Peer map of all the friends of a peer
  */
-static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
+static struct GNUNET_CONTAINER_MultiPeerMap *friends_peermap;
 
 /**
- * Tail map, mapping tail identifiers to `struct Trail`s
+ * Fingers per layer.
  */
-static struct GNUNET_CONTAINER_MultiHashMap *tail_map;
+static struct FingerTable fingers[NUMBER_LAYERED_ID];
+
+/**
+ * Tail map, mapping tail identifiers to `struct Trail`s
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *trail_map;
 
 /**
  * Tail heap, organizing trails by expiration time.
  */
-static struct GNUNET_CONTAINER_Heap *tail_heap;
+static struct GNUNET_CONTAINER_Heap *trail_heap;
 
 /**
  * Handle to CORE.
@@ -432,979 +597,972 @@ static struct GNUNET_CONTAINER_Heap *tail_heap;
 static struct GNUNET_CORE_Handle *core_api;
 
 
-
-
-
+/**
+ * Handle the put request from the client.
+ *
+ * @param key Key for the content
+ * @param block_type Type of the block
+ * @param options Routing options
+ * @param desired_replication_level Desired replication count
+ * @param expiration_time When does the content expire
+ * @param data Content to store
+ * @param data_size Size of content @a data in bytes
+ */
+void
+GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
+                           enum GNUNET_BLOCK_Type block_type,
+                           enum GNUNET_DHT_RouteOption options,
+                           uint32_t desired_replication_level,
+                           struct GNUNET_TIME_Absolute expiration_time,
+                           const void *data,
+                           size_t data_size)
+{
+  GDS_DATACACHE_handle_put (expiration_time,
+                            key,
+                            0, NULL,
+                            0, NULL,
+                            block_type,
+                            data_size,
+                            data);
+  GDS_CLIENTS_process_put (options,
+                           block_type,
+                           0, 0,
+                           0, NULL,
+                           expiration_time,
+                           key,
+                           data,
+                           data_size);
+}
 
 
 /**
- * Construct a trail setup message and forward it to target_friend
- * @param source_peer Peer which wants to setup the trail
- * @param ultimate_destination_finger_value Peer identity closest to this value
- *                                          will be finger to @a source_peer
- * @param best_known_destination Best known destination (could be finger or friend)
- *                               which should get this message. In case it is
- *                               friend, then it is same as target_friend
- * @param target_friend Friend to which message is forwarded now.
- * @param trail_length Total number of peers in trail setup so far.
- * @param trail_peer_list Trail setup so far
- * @param is_predecessor Is @a source_peer looking for trail to a predecessor or not.
- * @param trail_id Unique identifier for the trail we are trying to setup.
- * @param intermediate_trail_id Trail id of intermediate trail to reach to
- *                              best_known_destination when its a finger. If not
- *                              used then set to 0.
+ * Handle the get request from the client file. If I am destination do
+ * datacache put and return. Else find the target friend and forward message
+ * to it.
+ *
+ * @param key Key for the content
+ * @param block_type Type of the block
+ * @param options Routing options
+ * @param desired_replication_level Desired replication count
  */
 void
-GDS_NEIGHBOURS_send_trail_setup (struct GNUNET_PeerIdentity source_peer,
-                                 uint64_t ultimate_destination_finger_value,
-                                 struct GNUNET_PeerIdentity best_known_destination,
-                                 struct FriendInfo *target_friend,
-                                 unsigned int trail_length,
-                                 const struct GNUNET_PeerIdentity *trail_peer_list,
-                                 unsigned int is_predecessor,
-                                 struct GNUNET_HashCode trail_id,
-                                 struct GNUNET_HashCode intermediate_trail_id)
+GDS_NEIGHBOURS_handle_get (const struct GNUNET_HashCode *key,
+                           enum GNUNET_BLOCK_Type block_type,
+                           enum GNUNET_DHT_RouteOption options,
+                           uint32_t desired_replication_level)
 {
-  struct P2PPendingMessage *pending;
-  struct PeerTrailSetupMessage *tsm;
-  struct GNUNET_PeerIdentity *peer_list;
-  size_t msize;
+  // find closest finger(s) on all layers
+  // use TrailRoute with PeerGetMessage embedded to contact peer
+}
+
 
-  msize = sizeof (struct PeerTrailSetupMessage) +
-          (trail_length * sizeof (struct GNUNET_PeerIdentity));
+/**
+ * Delete a trail, it died (timeout, link failure, etc.).
+ *
+ * @param trail trail to delete from all data structures
+ * @param inform_pred should we notify the predecessor?
+ * @param inform_succ should we inform the successor?
+ */
+static void
+delete_trail (struct Trail *trail,
+              int inform_pred,
+              int inform_succ)
+{
+  struct FriendInfo *friend;
+  struct GNUNET_MQ_Envelope *env;
+  struct TrailDestroyMessage *tdm;
+  struct Finger *finger;
 
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
+  friend = trail->pred;
+  if (NULL != friend)
   {
-    GNUNET_break (0);
-    return;
+    if (GNUNET_YES == inform_pred)
+    {
+      env = GNUNET_MQ_msg (tdm,
+                           GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY);
+      tdm->trail_id = trail->pred_id;
+      GNUNET_MQ_send (friend->mq,
+                      env);
+    }
+    GNUNET_CONTAINER_MDLL_remove (pred,
+                                  friend->pred_head,
+                                  friend->pred_tail,
+                                  trail);
   }
-
-  if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
+  friend = trail->succ;
+  if (NULL != friend)
   {
-    GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
-                               1, GNUNET_NO);
+    if (GNUNET_YES == inform_succ)
+    {
+      env = GNUNET_MQ_msg (tdm,
+                           GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY);
+      tdm->trail_id = trail->pred_id;
+      GNUNET_MQ_send (friend->mq,
+                      env);
+    }
+    GNUNET_CONTAINER_MDLL_remove (succ,
+                                  friend->pred_head,
+                                  friend->pred_tail,
+                                  trail);
   }
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  tsm = (struct PeerTrailSetupMessage *) &pending[1];
-  pending->msg = &(tsm->header);
-  tsm->header.size = htons (msize);
-  tsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP);
-  tsm->final_destination_finger_value = GNUNET_htonll (ultimate_destination_finger_value);
-  tsm->source_peer = source_peer;
-  tsm->best_known_destination = best_known_destination;
-  tsm->is_predecessor = htonl (is_predecessor);
-  tsm->trail_id = trail_id;
-  tsm->intermediate_trail_id = intermediate_trail_id;
-
-  if (trail_length > 0)
+  GNUNET_break (trail ==
+                GNUNET_CONTAINER_heap_remove_node (trail->hn));
+  finger = *trail->finger;
+  if (NULL != finger)
   {
-    peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
-    memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct GNUNET_PeerIdentity));
+    *trail->finger = NULL;
+    GNUNET_free (finger);
   }
-
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
+  GNUNET_free (trail);
 }
 
 
 /**
- * Construct a trail setup result message and forward it to target friend.
- * @param querying_peer Peer which sent the trail setup request and should get
- *                      the result back.
- * @param Finger Peer to which the trail has been setup to.
- * @param target_friend Friend to which this message should be forwarded.
- * @param trail_length Numbers of peers in the trail.
- * @param trail_peer_list Peers which are part of the trail from
- *                        querying_peer to Finger, NOT including them.
- * @param is_predecessor Is @a Finger predecessor to @a querying_peer ?
- * @param ultimate_destination_finger_value Value to which @a finger is the closest
- *                                          peer.
- * @param trail_id Unique identifier of the trail.
+ * Blah.
  */
-void
-GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity querying_peer,
-                                        struct GNUNET_PeerIdentity finger,
-                                        struct FriendInfo *target_friend,
-                                        unsigned int trail_length,
-                                        const struct GNUNET_PeerIdentity *trail_peer_list,
-                                        unsigned int is_predecessor,
-                                        uint64_t ultimate_destination_finger_value,
-                                        struct GNUNET_HashCode trail_id)
+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 P2PPendingMessage *pending;
-  struct PeerTrailSetupResultMessage *tsrm;
-  struct GNUNET_PeerIdentity *peer_list;
-  size_t msize;
-
-  msize = sizeof (struct PeerTrailSetupResultMessage) +
-          (trail_length * sizeof (struct GNUNET_PeerIdentity));
-
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
+  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)
   {
-    GNUNET_break (0);
-    return;
+    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;
+    }
   }
-
-  if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
+  else
   {
-    GNUNET_STATISTICS_update (GDS_stats,
-                              gettext_noop ("# P2P messages dropped due to full queue"),
-                              1, GNUNET_NO);
+    GNUNET_break_op (0 == path_length);
+    path_length = 0;
+    plen = 0;
   }
-
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->importance = 0;
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  tsrm = (struct PeerTrailSetupResultMessage *) &pending[1];
-  pending->msg = &tsrm->header;
-  tsrm->header.size = htons (msize);
-  tsrm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_RESULT);
-  tsrm->querying_peer = querying_peer;
-  tsrm->finger_identity = finger;
-  tsrm->is_predecessor = htonl (is_predecessor);
-  tsrm->trail_id = trail_id;
-  tsrm->ulitmate_destination_finger_value =
-          GNUNET_htonll (ultimate_destination_finger_value);
-  peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
-  memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
-
-  /* Send the message to chosen friend. */
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
+  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 notify successor confirmation message.
- * @param trail_id Unique Identifier of the trail.
- * @param trail_direction Destination to Source.
- * @param target_friend Friend to get this message next.
+ * Send the get result to requesting client.
+ *
+ * @param trail_id trail identifying where to send the result to, NULL for us
+ * @param key Key of the requested data.
+ * @param type Block type
+ * @param put_path_length Number of peers in @a put_path
+ * @param put_path Path taken to put the data at its stored location.
+ * @param expiration When will this result expire?
+ * @param data Payload to store
+ * @param data_size Size of the @a data
+ *
+ * FIXME: also pass options, so we know to record paths or not...
  */
 void
-GDS_NEIGHBOURS_send_notify_succcessor_confirmation (struct GNUNET_HashCode trail_id,
-                                                    unsigned int trail_direction,
-                                                     struct FriendInfo *target_friend)
+GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *trail_id,
+                                const struct GNUNET_HashCode *key,
+                                enum GNUNET_BLOCK_Type type,
+                                unsigned int put_path_length,
+                                const struct GNUNET_PeerIdentity *put_path,
+                                struct GNUNET_TIME_Absolute expiration,
+                                const void *data,
+                                size_t data_size)
 {
-  struct PeerNotifyConfirmationMessage *ncm;
-  struct P2PPendingMessage *pending;
-  size_t msize;
-
-  msize = sizeof (struct PeerNotifyConfirmationMessage);
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
-  {
-    GNUNET_break (0);
-    return;
-  }
-
-  if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
-  {
-    GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
-                               1, GNUNET_NO);
-  }
-
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->importance = 0;    /* FIXME */
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  ncm = (struct PeerNotifyConfirmationMessage *) &pending[1];
-  pending->msg = &ncm->header;
-  ncm->header.size = htons (msize);
-  ncm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_SUCCESSOR_CONFIRMATION);
-  ncm->trail_id = trail_id;
-  ncm->trail_direction = htonl (trail_direction);
-
-  /* Send the message to chosen friend. */
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
+  struct GNUNET_MessageHeader *payload;
+
+  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 (NULL /* FIXME: put something right */,
+                            trail_id,
+                            0 /* FIXME: put something right */,
+                            &my_identity,
+                            put_path,
+                            put_path_length,
+                            payload);
 }
 
 
 /**
- * Send trail rejection message to target friend
- * @param source_peer Peer which is trying to setup the trail.
- * @param ultimate_destination_finger_value Peer closest to this value will be
- *                                          @a source_peer's finger
- * @param congested_peer Peer which sent this message as it is congested.
- * @param is_predecessor Is source_peer looking for trail to a predecessor or not.
- * @param trail_peer_list Trails seen so far in trail setup before getting rejected
- *                        by congested_peer. This does NOT include @a source_peer
- *                        and congested_peer.
- * @param trail_length Total number of peers in trail_peer_list, NOT including
- *                     @a source_peer and @a congested_peer
- * @param trail_id Unique identifier of this trail.
- * @param congestion_timeout Duration given by congested peer as an estimate of
- *                           how long it may remain congested.
+ * Method called whenever a peer disconnects.
+ *
+ * @param cls closure
+ * @param peer peer identity this notification is about
  */
-void
-GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
-                                     uint64_t ultimate_destination_finger_value,
-                                     struct GNUNET_PeerIdentity congested_peer,
-                                     unsigned int is_predecessor,
-                                     const struct GNUNET_PeerIdentity *trail_peer_list,
-                                     unsigned int trail_length,
-                                     struct GNUNET_HashCode trail_id,
-                                     struct FriendInfo *target_friend,
-                                     const struct GNUNET_TIME_Relative congestion_timeout)
+static void
+handle_core_disconnect (void *cls,
+                        const struct GNUNET_PeerIdentity *peer)
 {
-  struct PeerTrailRejectionMessage *trm;
-  struct P2PPendingMessage *pending;
-  struct GNUNET_PeerIdentity *peer_list;
-  size_t msize;
+  struct FriendInfo *remove_friend;
+  struct Trail *t;
 
-  msize = sizeof (struct PeerTrailRejectionMessage) +
-          (trail_length * sizeof (struct GNUNET_PeerIdentity));
+  /* If disconnected to own identity, then return. */
+  if (0 == memcmp (&my_identity,
+                   peer,
+                   sizeof (struct GNUNET_PeerIdentity)))
+    return;
 
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
+  if (NULL == (remove_friend =
+               GNUNET_CONTAINER_multipeermap_get (friends_peermap,
+                                                  peer)))
   {
     GNUNET_break (0);
     return;
   }
 
-  if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
-  {
-    GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
-                               1, GNUNET_NO);
-  }
-
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->importance = 0;
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  trm = (struct PeerTrailRejectionMessage *)&pending[1];
-  pending->msg = &trm->header;
-  trm->header.size = htons (msize);
-  trm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION);
-  trm->source_peer = source_peer;
-  trm->congested_peer = congested_peer;
-  trm->congestion_time = congestion_timeout;
-  trm->is_predecessor = htonl (is_predecessor);
-  trm->trail_id = trail_id;
-  trm->ultimate_destination_finger_value =
-          GNUNET_htonll (ultimate_destination_finger_value);
-
-  peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
-  if (trail_length > 0)
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multipeermap_remove (friends_peermap,
+                                                       peer,
+                                                       remove_friend));
+  while (NULL != (t = remove_friend->succ_head))
+    delete_trail (t,
+                  GNUNET_YES,
+                  GNUNET_NO);
+  while (NULL != (t = remove_friend->pred_head))
+    delete_trail (t,
+                  GNUNET_NO,
+                  GNUNET_YES);
+  GNUNET_MQ_destroy (remove_friend->mq);
+  GNUNET_free (remove_friend);
+  if (0 ==
+      GNUNET_CONTAINER_multipeermap_size (friends_peermap))
   {
-    memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct GNUNET_PeerIdentity));
+    GNUNET_SCHEDULER_cancel (random_walk_task);
+    random_walk_task = NULL;
   }
-
-  /* Send the message to chosen friend. */
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
 }
 
 
 /**
- * Construct a verify successor message and forward it to target_friend.
- * @param source_peer Peer which wants to verify its successor.
- * @param successor Peer which is @a source_peer's current successor.
- * @param trail_id Unique Identifier of trail from @a source_peer to @a successor,
- *                 NOT including them.
- * @param trail List of peers which are part of trail to reach from @a source_peer
- *              to @a successor, NOT including them.
- * @param trail_length Total number of peers in @a trail.
- * @param target_friend Next friend to get this message.
+ * Pick random friend from friends for random walk.
  */
-void
-GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity source_peer,
-                                              struct GNUNET_PeerIdentity successor,
-                                              struct GNUNET_HashCode trail_id,
-                                              struct GNUNET_PeerIdentity *trail,
-                                              unsigned int trail_length,
-                                              struct FriendInfo *target_friend)
+static struct FriendInfo *
+pick_random_friend ()
 {
-  struct PeerVerifySuccessorMessage *vsm;
-  struct P2PPendingMessage *pending;
-  struct GNUNET_PeerIdentity *peer_list;
-  size_t msize;
+  GNUNET_CONTAINER_PeerMapIterator *it;
+  if (0 != GNUNET_CONTAINER_multipeermap_get_random (friends_peermap,
+                                                     *it,
+                                                     NULL) ){
+    static struct FriendInfo **friend;
+    struct GNUNET_PeerIdentity *key;
+
+    /* FIXME: i am not sure of this one */
+    key = NULL;
+
+    if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (it,
+                                                                  key,
+                                                                  (void *) friend))
+    {
+      return *friend;
+    }
+  }
+  return NULL;
+}
 
-  msize = sizeof (struct PeerVerifySuccessorMessage) +
-         (trail_length * sizeof (struct GNUNET_PeerIdentity));
 
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
-  {
-    GNUNET_break (0);
-    return;
-  }
+/**
+ * One of our trails might have timed out, check and
+ * possibly initiate cleanup.
+ *
+ * @param cls NULL
+ * @param tc unused
+ */
+static void
+trail_timeout_callback (void *cls,
+                        const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct Trail *trail;
+  struct GNUNET_TIME_Relative left;
 
-  if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
+  trail_timeout_task = NULL;
+  while (NULL != (trail = GNUNET_CONTAINER_heap_peek (trail_heap)))
   {
-    GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
-                               1, GNUNET_NO);
+    left = GNUNET_TIME_absolute_get_remaining (trail->expiration_time);
+    if (0 != left.rel_value_us)
+      break;
+    delete_trail (trail,
+                  GNUNET_YES,
+                  GNUNET_YES);
   }
-
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->importance = 0;    /* FIXME */
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  vsm = (struct PeerVerifySuccessorMessage *) &pending[1];
-  pending->msg = &vsm->header;
-  vsm->header.size = htons (msize);
-  vsm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR);
-  vsm->source_peer = source_peer;
-  vsm->successor = successor;
-  vsm->trail_id = trail_id;
-  peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
-  memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
-
-  /* Send the message to chosen friend. */
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
+  if (NULL != trail)
+    trail_timeout_task = GNUNET_SCHEDULER_add_delayed (left,
+                                                       &trail_timeout_callback,
+                                                       NULL);
 }
 
 
 /**
- * FIXME: In every function we pass target friend except for this one.
- * so, either change everything or this one. also, should se just store
- * the pointer to friend in routing table rather than gnunet_peeridentity.
- * if yes then we should keep friend info in.h  andmake lot of changes.
- * Construct a trail teardown message and forward it to target friend.
+ * Initiate a random walk.
  *
- * @param trail_id Unique identifier of the trail.
- * @param trail_direction Direction of trail.
- * @param target_friend Friend to get this message.
+ * @param cls NULL
+ * @param tc unused
  */
-void
-GDS_NEIGHBOURS_send_trail_teardown (const struct GNUNET_HashCode *trail_id,
-                                    unsigned int trail_direction,
-                                    const struct GNUNET_PeerIdentity *peer)
+static void
+do_random_walk (void *cls,
+                const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct PeerTrailTearDownMessage *ttdm;
-  struct P2PPendingMessage *pending;
-  struct FriendInfo *target_friend;
-  size_t msize;
-
-  msize = sizeof (struct PeerTrailTearDownMessage);
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
+  static unsigned int walk_layer;
+  struct FriendInfo *friend;
+  struct GNUNET_MQ_Envelope *env;
+  struct RandomWalkMessage *rwm;
+  struct FingerTable *ft;
+  struct Finger *finger;
+  struct Trail *trail;
+
+  random_walk_task = NULL;
+  friend = pick_random_friend ();
+
+  trail = GNUNET_new (struct Trail);
+  /* We create the random walk so, no predecessor */
+  trail->succ = friend;
+  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
+                                    &trail->succ_id);
+  if (GNUNET_OK !=
+      GNUNET_CONTAINER_multihashmap_put (trail_map,
+                                         &trail->succ_id,
+                                         trail,
+                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
   {
     GNUNET_break (0);
+    GNUNET_free (trail);
     return;
   }
-
-  if (NULL == (target_friend =
-               GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)))
+  GNUNET_CONTAINER_MDLL_insert (succ,
+                                friend->succ_head,
+                                friend->succ_tail,
+                                trail);
+  trail->expiration_time = GNUNET_TIME_relative_to_absolute (TRAIL_TIMEOUT);
+  trail->hn = GNUNET_CONTAINER_heap_insert (trail_heap,
+                                            trail,
+                                            trail->expiration_time.abs_value_us);
+  if (NULL == trail_timeout_task)
+    trail_timeout_task = GNUNET_SCHEDULER_add_delayed (TRAIL_TIMEOUT,
+                                                       &trail_timeout_callback,
+                                                       NULL);
+  env = GNUNET_MQ_msg (rwm,
+                       GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK);
+  rwm->hops_taken = htonl (0);
+  rwm->trail_id = trail->succ_id;
+  GNUNET_MQ_send (friend->mq,
+                  env);
+  /* clean up 'old' entry (implicitly via trail cleanup) */
+  ft = &fingers[walk_layer];
+
+  if ( (NULL != ft->fingers) &&
+       (NULL != (finger = ft->fingers[ft->walk_offset])) )
+    delete_trail (finger->trail,
+                  GNUNET_NO,
+                  GNUNET_YES);
+  if (ft->finger_array_size < 42)
   {
-    /* FIXME: In what case friend can be null. ?*/
-    GNUNET_break (0);
-    return;
+    // FIXME: must have finger array of the right size here,
+    // FIXME: growing / shrinking are tricky -- with pointers
+    // from Trails!!!
   }
 
-  if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
-  {
-    GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
-                               1, GNUNET_NO);
-  }
+  GNUNET_assert (NULL == ft->fingers[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->walk_offset = (ft->walk_offset + 1) % ft->finger_array_size;
 
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->importance = 0;    /* FIXME */
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  ttdm = (struct PeerTrailTearDownMessage *) &pending[1];
-  pending->msg = &ttdm->header;
-  ttdm->header.size = htons (msize);
-  ttdm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN);
-  ttdm->trail_id = *trail_id;
-  ttdm->trail_direction = htonl (trail_direction);
-
-  /* Send the message to chosen friend. */
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
+  walk_layer = (walk_layer + 1) % NUMBER_LAYERED_ID;
+  random_walk_task = GNUNET_SCHEDULER_add_delayed (RANDOM_WALK_DELAY,
+                                                   &do_random_walk,
+                                                   NULL);
 }
 
 
 /**
- * Construct a verify successor result message and send it to target_friend
- * @param querying_peer Peer which sent the verify successor message.
- * @param source_successor Current_successor of @a querying_peer.
- * @param current_predecessor Current predecessor of @a successor. Could be same
- *                            or different from @a querying_peer.
- * @param trail_id Unique identifier of the trail from @a querying_peer to
- *                 @a successor, NOT including them.
- * @param trail List of peers which are part of trail from @a querying_peer to
- *                 @a successor, NOT including them.
- * @param trail_length Total number of peers in @a trail
- * @param trail_direction Direction in which we are sending the message. In this
- *                        case we are sending result from @a successor to @a querying_peer.
- * @param target_friend Next friend to get this message.
+ * Method called whenever a peer connects.
+ *
+ * @param cls closure
+ * @param peer_identity peer identity this notification is about
  */
-void
-GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity querying_peer,
-                                             struct GNUNET_PeerIdentity current_successor,
-                                             struct GNUNET_PeerIdentity probable_successor,
-                                             struct GNUNET_HashCode trail_id,
-                                             const struct GNUNET_PeerIdentity *trail,
-                                             unsigned int trail_length,
-                                             enum GDS_ROUTING_trail_direction trail_direction,
-                                             struct FriendInfo *target_friend)
+static void
+handle_core_connect (void *cls,
+                     const struct GNUNET_PeerIdentity *peer_identity)
 {
-  struct PeerVerifySuccessorResultMessage *vsmr;
-  struct P2PPendingMessage *pending;
-  struct GNUNET_PeerIdentity *peer_list;
-  size_t msize;
+  struct FriendInfo *friend;
 
-  msize = sizeof (struct PeerVerifySuccessorResultMessage) +
-          (trail_length * sizeof(struct GNUNET_PeerIdentity));
+  /* Check for connect to self message */
+  if (0 == memcmp (&my_identity,
+                   peer_identity,
+                   sizeof (struct GNUNET_PeerIdentity)))
+    return;
 
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
+  /* If peer already exists in our friend_peermap, then exit. */
+  if (GNUNET_YES ==
+      GNUNET_CONTAINER_multipeermap_contains (friends_peermap,
+                                              peer_identity))
   {
     GNUNET_break (0);
     return;
   }
 
-  if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
+  friend = GNUNET_new (struct FriendInfo);
+  friend->id = *peer_identity;
+  friend->mq = GNUNET_CORE_mq_create (core_api,
+                                      peer_identity);
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_CONTAINER_multipeermap_put (friends_peermap,
+                                                    peer_identity,
+                                                    friend,
+                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+  if (NULL == random_walk_task)
   {
-    GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
-                               1, GNUNET_NO);
+    /* random walk needs to be started -- we have a first connection */
+    random_walk_task = GNUNET_SCHEDULER_add_now (&do_random_walk,
+                                                 NULL);
   }
-
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->importance = 0;    /* FIXME */
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  vsmr = (struct PeerVerifySuccessorResultMessage *) &pending[1];
-  pending->msg = &vsmr->header;
-  vsmr->header.size = htons (msize);
-  vsmr->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT);
-  vsmr->querying_peer = querying_peer;
-  vsmr->current_successor = current_successor;
-  vsmr->probable_successor = probable_successor;
-  vsmr->trail_direction = htonl (trail_direction);
-  vsmr->trail_id = trail_id;
-  peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
-  memcpy (peer_list, trail, trail_length * sizeof (struct GNUNET_PeerIdentity));
-
-   /* Send the message to chosen friend. */
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
 }
 
+
 /**
- * Construct a Put message and send it to target_peer.
- * @param key Key for the content
- * @param block_type Type of the block
- * @param options Routing options
- * @param desired_replication_level Desired replication count
- * @param best_known_dest Peer to which this message should reach eventually,
- *                        as it is best known destination to me.
- * @param intermediate_trail_id Trail id in case
- * @param target_peer Peer to which this message will be forwarded.
- * @param hop_count Number of hops traversed so far.
- * @param put_path_length Total number of peers in @a put_path
- * @param put_path Number of peers traversed so far
- * @param expiration_time When does the content expire
- * @param data Content to store
- * @param data_size Size of content @a data in bytes
+ * To be called on core init/fail.
+ *
+ * @param cls service closure
+ * @param identity the public identity of this peer
  */
-void
-GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
-                         enum GNUNET_BLOCK_Type block_type,
-                                          enum GNUNET_DHT_RouteOption options,
-                                          uint32_t desired_replication_level,
-                                          struct GNUNET_PeerIdentity best_known_dest,
-                                          struct GNUNET_HashCode intermediate_trail_id,
-                                          struct GNUNET_PeerIdentity *target_peer,
-                         uint32_t hop_count,
-                         uint32_t put_path_length,
-                         struct GNUNET_PeerIdentity *put_path,
-                         struct GNUNET_TIME_Absolute expiration_time,
-                         const void *data, size_t data_size)
+static void
+core_init (void *cls,
+           const struct GNUNET_PeerIdentity *identity)
 {
-  struct PeerPutMessage *ppm;
-  struct P2PPendingMessage *pending;
-  struct FriendInfo *target_friend;
-  struct GNUNET_PeerIdentity *pp;
-  size_t msize;
-
-  msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
-          sizeof (struct PeerPutMessage);
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
-  {
-    put_path_length = 0;
-    msize = data_size + sizeof (struct PeerPutMessage);
-  }
-
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
-  {
-    DEBUG("msize = %lu\n",msize);
-    GNUNET_break (0);
-    return;
-  }
-
-  GNUNET_assert (NULL !=
-                 (target_friend =
-                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->timeout = expiration_time;
-  ppm = (struct PeerPutMessage *) &pending[1];
-  pending->msg = &ppm->header;
-  ppm->header.size = htons (msize);
-  ppm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_PUT);
-  ppm->options = htonl (options);
-  ppm->block_type = htonl (block_type);
-  ppm->hop_count = htonl (hop_count + 1);
-  ppm->desired_replication_level = htonl (desired_replication_level);
-  ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
-  ppm->best_known_destination = best_known_dest;
-  ppm->intermediate_trail_id = intermediate_trail_id;
-  ppm->key = *key;
-  pp = (struct GNUNET_PeerIdentity *) &ppm[1];
-  ppm->put_path_length = htonl (put_path_length);
-  if(put_path_length > 0)
-  {
-    memcpy (pp, put_path,
-            sizeof (struct GNUNET_PeerIdentity) * put_path_length);
-  }
-  memcpy (&pp[put_path_length], data, data_size);
-  GNUNET_assert (NULL != target_friend);
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
+  my_identity = *identity;
 }
 
 
 /**
- * Handle the put request from the client.
- * @param key Key for the content
- * @param block_type Type of the block
- * @param options Routing options
- * @param desired_replication_level Desired replication count
- * @param expiration_time When does the content expire
- * @param data Content to store
- * @param data_size Size of content @a data in bytes
+ * Handle a `struct RandomWalkMessage` from a
+ * #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK message.
+ *
+ * @param cls closure (NULL)
+ * @param peer sender identity
+ * @param message the setup message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-void
-GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
-                           enum GNUNET_BLOCK_Type block_type,
-                           enum GNUNET_DHT_RouteOption options,
-                           uint32_t desired_replication_level,
-                           struct GNUNET_TIME_Absolute expiration_time,
-                           const void *data, size_t data_size)
+static int
+handle_dht_p2p_random_walk (void *cls,
+                            const struct GNUNET_PeerIdentity *peer,
+                            const struct GNUNET_MessageHeader *message)
 {
-  struct GNUNET_PeerIdentity best_known_dest;
-  struct GNUNET_HashCode intermediate_trail_id;
-  struct GNUNET_PeerIdentity next_hop;
-  uint64_t key_value;
-  struct Closest_Peer successor;
-
-  memcpy (&key_value, key, sizeof (uint64_t));
-  key_value = GNUNET_ntohll (key_value);
-  successor = find_local_best_known_next_hop (key_value,
-                                              GDS_FINGER_TYPE_NON_PREDECESSOR);
-  best_known_dest = successor.best_known_destination;
-  next_hop = successor.next_hop;
-  intermediate_trail_id = successor.trail_id;
-
-  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity))
+  const struct RandomWalkMessage *m;
+  struct Trail *t;
+  struct FriendInfo *pred;
+
+  m = (const struct RandomWalkMessage *) message;
+  pred = GNUNET_CONTAINER_multipeermap_get (friends_peermap,
+                                            peer);
+  t = GNUNET_new (struct Trail);
+  t->pred_id = m->trail_id;
+  t->pred = pred;
+  if (GNUNET_OK !=
+      GNUNET_CONTAINER_multihashmap_put (trail_map,
+                                         &t->pred_id,
+                                         t,
+                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
   {
-    DEBUG("\n PUT_REQUEST_SUCCESSFUL for key = %s",GNUNET_h2s(key));
-    /* I am the destination. */
-    GDS_DATACACHE_handle_put (expiration_time, key, 0, NULL,
-                              block_type,data_size,data);
-    GDS_CLIENTS_process_put (options, block_type, 0,
-                             ntohl (desired_replication_level),
-                             1, &my_identity, expiration_time, //FIXME: GNUNETnthoh something on expiration time.
-                             key, data, data_size);
-    return;
+    GNUNET_break_op (0);
+    GNUNET_free (t);
+    return GNUNET_SYSERR;
   }
-  /* In case we are sending the request to  a finger, then send across all of its
-   trail.*/
-#if ENABLE_MALICIOUS
-  if (0 != GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
-                                            &successor.next_hop))
+  GNUNET_CONTAINER_MDLL_insert (pred,
+                                pred->pred_head,
+                                pred->pred_tail,
+                                t);
+  t->expiration_time = GNUNET_TIME_relative_to_absolute (TRAIL_TIMEOUT);
+  t->hn = GNUNET_CONTAINER_heap_insert (trail_heap,
+                                        t,
+                                        t->expiration_time.abs_value_us);
+  if (NULL == trail_timeout_task)
+    trail_timeout_task = GNUNET_SCHEDULER_add_delayed (TRAIL_TIMEOUT,
+                                                       &trail_timeout_callback,
+                                                       NULL);
+
+  if (ntohl (m->hops_taken) > GDS_NSE_get ())
   {
-    struct FingerInfo *next_hop_finger;
-    unsigned int i;
-
-    next_hop_finger = &finger_table[successor.finger_table_index];
-    for (i = 0; i < next_hop_finger->trails_count; i++)
+    /* 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
     {
-      if (GNUNET_YES == next_hop_finger->trail_list[i].is_present)
+      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)
+      {
+        GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
+                                          &rwrm->location);
+      }
+      else
       {
-        if(0 == next_hop_finger->trail_list[i].trail_length)
-        {
-           GDS_NEIGHBOURS_send_put (key, block_type, options, desired_replication_level,
-                                    best_known_dest, intermediate_trail_id, &next_hop,
-                                    0, 1, &my_identity, expiration_time,
-                                    data, data_size);
-           return;
-        }
-        next_hop = next_hop_finger->trail_list[i].trail_head->peer;
-        GDS_NEIGHBOURS_send_put (key, block_type, options, desired_replication_level,
-                                 best_known_dest,
-                                 next_hop_finger->trail_list[i].trail_id,
-                                 &next_hop, 0, 1, &my_identity,
-                                 expiration_time,
-                                 data, data_size);
-       }
+        struct Finger *f;
+
+        f = ft->fingers[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
+                                                  ft->number_valid_fingers)];
+        rwrm->location = f->destination;
+      }
     }
-    return;
+    GNUNET_MQ_send (pred->mq,
+                    env);
   }
-#endif
- GDS_NEIGHBOURS_send_put (key, block_type, options, desired_replication_level,
-                          best_known_dest, intermediate_trail_id, &next_hop,
-                          0, 1, &my_identity, expiration_time,
-                          data, data_size);
+  else
+  {
+    struct GNUNET_MQ_Envelope *env;
+    struct RandomWalkMessage *rwm;
+    struct FriendInfo *succ;
+
+    /* extend the trail by another random hop */
+    succ = pick_random_friend ();
+    GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
+                                      &t->succ_id);
+    t->succ = succ;
+    if (GNUNET_OK !=
+        GNUNET_CONTAINER_multihashmap_put (trail_map,
+                                           &t->succ_id,
+                                           t,
+                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+    {
+      GNUNET_break (0);
+      GNUNET_CONTAINER_MDLL_remove (pred,
+                                    pred->pred_head,
+                                    pred->pred_tail,
+                                    t);
+      GNUNET_free (t);
+      return GNUNET_OK;
+    }
+    GNUNET_CONTAINER_MDLL_insert (succ,
+                                  succ->succ_head,
+                                  succ->succ_tail,
+                                  t);
+    env = GNUNET_MQ_msg (rwm,
+                         GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK);
+    rwm->hops_taken = htons (1 + ntohs (m->hops_taken));
+    rwm->layer = m->layer;
+    rwm->trail_id = t->succ_id;
+    GNUNET_MQ_send (succ->mq,
+                    env);
+  }
+  return GNUNET_OK;
 }
 
+
 /**
- * Construct a Get message and send it to target_peer.
- * @param key Key for the content
- * @param block_type Type of the block
- * @param options Routing options
- * @param desired_replication_level Desired replication count
- * @param best_known_dest Peer which should get this message. Same as target peer
- *                        if best_known_dest is a friend else its a finger.
- * @param intermediate_trail_id  Trail id to reach to @a best_known_dest
- *                              in case it is a finger else set to 0.
- * @param target_peer Peer to which this message will be forwarded.
- * @param hop_count Number of hops traversed so far.
- * @param data Content to store
- * @param data_size Size of content @a data in bytes
- * @param get_path_length Total number of peers in @a get_path
- * @param get_path Number of peers traversed so far
+ * Handle a `struct RandomWalkResponseMessage` from a GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE
+ * message.
+ *
+ * @param cls closure (NULL)
+ * @param peer sender identity
+ * @param message the setup response message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-void
-GDS_NEIGHBOURS_send_get (const struct GNUNET_HashCode *key,
-                         enum GNUNET_BLOCK_Type block_type,
-                         enum GNUNET_DHT_RouteOption options,
-                         uint32_t desired_replication_level,
-                         struct GNUNET_PeerIdentity best_known_dest,
-                         struct GNUNET_HashCode intermediate_trail_id,
-                         struct GNUNET_PeerIdentity *target_peer,
-                         uint32_t hop_count,
-                         uint32_t get_path_length,
-                         struct GNUNET_PeerIdentity *get_path)
+static int
+handle_dht_p2p_random_walk_response (void *cls,
+                                     const struct GNUNET_PeerIdentity *peer,
+                                     const struct GNUNET_MessageHeader *message)
 {
-  struct PeerGetMessage *pgm;
-  struct P2PPendingMessage *pending;
-  struct FriendInfo *target_friend;
-  struct GNUNET_PeerIdentity *gp;
-  size_t msize;
-
-  msize = sizeof (struct PeerGetMessage) +
-          (get_path_length * sizeof (struct GNUNET_PeerIdentity));
+  const struct RandomWalkResponseMessage *rwrm;
+
+  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'
+  /*
+   * Steps :
+   *  1 check if we are the correct layer
+   *  1.a if true : add the returned value (finger) in the db structure
+   *  1.b if true : do nothing
+   */
+  /* FIXME: add the value in db structure 1.a */
 
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  GNUNET_assert (NULL !=
-                 (target_friend =
-                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, target_peer)));
-
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  pending->importance = 0;    /* FIXME */
-  pgm = (struct PeerGetMessage *) &pending[1];
-  pending->msg = &pgm->header;
-  pgm->header.size = htons (msize);
-  pgm->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET);
-  pgm->get_path_length = htonl (get_path_length);
-  pgm->best_known_destination = best_known_dest;
-  pgm->key = *key;
-  pgm->intermediate_trail_id = intermediate_trail_id;
-  pgm->hop_count = htonl (hop_count + 1);
-  pgm->get_path_length = htonl (get_path_length);
-  gp = (struct GNUNET_PeerIdentity *) &pgm[1];
-  memcpy (gp, get_path,
-          sizeof (struct GNUNET_PeerIdentity) * get_path_length);
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
+  return GNUNET_OK;
 }
 
 
 /**
- * Handle the get request from the client file. If I am destination do
- * datacache put and return. Else find the target friend and forward message
- * to it.
- * @param key Key for the content
- * @param block_type Type of the block
- * @param options Routing options
- * @param desired_replication_level Desired replication count
+ * Handle a `struct TrailDestroyMessage`.
+ *
+ * @param cls closure (NULL)
+ * @param peer sender identity
+ * @param message the finger destroy message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-void
-GDS_NEIGHBOURS_handle_get(const struct GNUNET_HashCode *key,
-                          enum GNUNET_BLOCK_Type block_type,
-                          enum GNUNET_DHT_RouteOption options,
-                          uint32_t desired_replication_level)
+static int
+handle_dht_p2p_trail_destroy (void *cls,
+                             const struct GNUNET_PeerIdentity *peer,
+                             const struct GNUNET_MessageHeader *message)
 {
-  struct Closest_Peer successor;
-  struct GNUNET_PeerIdentity best_known_dest;
-  struct GNUNET_HashCode intermediate_trail_id;
-  uint64_t key_value;
-
-  memcpy (&key_value, key, sizeof (uint64_t));
-  key_value = GNUNET_ntohll (key_value);
-
-  successor = find_local_best_known_next_hop (key_value,
-                                              GDS_FINGER_TYPE_NON_PREDECESSOR);
-
-  best_known_dest = successor.best_known_destination;
-  intermediate_trail_id = successor.trail_id;
-
-  /* I am the destination. I have the data. */
-  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
-                                            &best_known_dest))
-  {
-    GDS_DATACACHE_handle_get (key,block_type, NULL, 0,
-                              NULL, 0, 1, &my_identity, NULL,&my_identity);
-    return;
-  }
+  const struct TrailDestroyMessage *tdm;
+  struct Trail *trail;
+
+  tdm = (const struct TrailDestroyMessage *) message;
+  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;
+}
 
-#if ENABLE_MALICIOUS
-  struct GNUNET_PeerIdentity next_hop;
-  if (0 != GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
-                                            &successor.next_hop))
-  {
-    struct FingerInfo *next_hop_finger;
-    unsigned int i;
 
-    next_hop_finger = &finger_table[successor.finger_table_index];
-    for (i = 0; i < next_hop_finger->trails_count; i++)
-    {
-      if (GNUNET_YES == next_hop_finger->trail_list[i].is_present)
-      {
-        if(0 == next_hop_finger->trail_list[i].trail_length)
-        {
-           GDS_NEIGHBOURS_send_get (key, block_type, options,
-                                    desired_replication_level,
-                                    best_known_dest,intermediate_trail_id,
-                                    &successor.next_hop,
-                                    0, 1, &my_identity);
-           return;
-        }
-        next_hop = next_hop_finger->trail_list[i].trail_head->peer;
-        GDS_NEIGHBOURS_send_get (key, block_type, options, desired_replication_level,
-                                 best_known_dest,
-                                 next_hop_finger->trail_list[i].trail_id,
-                                 &next_hop, 0, 1, &my_identity);
-       }
-    }
-    return;
-  }
+/**
+ * Handle a `struct FindSuccessorMessage` from a #GNUNET_MESSAGE_TYPE_WDHT_SUCCESSOR_FIND
+ * message.
+ *
+ * @param cls closure (NULL)
+ * @param trail_id path to the originator
+ * @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_MessageHeader *message)
+{
+  const struct FindSuccessorMessage *fsm;
+
+  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
-  GDS_NEIGHBOURS_send_get (key, block_type, options, desired_replication_level,
-                           best_known_dest,intermediate_trail_id, &successor.next_hop,
-                           0, 1, &my_identity);
+  return GNUNET_OK;
 }
 
 
 /**
- * Send the get result to requesting client.
+ * Handle a `struct PeerGetMessage`.
  *
- * @param key Key of the requested data.
- * @param type Block type
- * @param target_peer Next peer to forward the message to.
- * @param source_peer Peer which has the data for the key.
- * @param put_path_length Number of peers in @a put_path
- * @param put_path Path taken to put the data at its stored location.
- * @param get_path_length Number of peers in @a get_path
- * @param get_path Path taken to reach to the location of the key.
- * @param expiration When will this result expire?
- * @param data Payload to store
- * @param data_size Size of the @a data
+ * @param cls closure (NULL)
+ * @param trail_id path to the originator
+ * @param message the peer get message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-void
-GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
-                                enum GNUNET_BLOCK_Type type,
-                                const struct GNUNET_PeerIdentity *target_peer,
-                                const struct GNUNET_PeerIdentity *source_peer,
-                                unsigned int put_path_length,
-                                const struct GNUNET_PeerIdentity *put_path,
-                                unsigned int get_path_length,
-                                const struct GNUNET_PeerIdentity *get_path,
-                                struct GNUNET_TIME_Absolute expiration,
-                                const void *data, size_t data_size)
+static int
+handle_dht_p2p_peer_get (void *cls,
+                         const struct GNUNET_HashCode *trail_id,
+                         const struct GNUNET_MessageHeader *message)
 {
-  struct PeerGetResultMessage *get_result;
-  struct GNUNET_PeerIdentity *paths;
-  struct P2PPendingMessage *pending;
-  struct FriendInfo *target_friend;
-  int current_path_index;
-  size_t msize;
-
-  msize = (put_path_length + get_path_length )* sizeof (struct GNUNET_PeerIdentity) +
-          data_size +
-          sizeof (struct PeerGetResultMessage);
-
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
-  {
-    put_path_length = 0;
-    msize = msize - put_path_length * sizeof (struct GNUNET_PeerIdentity);
-  }
-
-  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
-  {
-    GNUNET_break(0);
-    return;
-  }
-  current_path_index = 0;
-  if(get_path_length > 0)
-  {
-    current_path_index = search_my_index(get_path, get_path_length);
-    if (-1 == current_path_index)
-    {
-      GNUNET_break (0);
-      return;
-    }
-    if ((get_path_length + 1) == current_path_index)
-    {
-      DEBUG ("Peer found twice in get path. Not allowed \n");
-      GNUNET_break (0);
-      return;
-    }
-  }
-  if (0 == current_path_index)
-  {
-    DEBUG ("GET_RESULT TO CLIENT KEY = %s, Peer = %s",GNUNET_h2s(key),GNUNET_i2s(&my_identity));
-    GDS_CLIENTS_handle_reply (expiration, key, get_path_length,
-                              get_path, put_path_length,
-                              put_path, type, data_size, data);
-    return;
-  }
+  const struct PeerGetMessage *pgm;
+
+  // FIXME: note: never called like this, message embedded with trail route!
+  pgm = (const struct PeerGetMessage *) message;
+  // -> lookup in datacache (figure out way to remember trail!)
+     /*
+    * steps :
+    *   1 extract the result
+    *   2 save the peer
+    *   3 send it using the good trail
+    *
+    * What do i do when i don't have the key/value?
+    */
 
-  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
-  pending->timeout = GNUNET_TIME_relative_to_absolute (PENDING_MESSAGE_TIMEOUT);
-  pending->importance = 0;
-  get_result = (struct PeerGetResultMessage *)&pending[1];
-  pending->msg = &get_result->header;
-  get_result->header.size = htons (msize);
-  get_result->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT);
-  get_result->key = *key;
-  get_result->querying_peer = *source_peer;
-  get_result->expiration_time = expiration;
-  get_result->get_path_length = htonl (get_path_length);
-  get_result->put_path_length = htonl (put_path_length);
-  paths = (struct GNUNET_PeerIdentity *)&get_result[1];
-  memcpy (paths, put_path,
-          put_path_length * sizeof (struct GNUNET_PeerIdentity));
-  memcpy (&paths[put_path_length], get_path,
-          get_path_length * sizeof (struct GNUNET_PeerIdentity));
-  memcpy (&paths[put_path_length + get_path_length], data, data_size);
-
-  GNUNET_assert (NULL !=
-                (target_friend =
-                 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
-                                                    &get_path[current_path_index - 1])));
-  GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, pending);
-  target_friend->pending_count++;
-  process_friend_queue (target_friend);
+  return GNUNET_OK;
 }
 
 
 /**
- * Method called whenever a peer disconnects.
+ * Handle a `struct PeerGetResultMessage`.
  *
- * @param cls closure
- * @param peer peer identity this notification is about
+ * @param cls closure (NULL)
+ * @param trail_id path to the originator
+ * @param message the peer get result message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-static void
-handle_core_disconnect (void *cls,
-                        const struct GNUNET_PeerIdentity *peer)
+static int
+handle_dht_p2p_peer_get_result (void *cls,
+                                const struct GNUNET_HashCode *trail_id,
+                                const struct GNUNET_MessageHeader *message)
 {
-  struct FriendInfo *remove_friend;
-
-  /* If disconnected to own identity, then return. */
-  if (0 == memcmp (&my_identity,
-                   peer,
-                   sizeof (struct GNUNET_PeerIdentity)))
-    return;
-
-  if (NULL == (remove_friend =
-               GNUNET_CONTAINER_multipeermap_get (friend_peermap,
-                                                  peer)))
-  {
-    GNUNET_break (0);
-    return;
-  }
-
-  GNUNET_assert (GNUNET_YES ==
-                 GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
-                                                       peer,
-                                                       remove_friend));
-  /* FIXME: do stuff */
+  const struct PeerGetResultMessage *pgrm;
+
+  pgrm = (const struct PeerGetResultMessage *) message;
+  // pretty much: parse, & pass to client (there is some call for that...)
+
+#if 0
+  GDS_CLIENTS_process_get (options,
+                           type,
+                           0, 0,
+                           path_length, path,
+                           key);
+  (void) GDS_DATACACHE_handle_get (trail_id,
+                                   key,
+                                   type,
+                                   xquery,
+                                   xquery_size,
+                                   &reply_bf,
+                                   reply_bf_mutator);
+#endif
+  return GNUNET_OK;
 }
 
 
 /**
- * Method called whenever a peer connects.
+ * Handle a `struct PeerPutMessage`.
  *
- * @param cls closure
- * @param peer_identity peer identity this notification is about
+ * @param cls closure (NULL)
+ * @param trail_id path to the originator
+ * @param message the peer put message
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-static void
-handle_core_connect (void *cls,
-                     const struct GNUNET_PeerIdentity *peer_identity)
+static int
+handle_dht_p2p_peer_put (void *cls,
+                         const struct GNUNET_HashCode *trail_id,
+                         const struct GNUNET_MessageHeader *message)
 {
-  struct FriendInfo *friend;
+  const struct PeerGetResultMessage *pgrm;
+
+  pgrm = (const struct PeerGetResultMessage *) message;
+  // parse & store in datacache, this is in response to us asking for successors.
+  /*
+   * steps :
+   * 1 check the size of the message
+   * 2 use the API to add the value in the "database". Check on the xdht file, how to do it.
+   * 3 Did i a have to return a notification or did i have to return GNUNET_[OK|SYSERR]?
+   */
+#if 0
+  GDS_DATACACHE_handle_put (expiration_time,
+                            key,
+                            path_length, path,
+                            block_type,
+                            data_size,
+                            data);
+  GDS_CLIENTS_process_put (options,
+                           block_type,
+                           0, 0,
+                           path_length, path,
+                           expiration_time,
+                           key,
+                           data,
+                           data_size);
+#endif
+  return GNUNET_OK;
+}
 
-  /* Check for connect to self message */
-  if (0 == memcmp (&my_identity,
-                   peer_identity,
-                   sizeof (struct GNUNET_PeerIdentity)))
-    return;
 
-  /* If peer already exists in our friend_peermap, then exit. */
-  if (GNUNET_YES ==
-      GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
-                                              peer_identity))
-  {
-    GNUNET_break (0);
-    return;
-  }
 
-  friend = GNUNET_new (struct FriendInfo);
-  friend->id = *peer_identity;
 
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONTAINER_multipeermap_put (friend_peermap,
-                                                    peer_identity,
-                                                    friend,
-                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-  /* do work? */
-}
+/**
+ * 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_MessageHeader *message);
 
 
 /**
- * To be called on core init/fail.
- *
- * @param cls service closure
- * @param identity the public identity of this peer
+ * Definition of a handler for a message received along some trail.
  */
-static void
-core_init (void *cls,
-           const struct GNUNET_PeerIdentity *identity)
+struct TrailHandler
 {
-  my_identity = *identity;
-}
+  /**
+   * 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 FingerSetupMessage`.
+ * Handle a `struct TrailRouteMessage`.
  *
  * @param cls closure (NULL)
  * @param peer sender identity
- * @param message the setup message
+ * @param message the finger destroy message
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
-handle_dht_p2p_finger_setup (void *cls,
-                             const struct GNUNET_PeerIdentity *peer,
-                             const struct GNUNET_MessageHeader *message)
+handle_dht_p2p_trail_route (void *cls,
+                            const struct GNUNET_PeerIdentity *peer,
+                            const struct GNUNET_MessageHeader *message)
 {
-  const struct FingerSetupMessage *fsm;
+  static const struct TrailHandler handlers[] = {
+    { &handle_dht_p2p_successor_find, NULL,
+      GNUNET_MESSAGE_TYPE_WDHT_SUCCESSOR_FIND,
+      sizeof (struct FindSuccessorMessage) },
+    { 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;
+  }
 
-  fsm = (const struct FingerSetupMessage *) message;
+  /* 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,
+                      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
@@ -1413,16 +1571,21 @@ int
 GDS_NEIGHBOURS_init (void)
 {
   static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
-    { &handle_dht_p2p_finger_setup,
-      GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP,
-      sizeof (struct FingerSetupMessage) },
+    { &handle_dht_p2p_random_walk,
+      GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK,
+      sizeof (struct RandomWalkMessage) },
+    { &handle_dht_p2p_random_walk_response,
+      GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE,
+      sizeof (struct RandomWalkResponseMessage) },
+    { &handle_dht_p2p_trail_destroy,
+      GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY,
+      sizeof (struct TrailDestroyMessage) },
+    { &handle_dht_p2p_trail_route,
+      GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE,
+      0},
     {NULL, 0, 0}
   };
 
-#if ENABLE_MALICIOUS
-  act_malicious = 0;
-#endif
-
   core_api =
     GNUNET_CORE_connect (GDS_cfg, NULL,
                          &core_init,
@@ -1434,9 +1597,9 @@ GDS_NEIGHBOURS_init (void)
 
   if (NULL == core_api)
     return GNUNET_SYSERR;
-
-  //TODO: check size of this peer map?
-  friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
+  friends_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
+  trail_map = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
+  trail_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
   return GNUNET_OK;
 }
 
@@ -1451,10 +1614,19 @@ GDS_NEIGHBOURS_done (void)
     return;
   GNUNET_CORE_disconnect (core_api);
   core_api = NULL;
-
-  GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
-  GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
-  friend_peermap = NULL;
+  GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friends_peermap));
+  GNUNET_CONTAINER_multipeermap_destroy (friends_peermap);
+  friends_peermap = NULL;
+  GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (trail_map));
+  GNUNET_CONTAINER_multihashmap_destroy (trail_map);
+  trail_map = NULL;
+  GNUNET_CONTAINER_heap_destroy (trail_heap);
+  trail_heap = NULL;
+  if (NULL != trail_timeout_task)
+  {
+    GNUNET_SCHEDULER_cancel (trail_timeout_task);
+    trail_timeout_task = NULL;
+  }
 }