gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r33446 - gnunet/src/dht


From: gnunet
Subject: [GNUnet-SVN] r33446 - gnunet/src/dht
Date: Fri, 30 May 2014 17:43:38 +0200

Author: bartpolot
Date: 2014-05-30 17:43:38 +0200 (Fri, 30 May 2014)
New Revision: 33446

Modified:
   gnunet/src/dht/gnunet-service-xdht_neighbours.c
   gnunet/src/dht/gnunet-service-xdht_routing.c
Log:
- code review notes

Modified: gnunet/src/dht/gnunet-service-xdht_neighbours.c
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_neighbours.c     2014-05-30 10:51:55 UTC 
(rev 33445)
+++ gnunet/src/dht/gnunet-service-xdht_neighbours.c     2014-05-30 15:43:38 UTC 
(rev 33446)
@@ -75,17 +75,17 @@
 #define MAXIMUM_TRAILS_PER_FINGER 2
 
 /**
- * Used to distinguish put/get request use of find_successor() from others 
+ * Used to distinguish put/get request use of find_successor() from others
  */
 #define PUT_GET_REQUEST 65
 
 /**
- * Maximum number of trails that can go through a friend. 
+ * Maximum number of trails that can go through a friend.
  */
 #define TRAIL_THROUGH_FRIEND_THRESHOLD 64
 
 /**
- * Finger map index for predecessor entry in finger peermap. 
+ * Finger map index for predecessor entry in finger peermap.
  */
 #define PREDECESSOR_FINGER_ID 64
 
@@ -105,43 +105,47 @@
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP
    */
   struct GNUNET_MessageHeader header;
-  
+
+  /* Bitmask of options, 1 = IS_PREDECESSOR */
+  // int32_t options;
+
   /**
-   * Peer closest to this value will be our finger. 
+   * Peer closest to this value will be our finger.
    */
   uint64_t ultimate_destination_finger;
 
   /**
-   * Source peer which wants to setup the trail to one of its finger. 
+   * Source peer which wants to setup the trail to one of its finger.
    */
   struct GNUNET_PeerIdentity source_peer;
-  
+
   /**
    * Peer to which this packet is forwarded.
    */
-  struct GNUNET_PeerIdentity next_destination;
-  
+  struct GNUNET_PeerIdentity next_destination; // rename "best_known_dest"
+
   /**
-   * Index into finger peer map, in Network Byte Order. 
+   * Index into finger peer map, in Network Byte Order.
    */
-  uint32_t finger_map_index;
-  
+  uint32_t finger_map_index; // remove this, include 
ultimate_destination_finger in response, calculate index from it
+
   /**
-   * Number of entries in trail list, in Network Byte Order.
+   * Number of entries in trail_list, in Network Byte Order.
    */
-  uint32_t trail_length GNUNET_PACKED;
-  
+  uint32_t trail_length GNUNET_PACKED; // remove this, calculte length from 
message size
+
   /**
    * Trail id of any intermediate trail we may encounter while doing trail 
setup.
    */
   struct GNUNET_HashCode intermediate_trail_id;
-  
+
   /**
-   * Trail id for trail which we are trying to setup. 
+   * Trail id for trail which we are trying to setup.
    */
-  struct GNUNET_HashCode new_trail_id;
-  
+  struct GNUNET_HashCode new_trail_id; // rename to "trail_id"
+
   /* Trail formed in the process. */
+  /* GNUNET_PeerIdentity trail_list[] */
 };
 
 /**
@@ -149,42 +153,43 @@
  */
 struct PeerTrailSetupResultMessage
 {
-  
+
   /**
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
-   * Finger to which we have found the path. 
+   * Finger to which we have found the path.
    */
   struct GNUNET_PeerIdentity finger_identity;
 
   /**
-   * Peer which was looking for the trail to finger. 
+   * Peer which was looking for the trail to finger.
    */
-  struct GNUNET_PeerIdentity destination_peer;
-  
+  struct GNUNET_PeerIdentity destination_peer; // querying_peer
+
   /**
    * Index into finger peer map in NBO.
    */
-  uint32_t finger_map_index;
-  
+  uint32_t finger_map_index; // flag/option with IS_PREDECESSOR
+
   /**
    * Number of entries in trail list in NBO.
    */
-  uint32_t trail_length GNUNET_PACKED;
-  
+  uint32_t trail_length GNUNET_PACKED; // remove, calculate
+
   /**
-   * Identifier of the trail. 
+   * Identifier of the trail.
    */
   struct GNUNET_HashCode trail_id;
-  
-  /* Trail from destination_peer to finger_identity */
+
+  /* Trail from "destination_peer" to finger_identity, NOT including both */
+  /* struct GNUNET_PeerIdentity trail[] */
 };
 
 /**
- * P2P Verify Successor Message. 
+ * P2P Verify Successor Message.
  */
 struct PeerVerifySuccessorMessage
 {
@@ -192,27 +197,27 @@
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
-   * Source peer which wants to verify its successor. 
+   * Source peer which wants to verify its successor.
    */
   struct GNUNET_PeerIdentity source_peer;
-  
+
   /**
    * My current successor.
    */
   struct GNUNET_PeerIdentity successor;
-  
+
   /**
    * Identifier of trail to reach from source_peer to successor.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /**
    * Total number of peers to reach from source to successor.
    */
   unsigned int trail_length;
-  
+
   /* Trail. */
 };
 
@@ -225,44 +230,44 @@
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR_RESULT
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
-   * Destination peer which sent the request to verify its successor. 
+   * Destination peer which sent the request to verify its successor.
    */
   struct GNUNET_PeerIdentity destination_peer;
-  
+
   /**
    * Successor to which PeerVerifySuccessorMessage was sent.
    */
   struct GNUNET_PeerIdentity source_successor;
-  
+
   /**
    * source_successor's predecessor
    */
   struct GNUNET_PeerIdentity my_predecessor;
-  
+
   /**
-   * Trail identifier of trail from my_predecessor to source_successor. 
+   * Trail identifier of trail from my_predecessor to source_successor.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /**
-   * Direction in which we are looking at the trail. 
+   * Direction in which we are looking at the trail.
    */
   enum GDS_ROUTING_trail_direction trail_direction;
-  
+
   /**
    * Total number of peers in trail from source_successor to my_predecessor
-   * if my_predecessor is not same as destination_peer. 
+   * if my_predecessor is not same as destination_peer.
    */
-  uint32_t trail_length; 
-  
-  /* Trail from source_successor to my_predecessor where 
+  uint32_t trail_length;
+
+  /* Trail from source_successor to my_predecessor where
    * my_predecessor != destination_peer*/
 };
 
 /**
- * P2P Notify New Successor Message. 
+ * P2P Notify New Successor Message.
  */
 struct PeerNotifyNewSuccessorMessage
 {
@@ -270,32 +275,32 @@
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_NOTIFY_NEW_SUCCESSOR
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
-   * Source peer which wants to notify its new successor. 
+   * Source peer which wants to notify its new successor.
    */
   struct GNUNET_PeerIdentity source_peer;
-  
+
   /**
    * New successor identity.
    */
   struct GNUNET_PeerIdentity destination_peer;
-  
+
   /**
    * Total number of peers in trail from source_peer to destination_peer
    */
   unsigned int trail_length;
-  
+
   /**
-   * Unique identifier of the trail. 
+   * Unique identifier of the trail.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /* Trail. */
 };
 
 /**
- * P2P Trail Compression Message. 
+ * P2P Trail Compression Message.
  */
 struct PeerTrailCompressionMessage
 {
@@ -303,33 +308,33 @@
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_COMPRESSION
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
-   * Source peer of this trail.  
+   * Source peer of this trail.
    */
   struct GNUNET_PeerIdentity source_peer;
-  
+
   /**
-   * Destination of this trail. 
+   * Destination of this trail.
    */
   struct GNUNET_PeerIdentity destination_peer;
-  
+
   /**
-   * Trail from source_peer to destination_peer compressed such that 
-   * new_first_friend is the first hop in the trail from source to 
-   * destination. 
+   * Trail from source_peer to destination_peer compressed such that
+   * new_first_friend is the first hop in the trail from source to
+   * destination.
    */
   struct GNUNET_PeerIdentity new_first_friend;
-  
+
   /**
-   * Unique identifier of trail. 
+   * Unique identifier of trail.
    */
   struct GNUNET_HashCode trail_id;
 };
 
 
 /**
- * P2P Trail Tear Down message. 
+ * P2P Trail Tear Down message.
  */
 struct PeerTrailTearDownMessage
 {
@@ -337,24 +342,24 @@
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_TEARDOWN
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
    * Source peer of the trail.
    */
   struct GNUNET_PeerIdentity source_peer;
-  
+
   /**
    * Destination peer of the trail.
    */
   struct GNUNET_PeerIdentity destination_peer;
-  
+
   /**
    * Unique identifier of the trail.
    */
   struct GNUNET_HashCode TRAIL_ID;
-  
+
   /**
-   * Direction of trail. 
+   * Direction of trail.
    */
   enum GDS_ROUTING_trail_direction trail_direction;
 };
@@ -369,48 +374,48 @@
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_REJECTION
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
-   * Source peer which wants to set up the trail. 
+   * Source peer which wants to set up the trail.
    */
   struct GNUNET_PeerIdentity source_peer;
-  
+
   /**
-   * Peer which sent trail rejection message. 
+   * Peer which sent trail rejection message.
    */
   struct GNUNET_PeerIdentity congested_peer;
-  
+
   /**
    * Peer identity which will be successor to this value will be finger of
-   * source_peer. 
+   * source_peer.
    */
   uint64_t ultimate_destination_finger_identity_value;
-  
+
   /**
    * Index in finger peer map of source peer.
    */
   uint32_t finger_map_index;
-  
+
   /**
    * Total number of peers in the trail.
    */
   uint32_t trail_length;
-  
+
   /**
-   * Identifier for the trail source peer is trying to setup. 
+   * Identifier for the trail source peer is trying to setup.
    */
   struct GNUNET_HashCode trail_id;
   /**
-   * Relative time for which congested_peer will remain congested. 
+   * Relative time for which congested_peer will remain congested.
    */
   struct GNUNET_TIME_Relative congestion_time;
-  
+
   /* Trail_list from source_peer to peer which sent the message for trail setup
    * to congested peer.*/
 };
 
 /**
- * P2P Add Trail Message. 
+ * P2P Add Trail Message.
  */
 struct PeerAddTrailMessage
 {
@@ -418,32 +423,32 @@
    * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
-   * Source peer of the routing trail. 
+   * Source peer of the routing trail.
    */
   struct GNUNET_PeerIdentity source_peer;
-  
+
   /**
-   * Destination peer of the routing trail. 
+   * Destination peer of the routing trail.
    */
   struct GNUNET_PeerIdentity destination_peer;
-  
+
   /**
-   * Total number of peers from source peer to destination peer. 
+   * Total number of peers from source peer to destination peer.
    */
   unsigned int trail_length;
-  
+
   /**
-   * Unique identifier of the trail. 
+   * Unique identifier of the trail.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /* Trail from source peer to destination peer. */
 };
 
-GNUNET_NETWORK_STRUCT_END        
-        
+GNUNET_NETWORK_STRUCT_END
+
 /**
  * Linked list of messages to send to a particular other peer.
  */
@@ -463,12 +468,12 @@
    * Message importance level.  FIXME: used? useful?
    */
   unsigned int importance;
-  
+
   /**
    * When does this message time out?
    */
   struct GNUNET_TIME_Absolute timeout;
-  
+
   /**
    * Actual message to be sent, allocated at the end of the struct:
    * // msg = (cast) &pm[1];
@@ -478,32 +483,32 @@
 
 };
 
-        
-/** 
+
+/**
  *  Entry in friend_peermap.
  */
 struct FriendInfo
 {
   /**
-   * Friend Identity 
+   * Friend Identity
    */
   struct GNUNET_PeerIdentity id;
 
   /**
-   * Number of trails for which this friend is the first hop. 
+   * Number of trails for which this friend is the first hop.
    */
   unsigned int trails_count;
-  
+
   /**
    * Count of outstanding messages for this friend.
    */
   unsigned int pending_count;
-  
+
   /**
-   * In case not 0, then amount of time for which this friend is congested. 
+   * In case not 0, then amount of time for which this friend is congested.
    */
-  struct GNUNET_TIME_Absolute congestion_duration;
-  
+  struct GNUNET_TIME_Absolute congestion_timestamp;
+
   /**
    * Head of pending messages to be sent to this friend.
    */
@@ -513,7 +518,7 @@
    * Tail of pending messages to be sent to this friend.
    */
   struct P2PPendingMessage *tail;
- 
+
   /**
    * Core handle for sending messages to this friend.
    */
@@ -524,20 +529,20 @@
 /**
  * An individual trail to reach to a finger.
  */
-struct Trail
+struct Trail // "node" "element" "relay"
 {
   /**
     * Pointer to next item in the list
     */
   struct Trail *next;
-  
+
   /**
     * Pointer to prev item in the list
     */
   struct Trail *prev;
-  
+
   /**
-   * An element in this trail. 
+   * An element in this trail.
    */
   struct GNUNET_PeerIdentity peer;
 };
@@ -545,36 +550,37 @@
 /**
  * List of all trails to reach a particular finger.
  */
-struct TrailList
+struct TrailList // "trail": list of "elements"
 {
   /**
    * Head of trail.
    */
   struct Trail *trail_head;
-  
+
   /**
    * Tail of trail.
    */
   struct Trail *trail_tail;
-  
+
   /**
-   * Unique identifier of this trail. 
+   * Unique identifier of this trail.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /**
-   * Length of trail pointed 
+   * Length of trail pointed
    */
   unsigned int trail_length;
-  
+
   /**
-   * Number of trails that the first friend of this trail is a part of. 
+   * Number of trails that the first friend of this trail is a part of.
    */
-  unsigned int first_friend_trail_count;
+  unsigned int first_friend_trail_count; // remove this, duplicate variable!
+                                         // include a pointer to Friend or 
function that calculates it
 };
 
 /**
- * An entry in finger_hashmap. 
+ * An entry in finger_hashmap.
  */
 struct FingerInfo
 {
@@ -582,21 +588,21 @@
    * Finger identity.
    */
   struct GNUNET_PeerIdentity finger_identity;
-  
+
   /**
    * Index in finger peer map
    */
-  unsigned int finger_map_index;  
-  
+  uint32_t finger_map_index;
+
   /**
-   * Number of trails setup so far for this finger. 
+   * Number of trails setup so far for this finger.
    */
-  unsigned int trails_count;
-  
+  uint32_t trails_count;
+
   /**
-   * Array of trails. 
+   * Array of trails.
    */
-  struct TrailList trail_list[MAXIMUM_TRAILS_PER_FINGER];
+  struct TrailList trail_list[MAXIMUM_TRAILS_PER_FINGER]; // OK!
 };
 
 /**
@@ -608,26 +614,26 @@
    * 64 bit value of the peer
    */
   uint64_t value;
- 
+
   /**
-   * Trail id to reach to peer. 
+   * Trail id to reach to peer.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /**
    * First hop, NULL in case of friend and my_identity
    */
   struct GNUNET_PeerIdentity next_hop;
-  
+
   /**
    * Next destination. In case of friend and my_identity , it is same as 
next_hop
-   * In case of finger it is finger identity. 
+   * In case of finger it is finger identity.
    */
   struct GNUNET_PeerIdentity next_destination;
 };
 
 /**
- * Data structure to store the trail chosen to reach to finger. 
+ * Data structure to store the trail chosen to reach to finger.
  */
 struct Correct_Trail
 {
@@ -635,21 +641,21 @@
    * First friend in the trail to reach finger.
    */
   struct FriendInfo friend;
-  
+
   /**
-   * Identifier of this trail. 
+   * Identifier of this trail.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /**
-   * Total number of peers in this trail. 
+   * Total number of peers in this trail.
    */
   unsigned int trail_length;
 };
 
 /**
  * Task that sends FIND FINGER TRAIL requests. This task is started when we 
have
- * get our first friend. 
+ * get our first friend.
  */
 static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
 
@@ -674,7 +680,7 @@
 static struct GNUNET_CORE_Handle *core_api;
 
 /**
- * The current finger index that we have want to find trail to. We start the 
+ * The current finger index that we have want to find trail to. We start the
  * search with value = 0, i.e. successor peer and then go to 
PREDCESSOR_FINGER_ID
  * and decrement it. For any index 63 <= index < 0, if finger is same as 
successor,
  * we reset this index to 0.
@@ -699,13 +705,13 @@
   struct P2PPendingMessage *pending;
   size_t off;
   size_t msize;
-  
+
   peer->th = NULL;
   while ((NULL != (pending = peer->head)) &&
          (0 == GNUNET_TIME_absolute_get_remaining 
(pending->timeout).rel_value_us))
   {
     peer->pending_count--;
-    GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);  
+    GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
     GNUNET_free (pending);
   }
   if (NULL == pending)
@@ -749,7 +755,7 @@
                                            &core_transmit_notify, peer);
     GNUNET_break (NULL != peer->th);
   }
- 
+
   return off;
 }
 
@@ -764,17 +770,17 @@
 process_friend_queue (struct FriendInfo *peer)
 {
   struct P2PPendingMessage *pending;
-  
+
   if (NULL == (pending = peer->head))
     return;
   if (NULL != peer->th)
     return;
-  
+
   GNUNET_STATISTICS_update (GDS_stats,
                             gettext_noop
                             ("# Bytes of bandwidth requested from core"),
                             ntohs (pending->msg->size), GNUNET_NO);
-  
+
   peer->th =
       GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
                                          pending->importance,
@@ -789,18 +795,18 @@
 /**
  * Construct a trail setup message and forward it to target_friend
  * @param source_peer Source peer which wants to setup the trail
- * @param ultimate_destination_finger Peer identity closest to this value will 
+ * @param ultimate_destination_finger Peer identity closest to this value will
  *                                    be finger to @a source_peer
  * @param next_destination Peer which should get the packet. I can be same as
- *                         target_friend or different. 
- * @param target_friend Friend to which message is forwarded now. 
+ *                         target_friend or different.
+ * @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 finger_map_index Index in finger map for which we are looking for 
finger.
  * @param trail_id Unique identifier for the trail we are trying to setup.
  * @param intermediate_trail_id Trail id of any intermediate trail we may have 
to
  *                              traverse during trail setup. If not used then 
set to
- *                              0. 
+ *                              0.
  */
 void
 GDS_NEIGHBOURS_send_trail_setup (const struct GNUNET_PeerIdentity source_peer,
@@ -810,42 +816,45 @@
                                  unsigned int trail_length,
                                  const struct GNUNET_PeerIdentity 
*trail_peer_list,
                                  unsigned int finger_map_index,
-                                 struct GNUNET_HashCode new_trail_id, 
-                                 struct GNUNET_HashCode intermediate_trail_id)
+                                 struct GNUNET_HashCode new_trail_id,
+                                 struct GNUNET_HashCode *intermediate_trail_id)
 {
   struct P2PPendingMessage *pending;
   struct PeerTrailSetupMessage *tsm;
   struct GNUNET_PeerIdentity *peer_list;
   size_t msize;
-  
-  msize = sizeof (struct PeerTrailSetupMessage) + 
+
+  msize = sizeof (struct PeerTrailSetupMessage) +
           (trail_length * sizeof (struct GNUNET_PeerIdentity));
-  
+
   if (msize >= GNUNET_SERVER_MAX_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 = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
-  tsm = (struct PeerTrailSetupMessage *) &pending[1]; 
+  tsm = (struct PeerTrailSetupMessage *) &pending[1];
   pending->msg = &tsm->header;
   tsm->header.size = htons (msize);
   tsm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP);
   tsm->ultimate_destination_finger = GNUNET_htonll 
(ultimate_destination_finger);
   tsm->source_peer = source_peer;
   tsm->next_destination = next_destination;
-  tsm->trail_length = htonl (trail_length); 
+  tsm->trail_length = htonl (trail_length);
   tsm->finger_map_index = htonl (finger_map_index);
   tsm->new_trail_id = new_trail_id;
-  tsm->intermediate_trail_id = intermediate_trail_id;
+  if (NULL == intermediate_trail_id)
+    memset (tsm->intermediate_trail_id, 0, sizeof 
(tsm->intermediate_trail_id));
+  else
+    tsm->intermediate_trail_id = *intermediate_trail_id;
   if (trail_length > 0)
   {
     peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
@@ -865,8 +874,8 @@
  * @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 source to 
destination.
- * @param finger_map_index Index in finger peer map 
- * @param trail_id Unique identifier of the trail. 
+ * @param finger_map_index Index in finger peer map
+ * @param trail_id Unique identifier of the trail.
  */
 void
 GDS_NEIGHBOURS_send_trail_setup_result (struct GNUNET_PeerIdentity 
destination_peer,
@@ -881,26 +890,26 @@
   struct PeerTrailSetupResultMessage *tsrm;
   struct GNUNET_PeerIdentity *peer_list;
   size_t msize;
-  
-  msize = sizeof (struct PeerTrailSetupResultMessage) + 
+
+  msize = sizeof (struct PeerTrailSetupResultMessage) +
           (trail_length * sizeof (struct GNUNET_PeerIdentity));
-  
+
   if (msize >= GNUNET_SERVER_MAX_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;    
+
+  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
+  pending->importance = 0;
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
-  tsrm = (struct PeerTrailSetupResultMessage *) &pending[1]; 
+  tsrm = (struct PeerTrailSetupResultMessage *) &pending[1];
   pending->msg = &tsrm->header;
   tsrm->header.size = htons (msize);
   tsrm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT);
@@ -909,7 +918,7 @@
   tsrm->trail_length = htonl (trail_length);
   tsrm->finger_map_index = htonl (finger_map_index);
   tsrm->trail_id = trail_id;
-  
+
   peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
   if (trail_length > 0)
   {
@@ -925,16 +934,16 @@
 /**
  * Send trail rejection message to next_hop
  * @param source_peer Source peer which is trying to setup the trail.
- * @param finger_identity Peer closest to this value will be @a source_peer's 
finger 
+ * @param finger_identity 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 next_hop Peer to which we are forwarding this message. 
+ * @param next_hop Peer to which we are forwarding this message.
  * @param finger_map_index Index in finger peermap for which we are searching 
for finger.
  * @param trail_peer_list Trails seen so far in trail setup before getting 
rejected
  *                        by congested_peer
  * @param trail_length Total number of peers in trail_peer_list
  * @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.  
+ * @param congestion_timeout Duration given by congested peer as an estimate of
+ *                           how long it may remain congested.
  */
 void
 GDS_NEIGHBOURS_send_trail_rejection (struct GNUNET_PeerIdentity source_peer,
@@ -942,7 +951,7 @@
                                      struct GNUNET_PeerIdentity congested_peer,
                                      unsigned int finger_map_index,
                                      struct GNUNET_PeerIdentity 
*trail_peer_list,
-                                     unsigned int trail_length, 
+                                     unsigned int trail_length,
                                      struct GNUNET_HashCode trail_id,
                                      struct FriendInfo *target_friend,
                                      const struct GNUNET_TIME_Relative 
congestion_timeout)
@@ -951,24 +960,24 @@
   struct P2PPendingMessage *pending;
   struct GNUNET_PeerIdentity *peer_list;
   size_t msize;
-  
-  msize = sizeof (struct PeerTrailRejectionMessage) + 
+
+  msize = sizeof (struct PeerTrailRejectionMessage) +
           (trail_length * sizeof (struct GNUNET_PeerIdentity));
-  
+
   if (msize >= GNUNET_SERVER_MAX_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;    
+
+  pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
+  pending->importance = 0;
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   trm = (struct PeerTrailRejectionMessage *)&pending[1];
   pending->msg = &trm->header;
@@ -979,7 +988,7 @@
   trm->congestion_time = congestion_timeout;
   trm->finger_map_index = htonl (finger_map_index);
   trm->trail_id = trail_id;
-  
+
   peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
   if (trail_length > 0)
   {
@@ -993,13 +1002,13 @@
 
 
 /**
- * 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 Identifier of trail to reach successor. 
+ * 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 Identifier of trail to reach successor.
  * @param trail Trail to reach from source_peer to successor
  * @param trail_length Total number of peers in @a trail.
- * @param target_friend Message send to this friend. 
+ * @param target_friend Message send to this friend.
  */
 void
 GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity 
source_peer,
@@ -1013,21 +1022,21 @@
   struct P2PPendingMessage *pending;
   struct GNUNET_PeerIdentity *peer_list;
   size_t msize;
-  
+
   msize = sizeof (struct PeerVerifySuccessorMessage);
   if (msize >= GNUNET_SERVER_MAX_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 = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;    /* FIXME */
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   vsm = (struct PeerVerifySuccessorMessage *) &pending[1];
@@ -1038,13 +1047,13 @@
   vsm->successor = successor;
   vsm->trail_id = trail_id;
   vsm->trail_length = htonl (trail_length);
-  
+
   if (trail_length > 0)
   {
     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++;
@@ -1056,9 +1065,9 @@
  * Construct a trail teardown message and send it to target_friend
  * @param source_peer Source of the trail.
  * @param destination_peer Destination of the trail.
- * @param trail_id Unique identifier of the trail. 
- * @param trail_direction Direction of trail. 
- * @param target_friend Friend to get this message. 
+ * @param trail_id Unique identifier of the trail.
+ * @param trail_direction Direction of trail.
+ * @param target_friend Friend to get this message.
  */
 void
 GDS_NEIGHBOURS_send_trail_teardown (struct GNUNET_PeerIdentity source_peer,
@@ -1070,22 +1079,22 @@
   struct PeerTrailTearDownMessage *ttdm;
   struct P2PPendingMessage *pending;
   size_t msize;
-  
+
   msize = sizeof (struct PeerTrailTearDownMessage);
-  
+
   if (msize >= GNUNET_SERVER_MAX_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 = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;    /* FIXME */
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   ttdm = (struct PeerTrailTearDownMessage *) &pending[1];
@@ -1096,7 +1105,7 @@
   ttdm->destination_peer = destination_peer;
   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++;
@@ -1128,23 +1137,23 @@
   struct P2PPendingMessage *pending;
   struct GNUNET_PeerIdentity *peer_list;
   size_t msize;
-  
-  msize = sizeof (struct PeerVerifySuccessorResultMessage) + 
+
+  msize = sizeof (struct PeerVerifySuccessorResultMessage) +
           (trail_length * sizeof(struct GNUNET_PeerIdentity));
-  
+
   if (msize >= GNUNET_SERVER_MAX_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 = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;    /* FIXME */
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   vsmr = (struct PeerVerifySuccessorResultMessage *) &pending[1];
@@ -1155,13 +1164,13 @@
   vsmr->my_predecessor = succ_predecessor;
   vsmr->source_successor = source_successor;
   vsmr->trail_direction = htonl (trail_direction);
-  
+
   if (trail_length > 0)
   {
     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++;
@@ -1177,7 +1186,7 @@
  * @param new_successor_trail_length
  * @param new_succesor_trail_id
  */
-void 
+void
 GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity 
source_peer,
                                           struct GNUNET_PeerIdentity 
new_successor,
                                           struct GNUNET_PeerIdentity 
*new_successor_trail,
@@ -1189,23 +1198,23 @@
   struct P2PPendingMessage *pending;
   struct GNUNET_PeerIdentity *peer_list;
   size_t msize;
-  
-  msize = sizeof (struct PeerNotifyNewSuccessorMessage) + 
+
+  msize = sizeof (struct PeerNotifyNewSuccessorMessage) +
           (new_successor_trail_length * sizeof(struct GNUNET_PeerIdentity));
-  
+
   if (msize >= GNUNET_SERVER_MAX_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 = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;    /* FIXME */
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   nsm = (struct PeerNotifyNewSuccessorMessage *) &pending[1];
@@ -1219,10 +1228,10 @@
   if (new_successor_trail_length > 0)
   {
     peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
-    memcpy (peer_list, new_successor_trail, 
+    memcpy (peer_list, new_successor_trail,
             new_successor_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++;
@@ -1233,11 +1242,11 @@
 /**
  * Construct an add_trail message and send it to target_friend
  * @param source_peer Source of the trail.
- * @param destination_peer Destination of the trail. 
- * @param trail_id Unique identifer of the trail 
+ * @param destination_peer Destination of the trail.
+ * @param trail_id Unique identifer of the trail
  * @param trail Trail from @a source_peer to @a destination_peer
  * @param trail_length Total number of peers in @a trail.
- * @param target_friend Next peer to get this message. 
+ * @param target_friend Next peer to get this message.
  */
 void
 GDS_NEIGHBOURS_send_add_trail (struct GNUNET_PeerIdentity source_peer,
@@ -1251,23 +1260,23 @@
   struct GNUNET_PeerIdentity *peer_list;
   struct P2PPendingMessage *pending;
   size_t msize;
-  
-  msize = sizeof (struct PeerAddTrailMessage) + 
+
+  msize = sizeof (struct PeerAddTrailMessage) +
           (trail_length * sizeof(struct GNUNET_PeerIdentity));
-  
+
   if (msize >= GNUNET_SERVER_MAX_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 = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;    /* FIXME */
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   adm = (struct PeerAddTrailMessage *) &pending[1];
@@ -1278,28 +1287,29 @@
   adm->destination_peer = destination_peer;
   adm->trail_length = htonl (trail_length);
   adm->trail_id = trail_id;
-  
+
   if (trail_length > 0)
   {
     peer_list = (struct GNUNET_PeerIdentity *)&adm[1];
     memcpy (peer_list, trail, sizeof (struct GNUNET_PeerIdentity) * 
trail_length);
   }
-  
+
   /* 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);
-  
+
 }
 
 
 /**
+ * Merge into "teardown", add source and "last/new_first" in message struct.
  * Construct a trail compression message and send it to target_friend.
- * @param source_peer Source of the trail. 
- * @param destination_finger Destination of trail. 
+ * @param source_peer Source of the trail.
+ * @param destination_finger Destination of trail.
  * @param trail_id Unique identifier of trail.
  * @param first_friend First hop in compressed trail to reach from source to 
finger
- * @param target_friend Next friend to get this message. 
+ * @param target_friend Next friend to get this message.
  */
 void
 GDS_NEIGHBOURS_send_trail_compression (struct GNUNET_PeerIdentity source_peer,
@@ -1311,23 +1321,23 @@
   struct P2PPendingMessage *pending;
   struct PeerTrailCompressionMessage *tcm;
   size_t msize;
-  
+
   msize = sizeof (struct PeerTrailCompressionMessage);
-  
+
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
   {
     GNUNET_break (0);
     return;
   }
-  
+
   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
-  {  
-    GNUNET_STATISTICS_update (GDS_stats, 
+  {
+    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 = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;    /* FIXME */
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   tcm = (struct PeerTrailCompressionMessage *) &pending[1];
@@ -1338,33 +1348,33 @@
   tcm->new_first_friend = first_friend;
   tcm->trail_id = trail_id;
   tcm->destination_peer = destination_peer;
-  
+
   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, 
pending);
   target_friend->pending_count++;
   process_friend_queue (target_friend);
-  
+
 }
 
 
 /**
- * Seach my location in trail. 
+ * Seach my location in trail.
  * @param trail List of peers
  * @return my_index if found
- *         #GNUNET_SYSERR if no entry found. 
+ *         -1 if no entry found.
  */
 static int
 search_my_index (const struct GNUNET_PeerIdentity *trail,
                  int trail_length)
 {
   int i;
-  
+
   for (i = 0; i < trail_length; i++)
   {
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &trail[i]))
       return i;
   }
-  
-  return GNUNET_SYSERR;
+
+  return -1;
 }
 
 
@@ -1372,10 +1382,10 @@
  * Iterate over the list of all the trails to reach Finger. In case the first
  * friend to reach the finger has crossed the trail threshold or is congested,
  * then don't select it. In case there multiple available good trails to reach
- * to Finger, choose the one with shortest trail length. 
- * @param finger Finger 
+ * to Finger, choose the one with shortest trail length.
+ * @param finger Finger
  * @return struct Correct_Trail which contains the first friend , trail id
- * and trail length. NULL in case none of the trails are free. 
+ * and trail length. NULL in case none of the trails are free.
  */
 static struct Correct_Trail *
 select_trail_to_finger (struct FingerInfo *finger)
@@ -1384,27 +1394,27 @@
   struct TrailList *iterator;
   struct Correct_Trail *finger_trail;
   int i;
-  
-  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity, 
+
+  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
                                             &my_identity))
     return NULL;
-  
+
   finger_trail = GNUNET_new (struct Correct_Trail);
-  
+
   for (i = 0; i < finger->trails_count; i++)
   {
     iterator = &finger->trail_list[i];
     if (iterator->trail_length > 0)
     {
-      friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+      friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                   &iterator->trail_head->peer);
     }
     else
     {
-      friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+      friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                   &finger->finger_identity);
     }
-    
+
     if ((friend->trails_count < TRAIL_THROUGH_FRIEND_THRESHOLD)||
       ((0 == GNUNET_TIME_absolute_get_remaining 
(friend->congestion_duration).rel_value_us)))
     {
@@ -1415,7 +1425,7 @@
         finger_trail->trail_length = 0;
         return finger_trail;
       }
-      
+
       if (finger_trail->trail_length > iterator->trail_length)
       {
         finger_trail->friend = *friend;
@@ -1424,7 +1434,7 @@
       }
     }
   }
-  
+
   return finger_trail;
 }
 
@@ -1443,10 +1453,10 @@
 {
   uint64_t peer1_value;
   uint64_t peer2_value;
-  
+
   memcpy (&peer1_value, peer1, sizeof (uint64_t));
   memcpy (&peer2_value, peer2, sizeof (uint64_t));
-  
+
   if ((peer1_value <= value) && (value <= peer2_value))
     return peer2;
   else if ((peer2_value <= value) && (value <= peer1_value))
@@ -1476,10 +1486,10 @@
 {
   uint64_t peer1_value;
   uint64_t peer2_value;
-  
+
   memcpy (&peer1_value, peer1, sizeof (uint64_t));
   memcpy (&peer2_value, peer2, sizeof (uint64_t));
-  
+
   if ((peer1_value <= value) && (value <= peer2_value))
     return peer1;
   else if ((peer2_value <= value) && (value <= peer1_value))
@@ -1502,9 +1512,9 @@
  * @param peer2 Second peer
  * @param value Value relative to which we find the closest
  * @param finger_map_index Index in finger map. If equal to 
PREDECESSOR_FINGER_ID,
- *                         then we use different logic than other  
+ *                         then we use different logic than other
  *                         finger_map_index
- * @return Closest peer among two peers. 
+ * @return Closest peer among two peers.
  */
 static struct GNUNET_PeerIdentity *
 select_closest_peer (struct GNUNET_PeerIdentity *peer1,
@@ -1513,29 +1523,31 @@
                      unsigned int finger_map_index)
 {
   struct GNUNET_PeerIdentity *closest_peer;
-  
+
   if (PREDECESSOR_FINGER_ID == finger_map_index)
     closest_peer = select_closest_predecessor (peer1, peer2, value);
   else
     closest_peer = select_closest_finger (peer1, peer2, value);
-  
+
   return closest_peer;
 }
 
 
 /**
  * Find the successor for destination_finger_value among my_identity, all my
- * friend and all my fingers. Don't consider friends/ fingers with first 
friend in
- * the trail which are congested or have crossed the threshold. 
+ * friend and all my fingers. Don't consider friends or fingers
+ * which are congested or have crossed the threshold.
  * @param destination_finger_value Peer closest to this value will be the next 
successor.
- * @param next_destination [out] Updated to friend identity in case a friend 
is 
+ * @param next_destination [out] Updated to friend identity in case a friend is
  *                               successor, updated to first friend to reach 
to finger
- *                               in case finger is the destination. 
+ *                               in case finger is the destination.
  * @param new_intermediate_trail_id [out] In case we finger is the @a 
next_destination,
- *                                then we updated the field with trail id to 
reach 
- *                                to that finger. 
- * @param finger_map_index Index in finger peermap for which we are looking 
for a finger. 
- * @return 
+ *                                then we updated the field with trail id to 
reach
+ *                                to that finger.
+ * @param finger_map_index Index in finger peermap for which we are
+ *                         looking for a finger, to discern between
+ *                         IS_PREDECESSOR or not.
+ * @return
  */
 static struct GNUNET_PeerIdentity *
 find_successor (uint64_t destination_finger_value,
@@ -1552,25 +1564,25 @@
   struct FriendInfo *friend;
   struct FingerInfo *finger;
   int i;
-  
+
   successor = GNUNET_new (struct Closest_Peer);
   memcpy (&successor->value, &my_identity, sizeof (uint64_t));
   //successor->trail_id = 0;
   successor->next_hop = my_identity;
   successor->next_destination = my_identity;
-  
+
   friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
   for (i= 0; i < GNUNET_CONTAINER_multipeermap_size (friend_peermap); i++)
   {
-    GNUNET_assert (GNUNET_YES == 
+    GNUNET_assert (GNUNET_YES ==
       GNUNET_CONTAINER_multipeermap_iterator_next (friend_iter, NULL,
                                                   (const void **)&friend));
     if ((friend->trails_count > TRAIL_THROUGH_FRIEND_THRESHOLD)||
         (0 != GNUNET_TIME_absolute_get_remaining 
(friend->congestion_duration).rel_value_us))
       continue;
-    
+
     closest_peer = select_closest_peer (&my_identity, &friend->id,
-                                        destination_finger_value, 
+                                        destination_finger_value,
                                         finger_map_index);
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer, &friend->id))
     {
@@ -1578,43 +1590,43 @@
       //successor->trail_id = 0;
       successor->next_hop = friend->id;
       successor->next_destination = friend->id;
-    }  
+    }
   }
-  
+
   finger_iter = GNUNET_CONTAINER_multihashmap32_iterator_create 
(finger_hashmap);
   for (i = 0; i < GNUNET_CONTAINER_multihashmap32_size (finger_hashmap); i++)
   {
-    GNUNET_assert (GNUNET_YES == 
+    GNUNET_assert (GNUNET_YES ==
       GNUNET_CONTAINER_multihashmap32_iterator_next (finger_iter, NULL,
                                                      (void *)&finger));
    finger_trail = select_trail_to_finger (finger);
    if (NULL == finger_trail)
      continue;
-   
+
    closest_peer = select_closest_peer (&my_identity,
                                        &finger->finger_identity,
-                                       destination_finger_value, 
+                                       destination_finger_value,
                                        finger_map_index);
-   if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer, 
+   if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer,
                                              &finger->finger_identity))
    {
       memcpy (&successor->value, &finger->finger_identity, sizeof (uint64_t));
       successor->trail_id = finger_trail->trail_id;
       successor->next_hop = finger_trail->friend.id;
       successor->next_destination = finger->finger_identity;
-    }  
+    }
   }
-  
+
   next_destination = &successor->next_destination;
   new_intermediate_trail_id = &successor->trail_id;
   next_hop = &successor->next_hop;
-  
+
   return next_hop;
 }
 
-/**  
- * Construct a Put message and send it to target_peer. 
- * @param key Key for the content  
+/**
+ * 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
@@ -1623,7 +1635,7 @@
  * @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 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
@@ -1642,12 +1654,12 @@
                          struct GNUNET_TIME_Absolute expiration_time,
                          const void *data, size_t data_size)
 {
-  
+
 }
 
-/** 
- * Construct a Get message and send it to target_peer. 
- * @param key Key for the content  
+/**
+ * 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
@@ -1672,7 +1684,7 @@
                          uint32_t get_path_length,
                          struct GNUNET_PeerIdentity *get_path)
 {
-  
+
 }
 
 
@@ -1680,7 +1692,7 @@
  * Send the get result to requesting client.
  * @param key Key of the requested data.
  * @param type Block type
- * @param target_peer Next peer to forward the message to. 
+ * @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.
@@ -1688,11 +1700,11 @@
  * @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 data_size Size of the @a data
  */
-void 
+void
 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
-                                enum GNUNET_BLOCK_Type type, 
+                                enum GNUNET_BLOCK_Type type,
                                 struct GNUNET_PeerIdentity *target_peer,
                                 struct GNUNET_PeerIdentity *source_peer,
                                 unsigned int put_path_length,
@@ -1702,36 +1714,36 @@
                                 struct GNUNET_TIME_Absolute expiration,
                                 const void *data, size_t data_size)
 {
-  
+
 }
 
 
-/** 
+/**
  * Randomly choose one of your friends (which is not congested and have not 
crossed
  * trail threshold) from the friends_peer map
- * @return Friend Randomly chosen friend. 
+ * @return Friend Randomly chosen friend.
  *         NULL in case friend peermap is empty, or all the friends are either
- *              congested or have crossed trail threshold. 
+ *              congested or have crossed trail threshold.
  */
 static struct FriendInfo *
 select_random_friend ()
-{  
+{
   unsigned int current_size;
-  uint32_t index; 
+  uint32_t index;
   unsigned int j = 0;
   struct GNUNET_CONTAINER_MultiPeerMapIterator *iter;
   struct GNUNET_PeerIdentity key_ret;
   struct FriendInfo *friend;
-  
+
   current_size = GNUNET_CONTAINER_multipeermap_size (friend_peermap);
   if (0 == current_size)
     return NULL;
-  
+
   index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, current_size);
   iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
- 
+
   for (j = 0; j < index ; j++)
-    GNUNET_assert (GNUNET_YES == 
+    GNUNET_assert (GNUNET_YES ==
                    GNUNET_CONTAINER_multipeermap_iterator_next (iter, NULL, 
NULL));
   do
   {
@@ -1740,14 +1752,14 @@
       j = 0;
       GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
       iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
-      
+
     }
-    GNUNET_assert (GNUNET_YES == 
+    GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multipeermap_iterator_next (iter,
                                                              &key_ret,
                                                              (const void 
**)&friend));
-  
- 
+
+
     if ((TRAILS_THROUGH_FRIEND_THRESHOLD > friend->trails_count) &&
         (0 == GNUNET_TIME_absolute_get_remaining 
(friend->congestion_duration).rel_value_us))
     {
@@ -1756,17 +1768,18 @@
     friend = NULL;
     j++;
   } while (j != index);
-  
+
   GNUNET_CONTAINER_multipeermap_iterator_destroy (iter);
   return friend;
 }
 
 
 /**
+ * FIXME: pass current_search_finger_index as parameter
  * Compute finger_identity to which we want to setup the trail
- * @return finger_identity 
+ * @return finger_identity
  */
-static uint64_t 
+static uint64_t
 compute_finger_identity()
 {
   uint64_t my_id64;
@@ -1781,7 +1794,7 @@
  * Compute immediate predecessor identity in the network.
  * @return peer identity of immediate predecessor.
  */
-static uint64_t 
+static uint64_t
 compute_predecessor_identity()
 {
   uint64_t my_id64;
@@ -1793,8 +1806,8 @@
 
 
 /*
- * Choose a random friend and start looking for the trail to reach to 
- * finger identity through this random friend. 
+ * Choose a random friend and start looking for the trail to reach to
+ * finger identity through this random friend.
  *
  * @param cls closure for this task
  * @param tc the context under which the task is running
@@ -1806,11 +1819,9 @@
   struct FriendInfo *target_friend;
   struct GNUNET_TIME_Relative next_send_time;
   struct GNUNET_HashCode trail_id;
-  uint64_t trail_id_value;
-  struct GNUNET_HashCode intermediate_trail_id;
   unsigned int finger_map_index;
   uint64_t finger_identity;
-  
+
   next_send_time.rel_value_us =
       DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
       GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
@@ -1818,49 +1829,53 @@
   find_finger_trail_task =
       GNUNET_SCHEDULER_add_delayed (next_send_time, 
&send_find_finger_trail_message,
                                     NULL);
-  
-  target_friend = select_random_friend (); 
-  if (NULL == target_friend) 
+
+  target_friend = select_random_friend ();
+  if (NULL == target_friend)
   {
     return;
   }
-  
+
   if (PREDECESSOR_FINGER_ID == current_search_finger_index)
   {
-    finger_identity = compute_predecessor_identity();  
+    finger_identity = compute_predecessor_identity();
   }
   else
   {
     finger_identity = compute_finger_identity();
   }
-    
+
   finger_map_index = current_search_finger_index;
-  
-  trail_id_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, 
UINT64_MAX);
-  memcpy (&trail_id, &trail_id_value, sizeof (struct GNUNET_HashCode));
-  GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_identity, 
+
+  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
+                              &trail_id, sizeof (trail_id));
+  GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_identity,
                                    target_friend->id, target_friend, 0, NULL,
-                                   finger_map_index, trail_id, 
intermediate_trail_id);
+                                   finger_map_index, trail_id, NULL);
 }
 
 
 /**
- * In case there are already maximum number of possible trail to reach to a 
finger,
- * then check if the new trail's length is lesser than any of the existing 
trails.
+ * In case there are already maximum number of possible trails to reach to a
+ * finger, then check if the new trail's length is lesser than any of the
+ * existing trails.
  * If yes then replace that old trail by new trail.
- * Note: Here we are taking length as a parameter to choose the best possible 
trail,
- * but there could be other parameters also like - 1. duration of existence of 
a
- * trail - older the better. 2. if the new trail is completely disjoint than 
the 
- * other trails, then may be choosing it is better. 
+ *
+ * Note: Here we are taking length as a parameter to choose the best possible
+ * trail, but there could be other parameters also like:
+ * 1. duration of existence of a trail - older the better.
+ * 2. if the new trail is completely disjoint than the
+ *    other trails, then may be choosing it is better.
+ *
  * @param existing_finger
  * @param new_finger_trail
  * @param new_finger_trail_length
  * @param new_finger_trail_id
  */
 static void
-select_and_replace_trail (struct FingerInfo *existing_finger, 
+select_and_replace_trail (struct FingerInfo *existing_finger,
                           struct GNUNET_PeerIdentity *new_trail,
-                          unsigned int new_trail_length, 
+                          unsigned int new_trail_length,
                           struct GNUNET_HashCode new_trail_id)
 {
   struct TrailList *trail_list_iterator;
@@ -1868,12 +1883,12 @@
   unsigned int largest_trail_index;
   struct Trail *trail_element;
   unsigned int i;
-  
+
   largest_trail_length = new_trail_length;
   largest_trail_index = MAXIMUM_TRAILS_PER_FINGER + 1;
-  
+
   GNUNET_assert (MAXIMUM_TRAILS_PER_FINGER == existing_finger->trails_count);
-  
+
   for (i = 0; i < existing_finger->trails_count; i++)
   {
     trail_list_iterator = &existing_finger->trail_list[i];
@@ -1883,38 +1898,39 @@
       largest_trail_index = i;
     }
   }
-  
+
   if (largest_trail_index == (MAXIMUM_TRAILS_PER_FINGER + 1))
+  {
+    // tear down new trail: it's not better than the existing ones
     return;
-  
+  }
+
   /* Send trail teardown message across the replaced trail. */
   struct TrailList *replace_trail = 
&existing_finger->trail_list[largest_trail_index];
-  struct FriendInfo *target_friend = 
-  GNUNET_CONTAINER_multipeermap_get (friend_peermap,
-                                     &replace_trail->trail_head->peer);
-  
-  GDS_NEIGHBOURS_send_trail_teardown (my_identity, 
-                                      existing_finger->finger_identity, 
-                                      replace_trail->trail_id, 
+  struct FriendInfo *target_friend =
+      GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+                                         &replace_trail->trail_head->peer);
+
+  GDS_NEIGHBOURS_send_trail_teardown (my_identity,
+                                      existing_finger->finger_identity,
+                                      replace_trail->trail_id,
                                       GDS_ROUTING_SRC_TO_DEST, target_friend);
-  /* Free the trail .*/
+  /* Free the trail. */
   while (NULL != (trail_element = replace_trail->trail_head))
   {
     GNUNET_CONTAINER_DLL_remove (replace_trail->trail_head,
                                  replace_trail->trail_tail, trail_element);
     GNUNET_free (trail_element);
   }
-  
+
   /* Add new trial at that location. */
   i = 0;
-  while ( i < new_trail_length)
+  while (i < new_trail_length)
   {
     struct Trail *element = GNUNET_new (struct Trail);
-    element->next = NULL;
-    element->prev = NULL;
-    element->peer = new_trail[0];
-    
-    GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head, 
+    element->peer = new_trail[i];
+
+    GNUNET_CONTAINER_DLL_insert_tail (replace_trail->trail_head,
                                       replace_trail->trail_tail,
                                       element);
   }
@@ -1923,12 +1939,12 @@
 
 /**
  * Check if the new trail to reach to finger is unique or do we already have
- * such a trail present for finger. 
- * @param existing_finger Finger identity 
+ * such a trail present for finger.
+ * @param existing_finger Finger identity
  * @param new_trail New trail to reach @a existing_finger
  * @param trail_length Total number of peers in new_trail.
  * @return #GNUNET_YES if the new trail is unique
- *         #GNUNET_NO if same trail is already present. 
+ *         #GNUNET_NO if same trail is already present.
  */
 static int
 is_new_trail_unique (struct FingerInfo *existing_finger,
@@ -1940,15 +1956,17 @@
   int i;
   int j;
   int trail_unique = GNUNET_NO;
-  
+
   for (i = 0; i < existing_finger->trails_count; i++)
   {
     trail_list_iterator = &existing_finger->trail_list[i];
+    if (trail_list_iterator->trail_length != trail_length)
+      continue;
     trail_element = trail_list_iterator->trail_head;
     for (j = 0; j < trail_list_iterator->trail_length; j++)
     {
       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&new_trail[j],
-                                                 &trail_element->peer))
+                                                &trail_element->peer))
       {
         trail_unique = GNUNET_YES;
         break;
@@ -1960,36 +1978,36 @@
 
 
 /**
- * Add a new trail to existing finger. 
+ * Add a new trail to existing finger.
  * @param existing_finger
  * @param new_finger_trail
  * @param new_finger_trail_length
  * @param new_finger_trail_id
  */
 static void
-add_new_trail (struct FingerInfo *existing_finger, 
+add_new_trail (struct FingerInfo *existing_finger,
                struct GNUNET_PeerIdentity *new_trail,
-               unsigned int new_trail_length, 
+               unsigned int new_trail_length,
                struct GNUNET_HashCode new_trail_id)
 {
   struct TrailList *trail_list_iterator;
   struct FriendInfo *first_friend;
-  int i = 0;
-  int j;
-  
-  if (GNUNET_NO == is_new_trail_unique (existing_finger,
-                                         new_trail,
-                                         new_trail_length))
+  int i;
+
+  if (GNUNET_NO == is_new_trail_unique (existing_finger, new_trail,
+                                        new_trail_length))
+  {
     return;
-  
-  do
-  {
-    trail_list_iterator = &existing_finger->trail_list[i];
-    i++;
-  } while (trail_list_iterator->trail_head != NULL);
-  
+  }
+
+  // FIXME checking trail_head is NOT a valid way to verify an open slot
+  for (i = 0; existing_finger->trail_list[i]->trail_head != NULL; i++)
+    GNUNET_assert (i < MAXIMUM_TRAILS_PER_FINGER);
+
+  trail_list_iterator = &existing_finger->trail_list[i];
+
   if (new_trail_length > 0)
-    first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+    first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                       &new_trail[0]);
   else
     first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
@@ -1997,16 +2015,14 @@
   first_friend->trails_count++;
   trail_list_iterator->first_friend_trail_count = first_friend->trails_count;
   trail_list_iterator->trail_length = new_trail_length;
-  
-  for (j = 0; j < new_trail_length; j++)
+
+  for (i = 0; i < new_trail_length; i++)
   {
     struct Trail *element;
     element = GNUNET_new (struct Trail);
-    
-    element->next = NULL;
-    element->prev = NULL;
-    element->peer = new_trail[j];
-    GNUNET_CONTAINER_DLL_insert_tail (trail_list_iterator->trail_head, 
+
+    element->peer = new_trail[i];
+    GNUNET_CONTAINER_DLL_insert_tail (trail_list_iterator->trail_head,
                                       trail_list_iterator->trail_tail,
                                       element);
   }
@@ -2015,34 +2031,34 @@
 
 
 /**
- * Send trail teardown message on all trails associated with finger. 
+ * Send trail teardown message on all trails associated with finger.
  * @param finger_to_be_removed
  */
 static void
 send_trail_teardown (struct FingerInfo *finger)
 {
   struct TrailList *trail_list_iterator;
-  struct GNUNET_HashCode trail_id;
   struct FriendInfo *target_friend;
   int i;
-  
+
   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity, 
&my_identity)
-     || (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+     || (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                     &finger->finger_identity)))
     return;
-  
+
   for (i = 0; i < finger->trails_count; i++)
   {
     trail_list_iterator = &finger->trail_list[i];
     if (trail_list_iterator->trail_length > 0)
     {
-      trail_id = trail_list_iterator->trail_id;
-      target_friend = 
+      target_friend =
               GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                  
&trail_list_iterator->trail_head->peer);
-    GDS_NEIGHBOURS_send_trail_teardown (my_identity, finger->finger_identity,
-                                        trail_id, GDS_ROUTING_SRC_TO_DEST,
-                                        target_friend);
+      // check target_friend
+      GDS_NEIGHBOURS_send_trail_teardown (my_identity, finger->finger_identity,
+                                          trail_list_iterator->trail_id,
+                                          GDS_ROUTING_SRC_TO_DEST,
+                                          target_friend);
     }
   }
 }
@@ -2059,23 +2075,24 @@
   struct TrailList *trail_list_iterator;
   struct FriendInfo *target_friend;
   int i = 0;
-  
+
   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
                                             &my_identity))
     return;
-  
+
   for (i = 0; i < finger->trails_count; i++)
   {
     trail_list_iterator = &finger->trail_list[i];
     if (trail_list_iterator->trail_length > 0)
-      target_friend = 
-              GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+      target_friend =
+              GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                  
&trail_list_iterator->trail_head->peer);
     else
-     target_friend = 
-              GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+     target_friend =
+              GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                  &finger->finger_identity);
-    
+
+    // check target_friend for NULL
     target_friend->trails_count--;
     trail_list_iterator->first_friend_trail_count--;
   }
@@ -2084,7 +2101,7 @@
 
 
 /**
- * Free finger and its trail.  
+ * Free finger and its trail.
  * @param finger Finger to be freed.
  */
 static void
@@ -2093,14 +2110,15 @@
   struct TrailList *trail_list_iterator;
   struct Trail *trail_element;
   unsigned int i;
-  
+
   for (i = 0; i < finger->trails_count; i++)
   {
     trail_list_iterator = &finger->trail_list[i];
     while (NULL != (trail_element = trail_list_iterator->trail_head))
     {
       GNUNET_CONTAINER_DLL_remove (trail_list_iterator->trail_head,
-                                   trail_list_iterator->trail_tail, 
trail_element);
+                                   trail_list_iterator->trail_tail,
+                                   trail_element);
       GNUNET_free (trail_element);
     }
   }
@@ -2109,21 +2127,25 @@
 
 
 /**
- * Check if new finger is closer than existing_finger. If both new finger and 
+ * FIXME: merge into finger_table_add
+ * FIXME: leave as simple a check
+ * Check if new finger is closer than existing_finger. If both new finger and
  * existing finger are same then we may add a new trail (if there is space)
  * or choose the best trail among existing trails and new trails.
  * @param existing_finger Finger present in finger_peermap at @a 
finger_map_index
+ *                        or NULL if none
  * @param new_finger_identity Peer identity of new finger.
- * @param new_finger_trail Trail to reach from source to new_finger. 
+ * FIXME: all the following params *should* not be necessary
+ * @param new_finger_trail Trail to reach from source to new_finger.
  * @param new_finger_trail_length Total number of peers in @a new_finger_trail.
- * @param trail_id Unique identifier of trail. 
- * @param finger_map_index Index in finger map. 
+ * @param trail_id Unique identifier of trail.
+ * @param finger_map_index Index in finger map.
  * @return #GNUNET_YES if the new finger is closest.
- *         #GNUNET_NO either new_finger and existing_finger are same, or 
+ *         #GNUNET_NO either new_finger and existing_finger are same, or
  *                    existing_finger is closest.
  */
 static int
-is_new_finger_closest (struct FingerInfo *existing_finger, 
+is_new_finger_closest (struct FingerInfo *existing_finger,
                        struct GNUNET_PeerIdentity new_finger_identity,
                        struct GNUNET_PeerIdentity *new_finger_trail,
                        unsigned int new_finger_trail_length,
@@ -2132,10 +2154,10 @@
 {
   struct GNUNET_PeerIdentity *closest_peer;
   uint64_t my_id64;
-  
+
   if (NULL == existing_finger)
     return GNUNET_YES;
-  
+
   if (0 != GNUNET_CRYPTO_cmp_peer_identity 
(&(existing_finger->finger_identity),
                                             &new_finger_identity))
   {
@@ -2143,13 +2165,12 @@
     closest_peer = select_closest_peer (&existing_finger->finger_identity,
                                         &new_finger_identity,
                                         my_id64, finger_map_index);
-  
+
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&new_finger_identity, 
closest_peer))
     {
-      if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, 
-                                                &new_finger_identity)) /* 
FIXME: not sure what to do here? */
-        return GNUNET_NO;
-    
+      GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
+                                                           
&new_finger_identity));
+
       send_trail_teardown (existing_finger);
       decrement_friend_trail_count (existing_finger);
       free_finger (existing_finger);
@@ -2158,22 +2179,23 @@
   }
   else
   {
-    if (0 != GNUNET_CRYPTO_cmp_peer_identity 
(&(existing_finger->finger_identity), 
+    if (0 == GNUNET_CRYPTO_cmp_peer_identity 
(&(existing_finger->finger_identity),
                                               &my_identity))
     {
-      if (NULL == 
-          GNUNET_CONTAINER_multipeermap_get (friend_peermap,
-                                             
&(existing_finger->finger_identity)))
-      {
-        if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
-          add_new_trail (existing_finger, new_finger_trail,
-                         new_finger_trail_length, new_finger_trail_id);
-        else
-          select_and_replace_trail (existing_finger, new_finger_trail,
-                                    new_finger_trail_length, 
new_finger_trail_id); 
-      }
+      return GNUNET_NO;
     }
-  } 
+    if (NULL ==
+        GNUNET_CONTAINER_multipeermap_get (friend_peermap,
+                                            
&(existing_finger->finger_identity)))
+    {
+      if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
+        add_new_trail (existing_finger, new_finger_trail,
+                        new_finger_trail_length, new_finger_trail_id);
+      else
+        select_and_replace_trail (existing_finger, new_finger_trail,
+                                  new_finger_trail_length, 
new_finger_trail_id);
+    }
+  }
   return GNUNET_NO;
 }
 
@@ -2183,11 +2205,11 @@
  * @param finger_identity Peer Identity of new finger
  * @param finger_trail Trail to reach from me to finger (excluding both end 
points).
  * @param finger_trail_length Total number of peers in @a finger_trail.
- * @param trail_id Unique identifier of the trail. 
- * @param finger_map_index Index in finger hashmap. 
+ * @param trail_id Unique identifier of the trail.
+ * @param finger_map_index Index in finger hashmap.
  * @return #GNUNET_OK if new entry is added
  *         #GNUNET_NO -- FIXME: need to check what value does hahsmap put
- *                       returns on failure. 
+ *                       returns on failure.
  */
 static int
 add_new_entry (struct GNUNET_PeerIdentity finger_identity,
@@ -2200,12 +2222,12 @@
   struct FriendInfo *first_trail_hop;
   struct TrailList *first_trail;
   int i = 0;
-  
+
   new_entry = GNUNET_new (struct FingerInfo);
   new_entry->finger_identity = finger_identity;
   new_entry->finger_map_index = finger_map_index;
   new_entry->trails_count = 1;
-  
+
   if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
   {
     if (finger_trail_length > 0)
@@ -2218,86 +2240,88 @@
       first_trail_hop = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                            &finger_identity);
     }
-    
+
     first_trail_hop->trails_count++;
     first_trail = &new_entry->trail_list[0];
     first_trail->first_friend_trail_count = first_trail_hop->trails_count;
-    
+
     while (i < finger_trail_length)
     {
       struct Trail *element = GNUNET_new (struct Trail);
-      
+
       element->next = NULL;
       element->prev = NULL;
       element->peer = finger_trail[i];
-      GNUNET_CONTAINER_DLL_insert_tail (first_trail->trail_head, 
+      GNUNET_CONTAINER_DLL_insert_tail (first_trail->trail_head,
                                         first_trail->trail_tail,
                                         element);
       i++;
     }
   }
- 
-  return GNUNET_CONTAINER_multihashmap32_put (finger_hashmap, 
+
+  return GNUNET_CONTAINER_multihashmap32_put (finger_hashmap,
                                               new_entry->finger_map_index,
                                               new_entry,
-                                              
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);   
+                                              
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
 }
 
 
 /**
  * Scan the trail to check if there is any other friend in the trail other than
- * first hop. If yes the shortcut the trail, send trail compression message to
+ * first hop. If yes then shortcut the trail, send trail compression message to
  * peers which are no longer part of trail and send back the updated trail
- * and trail_length to calling function. 
- * @param finger_identity Finger whose trail we will scan. 
- * @param finger_trail [in, out] Trail to reach from source to finger,         
     
+ * and trail_length to calling function.
+ * @param finger_identity Finger whose trail we will scan.
+ * @param finger_trail [in, out] Trail to reach from source to finger,
  * @param finger_trail_length  Total number of peers in original finger_trail.
  * @param finger_trail_id Unique identifier of the finger trail.
  * @return updated trail length in case we shortcut the trail, else original
- *         trail length.  
+ *         trail length.
  */
 static int
 scan_and_compress_trail (struct GNUNET_PeerIdentity finger_identity,
-                         struct GNUNET_PeerIdentity *trail,
+                         const struct GNUNET_PeerIdentity *trail,
                          unsigned int trail_length,
                          struct GNUNET_HashCode trail_id)
 {
   struct FriendInfo *target_friend;
   int i;
-  
+
   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
   {
-    trail = NULL;
     return 0;
   }
-  
+
   if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&finger_identity))
   {
     if (trail_length > 0)
     {
-      target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+      target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                        &trail[0]);
-      GDS_NEIGHBOURS_send_trail_compression (my_identity, finger_identity, 
-                                           trail_id, finger_identity, 
+      GDS_NEIGHBOURS_send_trail_compression (my_identity, finger_identity,
+                                           trail_id, finger_identity,
                                            target_friend);
       trail = NULL;
     }
     return 0;
   }
-  
-  for ( i = trail_length - 1; i > 0; i--)
+
+  for (i = trail_length - 1; i > 0; i--)
   {
     if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, &trail[i]))
     {
       struct FriendInfo *target_friend;
       int j = 0;
 
-      target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+      target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                          &trail[0]);
-      GDS_NEIGHBOURS_send_trail_compression (my_identity, finger_identity, 
-                                             trail_id, trail[i], 
+      GDS_NEIGHBOURS_send_trail_compression (my_identity, finger_identity,
+                                             trail_id, trail[i],
                                              target_friend);
-     
+
+      // FIXME WARNING WARNING WARNING rewrite!!! consider directly creating a
+      // struct TrailList (current) // struct Trial (after renaming).
+
       /* Copy the trail from index i to index trail_length -1 and change
        trail length and return */
       while (i < trail_length)
@@ -2315,8 +2339,9 @@
 
 
 /**
- * Send verify successor message to your successor on all trails to reach 
successor.
- * @param successor My current successor 
+ * Send verify successor message to your successor on all trails to reach
+ * the successor.
+ * @param successor My current successor
  */
 static void
 send_verify_successor_message (struct FingerInfo *successor)
@@ -2329,27 +2354,27 @@
   unsigned int trail_length;
   int i;
   int j;
-  
-  for (i = 0; i < successor->trails_count; i++)
+
+  for (i = 0; i < MAXIMUM_TRAILS_PER_FINGER; i++)
   {
     trail_list_iterator = &successor->trail_list[i];
-    
+
+//      FIXME check if this entry in the trail list is valid!
+//     if (NULL == trail_list_iterator->SOMETHING)
+//       continue;
+
     if (trail_list_iterator->trail_length > 0)
     {
+      struct Trail *element;
+
       trail_length = trail_list_iterator->trail_length;
-      j = 0;
-      trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
-                             trail_length);
-      struct Trail *element;
+      trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)
+                             * trail_length);
       element = trail_list_iterator->trail_head;
-      while ( j < trail_length)
-      {
+      for (j = 0; j < trail_length; j++, element = element->next)
         trail[j] = element->peer;
-        element = element->next;
-        j++;
-      }
       next_hop = trail_list_iterator->trail_head->peer;
-    }  
+    }
     else
     {
       trail = NULL;
@@ -2358,10 +2383,12 @@
     }
     trail_id = trail_list_iterator->trail_id;
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop);
+    // check friend for NULL
     GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
                                                   successor->finger_identity,
                                                   trail_id, trail, 
trail_length,
                                                   target_friend);
+    GNUNET_free_non_null (trail);
   }
 }
 
@@ -2382,8 +2409,8 @@
  *                    it was same as existing finger at finger map index.
  */
 static int
-finger_table_add (struct GNUNET_PeerIdentity new_finger_identity,
-                  struct GNUNET_PeerIdentity *new_finger_trail,
+finger_table_add (struct GNUNET_PeerIdentity new_finger_identity, // 
"finger_id"
+                  const struct GNUNET_PeerIdentity *new_finger_trail, // 
"trail"
                   unsigned int new_finger_trail_length,
                   unsigned int finger_map_index,
                   struct GNUNET_HashCode new_finger_trail_id)
@@ -2391,60 +2418,73 @@
   struct FingerInfo *existing_finger;
   struct FingerInfo *successor;
   unsigned int new_entry_added = GNUNET_NO;
-  
-  int new_finger_updated_trail_length = 
-       scan_and_compress_trail (new_finger_identity, new_finger_trail, 
+  int new_finger_updated_trail_length; // new_finger_trail_length
+
+  new_finger_updated_trail_length =
+       scan_and_compress_trail (new_finger_identity, new_finger_trail,
                                 new_finger_trail_length, new_finger_trail_id);
- 
+
   existing_finger = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
                                                          finger_map_index);
-  
+
   if  (GNUNET_YES == is_new_finger_closest (existing_finger,
                                             new_finger_identity,
-                                            new_finger_trail, 
+                                            new_finger_trail,
                                             new_finger_updated_trail_length,
                                             new_finger_trail_id, 
finger_map_index))
   {
-    GNUNET_assert (GNUNET_YES == add_new_entry (new_finger_identity, 
-                                                new_finger_trail, 
-                                                
new_finger_updated_trail_length, 
-                                                new_finger_trail_id, 
+    // send_destroy_existing_finger ...
+    // free_exisiting_finger ...
+    GNUNET_assert (GNUNET_YES == add_new_entry (new_finger_identity,
+                                                new_finger_trail,
+                                                
new_finger_updated_trail_length,
+                                                new_finger_trail_id,
                                                 finger_map_index));
     new_entry_added = GNUNET_YES;
   }
-  
-  successor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap, 
-                                                   finger_map_index);
-  if (0 == finger_map_index)
-  {   
-    current_search_finger_index = PREDECESSOR_FINGER_ID;
- 
-    if (0 != GNUNET_CRYPTO_cmp_peer_identity 
(&my_identity,&new_finger_identity))
+//   else if if_new_finger_equal () {
+//
+//   }
+//   else // existing finger is closest
+//   {
+//
+//   }
+
+  // FIXME move block to "update_succesor"
+  {
+    successor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap, 0);
+    // WARNING FIXME check that current_search_finger_index does not go out of 
bounds
+    if (0 == finger_map_index)
     {
-      send_verify_successor_message (successor);
+      current_search_finger_index = PREDECESSOR_FINGER_ID;
+
+      if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, 
&new_finger_identity))
+      {
+        send_verify_successor_message (successor);
+      }
     }
+    else if (0 == GNUNET_CRYPTO_cmp_peer_identity (&new_finger_identity,
+                                                  
&(successor->finger_identity)))
+    {
+      current_search_finger_index = 0;
+    }
+    else
+      current_search_finger_index = current_search_finger_index - 1;
   }
-  else if (0 == GNUNET_CRYPTO_cmp_peer_identity (&new_finger_identity, 
-                                                 
&(successor->finger_identity)))
-  {
-    current_search_finger_index = 0;
-  }
-  else 
-    current_search_finger_index = current_search_finger_index - 1;
-  
+
   return new_entry_added;
 }
 
 
 /**
- * Core handler for P2P put messages. 
+ * Core handler for P2P put messages.
  * @param cls closure
  * @param peer sender of the request
  * @param message message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
-static int 
+static int
 handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
                     const struct GNUNET_MessageHeader *message)
 {
@@ -2485,7 +2525,7 @@
 }
 
 
-/* Core handle for PeerTrailSetupMessage. 
+/* Core handle for PeerTrailSetupMessage.
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
@@ -2495,9 +2535,9 @@
 handle_dht_p2p_trail_setup (void *cls, const struct GNUNET_PeerIdentity *peer,
                             const struct GNUNET_MessageHeader *message)
 {
-  struct PeerTrailSetupMessage *trail_setup; 
+  struct PeerTrailSetupMessage *trail_setup;
   struct GNUNET_PeerIdentity *trail_peer_list;
-  struct GNUNET_PeerIdentity next_destination;
+  struct GNUNET_PeerIdentity next_destination; // 
"local_best_known_destination"
   struct GNUNET_PeerIdentity *current_destination;
   struct GNUNET_PeerIdentity *next_hop;
   struct GNUNET_PeerIdentity next_peer;
@@ -2512,23 +2552,25 @@
   size_t msize;
 
   msize = ntohs (message->size);
-  if (msize != sizeof (struct PeerTrailSetupMessage))
+  /* There are PeerId's appended to the end of the message! */
+  if (msize < sizeof (struct PeerTrailSetupMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   trail_setup = (struct PeerTrailSetupMessage *) message;
-  trail_length = ntohl (trail_setup->trail_length); 
+  trail_length = ntohl (trail_setup->trail_length); // (msize - sizeof (msg)) 
/ sizeof(PI)
+  // if ((msize - sizeof (msg)) % sizeof(PI) != 0)
   if ((msize != sizeof (struct PeerTrailSetupMessage) +
        trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
        (trail_length >
         GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
   {
     GNUNET_break_op (0);
-    return GNUNET_OK; 
+    return GNUNET_OK;
   }
-  
+
   trail_peer_list = (struct GNUNET_PeerIdentity *)&trail_setup[1];
   current_destination = &trail_setup->next_destination;
   intermediate_trail_id = trail_setup->intermediate_trail_id;
@@ -2536,49 +2578,56 @@
   ultimate_destination_finger_value = GNUNET_ntohll 
(trail_setup->ultimate_destination_finger);
   source = trail_setup->source_peer;
   finger_map_index = ntohl (trail_setup->finger_map_index);
-  
+
   if (GNUNET_YES == GDS_ROUTING_threshold_reached())
   {
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
     GDS_NEIGHBOURS_send_trail_rejection (source, 
ultimate_destination_finger_value,
                                          my_identity, finger_map_index,
                                          trail_peer_list, trail_length,
-                                         new_trail_id, target_friend, 
+                                         new_trail_id, target_friend,
                                          CONGESTION_TIMEOUT);
     return GNUNET_OK;
   }
-  
+
   next_hop = find_successor (ultimate_destination_finger_value, 
&next_destination,
                              &new_intermediate_trail_id, finger_map_index);
-  
+
+  /* Are we just a part of a trail towards a finger? */
   if (0 != (GNUNET_CRYPTO_cmp_peer_identity(&my_identity, 
current_destination)))
   {
     struct GNUNET_PeerIdentity *closest_peer;
-    struct GNUNET_PeerIdentity *peer1 = 
-    GDS_ROUTING_get_next_hop (intermediate_trail_id, GDS_ROUTING_SRC_TO_DEST);
-    if (0 != GNUNET_CRYPTO_cmp_peer_identity (peer1, next_hop))
-    {
-       closest_peer = select_closest_peer (peer1, next_hop, 
-                                           ultimate_destination_finger_value,
-                                           finger_map_index);
-    }
-    if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer1, closest_peer) ||
-        (0 == GNUNET_CRYPTO_cmp_peer_identity (peer1, next_hop)))
-    {
-      next_hop = peer1;
-      next_destination = *current_destination;
-      new_intermediate_trail_id = intermediate_trail_id;
-    }
+//     struct GNUNET_PeerIdentity *peer1 =
+//         GDS_ROUTING_get_next_hop (intermediate_trail_id,
+//                                   GDS_ROUTING_SRC_TO_DEST);
+    /* Is next_destination better than the original best_known_dest? */
+    // BIG FIXME START
+//     if (0 != GNUNET_CRYPTO_cmp_peer_identity (next_destination,
+//                                               current_destination))
+//     {
+//        closest_peer = select_closest_peer (peer1, next_hop,
+//                                            
ultimate_destination_finger_value,
+//                                            finger_map_index);
+//     }
+//     if (0 == GNUNET_CRYPTO_cmp_peer_identity (peer1, closest_peer) ||
+//         (0 == GNUNET_CRYPTO_cmp_peer_identity (peer1, next_hop)))
+//     {
+//       next_hop = peer1;
+//       next_destination = *current_destination;
+//       new_intermediate_trail_id = intermediate_trail_id;
+//     }
+    // BIG FIXME END
   }
-  
+
   GNUNET_assert (NULL != next_hop);
-  if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))/* This 
means I am the final destination */
+  /* Am I the final destination? */
+  if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))
   {
     if (0 == trail_length)
       memcpy (&next_peer, &source, sizeof (struct GNUNET_PeerIdentity));
     else
       memcpy (&next_peer, &trail_peer_list[trail_length-1], sizeof (struct 
GNUNET_PeerIdentity));
-    
+
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_peer);
     GDS_NEIGHBOURS_send_trail_setup_result (source,
                                             my_identity,
@@ -2589,14 +2638,14 @@
   else
   {
     struct GNUNET_PeerIdentity peer_list[trail_length + 1];
-    
+
     memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct 
GNUNET_PeerIdentity));
     peer_list[trail_length] = my_identity;
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop);
     GDS_NEIGHBOURS_send_trail_setup (source,
                                      ultimate_destination_finger_value,
-                                     next_destination, 
-                                     target_friend, trail_length + 1, 
peer_list, 
+                                     next_destination,
+                                     target_friend, trail_length + 1, 
peer_list,
                                      finger_map_index, new_trail_id,
                                      new_intermediate_trail_id);
   }
@@ -2615,25 +2664,26 @@
 handle_dht_p2p_trail_setup_result(void *cls, const struct GNUNET_PeerIdentity 
*peer,
                                   const struct GNUNET_MessageHeader *message)
 {
-  struct PeerTrailSetupResultMessage *trail_result;
-  struct GNUNET_PeerIdentity *trail_peer_list;
+  const struct PeerTrailSetupResultMessage *trail_result;
+  const struct GNUNET_PeerIdentity *trail_peer_list;
   struct GNUNET_PeerIdentity destination_peer;
-  struct GNUNET_PeerIdentity finger_identity;    
+  struct GNUNET_PeerIdentity finger_identity;
   uint32_t trail_length;
   uint32_t finger_map_index;
   struct GNUNET_HashCode trail_id;
   size_t msize;
-  
+
   msize = ntohs (message->size);
-  if (msize != sizeof (struct PeerTrailSetupResultMessage))
+  if (msize < sizeof (struct PeerTrailSetupResultMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
-  trail_result = (struct PeerTrailSetupResultMessage *) message; 
-  trail_length = ntohl (trail_result->trail_length); 
-  
+
+  trail_result = (const struct PeerTrailSetupResultMessage *) message;
+
+  // calculate trail_length with message size, check for % 0
+  trail_length = ntohl (trail_result->trail_length);
   if ((msize !=
        sizeof (struct PeerTrailSetupResultMessage) +
        trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
@@ -2643,44 +2693,49 @@
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   finger_map_index = htonl (trail_result->finger_map_index);
   destination_peer = trail_result->destination_peer;
   finger_identity = trail_result->finger_identity;
   trail_id = trail_result->trail_id;
-  trail_peer_list = (struct GNUNET_PeerIdentity *) &trail_result[1];
-  
+  trail_peer_list = (const struct GNUNET_PeerIdentity *) &trail_result[1];
+
+  // FIXME: check that trail_peer_list[my_index + 1] == peer or
+  // my_index == trail_length - 1 AND finger_identity == peer
+
+  /* Am I the one who initiated the query? */
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&destination_peer,
                                              &my_identity)))
   {
-    finger_table_add (finger_identity, trail_peer_list, 
-                      trail_length, 
+    finger_table_add (finger_identity, trail_peer_list,
+                      trail_length,
                       finger_map_index, trail_id);
     return GNUNET_YES;
   }
-  
+
   struct GNUNET_PeerIdentity next_hop;
   struct FriendInfo *target_friend;
   int my_index;
 
   my_index = search_my_index(trail_peer_list, trail_length);
-  if (my_index == GNUNET_SYSERR) 
+  if (-1 == my_index)
   {
     GNUNET_break_op(0);
     return GNUNET_SYSERR;
   }
-  
+
+
   if (my_index == 0)
     next_hop = trail_result->destination_peer;
   else
     next_hop = trail_peer_list[my_index - 1];
-  
+
   if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->destination_peer),
                                                
&(trail_result->finger_identity))))
   {
     GDS_ROUTING_add (trail_id, next_hop, *peer);
   }
-  
+
   target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop);
   GDS_NEIGHBOURS_send_trail_setup_result (destination_peer, finger_identity,
                                           target_friend, trail_length, 
trail_peer_list,
@@ -2690,10 +2745,10 @@
 
 
 /**
- * Invert the trail. 
- * @param trail Trail to be inverted  
+ * Invert the trail.
+ * @param trail Trail to be inverted
  * @param trail_length Total number of peers in the trail.
- * @return Updated trail 
+ * @return Updated trail
  */
 static struct GNUNET_PeerIdentity *
 invert_trail (struct GNUNET_PeerIdentity *trail,
@@ -2702,7 +2757,7 @@
   int i;
   int j;
   struct GNUNET_PeerIdentity *inverted_trail;
-  
+
   inverted_trail = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity) *
                                   trail_length);
   i = 0;
@@ -2719,11 +2774,11 @@
 
 /**
  * Check if the new finger can be our predecessor. If yes then update 
predecessor
- * 
+ *
  * @param new_finger
  * @param new_finger_trail
  * @param new_finger_trail_length
- * @return 
+ * @return
  */
 static int
 is_new_entry_correct_predecessor (struct FingerInfo *my_predecessor,
@@ -2733,10 +2788,10 @@
 {
   struct GNUNET_PeerIdentity *updated_trail;
   struct GNUNET_HashCode new_trail_id;
-  
+
   updated_trail =  invert_trail (new_finger_trail, new_finger_trail_length);
   if (GNUNET_YES == is_new_finger_closest (my_predecessor, new_finger,
-                                           new_finger_trail, 
+                                           new_finger_trail,
                                            new_finger_trail_length,
                                            new_trail_id, 
PREDECESSOR_FINGER_ID))
   {
@@ -2751,11 +2806,11 @@
 /**
  * In case the source peer of verify successor message is not my successor,
  * then construct a trail from source peer to my current predecessor.
- * @param my_predecessor my current predecessor. 
+ * @param my_predecessor my current predecessor.
  * @param current_trail Trail from source to me.
  * @param current_trail_length Total number of peers in @a current_trail
- * @param new_trail_length [out] Total number of peers in updated trail.   
- * @return Updated trail from source peer to my_predecessor. 
+ * @param new_trail_length [out] Total number of peers in updated trail.
+ * @return Updated trail from source peer to my_predecessor.
  */
 static struct GNUNET_PeerIdentity *
 trail_source_to_my_predecessor (struct FingerInfo *my_predecessor,
@@ -2770,7 +2825,7 @@
   unsigned int j;
   unsigned int shortest_trail_length = 0;
   unsigned int trail_index = 0;
-  
+
   for (i = 0; i < my_predecessor->trails_count; i++)
   {
     trail_list_iterator = &my_predecessor->trail_list[i];
@@ -2779,14 +2834,14 @@
     shortest_trail_length = trail_list_iterator->trail_length;
     trail_index = i;
   }
-  
+
   *new_trail_length = current_trail_length + shortest_trail_length + 1;
   new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
                              *new_trail_length);
-  memcpy (new_trail, current_trail, 
+  memcpy (new_trail, current_trail,
          current_trail_length * sizeof (struct GNUNET_PeerIdentity));
   new_trail[current_trail_length + 1] = my_identity;
-  
+
   i = 0;
   j = current_trail_length + 1;
   trail_list_iterator = &my_predecessor->trail_list[trail_index];
@@ -2798,7 +2853,7 @@
     i++;
     trail_iterator = trail_iterator->next;
   }
-  
+
   *new_trail_length = j;
   return new_trail;
 }
@@ -2815,7 +2870,7 @@
 handle_dht_p2p_verify_successor(void *cls, const struct GNUNET_PeerIdentity 
*peer,
                                 const struct GNUNET_MessageHeader *message)
 {
-  struct PeerVerifySuccessorMessage *vsm; 
+  struct PeerVerifySuccessorMessage *vsm;
   struct GNUNET_PeerIdentity successor;
   struct GNUNET_PeerIdentity source_peer;
   struct GNUNET_PeerIdentity *next_hop;
@@ -2827,14 +2882,14 @@
   unsigned int trail_length;
   unsigned int new_trail_length;
   size_t msize;
-  
+
   msize = ntohs (message->size);
   if (msize != sizeof (struct PeerVerifySuccessorMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   vsm = (struct PeerVerifySuccessorMessage *) message;
   trail_length = ntohl (vsm->trail_length);
   if ((msize != sizeof (struct PeerVerifySuccessorMessage) +
@@ -2848,32 +2903,32 @@
   source_peer = vsm->source_peer;
   successor = vsm->successor;
   trail_id = vsm->trail_id;
-  
+
   if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity)))
   {
     next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
     if (NULL == next_hop)
     {
-      GNUNET_break (0); 
+      GNUNET_break (0);
       return GNUNET_SYSERR;
-    }  
+    }
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop);
-    GDS_NEIGHBOURS_send_verify_successor_message (source_peer, successor, 
+    GDS_NEIGHBOURS_send_verify_successor_message (source_peer, successor,
                                                   trail_id, trail, 
trail_length,
                                                   target_friend);
     return GNUNET_OK;
   }
-  
+
   my_predecessor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
                                                         PREDECESSOR_FINGER_ID);
   target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
   if (GNUNET_NO == is_new_entry_correct_predecessor (my_predecessor, 
source_peer,
                                                      trail, trail_length))
   {
-    if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source_peer, 
+    if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
                                               
&my_predecessor->finger_identity))
     {
-      new_trail = trail_source_to_my_predecessor (my_predecessor, trail, 
+      new_trail = trail_source_to_my_predecessor (my_predecessor, trail,
                                                   trail_length, 
&new_trail_length);
     }
   }
@@ -2881,21 +2936,21 @@
   {
     my_predecessor = GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
                                                           
PREDECESSOR_FINGER_ID);
-    GDS_NEIGHBOURS_send_add_trail (my_predecessor->finger_identity, 
+    GDS_NEIGHBOURS_send_add_trail (my_predecessor->finger_identity,
                                    source_peer, trail_id,
                                    trail, trail_length,
                                    target_friend);
     new_trail_length = trail_length;
     new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
                                new_trail_length);
-    memcpy (new_trail, trail, sizeof (struct GNUNET_PeerIdentity) * 
+    memcpy (new_trail, trail, sizeof (struct GNUNET_PeerIdentity) *
                               trail_length);
   }
-  
+
   GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity,
                                                my_predecessor->finger_identity,
-                                               trail_id, new_trail, 
-                                               new_trail_length, 
+                                               trail_id, new_trail,
+                                               new_trail_length,
                                                GDS_ROUTING_DEST_TO_SRC,
                                                target_friend);
   return GNUNET_OK;
@@ -2906,7 +2961,7 @@
  * FIXME: I will keep the logic to remove the old trail to reach from me to
  * my old successor here and move adding a new trail from me to new successor 
to notify
  * new successor. And in case if the new successor also take it as predecessor
- * then call add_trail. 
+ * then call add_trail.
  * Core handle for p2p verify successor result messages.
  * @param cls closure
  * @param message message
@@ -2928,7 +2983,7 @@
   struct GNUNET_PeerIdentity *next_hop;
   struct FriendInfo *target_friend;
   size_t msize;
-  
+
   msize = ntohs (message->size);
   if (msize != sizeof (struct PeerVerifySuccessorResultMessage))
   {
@@ -2939,10 +2994,10 @@
   new_trail_length = ntohl (vsrm->trail_length);
   trail_direction = ntohl (vsrm->trail_direction);
   trail_id = vsrm->trail_id;
-  
+
   if ((msize !=
        sizeof (struct PeerVerifySuccessorResultMessage) +
-       new_trail_length * 
+       new_trail_length *
        sizeof (struct GNUNET_PeerIdentity)) ||
        (new_trail_length >
        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
@@ -2950,12 +3005,12 @@
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   new_trail = (struct GNUNET_PeerIdentity *) &vsrm[1];
   destination_peer = vsrm->destination_peer;
   my_new_successor = vsrm->my_predecessor;
   old_successor = vsrm->source_successor;
-  
+
   if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&destination_peer, &my_identity)))
   {
     struct GNUNET_HashCode new_finger_trail_id;
@@ -2980,11 +3035,11 @@
     }
     return GNUNET_OK;
   }
-  
-  GNUNET_assert (NULL != (next_hop = 
+
+  GNUNET_assert (NULL != (next_hop =
                          GDS_ROUTING_get_next_hop (trail_id, 
trail_direction)));
-  target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop); 
-  GDS_NEIGHBOURS_send_verify_successor_result (destination_peer, 
+  target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
+  GDS_NEIGHBOURS_send_verify_successor_result (destination_peer,
                                                vsrm->source_successor,
                                                my_new_successor, trail_id,
                                                new_trail,
@@ -2994,7 +3049,7 @@
 }
 
 
-/* 
+/*
  * Core handle for p2p notify new successor messages.
  * @param cls closure
  * @param message message
@@ -3015,17 +3070,17 @@
   struct GNUNET_PeerIdentity next_hop;
   size_t msize;
   uint32_t trail_length;
-  
+
   msize = ntohs (message->size);
   if (msize != sizeof (struct PeerNotifyNewSuccessorMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   nsm = (struct PeerNotifyNewSuccessorMessage *) message;
   trail_length = ntohl (nsm->trail_length);
-  
+
   if ((msize < sizeof (struct PeerNotifyNewSuccessorMessage) +
                trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
       (trail_length >
@@ -3034,35 +3089,35 @@
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   trail = (struct GNUNET_PeerIdentity *) &nsm[1];
   source  = nsm->source_peer;
   destination = nsm->destination_peer;
   trail_id = nsm->trail_id;
-  
+
   if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &destination))
   {
     struct GNUNET_HashCode new_trail_id;
     struct FingerInfo *my_predecessor;
-    
-    my_predecessor = 
+
+    my_predecessor =
             GNUNET_CONTAINER_multihashmap32_get (finger_hashmap,
                                                  PREDECESSOR_FINGER_ID);
     /* FIXME: get new_trail_id*/
-   
+
     if (GNUNET_YES == is_new_entry_correct_predecessor (my_predecessor,
-                                                        source, trail, 
+                                                        source, trail,
                                                         trail_length))
     {
-      
-      target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+
+      target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                          peer);
       GDS_NEIGHBOURS_send_add_trail (my_identity, source, new_trail_id,
-                                     trail, trail_length, target_friend); 
+                                     trail, trail_length, target_friend);
       return GNUNET_OK;
     }
   }
-  
+
   my_index = search_my_index (trail, trail_length);
   if (GNUNET_SYSERR == my_index)
   {
@@ -3075,22 +3130,22 @@
     next_hop = trail[my_index + 1];
   GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, next_hop));
   target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop);
-  GDS_NEIGHBOURS_send_notify_new_successor (source, destination, trail, 
+  GDS_NEIGHBOURS_send_notify_new_successor (source, destination, trail,
                                             trail_length,
                                             trail_id, target_friend);
   return GNUNET_OK;
-}                                           
+}
 
 
 /**
  * FIXME: Here you should keep the trail id with you.
- * Core handler for P2P trail rejection message 
+ * Core handler for P2P trail rejection message
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-static int 
+static int
 handle_dht_p2p_trail_rejection(void *cls, const struct GNUNET_PeerIdentity 
*peer,
                                const struct GNUNET_MessageHeader *message)
 {
@@ -3108,17 +3163,17 @@
   uint64_t ultimate_destination_finger_value;
   unsigned int finger_map_index;
   size_t msize;
-  
+
   msize = ntohs (message->size);
   if (msize != sizeof (struct PeerTrailRejectionMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   trail_rejection = (struct PeerTrailRejectionMessage *) message;
   trail_length = ntohl (trail_rejection->trail_length);
-  
+
   if ((msize != sizeof (struct PeerTrailRejectionMessage) +
                trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
       (trail_length >
@@ -3127,38 +3182,38 @@
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   trail_peer_list = (struct GNUNET_PeerIdentity *)&trail_rejection[1];
   finger_map_index = ntohl (trail_rejection->finger_map_index);
   congestion_timeout = trail_rejection->congestion_time;
   source = trail_rejection->source_peer;
   trail_id = trail_rejection->trail_id;
-  ultimate_destination_finger_value = 
+  ultimate_destination_finger_value =
   trail_rejection->ultimate_destination_finger_identity_value;
-  
+
   /* First set the congestion time of the friend that sent you this message. */
   target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
-  target_friend->congestion_duration = GNUNET_TIME_absolute_add 
(GNUNET_TIME_absolute_get(),
+  target_friend->congestion_timestamp = GNUNET_TIME_absolute_add 
(GNUNET_TIME_absolute_get(),
                                                                  
congestion_timeout);
-  
+
   if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &source)))
   {
     return GNUNET_OK;
   }
-  
+
   /* If I am congested then pass this message to peer before me in trail. */
   if(GNUNET_YES == GDS_ROUTING_threshold_reached())
   {
     struct GNUNET_PeerIdentity *new_trail;
     unsigned int new_trail_length;
-    
+
     if (trail_length == 1)
     {
       new_trail = NULL;
       new_trail_length = 0;
       next_hop = &source;
     }
-    else 
+    else
     {
       next_hop = &trail_peer_list[trail_length - 2];
       /* Remove myself from the trail. */
@@ -3166,29 +3221,29 @@
       new_trail = GNUNET_malloc (new_trail_length * sizeof (struct 
GNUNET_PeerIdentity));
       memcpy (new_trail, trail_peer_list, new_trail_length * sizeof (struct 
GNUNET_PeerIdentity));
     }
-    
+
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop);
-    GDS_NEIGHBOURS_send_trail_rejection (source, 
+    GDS_NEIGHBOURS_send_trail_rejection (source,
                                          ultimate_destination_finger_value,
                                          my_identity, finger_map_index,
                                          new_trail,new_trail_length,trail_id,
                                          target_friend, CONGESTION_TIMEOUT);
     return GNUNET_YES;
   }
-  
+
   /* Look for next_hop to pass the trail setup message */
-  next_hop = find_successor (ultimate_destination_finger_value, 
+  next_hop = find_successor (ultimate_destination_finger_value,
                              &next_destination,
                              &new_intermediate_trail_id,
                              finger_map_index);
-  
+
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity)))/* This 
means I am the final destination */
   {
     if (0 == trail_length)
       next_peer = source;
     else
       next_peer = trail_peer_list[trail_length-1];
-    
+
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_peer);
     GDS_NEIGHBOURS_send_trail_setup_result (source,
                                             my_identity,
@@ -3199,15 +3254,15 @@
   else
   {
     struct GNUNET_PeerIdentity peer_list[trail_length + 1];
-    
+
     memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct 
GNUNET_PeerIdentity));
     peer_list[trail_length] = my_identity;
-    
+
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop);
     GDS_NEIGHBOURS_send_trail_setup (source,
                                      ultimate_destination_finger_value,
-                                     next_destination, 
-                                     target_friend, trail_length + 1, 
peer_list, 
+                                     next_destination,
+                                     target_friend, trail_length + 1, 
peer_list,
                                      finger_map_index, trail_id,
                                      new_intermediate_trail_id);
   }
@@ -3222,7 +3277,7 @@
  * @param peer peer identity this notification is about
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-static int 
+static int
 handle_dht_p2p_trail_compression (void *cls, const struct GNUNET_PeerIdentity 
*peer,
                                const struct GNUNET_MessageHeader *message)
 {
@@ -3231,29 +3286,29 @@
   struct GNUNET_HashCode trail_id;
   struct FriendInfo *target_friend;
   size_t msize;
-  
+
   msize = ntohs (message->size);
   if (msize != sizeof (struct PeerTrailCompressionMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  
+
   trail_compression = (struct PeerTrailCompressionMessage *) message;
   trail_id = trail_compression->trail_id;
-  
+
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity 
(&(trail_compression->new_first_friend),
                                              &my_identity)))
   {
-     if(0 != (GNUNET_CRYPTO_cmp_peer_identity 
(&(trail_compression->destination_peer), 
+     if(0 != (GNUNET_CRYPTO_cmp_peer_identity 
(&(trail_compression->destination_peer),
                                                &my_identity)))
      {
-       GDS_ROUTING_update_trail_prev_hop (trail_id, 
+       GDS_ROUTING_update_trail_prev_hop (trail_id,
                                           trail_compression->source_peer);
      }
      return GNUNET_OK;
   }
-  
+
   next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
   if (NULL == next_hop)
   {
@@ -3262,7 +3317,7 @@
   }
   GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
   target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, next_hop);
-  GDS_NEIGHBOURS_send_trail_compression (trail_compression->source_peer, 
+  GDS_NEIGHBOURS_send_trail_compression (trail_compression->source_peer,
                                          trail_compression->destination_peer,
                                          trail_id,
                                          trail_compression->new_first_friend,
@@ -3272,13 +3327,13 @@
 
 
 /**
- * Core handler for trail teardown message. 
+ * Core handler for trail teardown message.
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-static int 
+static int
 handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity 
*peer,
                                const struct GNUNET_MessageHeader *message)
 {
@@ -3286,31 +3341,31 @@
   struct GNUNET_HashCode trail_id;
   enum GDS_ROUTING_trail_direction trail_direction;
   size_t msize;
-  
+
   msize = ntohs (message->size);
   if (msize != sizeof (struct PeerTrailTearDownMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  
+
   trail_teardown = (struct PeerTrailTearDownMessage *) message;
   trail_direction = ntohl (trail_teardown->trail_direction);
   trail_id = trail_teardown->TRAIL_ID;
-  
+
   if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
                                             &trail_teardown->destination_peer))
   {
     struct GNUNET_PeerIdentity *next_hop;
     struct FriendInfo *target_friend;
-    
+
     next_hop = GDS_ROUTING_get_next_hop (trail_id, trail_direction);
     if (NULL == next_hop)
     {
       GNUNET_break (0);
       return GNUNET_SYSERR;
     }
-    
+
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                        next_hop);
     GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
@@ -3326,15 +3381,15 @@
 /**
  * Fixme: this function is called only in case in notify new successor, the new
  * successor wants to add the source of the peer as its predecessor. Identify
- * if there is any other use case where it is required and if yes then adapt 
the 
- * code for it. 
- * Core handle for p2p add trail message. 
+ * if there is any other use case where it is required and if yes then adapt 
the
+ * code for it.
+ * Core handle for p2p add trail message.
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
-static int 
+static int
 handle_dht_p2p_add_trail (void *cls, const struct GNUNET_PeerIdentity *peer,
                           const struct GNUNET_MessageHeader *message)
 {
@@ -3347,17 +3402,17 @@
   unsigned int trail_length;
   unsigned int my_index;
   size_t msize;
-  
+
   msize = ntohs (message->size);
   if (msize != sizeof (struct PeerAddTrailMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  
+
   add_trail = (struct PeerAddTrailMessage *) message;
   trail_length = ntohl (add_trail->trail_length);
-  
+
   if ((msize < sizeof (struct PeerAddTrailMessage) +
                trail_length * sizeof (struct GNUNET_PeerIdentity)) ||
       (trail_length >
@@ -3366,29 +3421,29 @@
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  
+
   trail = (struct GNUNET_PeerIdentity *)&add_trail[1];
   destination_peer = add_trail->destination_peer;
   source_peer = add_trail->source_peer;
   trail_id = add_trail->trail_id;
-  
-  if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, 
+
+  if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
                                             &destination_peer))
   {
     struct FriendInfo *target_friend;
-    
+
     my_index = search_my_index (trail, trail_length);
     if (GNUNET_SYSERR == my_index)
     {
       GNUNET_break_op (0);
       return GNUNET_SYSERR;
     }
-    
+
     if (0 == my_index)
       next_hop = source_peer;
     else
       next_hop = trail[trail_length - 1];
- 
+
     GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, next_hop, *peer));
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop);
     GDS_NEIGHBOURS_send_add_trail (source_peer, destination_peer, trail_id,
@@ -3402,11 +3457,11 @@
  *FIXME; call send_trail_teardown_message on all the trails of the finger that
  * you remove. Also you don't need to decerement friend trail count as that
  * friend is removed. But you can not send trail teardown message as the friend
- * is disconnected. then you don't have any next_hop. and in case there are 
- * multiple trails. and friend is the first trail then you remove only the 
trail.  
+ * is disconnected. then you don't have any next_hop. and in case there are
+ * multiple trails. and friend is the first trail then you remove only the 
trail.
  * Iterate over finger_hashmap, and remove entries if finger is the 
disconnected
  * peer or if disconnected peer is the first friend in the trail to reach the
- * finger. 
+ * finger.
  * @param cls closure
  * @param key current public key
  * @param value value in the hash map
@@ -3423,32 +3478,32 @@
   const struct GNUNET_PeerIdentity *disconnected_peer = cls;
   struct TrailList *trail_list;
   int i;
-  
+
   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_finger->finger_identity,
                                             disconnected_peer))
   {
     GNUNET_assert (GNUNET_YES ==
                    GNUNET_CONTAINER_multihashmap32_remove (finger_hashmap,
-                                                         key, 
+                                                         key,
                                                          remove_finger));
     free_finger (remove_finger);
     return GNUNET_YES;
   }
-  
+
   for (i = 0; i< remove_finger->trails_count; i++)
   {
-    trail_list = &remove_finger->trail_list[i];  
+    trail_list = &remove_finger->trail_list[i];
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&trail_list->trail_head->peer,
                                                 disconnected_peer))
     {
       GNUNET_assert (GNUNET_YES ==
                      GNUNET_CONTAINER_multihashmap32_remove (finger_hashmap,
-                                                           key, 
+                                                           key,
                                                            remove_finger));
        free_finger (remove_finger);
     }
   }
-  
+
   return GNUNET_YES;
 }
 
@@ -3464,20 +3519,20 @@
                                          const struct GNUNET_PeerIdentity 
*peer)
 {
   struct FriendInfo *remove_friend;
-  
+
   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
     return;
 
   remove_friend =
       GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
-  
+
   if (NULL == remove_friend)
   {
     GNUNET_break (0);
     return;
   }
-  
-  GNUNET_assert (GNUNET_SYSERR != 
+
+  GNUNET_assert (GNUNET_SYSERR !=
                  GNUNET_CONTAINER_multihashmap32_iterate (finger_hashmap,
                                                           
&remove_matching_finger,
                                                           (void *)peer));
@@ -3486,10 +3541,10 @@
                  GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
                                                        peer,
                                                        remove_friend));
-  
+
   if (0 != GNUNET_CONTAINER_multipeermap_size (friend_peermap))
     return;
-  
+
   if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
   {
       GNUNET_SCHEDULER_cancel (find_finger_trail_task);
@@ -3514,22 +3569,22 @@
   /* Check for connect to self message */
   if (0 == memcmp (&my_identity, peer_identity, sizeof (struct 
GNUNET_PeerIdentity)))
     return;
-  
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to %s\n", GNUNET_i2s 
(peer_identity));
-  
+
   /* 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;
   }
-  
+
   GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), 1,
                             GNUNET_NO);
 
   friend = GNUNET_new (struct FriendInfo);
   friend->id = *peer_identity;
-  
+
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CONTAINER_multipeermap_put (friend_peermap,
                                                     peer_identity, friend,
@@ -3565,7 +3620,7 @@
   static struct GNUNET_CORE_MessageHandler core_handlers[] = {
     {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
     {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
-    {&handle_dht_p2p_get_result, GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT, 0},   
+    {&handle_dht_p2p_get_result, GNUNET_MESSAGE_TYPE_DHT_P2P_GET_RESULT, 0},
     {&handle_dht_p2p_trail_setup, GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP, 0},
     {&handle_dht_p2p_trail_setup_result, 
GNUNET_MESSAGE_TYPE_DHT_P2P_TRAIL_SETUP_RESULT, 0},
     {&handle_dht_p2p_verify_successor, 
GNUNET_MESSAGE_TYPE_DHT_P2P_VERIFY_SUCCESSOR, 0},
@@ -3577,17 +3632,17 @@
     {&handle_dht_p2p_add_trail, GNUNET_MESSAGE_TYPE_DHT_P2P_ADD_TRAIL, 0},
     {NULL, 0, 0}
   };
-  
+
   core_api =
     GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
                          &handle_core_disconnect, NULL, GNUNET_NO, NULL,
                          GNUNET_NO, core_handlers);
   if (NULL == core_api)
     return GNUNET_SYSERR;
-  
+
   friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
   finger_hashmap = GNUNET_CONTAINER_multihashmap32_create (MAX_FINGERS * 4/3);
-  
+
   return GNUNET_OK;
 }
 
@@ -3599,14 +3654,14 @@
 {
   if (NULL == core_api)
     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_multihashmap32_size (finger_hashmap));
   GNUNET_CONTAINER_multihashmap32_destroy (finger_hashmap);
   finger_hashmap = NULL;
@@ -3625,8 +3680,8 @@
  *
  * @return my identity
  */
-struct GNUNET_PeerIdentity 
+struct GNUNET_PeerIdentity
 GDS_NEIGHBOURS_get_my_id (void)
 {
   return my_identity;
-}
\ No newline at end of file
+}

Modified: gnunet/src/dht/gnunet-service-xdht_routing.c
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_routing.c        2014-05-30 10:51:55 UTC 
(rev 33445)
+++ gnunet/src/dht/gnunet-service-xdht_routing.c        2014-05-30 15:43:38 UTC 
(rev 33446)
@@ -29,15 +29,15 @@
 #include "gnunet-service-xdht.h"
 
 /**
- * Maximum number of entries in routing table. 
+ * Maximum number of entries in routing table.
  */
 #define ROUTING_TABLE_THRESHOLD 64
 
 
 /**
- * FIXME: do we need to store destination and source. 
+ * FIXME: do we need to store destination and source.
  * because in trail teardown we will reach destination but it will not find any
- * entry in routing table. so we should store destination and source. 
+ * entry in routing table. so we should store destination and source.
  * Routing table entry .
  */
 struct RoutingTrail
@@ -46,16 +46,16 @@
    * Global Unique identifier of the trail.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /**
    * The peer to which this request should be passed to.
    */
-  struct GNUNET_PeerIdentity next_hop;
-  
+  struct GNUNET_PeerIdentity next_hop; // change to struct FriendInfo *
+
   /**
-   * Peer just before next hop in the trail. 
+   * Peer just before next hop in the trail.
    */
-  struct GNUNET_PeerIdentity prev_hop;
+  struct GNUNET_PeerIdentity prev_hop;  // change to struct FriendInfo *
 };
 
 /**
@@ -69,19 +69,19 @@
  * your prev. hop.
  * @param trail_id
  * @return #GNUNET_OK success
- *         #GNUNET_SYSERR in case no matching entry found in routing table. 
+ *         #GNUNET_SYSERR in case no matching entry found in routing table.
  */
 int
 GDS_ROUTING_update_trail_prev_hop (const struct GNUNET_HashCode trail_id,
                                    struct GNUNET_PeerIdentity prev_hop)
 {
   struct RoutingTrail *trail;
-  
+
   trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
-  
+
   if (NULL == trail)
     return GNUNET_SYSERR;
-  
+
   trail->prev_hop = prev_hop;
   return GNUNET_OK;
 }
@@ -89,21 +89,21 @@
 
 /**
  * Get the next hop for trail corresponding to trail_id
- * @param trail_id Trail id to be searched. 
+ * @param trail_id Trail id to be searched.
  * @return Next_hop if found
- *         NULL If next hop not found. 
+ *         NULL If next hop not found.
  */
 struct GNUNET_PeerIdentity *
 GDS_ROUTING_get_next_hop (const struct GNUNET_HashCode trail_id,
                           enum GDS_ROUTING_trail_direction trail_direction)
 {
   struct RoutingTrail *trail;
- 
+
   trail = GNUNET_CONTAINER_multihashmap_get (routing_table, &trail_id);
 
   if (NULL == trail)
     return NULL;
-  
+
   switch (trail_direction)
   {
     case GDS_ROUTING_SRC_TO_DEST:
@@ -118,26 +118,26 @@
 /**
  * Remove trail with trail_id
  * @param trail_id Trail id to be removed
- * @return #GNUNET_YES success 
+ * @return #GNUNET_YES success
  *         #GNUNET_NO if entry not found.
  */
 int
 GDS_ROUTING_remove_trail (const struct GNUNET_HashCode remove_trail_id)
 {
   struct RoutingTrail *remove_entry;
-  
+
   remove_entry = GNUNET_CONTAINER_multihashmap_get (routing_table, 
&remove_trail_id);
-  
+
   if (NULL == remove_entry)
     return GNUNET_NO;
-  
+
   if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (routing_table,
-                                                          &remove_trail_id, 
+                                                          &remove_trail_id,
                                                           remove_entry))
   {
     GNUNET_free (remove_entry);
     return GNUNET_YES;
-  }  
+  }
   return GNUNET_NO;
 }
 
@@ -156,7 +156,7 @@
 {
   struct RoutingTrail *remove_trail = cls;
   struct GNUNET_PeerIdentity *peer = value;
-  
+
   if ((0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_trail->next_hop, peer)) ||
       (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_trail->prev_hop, peer)))
   {
@@ -165,8 +165,8 @@
                                                          
&remove_trail->trail_id,
                                                          remove_trail));
     GNUNET_free (remove_trail);
-  }  
-  return GNUNET_YES;  
+  }
+  return GNUNET_YES;
 }
 
 
@@ -176,7 +176,7 @@
  * that the trail is broken to any one who is part of trail. Should we 
communicate or
  * not. And if not then the cases where trail setup fails because next_hop = 
NULL
  * or something like that. VERY URGENT.
- * Remove every trail where peer is either next_hop or prev_hop 
+ * Remove every trail where peer is either next_hop or prev_hop
  * @param peer Peer to be searched.
  */
 void
@@ -197,31 +197,31 @@
  *                         but with different prev_hop/next_hop
  */
 int
-GDS_ROUTING_add (struct GNUNET_HashCode new_trail_id, 
+GDS_ROUTING_add (struct GNUNET_HashCode new_trail_id,
                  const struct GNUNET_PeerIdentity prev_hop,
                  const struct GNUNET_PeerIdentity next_hop)
 {
   struct RoutingTrail *new_entry;
-  
+
   new_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
   new_entry->trail_id = new_trail_id;
   new_entry->next_hop = next_hop;
   new_entry->prev_hop = prev_hop;
-  return GNUNET_CONTAINER_multihashmap_put (routing_table, 
+  return GNUNET_CONTAINER_multihashmap_put (routing_table,
                                             &new_trail_id, new_entry,
                                             
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
 }
 
 
 /**
- * Check if the size of routing table has crossed threshold. 
+ * Check if the size of routing table has crossed threshold.
  * @return #GNUNET_YES, if threshold crossed else #GNUNET_NO.
  */
 int
 GDS_ROUTING_threshold_reached (void)
 {
-  return (GNUNET_CONTAINER_multihashmap_size(routing_table) > 
-          ROUTING_TABLE_THRESHOLD) ? GNUNET_YES:GNUNET_NO;    
+  return (GNUNET_CONTAINER_multihashmap_size(routing_table) >
+          ROUTING_TABLE_THRESHOLD) ? GNUNET_YES:GNUNET_NO;
 }
 
 
@@ -230,7 +230,7 @@
  */
 void
 GDS_ROUTING_init (void)
-{ 
+{
   routing_table = GNUNET_CONTAINER_multihashmap_create 
(ROUTING_TABLE_THRESHOLD * 4 / 3,
                                                         GNUNET_NO);
 }
@@ -246,4 +246,4 @@
   GNUNET_CONTAINER_multihashmap_destroy (routing_table);
 }
 
-/* end of gnunet-service-xdht_routing.c */
\ No newline at end of file
+/* end of gnunet-service-xdht_routing.c */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]