gnunet-svn
[Top][All Lists]
Advanced

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

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


From: gnunet
Subject: [GNUnet-SVN] r34101 - gnunet/src/dht
Date: Fri, 1 Aug 2014 17:56:55 +0200

Author: bartpolot
Date: 2014-08-01 17:56:55 +0200 (Fri, 01 Aug 2014)
New Revision: 34101

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

Modified: gnunet/src/dht/gnunet-service-xdht_neighbours.c
===================================================================
--- gnunet/src/dht/gnunet-service-xdht_neighbours.c     2014-08-01 13:08:35 UTC 
(rev 34100)
+++ gnunet/src/dht/gnunet-service-xdht_neighbours.c     2014-08-01 15:56:55 UTC 
(rev 34101)
@@ -45,7 +45,7 @@
 #include "dht.h"
 
 /**
- * TODO: 
+ * TODO:
  * 1. In X-Vine paper, there is no policy defined for replicating the data to
  * recover in case of peer failure. We can do it in Chord way. In R5N, the key
  * is hashed and then data is stored according to the key value generated after
@@ -54,7 +54,7 @@
 
 
 /**
- * Maximum possible fingers (including predecessor) of a peer 
+ * Maximum possible fingers (including predecessor) of a peer
  */
 #define MAX_FINGERS 65
 
@@ -69,7 +69,7 @@
 #define DHT_FIND_FINGER_TRAIL_INTERVAL GNUNET_TIME_relative_multiply 
(GNUNET_TIME_UNIT_SECONDS, 2)
 
 /**
- * How long to wait before sending another verify successor message. 
+ * How long to wait before sending another verify successor message.
  */
 #define DHT_SEND_VERIFY_SUCCESSOR_INTERVAL GNUNET_TIME_relative_multiply 
(GNUNET_TIME_UNIT_SECONDS, 1)
 
@@ -79,9 +79,9 @@
 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
 
 /**
- * Duration for which I may remain congested. 
- * Note: Its a static value. In future, a peer may do some analysis and 
calculate 
- * congestion_timeout based on 'some' parameters. 
+ * Duration for which I may remain congested.
+ * Note: Its a static value. In future, a peer may do some analysis and 
calculate
+ * congestion_timeout based on 'some' parameters.
  */
 #define CONGESTION_TIMEOUT GNUNET_TIME_relative_multiply 
(GNUNET_TIME_UNIT_MINUTES, 2)
 
@@ -101,13 +101,13 @@
 #define PREDECESSOR_FINGER_ID 64
 
 /**
- * Wrap around in peer identity circle. 
+ * Wrap around in peer identity circle.
  */
 #define PEER_IDENTITES_WRAP_AROUND pow(2, 64) - 1
 
 /**
  * FIXME: Its use only at 3 places check if you can remove it.
- * To check if a finger is predecessor or not. 
+ * To check if a finger is predecessor or not.
  */
 enum GDS_NEIGHBOURS_finger_type
 {
@@ -144,7 +144,7 @@
 
   /**
    * Replication level for this message
-   * In the current implementation, this value is not used. 
+   * In the current implementation, this value is not used.
    */
   uint32_t desired_replication_level GNUNET_PACKED;
 
@@ -152,24 +152,24 @@
    * Length of the PUT path that follows (if tracked).
    */
   uint32_t put_path_length GNUNET_PACKED;
-  
-  /** 
+
+  /**
    * Best known destination (could be my friend or finger) which should
-   * get this message next. 
+   * get this message next.
    */
   struct GNUNET_PeerIdentity best_known_destination;
-  
+
   /**
    * In case best_known_destination is a finger, then trail to reach
-   * to that finger. Else its default value is 0. 
+   * to that finger. Else its default value is 0.
    */
   struct GNUNET_HashCode intermediate_trail_id;
-  
+
   /**
    * When does the content expire?
    */
   struct GNUNET_TIME_AbsoluteNBO expiration_time;
-  
+
   /**
    * The key to store the value under.
    */
@@ -178,7 +178,7 @@
   /* put path (if tracked) */
 
   /* Payload */
- 
+
 };
 
 /**
@@ -190,7 +190,7 @@
    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_GET
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
    * Processing options
    */
@@ -200,40 +200,40 @@
    * Desired content type.
    */
   uint32_t block_type GNUNET_PACKED;
-  
+
   /**
    * Hop count
    */
   uint32_t hop_count GNUNET_PACKED;
- 
+
   /**
    * Desired replication level for this request.
-   * In the current implementation, this value is not used. 
+   * In the current implementation, this value is not used.
    */
   uint32_t desired_replication_level GNUNET_PACKED;
-  
+
   /**
-   * Total number of peers in get path. 
+   * Total number of peers in get path.
    */
   unsigned int get_path_length;
-  
+
   /**
    * Best known destination (could be my friend or finger) which should
-   * get this message next. 
+   * get this message next.
    */
   struct GNUNET_PeerIdentity best_known_destination;
-  
+
   /**
    * In case best_known_destination is a finger, then trail to reach
-   * to that finger. Else its default value is 0. 
+   * to that finger. Else its default value is 0.
    */
   struct GNUNET_HashCode intermediate_trail_id;
- 
+
   /**
    * The key we are looking for.
    */
   struct GNUNET_HashCode key;
-  
+
   /* Get path. */
   /* struct GNUNET_PeerIdentity[]*/
 };
@@ -252,23 +252,23 @@
    * The type for the data.
    */
   uint32_t type GNUNET_PACKED;
-  
+
   /**
    * Number of peers recorded in the outgoing path from source to the
    * stored location of this message.
    */
   uint32_t put_path_length GNUNET_PACKED;
-  
+
   /**
    * Length of the GET path that follows (if tracked).
    */
   uint32_t get_path_length GNUNET_PACKED;
-  
+
   /**
-   * Peer which queried for get and should get the result. 
+   * Peer which queried for get and should get the result.
    */
   struct GNUNET_PeerIdentity querying_peer;
-  
+
   /**
    * When does the content expire?
    */
@@ -278,7 +278,7 @@
    * The key of the corresponding GET request.
    */
   struct GNUNET_HashCode key;
- 
+
   /* put path (if tracked) */
 
   /* get path (if tracked) */
@@ -296,12 +296,12 @@
    * Type: #GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP
    */
   struct GNUNET_MessageHeader header;
-  
+
   /**
    * Is source_peer trying to setup the trail to a predecessor or any finger.
    */
-  uint32_t is_predecessor; 
-  
+  uint32_t is_predecessor;
+
   /**
    * Peer closest to this value will be our finger.
    */
@@ -314,20 +314,24 @@
 
   /**
    * Best known destination (could be my friend or finger) which should
-   * get this message next. 
+   * get this message next.
+   *
+   * FIXME: this could be removed if we include trail_source / trail_dest
+   * in the routing table. This way we save 32 bytes of bandwidth by using
+   * extra 8 bytes of memory (2 * sizeof (GNUNET_PEER_ID))
    */
-  struct GNUNET_PeerIdentity best_known_destination; 
+  struct GNUNET_PeerIdentity best_known_destination;
 
   /**
    * In case best_known_destination is a finger, then trail id of trail to
    * reach to this finger.
    */
   struct GNUNET_HashCode intermediate_trail_id;
-  
+
   /**
    * Trail id for trail which we are trying to setup.
    */
-  struct GNUNET_HashCode trail_id; 
+  struct GNUNET_HashCode trail_id;
 
   /* List of peers which are part of trail setup so far.
    * Trail does NOT include source_peer and peer which will be closest to
@@ -355,27 +359,27 @@
   /**
    * Peer which started trail_setup to find trail to finger_identity
    */
-  struct GNUNET_PeerIdentity querying_peer; 
+  struct GNUNET_PeerIdentity querying_peer;
 
   /**
    * Is the trail setup to querying_peer's predecessor or finger?
    */
-  uint32_t is_predecessor; 
+  uint32_t is_predecessor;
 
   /**
-   * Value to which finger_identity is the closest peer. 
+   * Value to which finger_identity is the closest peer.
    */
   uint64_t ulitmate_destination_finger_value;
-  
+
   /**
    * Identifier of the trail from querying peer to finger_identity, NOT
-   * including both endpoints. 
+   * including both endpoints.
    */
   struct GNUNET_HashCode trail_id;
 
-  /* List of peers which are part of the trail from querying peer to 
+  /* List of peers which are part of the trail from querying peer to
    * finger_identity, NOT including both endpoints.
-   * struct GNUNET_PeerIdentity trail[] 
+   * struct GNUNET_PeerIdentity trail[]
    */
 };
 
@@ -404,9 +408,9 @@
    */
   struct GNUNET_HashCode trail_id;
 
-  /* List of the peers which are part of trail to reach  from source_peer 
+  /* List of the peers which are part of trail to reach  from source_peer
    * to successor, NOT including them
-   * struct GNUNET_PeerIdentity trail[] 
+   * struct GNUNET_PeerIdentity trail[]
    */
 };
 
@@ -433,7 +437,7 @@
   /**
    * Current Predecessor of source_successor. It can be same as querying peer
    * or different. In case it is different then it can be querying_peer's
-   * probable successor. 
+   * probable successor.
    */
   struct GNUNET_PeerIdentity probable_successor;
 
@@ -479,8 +483,8 @@
    */
   struct GNUNET_HashCode trail_id;
 
-  /* List of peers in trail from source_peer to new_successor, 
-   * NOT including the endpoints. 
+  /* List of peers in trail from source_peer to new_successor,
+   * NOT including the endpoints.
    * struct GNUNET_PeerIdentity trail[]
    */
 };
@@ -552,7 +556,7 @@
   struct GNUNET_PeerIdentity source_peer;
 
   /**
-   * Peer which sent trail rejection message as it it congested. 
+   * Peer which sent trail rejection message as it it congested.
    */
   struct GNUNET_PeerIdentity congested_peer;
 
@@ -571,7 +575,7 @@
    * Identifier for the trail that source peer is trying to setup.
    */
   struct GNUNET_HashCode trail_id;
-  
+
   /**
    * Relative time for which congested_peer will remain congested.
    */
@@ -661,7 +665,7 @@
 
   /**
    * Number of trails for which this friend is the first hop or if the friend
-   * is finger. 
+   * is finger.
    */
   unsigned int trails_count;
 
@@ -695,7 +699,7 @@
 /**
  * An individual element of the trail to reach to a finger.
  */
-struct Trail_Element 
+struct Trail_Element
 {
   /**
     * Pointer to next item in the list
@@ -714,9 +718,9 @@
 };
 
 /**
- * Information about an individual trail. 
+ * Information about an individual trail.
  */
-struct Trail 
+struct Trail
 {
   /**
    * Head of trail.
@@ -737,9 +741,9 @@
    * Length of trail pointed
    */
   unsigned int trail_length;
-  
+
   /**
-   * Is there a valid trail entry. 
+   * Is there a valid trail entry.
    */
   unsigned int is_present;
 };
@@ -755,10 +759,10 @@
   struct GNUNET_PeerIdentity finger_identity;
 
   /**
-   * Is any finger stored at this finger index. 
+   * Is any finger stored at this finger index.
    */
   unsigned int is_present;
-  
+
   /**
    * Index in finger peer map
    */
@@ -773,7 +777,7 @@
   /**
    * Array of trails to reach to this finger.
    */
-  struct Trail trail_list[MAXIMUM_TRAILS_PER_FINGER]; 
+  struct Trail trail_list[MAXIMUM_TRAILS_PER_FINGER];
 };
 
 
@@ -785,15 +789,15 @@
 struct Closest_Peer
 {
   /**
-   * Destination finger value. 
+   * Destination finger value.
    */
   uint64_t destination_finger_value;
-  
+
   /**
-   * Is finger_value a predecessor or any other finger. 
+   * Is finger_value a predecessor or any other finger.
    */
   unsigned int is_predecessor;
-  
+
   /**
    * Trail id to reach to peer.
    * In case peer is my identity or friend, it is set to 0.
@@ -805,7 +809,7 @@
    * In case of finger it is finger identity.
    */
   struct GNUNET_PeerIdentity best_known_destination;
-  
+
   /**
    * In case best_known_destination is a finger, then first friend in the trail
    * to reach to it. In other case, same as best_known_destination.
@@ -842,8 +846,8 @@
 static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
 
 /**
- * Task that sends verify successor message. This task is started when we get 
- * our successor for the first time. 
+ * Task that sends verify successor message. This task is started when we get
+ * our successor for the first time.
  */
 static GNUNET_SCHEDULER_TaskIdentifier send_verify_successor_task;
 
@@ -858,7 +862,7 @@
 static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
 
 /**
- * Array of all the fingers. 
+ * Array of all the fingers.
  */
 static struct FingerInfo finger_table [MAX_FINGERS];
 
@@ -902,7 +906,7 @@
   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))
@@ -970,7 +974,7 @@
     return;
   if (NULL != peer->th)
     return;
- 
+
   GNUNET_STATISTICS_update (GDS_stats,
                             gettext_noop
                             ("# Bytes of bandwidth requested from core"),
@@ -990,18 +994,18 @@
 /**
  * Construct a trail setup message and forward it to target_friend
  * @param source_peer Peer which wants to setup the trail
- * @param ultimate_destination_finger_value Peer identity closest to this 
value 
+ * @param ultimate_destination_finger_value Peer identity closest to this value
  *                                          will be finger to @a source_peer
  * @param best_known_destination Best known destination (could be finger or 
friend)
- *                               which should get this message. In case it is 
+ *                               which should get this message. In case it is
  *                               friend, then it is same as target_friend
  * @param target_friend Friend to which message is forwarded now.
  * @param trail_length Total number of peers in trail setup so far.
  * @param trail_peer_list Trail setup so far
  * @param is_predecessor Is @a source_peer looking for trail to a predecessor 
or not.
  * @param trail_id Unique identifier for the trail we are trying to setup.
- * @param intermediate_trail_id Trail id of intermediate trail to reach to 
- *                              best_known_destination when its a finger. If 
not 
+ * @param intermediate_trail_id Trail id of intermediate trail to reach to
+ *                              best_known_destination when its a finger. If 
not
  *                              used then set to 0.
  */
 void
@@ -1034,7 +1038,7 @@
     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->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   tsm = (struct PeerTrailSetupMessage *) &pending[1];
@@ -1053,7 +1057,7 @@
     peer_list = (struct GNUNET_PeerIdentity *) &tsm[1];
     memcpy (peer_list, trail_peer_list, trail_length * sizeof(struct 
GNUNET_PeerIdentity));
   }
- 
+
   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, 
pending);
   target_friend->pending_count++;
   process_friend_queue (target_friend);
@@ -1063,15 +1067,15 @@
 /**
  * Construct a trail setup result message and forward it to target friend.
  * @param querying_peer Peer which sent the trail setup request and should get
- *                      the result back. 
+ *                      the result back.
  * @param Finger Peer to which the trail has been setup to.
  * @param target_friend Friend to which this message should be forwarded.
  * @param trail_length Numbers of peers in the trail.
- * @param trail_peer_list Peers which are part of the trail from 
- *                        querying_peer to Finger, NOT including them. 
+ * @param trail_peer_list Peers which are part of the trail from
+ *                        querying_peer to Finger, NOT including them.
  * @param is_predecessor Is @a Finger predecessor to @a querying_peer ?
  * @param ultimate_destination_finger_value Value to which @a finger is the 
closest
- *                                          peer. 
+ *                                          peer.
  * @param trail_id Unique identifier of the trail.
  */
 void
@@ -1100,8 +1104,9 @@
 
   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
   {
-    GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped 
due to full queue"),
-                               1, GNUNET_NO);
+    GNUNET_STATISTICS_update (GDS_stats,
+                              gettext_noop ("# P2P messages dropped due to 
full queue"),
+                              1, GNUNET_NO);
   }
 
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
@@ -1115,14 +1120,12 @@
   tsrm->finger_identity = finger;
   tsrm->is_predecessor = htonl (is_predecessor);
   tsrm->trail_id = trail_id;
-  tsrm->ulitmate_destination_finger_value = 
+  tsrm->ulitmate_destination_finger_value =
           GNUNET_htonll (ultimate_destination_finger_value);
   peer_list = (struct GNUNET_PeerIdentity *) &tsrm[1];
 
-  if (trail_length > 0)
-  {
-    memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct 
GNUNET_PeerIdentity));
-  }
+  memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct 
GNUNET_PeerIdentity));
+
   /* Send the message to chosen friend. */
   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, 
pending);
   target_friend->pending_count++;
@@ -1133,7 +1136,7 @@
 /**
  * Send trail rejection message to target friend
  * @param source_peer Peer which is trying to setup the trail.
- * @param ultimate_destination_finger_value Peer closest to this value will be 
+ * @param ultimate_destination_finger_value Peer closest to this value will be
  *                                          @a source_peer's finger
  * @param congested_peer Peer which sent this message as it is congested.
  * @param is_predecessor Is source_peer looking for trail to a predecessor or 
not.
@@ -1161,7 +1164,7 @@
   struct P2PPendingMessage *pending;
   struct GNUNET_PeerIdentity *peer_list;
   size_t msize;
-  
+
   msize = sizeof (struct PeerTrailRejectionMessage) +
           (trail_length * sizeof (struct GNUNET_PeerIdentity));
 
@@ -1176,7 +1179,7 @@
     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped 
due to full queue"),
                                1, GNUNET_NO);
   }
-  
+
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
@@ -1189,7 +1192,7 @@
   trm->congestion_time = congestion_timeout;
   trm->is_predecessor = htonl (is_predecessor);
   trm->trail_id = trail_id;
-  trm->ultimate_destination_finger_value = 
+  trm->ultimate_destination_finger_value =
           GNUNET_htonll (ultimate_destination_finger_value);
 
   peer_list = (struct GNUNET_PeerIdentity *) &trm[1];
@@ -1197,7 +1200,7 @@
   {
     memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct 
GNUNET_PeerIdentity));
   }
-  
+
   /* Send the message to chosen friend. */
   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, 
pending);
   target_friend->pending_count++;
@@ -1210,11 +1213,11 @@
  * @param source_peer Peer which wants to verify its successor.
  * @param successor Peer which is @a source_peer's current successor.
  * @param trail_id Unique Identifier of trail from @a source_peer to @a 
successor,
- *                 NOT including them. 
+ *                 NOT including them.
  * @param trail List of peers which are part of trail to reach from @a 
source_peer
- *              to @a successor, NOT including them. 
+ *              to @a successor, NOT including them.
  * @param trail_length Total number of peers in @a trail.
- * @param target_friend Next friend to get this message. 
+ * @param target_friend Next friend to get this message.
  */
 void
 GDS_NEIGHBOURS_send_verify_successor_message (struct GNUNET_PeerIdentity 
source_peer,
@@ -1242,7 +1245,7 @@
     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped 
due to full queue"),
                                1, GNUNET_NO);
   }
-  
+
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;    /* FIXME */
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
@@ -1253,7 +1256,7 @@
   vsm->source_peer = source_peer;
   vsm->successor = successor;
   vsm->trail_id = trail_id;
-  
+
   if (trail_length != 0)
   {
     peer_list = (struct GNUNET_PeerIdentity *) &vsm[1];
@@ -1271,8 +1274,8 @@
  * FIXME: In every function we pass target friend except for this one.
  * so, either change everything or this one. also, should se just store
  * the pointer to friend in routing table rather than gnunet_peeridentity.
- * if yes then we should keep friend info in.h  andmake lot of changes. 
- * Construct a trail teardown message and forward it to target friend. 
+ * if yes then we should keep friend info in.h  andmake lot of changes.
+ * Construct a trail teardown message and forward it to target friend.
  * @param trail_id Unique identifier of the trail.
  * @param trail_direction Direction of trail.
  * @param target_friend Friend to get this message.
@@ -1294,12 +1297,12 @@
     GNUNET_break (0);
     return;
   }
-  
+
   /*FIXME:In what case friend can be null. ?*/
-  if (NULL == (target_friend = 
+  if (NULL == (target_friend =
                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, &peer)));
   return;
-  
+
   if (target_friend->pending_count >= MAXIMUM_PENDING_PER_FRIEND)
   {
     GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped 
due to full queue"),
@@ -1325,18 +1328,18 @@
 
 /**
  * Construct a verify successor result message and send it to target_friend
- * @param querying_peer Peer which sent the verify successor message. 
- * @param source_successor Current_successor of @a querying_peer. 
+ * @param querying_peer Peer which sent the verify successor message.
+ * @param source_successor Current_successor of @a querying_peer.
  * @param current_predecessor Current predecessor of @a successor. Could be 
same
  *                            or different from @a querying_peer.
- * @param trail_id Unique identifier of the trail from @a querying_peer to 
+ * @param trail_id Unique identifier of the trail from @a querying_peer to
  *                 @a successor, NOT including them.
- * @param trail List of peers which are part of trail from @a querying_peer to 
+ * @param trail List of peers which are part of trail from @a querying_peer to
  *                 @a successor, NOT including them.
  * @param trail_length Total number of peers in @a trail
  * @param trail_direction Direction in which we are sending the message. In 
this
  *                        case we are sending result from @a successor to @a 
querying_peer.
- * @param target_friend Next friend to get this message. 
+ * @param target_friend Next friend to get this message.
  */
 void
 GDS_NEIGHBOURS_send_verify_successor_result (struct GNUNET_PeerIdentity 
querying_peer,
@@ -1380,7 +1383,7 @@
   vsmr->probable_successor = probable_successor;
   vsmr->trail_direction = htonl (trail_direction);
   vsmr->trail_id = trail_id;
-  
+
   if (trail_length > 0)
   {
     peer_list = (struct GNUNET_PeerIdentity *) &vsmr[1];
@@ -1397,14 +1400,14 @@
 /**
  * Construct a notify new successor message and send it to target_friend
  * @param source_peer Peer which wants to notify to its new successor that it
- *                    could be its predecessor. 
+ *                    could be its predecessor.
  * @param successor New successor of @a source_peer
- * @param successor_trail List of peers in Trail to reach from 
- *                            @a source_peer to @a new_successor, NOT 
including 
- *                            the endpoints. 
+ * @param successor_trail List of peers in Trail to reach from
+ *                            @a source_peer to @a new_successor, NOT including
+ *                            the endpoints.
  * @param successor_trail_length Total number of peers in @a 
new_successor_trail.
- * @param successor_trail_id Unique identifier of @a new_successor_trail. 
- * @param target_friend Next friend to get this message. 
+ * @param successor_trail_id Unique identifier of @a new_successor_trail.
+ * @param target_friend Next friend to get this message.
  */
 void
 GDS_NEIGHBOURS_send_notify_new_successor (struct GNUNET_PeerIdentity 
source_peer,
@@ -1444,7 +1447,7 @@
   nsm->new_successor = successor;
   nsm->source_peer = source_peer;
   nsm->trail_id = succesor_trail_id;
-  
+
   if (successor_trail_length > 0)
   {
     peer_list = (struct GNUNET_PeerIdentity *) &nsm[1];
@@ -1463,10 +1466,10 @@
  * 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 identifier of the trail from 
+ * @param trail_id Unique identifier of the trail from
  *                 @a source_peer to @a destination_peer, NOT including the 
endpoints.
  * @param trail List of peers in Trail from @a source_peer to @a 
destination_peer,
- *              NOT including the endpoints. 
+ *              NOT including the endpoints.
  * @param trail_length Total number of peers in @a trail.
  * @param target_friend Next friend to get this message.
  */
@@ -1508,7 +1511,7 @@
   adm->source_peer = source_peer;
   adm->destination_peer = destination_peer;
   adm->trail_id = trail_id;
-  
+
   if (trail_length > 0)
   {
     peer_list = (struct GNUNET_PeerIdentity *)&adm[1];
@@ -1553,7 +1556,7 @@
                               gettext_noop ("# P2P messages dropped due to 
full queue"),
                                                      1, GNUNET_NO);
   }
- 
+
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->importance = 0;    /* FIXME */
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
@@ -1585,7 +1588,7 @@
 {
   int i;
   int lowest_index = -1;
-  
+
   for (i = 0; i < trail_length; i++)
   {
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &trail[i]))
@@ -1599,15 +1602,15 @@
 
 /**
  * Check if the friend is congested or have reached maximum number of trails
- * it can be part of of. 
+ * it can be part of of.
  * @param friend Friend to be checked.
  * @return #GNUNET_NO if friend is not congested or have not crossed threshold.
- *         #GNUNET_YES if friend is either congested or have crossed threshold 
+ *         #GNUNET_YES if friend is either congested or have crossed threshold
  */
 static int
 is_friend_congested (struct FriendInfo *friend)
 {
-  if ((TRAILS_THROUGH_FRIEND_THRESHOLD > friend->trails_count) && 
+  if ((TRAILS_THROUGH_FRIEND_THRESHOLD > friend->trails_count) &&
       ((0 == GNUNET_TIME_absolute_get_remaining
              (friend->congestion_timestamp).rel_value_us)))
     return GNUNET_NO;
@@ -1617,36 +1620,35 @@
 
 
 /**
- * FIXME; not handling the wrap around logic correctly. 
  * Select closest finger to value.
  * @param peer1 First peer
  * @param peer2 Second peer
  * @param value Value to be compare
  * @return Closest peer
  */
-static struct GNUNET_PeerIdentity *
-select_closest_finger (struct GNUNET_PeerIdentity *peer1,
-                       struct GNUNET_PeerIdentity *peer2,
+const static struct GNUNET_PeerIdentity *
+select_closest_finger (const struct GNUNET_PeerIdentity *peer1,
+                       const struct GNUNET_PeerIdentity *peer2,
                        uint64_t value)
 {
   uint64_t peer1_value;
   uint64_t peer2_value;
-  
+
   memcpy (&peer1_value, peer1, sizeof (uint64_t));
   memcpy (&peer2_value, peer2, sizeof (uint64_t));
   peer1_value = GNUNET_ntohll (peer1_value);
   peer2_value = GNUNET_ntohll (peer2_value);
-  
+
   if (peer1_value == value)
   {
     return peer1;
   }
-  
+
   if (peer2_value == value)
   {
     return peer2;
   }
-   
+
   if (peer2_value < peer1_value)
   {
     if ((peer2_value < value) && (value < peer1_value))
@@ -1658,8 +1660,8 @@
     {
       return peer2;
     }
-  }  
-   
+  }
+
   if (peer1_value < peer2_value)
   {
     if ((peer1_value < value) && (value < peer2_value))
@@ -1683,25 +1685,25 @@
  * @param value Value to be compare
  * @return Peer which precedes value in the network.
  */
-static struct GNUNET_PeerIdentity *
-select_closest_predecessor (struct GNUNET_PeerIdentity *peer1,
-                            struct GNUNET_PeerIdentity *peer2,
+const static struct GNUNET_PeerIdentity *
+select_closest_predecessor (const struct GNUNET_PeerIdentity *peer1,
+                            const struct GNUNET_PeerIdentity *peer2,
                             uint64_t value)
 {
   uint64_t peer1_value;
   uint64_t peer2_value;
-  
+
   memcpy (&peer1_value, peer1, sizeof (uint64_t));
   memcpy (&peer2_value, peer2, sizeof (uint64_t));
   peer1_value = GNUNET_ntohll (peer1_value);
   peer2_value = GNUNET_ntohll (peer2_value);
-  
+
   if (peer1_value == value)
     return peer1;
-  
+
   if (peer2_value == value)
     return peer2;
-  
+
   if (peer1_value < peer2_value)
   {
     if ((peer1_value < value) && (value < peer2_value))
@@ -1714,7 +1716,7 @@
       return peer2;
     }
   }
-  
+
   if (peer2_value < peer1_value)
   {
     if ((peer2_value < value) && (value < peer1_value))
@@ -1742,11 +1744,11 @@
   struct GNUNET_PeerIdentity print_peer;
   struct GNUNET_PeerIdentity key_ret;
   int i;
-  
+
   print_peer = my_identity;
   FPRINTF (stderr,_("\nSUPU************  FRIEND_PEERMAP of 
%s"),GNUNET_i2s(&print_peer));
   friend_iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
-  
+
   for (i = 0; i < GNUNET_CONTAINER_multipeermap_size (friend_peermap); i++)
   {
     if(GNUNET_YES == GNUNET_CONTAINER_multipeermap_iterator_next (friend_iter,
@@ -1777,14 +1779,14 @@
   for (i = 0; i < MAX_FINGERS; i++)
   {
     finger = &finger_table[i];
-    
+
     if (GNUNET_NO == finger->is_present)
       continue;
-    
+
     print_peer = finger->finger_identity;
     FPRINTF (stderr,_("\nSUPU %s, %s, %d, finger_table[%d] = %s, trails_count 
= %d"),
             __FILE__, __func__,__LINE__,i,GNUNET_i2s (&print_peer), 
finger->trails_count);
-    
+
 #if 0
     for (j = 0; j < finger->trails_count; j++)
     {
@@ -1793,7 +1795,7 @@
       struct Trail_Element *element;
       element = trail->trail_head;
       for (k = 0; k < trail->trail_length; k++)
-      {  
+      {
         print_peer = element->peer;
         FPRINTF (stderr,_("\nSUPU %s, %s, %d,trail[%d] = %s "),__FILE__, 
__func__,__LINE__,k, GNUNET_i2s(&print_peer));
         element = element->next;
@@ -1811,27 +1813,19 @@
  * @param peer1 First peer
  * @param peer2 Second peer
  * @param value Value relative to which we find the closest
- * @param is_predecessor Is value a predecessor or any other finger. 
+ * @param is_predecessor Is value a predecessor or any other finger.
  * @return Closest peer among two peers.
  */
-static struct GNUNET_PeerIdentity *
-select_closest_peer (struct GNUNET_PeerIdentity *peer1,
-                     struct GNUNET_PeerIdentity *peer2,
+const static struct GNUNET_PeerIdentity *
+select_closest_peer (const struct GNUNET_PeerIdentity *peer1,
+                     const struct GNUNET_PeerIdentity *peer2,
                      uint64_t value,
                      unsigned int is_predecessor)
 {
-  struct GNUNET_PeerIdentity *closest_peer;
-  
   if (1 == is_predecessor)
-  {
-    closest_peer = select_closest_predecessor (peer1, peer2, value);
-    
-  }
-  else
-  {
-    closest_peer = select_closest_finger (peer1, peer2, value);
-  }
-  return closest_peer;
+    return select_closest_predecessor (peer1, peer2, value);
+
+  return select_closest_finger (peer1, peer2, value);
 }
 
 
@@ -1841,7 +1835,7 @@
  * then don't select it. In case there multiple available good trails to reach
  * to Finger, choose the one with shortest trail length.
  * Note: We use length as parameter. But we can use any other suitable 
parameter
- * also. 
+ * also.
  * @param finger Finger
  * @return struct Selected_Finger_Trail which contains the first friend , 
trail id
  * and trail length. NULL in case none of the trails are free.
@@ -1855,36 +1849,36 @@
   unsigned int i;
   unsigned int flag = 0;
   unsigned int j = 0;
- 
+
   finger_trail = GNUNET_new (struct Selected_Finger_Trail);
   GNUNET_assert (finger->trails_count > 0);
-  
+
   for (i = 0; i < finger->trails_count; i++)
   {
     iterator = &finger->trail_list[i];
-    
+
     /* No trail stored at this index. */
     if (GNUNET_NO == iterator->is_present)
       continue;
 
-    GNUNET_assert (NULL != 
-                  (friend = 
+    GNUNET_assert (NULL !=
+                  (friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                       
&iterator->trail_head->peer)));
-    
+
     /* First friend to reach trail is not free. */
     if (GNUNET_YES == is_friend_congested (friend))
     {
       j++;
       continue;
     }
-    
+
     if (!flag)
     {
       flag = 1;
       finger_trail->trail_length = iterator->trail_length;
       finger_trail->friend = *friend;
-      finger_trail->trail_id = iterator->trail_id;   
+      finger_trail->trail_id = iterator->trail_id;
     }
     else if (finger_trail->trail_length > iterator->trail_length)
     {
@@ -1894,50 +1888,52 @@
     }
   }
 
-  /* All the first friend in all the trails to reach to finger are either 
+  /* All the first friend in all the trails to reach to finger are either
    congested or have crossed trail threshold. */
   if (j == finger->trails_count)
     return NULL;
-  
+
   return finger_trail;
 }
 
 
 /**
  * Compare FINGER entry with current successor. If finger's first friend of all
- * its trail is not congested and  has not crossed trail threshold, then check 
+ * its trail is not congested and  has not crossed trail threshold, then check
  * if finger peer identity is closer to final_destination_finger_value than
- * current_successor. If yes then update current_successor. 
+ * current_successor. If yes then update current_successor.
  * @param current_successor[in/out]
- * @return 
+ * @return
  */
 static void
 compare_finger_and_current_successor (struct Closest_Peer 
*current_closest_peer)
 {
   struct FingerInfo *finger;
-  struct GNUNET_PeerIdentity *closest_peer;
+  const struct GNUNET_PeerIdentity *closest_peer;
   struct Selected_Finger_Trail *finger_trail;
   int i;
-  
+
   /* Iterate over finger table. */
   for (i = 0; i < MAX_FINGERS; i++)
   {
     finger = &finger_table[i];
-    
+
     if (GNUNET_NO == finger->is_present)
       continue;
 
-    /* If my identity is same as current closest peer then don't consider me*/
-    if (0 == 
-            GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
-                                             
&current_closest_peer->best_known_destination))
+    /* FIXME write correct comment here */
+    if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
+                                              
&current_closest_peer->best_known_destination))
       continue;
-    
+
     /* If I am my own finger, then ignore this finger. */
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
                                               &my_identity))
+    {
+      GNUNET_break (0);
       continue;
-    
+    }
+
     /* If finger is a friend, then do nothing. As we have already checked
      * for each friend in compare_friend_and_current_successor(). */
     if (NULL != (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
@@ -1945,22 +1941,21 @@
     {
       continue;
     }
-    
-    /* Choose one of the trail to reach to finger. */
-    finger_trail = select_finger_trail (finger);
-    
-    /* In case no trail found, ignore this finger. */
-    if (NULL == finger_trail)
-      continue;
 
-    closest_peer = select_closest_peer (&finger->finger_identity, 
+    closest_peer = select_closest_peer (&finger->finger_identity,
                                         
&current_closest_peer->best_known_destination,
                                         
current_closest_peer->destination_finger_value,
                                         current_closest_peer->is_predecessor);
-    
-    if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
-                                              closest_peer))
+
+    if (&finger->finger_identity == closest_peer)
     {
+      /* Choose one of the trail to reach to finger. */
+      finger_trail = select_finger_trail (finger);
+
+      /* In case no trail found, ignore this finger. */
+      if (NULL == finger_trail)
+        continue;
+
       current_closest_peer->best_known_destination = finger->finger_identity;
       current_closest_peer->next_hop = finger_trail->friend.id;
       current_closest_peer->trail_id = finger_trail->trail_id;
@@ -1973,10 +1968,10 @@
 
 /**
  * Compare friend entry with current successor.
- * If friend identity and current_successor is same, then do nothing. 
- * If friend is not congested and has not crossed trail threshold, then check 
- * if friend peer identity is closer to final_destination_finger_value than 
- * current_successor. If yes then update current_successor. 
+ * If friend identity and current_successor is same, then do nothing.
+ * If friend is not congested and has not crossed trail threshold, then check
+ * if friend peer identity is closer to final_destination_finger_value than
+ * current_successor. If yes then update current_successor.
  * @param cls closure
  * @param key current public key
  * @param value struct Closest_Peer
@@ -1990,30 +1985,32 @@
 {
   struct FriendInfo *friend = value;
   struct Closest_Peer *current_closest_peer = cls;
-  struct GNUNET_PeerIdentity *closest_peer;
-  
+  const struct GNUNET_PeerIdentity *closest_peer;
+
   /* Friend is either congested or has crossed threshold. */
   if (GNUNET_YES == is_friend_congested (friend))
     return GNUNET_YES;
- 
+
   /* If current_closest_peer and friend identity are same, then do nothing.*/
-  if (0 == 
-        GNUNET_CRYPTO_cmp_peer_identity (&friend->id,
-                                         
&current_closest_peer->best_known_destination))
+  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&friend->id,
+                                            
&current_closest_peer->best_known_destination))
+  {
+    GNUNET_break (0);
     return GNUNET_YES;
+  }
 
-  closest_peer = select_closest_peer (&friend->id, 
+  closest_peer = select_closest_peer (&friend->id,
                                       
&current_closest_peer->best_known_destination,
                                       
current_closest_peer->destination_finger_value,
                                       current_closest_peer->is_predecessor);
 
   /* Is friend the closest successor? */
-  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&friend->id, closest_peer))
+  if (&friend->id == closest_peer)
   {
     current_closest_peer->best_known_destination = friend->id;
     current_closest_peer->next_hop = friend->id;
   }
-  
+
   return GNUNET_YES;
 }
 
@@ -2023,19 +2020,19 @@
  * @param my_identity My peer identity
  * @return Updated closest_peer
  */
-static struct Closest_Peer 
+static struct Closest_Peer
 init_current_successor (struct GNUNET_PeerIdentity my_identity,
                         uint64_t destination_finger_value,
                         unsigned int is_predecessor)
 {
   struct Closest_Peer current_closest_peer;
-  
+
   memset (&current_closest_peer.trail_id, 0, sizeof(struct GNUNET_HashCode));
   current_closest_peer.destination_finger_value = destination_finger_value;
   current_closest_peer.is_predecessor = is_predecessor;
   current_closest_peer.next_hop = my_identity;
   current_closest_peer.best_known_destination = my_identity;
-  
+
   return current_closest_peer;
 }
 
@@ -2043,13 +2040,13 @@
 /**
  * FIXME: at the moment, there is not 100% get and put in case of non-malicious
  * peer. It could be because of the logic we wrote here. Verify if its correct.
- * If not then return immediate_successor. 
- * 
+ * If not then return immediate_successor.
+ *
  * Find the successor for destination_finger_value among my_identity, my
  * friends and my fingers. Don't consider friends or fingers which are either
  * congested or have crossed the threshold.
  * NOTE: In case a friend is also a finger, then it is always chosen as friend
- * not a finger. 
+ * not a finger.
  * @param destination_finger_value Peer closest to this value will be the next 
successor.
  * @param is_predecessor Are we looking for predecessor or finger?
  * @return Successor It is never NULL, in case none of friend or finger is 
closest,
@@ -2060,24 +2057,24 @@
                 unsigned int is_predecessor)
 {
   struct Closest_Peer current_closest_peer;
-  
+
    /* Initialize current_successor to my_identity. */
   current_closest_peer = init_current_successor (my_identity,
                                                  destination_finger_value,
                                                  is_predecessor);
- 
+
   /* Compare each friend entry with current_successor and update 
current_successor
    * with friend if its closest. */
-  GNUNET_assert 
-          (GNUNET_SYSERR != 
-           GNUNET_CONTAINER_multipeermap_iterate (friend_peermap, 
+  GNUNET_assert
+          (GNUNET_SYSERR !=
+           GNUNET_CONTAINER_multipeermap_iterate (friend_peermap,
                                                   
&compare_friend_and_current_closest_peer,
                                                   &current_closest_peer));
-   
+
   /* Compare each finger entry with current_successor and update 
current_successor
    * with finger if its closest. */
   compare_finger_and_current_successor (&current_closest_peer);
- 
+
   return current_closest_peer;
 }
 
@@ -2089,8 +2086,8 @@
  * @param options Routing options
  * @param desired_replication_level Desired replication count
  * @param best_known_dest Peer to which this message should reach eventually,
- *                        as it is best known destination to me. 
- * @param intermediate_trail_id Trail id in case 
+ *                        as it is best known destination to me.
+ * @param intermediate_trail_id Trail id in case
  * @param target_peer Peer to which this message will be forwarded.
  * @param hop_count Number of hops traversed so far.
  * @param put_path_length Total number of peers in @a put_path
@@ -2120,10 +2117,10 @@
   struct GNUNET_PeerIdentity next_hop;
 
   size_t msize;
-  
+
   msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
           sizeof (struct PeerPutMessage);
-  
+
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
   {
     put_path_length = 0;
@@ -2135,7 +2132,7 @@
     GNUNET_break (0);
     return;
   }
-  
+
   /* This is the first call made from clients file. So, we should search for 
the
      target_friend. */
   if (NULL == target_peer)
@@ -2143,33 +2140,33 @@
     uint64_t key_value;
     struct Closest_Peer successor;
 
-    memcpy (&key_value, key, sizeof (uint64_t));  
+    memcpy (&key_value, key, sizeof (uint64_t));
     key_value = GNUNET_ntohll (key_value);
-    
+
     successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
     best_known_dest = successor.best_known_destination;
     next_hop = successor.next_hop;
     intermediate_trail_id = successor.trail_id;
 
-    if (0 == GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)) 
+    if (0 == GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity))
     {
       /* I am the destination. */
-      GDS_DATACACHE_handle_put (expiration_time, key, 0, NULL, 
+      GDS_DATACACHE_handle_put (expiration_time, key, 0, NULL,
                                 ntohl (block_type),data_size,data);
       return;
     }
     else
-      GNUNET_assert (NULL != 
-                    (target_friend = 
-                     GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop)));  
+      GNUNET_assert (NULL !=
+                    (target_friend =
+                     GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop)));
   }
   else
   {
     GNUNET_assert (NULL !=
-                   (target_friend = 
-                   GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
target_peer))); 
+                   (target_friend =
+                   GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
target_peer)));
   }
-  
+
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->timeout = expiration_time;
   ppm = (struct PeerPutMessage *) &pending[1];
@@ -2184,7 +2181,7 @@
   ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
   ppm->best_known_destination = best_known_dest;
   ppm->key = *key;
-  
+
   pp = (struct GNUNET_PeerIdentity *) &ppm[1];
   if (put_path_length != 0)
   {
@@ -2192,7 +2189,7 @@
             sizeof (struct GNUNET_PeerIdentity) * put_path_length);
   }
   memcpy (&pp[put_path_length], data, data_size);
-  
+
   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, 
pending);
   target_friend->pending_count++;
   process_friend_queue (target_friend);
@@ -2233,10 +2230,10 @@
   struct FriendInfo *target_friend;
   struct GNUNET_PeerIdentity *gp;
   size_t msize;
-  
-  msize = sizeof (struct PeerGetMessage) + 
+
+  msize = sizeof (struct PeerGetMessage) +
           (get_path_length * sizeof (struct GNUNET_PeerIdentity));
-  
+
   /* In this case we don't make get_path_length = 0, as we need get path to
    * return the message back to querying client. */
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
@@ -2244,45 +2241,45 @@
     GNUNET_break (0);
     return;
   }
-  
+
   /* This is the first time we got request from our own client file. */
   if (NULL == target_peer)
   {
     uint64_t key_value;
     struct Closest_Peer successor;
-    
-    memcpy (&key_value, key, sizeof (uint64_t)); 
+
+    memcpy (&key_value, key, sizeof (uint64_t));
     key_value = GNUNET_ntohll (key_value);
     successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
-    
+
     best_known_dest = successor.best_known_destination;
     intermediate_trail_id = successor.trail_id;
-    
+
     /* I am the destination. I have the data. */
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
-                                              &best_known_dest)) 
+                                              &best_known_dest))
     {
-      GDS_DATACACHE_handle_get (key,block_type, NULL, 0, 
+      GDS_DATACACHE_handle_get (key,block_type, NULL, 0,
                                 NULL, 0, 1, &my_identity, NULL,&my_identity);
-      
+
       return;
     }
     else
     {
-      GNUNET_assert (NULL != 
-                    (target_friend = 
-                     GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+      GNUNET_assert (NULL !=
+                    (target_friend =
+                     GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                         &successor.next_hop)));
     }
-    
+
   }
   else
   {
-    GNUNET_assert (NULL != 
-                  (target_friend = 
+    GNUNET_assert (NULL !=
+                  (target_friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
target_peer))); //FIXME: assertion fails.
   }
-  
+
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
   pending->importance = 0;    /* FIXME */
@@ -2296,12 +2293,12 @@
   pgm->intermediate_trail_id = intermediate_trail_id;
   pgm->hop_count = htonl (hop_count + 1);
   gp = (struct GNUNET_PeerIdentity *) &pgm[1];
-  
+
   if (get_path_length != 0)
   {
     memcpy (gp, get_path, get_path_length * sizeof (struct 
GNUNET_PeerIdentity));
   }
-  
+
   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, 
pending);
   target_friend->pending_count++;
   process_friend_queue (target_friend);
@@ -2341,7 +2338,7 @@
   int current_path_index;
   size_t msize;
 
-  msize = (put_path_length + get_path_length )* sizeof (struct 
GNUNET_PeerIdentity) + 
+  msize = (put_path_length + get_path_length )* sizeof (struct 
GNUNET_PeerIdentity) +
           data_size +
           sizeof (struct PeerGetResultMessage);
 
@@ -2350,7 +2347,7 @@
     GNUNET_break (0);
     return;
   }
-  
+
   current_path_index = 0;
   if(get_path_length > 0)
   {
@@ -2363,7 +2360,7 @@
   }
   if (0 == current_path_index)
   {
-    GDS_CLIENTS_handle_reply (expiration, key, get_path_length, 
+    GDS_CLIENTS_handle_reply (expiration, key, get_path_length,
                               get_path, put_path_length,
                               put_path, type, data_size, data);
     return;
@@ -2371,7 +2368,7 @@
 
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
   pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
-  pending->importance = 0;   
+  pending->importance = 0;
   get_result = (struct PeerGetResultMessage *)&pending[1];
   pending->msg = &get_result->header;
   get_result->header.size = htons (msize);
@@ -2387,10 +2384,10 @@
   memcpy (&paths[put_path_length], get_path,
           get_path_length * sizeof (struct GNUNET_PeerIdentity));
   memcpy (&paths[put_path_length + get_path_length], data, data_size);
-  
-  GNUNET_assert (NULL != 
-                (target_friend = 
-                 GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+
+  GNUNET_assert (NULL !=
+                (target_friend =
+                 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                     
&get_path[current_path_index - 1])));
   GNUNET_CONTAINER_DLL_insert_tail (target_friend->head, target_friend->tail, 
pending);
   target_friend->pending_count++;
@@ -2416,7 +2413,7 @@
   struct FriendInfo *friend;
 
   current_size = GNUNET_CONTAINER_multipeermap_size (friend_peermap);
-  
+
   /* No friends.*/
   if (0 == current_size)
     return NULL;
@@ -2438,7 +2435,7 @@
       iter = GNUNET_CONTAINER_multipeermap_iterator_create (friend_peermap);
 
     }
-    
+
     /* Get the friend stored at the index, j*/
     GNUNET_assert (GNUNET_YES ==
                    GNUNET_CONTAINER_multipeermap_iterator_next (iter,
@@ -2461,8 +2458,8 @@
 
 
 /**
- * Compute 64 bit value of finger_identity corresponding to a finger index 
using 
- * chord formula. 
+ * Compute 64 bit value of finger_identity corresponding to a finger index 
using
+ * chord formula.
  * For all fingers, n.finger[i] = n + pow (2,i),
  * For predecessor, n.finger[PREDECESSOR_FINGER_ID] = n - 1, where
  * n = my_identity, i = finger_index, n.finger[i] = 64 bit finger value
@@ -2476,20 +2473,20 @@
 
   memcpy (&my_id64, &my_identity, sizeof (uint64_t));
   my_id64 = GNUNET_ntohll (my_id64);
-  
+
   /* Are we looking for immediate predecessor? */
   if (PREDECESSOR_FINGER_ID == finger_index)
     return (my_id64 - 1);
   else
   {
-    uint64_t add = (uint64_t)1 << finger_index;  
+    uint64_t add = (uint64_t)1 << finger_index;
     return (my_id64 + add);
   }
 }
 
 
 /*
- * Choose a random friend. Calculate the next finger identity to search,from 
+ * Choose a random friend. Calculate the next finger identity to search,from
  * current_search_finger_index. Start looking for the trail to reach to
  * finger identity through this random friend.
  *
@@ -2520,29 +2517,29 @@
    * in their routing table).  */
   if (GNUNET_YES == GDS_ROUTING_threshold_reached())
     return;
-  
-    
+
+
   target_friend = select_random_friend ();
   if (NULL == target_friend)
   {
     return;
   }
-  
+
   finger_id_value = compute_finger_identity_value 
(current_search_finger_index);
-  
+
   if (PREDECESSOR_FINGER_ID == current_search_finger_index)
     is_predecessor = 1;
   else
     is_predecessor = 0;
-  
+
   /* Generate a unique trail id for trail we are trying to setup. */
   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
                               &trail_id, sizeof (trail_id));
   memset(&intermediate_trail_id, 0, sizeof (struct GNUNET_HashCode));
-  
+
   GDS_NEIGHBOURS_send_trail_setup (my_identity, finger_id_value,
                                    target_friend->id, target_friend, 0, NULL,
-                                   is_predecessor, trail_id, 
+                                   is_predecessor, trail_id,
                                    intermediate_trail_id);
 }
 
@@ -2595,10 +2592,10 @@
   if (largest_trail_index == (MAXIMUM_TRAILS_PER_FINGER + 1))
   {
     struct GNUNET_PeerIdentity next_hop;
-    
+
     memcpy (&next_hop, &new_trail[0], sizeof(struct GNUNET_PeerIdentity));
     GDS_ROUTING_remove_trail (new_trail_id);
-    GDS_NEIGHBOURS_send_trail_teardown (new_trail_id, 
+    GDS_NEIGHBOURS_send_trail_teardown (new_trail_id,
                                         GDS_ROUTING_SRC_TO_DEST,
                                         next_hop);
     return;
@@ -2623,7 +2620,7 @@
   replace_trail->is_present = GNUNET_YES;
   replace_trail->trail_length = new_trail_length;
   replace_trail->trail_id = new_trail_id;
-  //FIXME: Do we need to add pointers for head and tail. 
+  //FIXME: Do we need to add pointers for head and tail.
   i = 0;
   while (i < new_trail_length)
   {
@@ -2658,20 +2655,20 @@
   int trail_unique = GNUNET_NO;
 
   GNUNET_assert (existing_finger->trails_count > 0);
-  
+
   /* Iterate over list of trails. */
   for (i = 0; i < existing_finger->trails_count; i++)
   {
     trail_list_iterator = &existing_finger->trail_list[i];
     GNUNET_assert (GNUNET_YES == trail_list_iterator->is_present);
-    
+
     /* New trail and existing trail length are not same. */
     if (trail_list_iterator->trail_length != trail_length)
     {
       trail_unique = GNUNET_YES;
       continue;
     }
-    
+
     trail_element = trail_list_iterator->trail_head;
     for (j = 0; j < trail_list_iterator->trail_length; j++)
     {
@@ -2683,18 +2680,18 @@
       }
       trail_element = trail_element->next;
     }
-    
+
     trail_unique = GNUNET_NO;
   }
-  
+
   return trail_unique;
 }
 
 
 /**
- * Add a new trail to existing finger. This function is called only when 
finger 
+ * Add a new trail to existing finger. This function is called only when finger
  * is not my own identity or a friend.
- * @param existing_finger Finger 
+ * @param existing_finger Finger
  * @param new_finger_trail New trail from me to finger, NOT including endpoints
  * @param new_finger_trail_length Total number of peers in @a new_finger_trail
  * @param new_finger_trail_id Unique identifier of the trail.
@@ -2714,27 +2711,27 @@
   {
     return;
   }
-  
+
   trail_list_iterator = 
&existing_finger->trail_list[existing_finger->trails_count];
   GNUNET_assert (GNUNET_NO == trail_list_iterator->is_present);
   trail_list_iterator->trail_id = new_trail_id;
   trail_list_iterator->trail_length = new_trail_length;
   existing_finger->trails_count++;
   trail_list_iterator->is_present = GNUNET_YES;
-  
+
   GNUNET_assert (NULL == (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                              
&existing_finger->finger_identity)));
   /* If finger is a friend then we never call this function. */
   GNUNET_assert (new_trail_length > 0);
-  
+
   first_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                     &new_trail[0]);
   first_friend->trails_count++;
-  
+
   for (i = 0; i < new_trail_length; i++)
   {
     struct Trail_Element *element;
-    
+
     element = GNUNET_new (struct Trail_Element);
     element->peer = new_trail[i];
     GNUNET_CONTAINER_DLL_insert_tail (trail_list_iterator->trail_head,
@@ -2742,19 +2739,19 @@
                                       element);
   }
   /* Do we need to add trail head and trail tail in the trail list itearator.*/
-  
+
 }
 
 
 /**
- * FIXME Check if this function is called for opposite direction if yes then 
- * take it as parameter. 
+ * FIXME Check if this function is called for opposite direction if yes then
+ * take it as parameter.
  * Get the next hop to send trail teardown message from routing table and
- * then delete the entry from routing table. Send trail teardown message for a 
- * specific trail of a finger. 
- * @param finger Finger whose trail is to be removed. 
- * @param trail List of peers in trail from me to a finger, NOT including 
- *              endpoints. 
+ * then delete the entry from routing table. Send trail teardown message for a
+ * specific trail of a finger.
+ * @param finger Finger whose trail is to be removed.
+ * @param trail List of peers in trail from me to a finger, NOT including
+ *              endpoints.
  */
 static void
 send_trail_teardown (struct FingerInfo *finger,
@@ -2762,31 +2759,31 @@
 {
   struct FriendInfo *friend;
   struct GNUNET_PeerIdentity *next_hop;
-  
-  GNUNET_assert (NULL != 
-                (next_hop = GDS_ROUTING_get_next_hop (trail->trail_id, 
+
+  GNUNET_assert (NULL !=
+                (next_hop = GDS_ROUTING_get_next_hop (trail->trail_id,
                                                       
GDS_ROUTING_SRC_TO_DEST)));
-  
+
   GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity 
(&finger->finger_identity,
                                                        &my_identity));
 
   GNUNET_assert (trail->is_present == GNUNET_YES);
-  
+
   /* Finger is not a friend. */
   if (trail->trail_length > 0)
   {
-    GNUNET_assert (NULL != (friend = 
+    GNUNET_assert (NULL != (friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                       
&trail->trail_head->peer)));
   }
   else
   {
-    GNUNET_assert (NULL != (friend = 
+    GNUNET_assert (NULL != (friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                       
&finger->finger_identity)));
   }
-    
-  GNUNET_assert (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, 
&friend->id)); //Fixme: assertion fails. 
+
+  GNUNET_assert (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, 
&friend->id)); //Fixme: assertion fails.
   GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail->trail_id));
   friend->trails_count--;
   GDS_NEIGHBOURS_send_trail_teardown (trail->trail_id,
@@ -2796,18 +2793,18 @@
 
 
 /**
- * Send trail teardown message across all the trails to reach to finger. 
- * @param finger Finger whose all the trail should be freed. 
+ * Send trail teardown message across all the trails to reach to finger.
+ * @param finger Finger whose all the trail should be freed.
  */
 static void
 send_all_finger_trails_teardown (struct FingerInfo *finger)
 {
   unsigned int i;
-  
+
   for (i = 0; i < finger->trails_count; i++)
   {
     struct Trail *trail;
-    
+
     trail = &finger->trail_list[i];
     GNUNET_assert (trail->is_present == GNUNET_YES);
     send_trail_teardown (finger, trail);
@@ -2818,7 +2815,7 @@
 
 /**
  * Free a specific trail
- * @param trail List of peers to be freed. 
+ * @param trail List of peers to be freed.
  */
 static void
 free_trail (struct Trail *trail)
@@ -2827,11 +2824,11 @@
 
   while (NULL != (trail_element = trail->trail_head))
   {
-    GNUNET_CONTAINER_DLL_remove (trail->trail_head, 
+    GNUNET_CONTAINER_DLL_remove (trail->trail_head,
                                  trail->trail_tail,
                                  trail_element);
     GNUNET_free_non_null (trail_element);
-  }  
+  }
   trail->trail_head = NULL;
   trail->trail_tail = NULL;
 }
@@ -2851,18 +2848,18 @@
   for (i = 0; i < finger->trails_count; i++)
   {
     trail = &finger->trail_list[i];
-    //FIXME: Check if there are any missing entry in this list because of 
+    //FIXME: Check if there are any missing entry in this list because of
     // how we insert. If not then no need of this check.
     if (GNUNET_NO == trail->is_present)
       continue;
-    
+
     if (trail->trail_length > 0)
     {
       free_trail (trail);
     }
     trail->is_present = GNUNET_NO;
   }
-  
+
   finger->is_present = GNUNET_NO;
   memset ((void *)&finger_table[finger_table_index], 0, sizeof 
(finger_table[finger_table_index]));
 }
@@ -2871,8 +2868,8 @@
 /**
  * FIXME: ensure that you are not adding any trail to reach to a friend which
  * is a finger. Also decide on should you increment trails count of a friend
- * which is also a finger. 
- * Add a new entry in finger table at finger_table_index. 
+ * which is also a finger.
+ * Add a new entry in finger table at finger_table_index.
  * In case finger identity is me or a friend, then don't add a trail. NOTE
  * trail length to reach to a finger can be 0 only if the finger is a friend
  * or my identity.
@@ -2894,12 +2891,12 @@
   struct FriendInfo *first_trail_hop;
   struct Trail *trail;
   int i = 0;
-  
+
   new_entry = GNUNET_new (struct FingerInfo);
   new_entry->finger_identity = finger_identity;
   new_entry->finger_table_index = finger_table_index;
   new_entry->is_present = GNUNET_YES;
-  
+
   /* If the new entry is my own identity. */
   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
   {
@@ -2908,10 +2905,10 @@
     GNUNET_free (new_entry);
     return;
   }
-  
+
   /* If finger is a friend, then we don't actually have a trail.
    *  Just a trail id */
-  if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+  if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                  &finger_identity))
   {
     new_entry->trail_list[0].trail_id = trail_id;
@@ -2921,8 +2918,8 @@
     new_entry->trail_list[0].trail_head = NULL;
     new_entry->trail_list[0].trail_tail = NULL;
     finger_table[finger_table_index] = *new_entry;
-    GNUNET_assert (NULL != 
-                (first_trail_hop = 
+    GNUNET_assert (NULL !=
+                (first_trail_hop =
                        GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                           &finger_identity)));
 
@@ -2930,18 +2927,18 @@
     GNUNET_free (new_entry);
     return;
   }
-  
+
   /* finger trail length can be 0 only in case if finger is my identity or
    finger is friend. We should never reach here. */
   GNUNET_assert (finger_trail_length > 0);
-  
-  GNUNET_assert (NULL != 
-                (first_trail_hop = 
+
+  GNUNET_assert (NULL !=
+                (first_trail_hop =
                        GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                           &finger_trail[0])));
   new_entry->trails_count = 1;
   first_trail_hop->trails_count++;
-   
+
   /* Copy the finger trail into trail. */
   trail = GNUNET_new (struct Trail);
   while (i < finger_trail_length)
@@ -2956,7 +2953,7 @@
                                       element);
     i++;
   }
-  
+
   /* Add trail to trail list. */
   new_entry->trail_list[0].trail_head = trail->trail_head;
   new_entry->trail_list[0].trail_tail = trail->trail_tail;
@@ -2992,7 +2989,7 @@
   struct FriendInfo *target_friend;
   struct GNUNET_PeerIdentity *new_trail;
   unsigned int i;
-  
+
   /* I am my own finger. */
   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
   {
@@ -3005,24 +3002,24 @@
     *new_trail_length = 0;
     return NULL;
   }
-  
+
   /* If finger identity is a friend. */
   if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&finger_identity))
   {
     *new_trail_length = 0;
-    
+
     /* If there is trail to reach this finger/friend */
     if (trail_length > 0)
     {
       /* Finger is your first friend. */
       GDS_ROUTING_update_trail_next_hop (trail_id, finger_identity);
-      GNUNET_assert (NULL != 
-                    (target_friend = 
+      GNUNET_assert (NULL !=
+                    (target_friend =
                      GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                         &trail[0])));
-      
 
-      GDS_NEIGHBOURS_send_trail_compression (my_identity, 
+
+      GDS_NEIGHBOURS_send_trail_compression (my_identity,
                                              trail_id, finger_identity,
                                              target_friend);
     }
@@ -3038,16 +3035,16 @@
       struct FriendInfo *target_friend;
       int j = 0;
 
-      GNUNET_assert (NULL != 
-                    (target_friend = 
+      GNUNET_assert (NULL !=
+                    (target_friend =
                      GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                         &trail[0])));
       GDS_ROUTING_update_trail_next_hop (trail_id, trail[i]);
-      GDS_NEIGHBOURS_send_trail_compression (my_identity, 
+      GDS_NEIGHBOURS_send_trail_compression (my_identity,
                                              trail_id, trail[i],
                                              target_friend);
 
-    
+
       /* Copy the trail from index i to index (trail_length -1) into a new 
trail
        *  and update new trail length */
       new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * 
(trail_length - i));
@@ -3062,9 +3059,9 @@
       return new_trail;
     }
   }
-  
+
   /* If we did not compress the trail, return the original trail back.*/
-  new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * 
trail_length); 
+  new_trail = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) * 
trail_length);
   *new_trail_length = trail_length;
   memcpy (new_trail, trail, trail_length * sizeof (struct 
GNUNET_PeerIdentity));
   return new_trail;
@@ -3074,7 +3071,7 @@
 /**
  * Periodic task to verify current successor. There can be multiple trails to 
reach
  * to successor, choose the shortest one and send verify successor message
- * across that trail. 
+ * across that trail.
  * @param cls closure for this task
  * @param tc the context under which the task is running
  */
@@ -3091,7 +3088,7 @@
   unsigned int trail_length;
   unsigned int j = 0;
   struct FingerInfo *successor;
-   
+
   /* Schedule another send_find_finger_trail_message task. */
   next_send_time.rel_value_us =
       DHT_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
@@ -3100,11 +3097,11 @@
   send_verify_successor_task =
       GNUNET_SCHEDULER_add_delayed (next_send_time, 
&send_verify_successor_message,
                                     NULL);
-  
+
   successor = &finger_table[0];
   i = 0;
   trail = &successor->trail_list[i];
-  
+
   /* Store the successor for path tracking */
   if (track_topology &&  (NULL != GDS_stats))
   {
@@ -3121,21 +3118,21 @@
     GNUNET_STATISTICS_update (GDS_stats, "key", 1, 0);
     GNUNET_free (key);
   }
-  
-  GNUNET_assert(0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, 
+
+  GNUNET_assert(0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity,
                                                       
&successor->finger_identity));
-  
+
   /* Trail stored at this index. */
   GNUNET_assert (GNUNET_YES == trail->is_present);
-    
+
   trail_id = trail->trail_id;
   trail_length = trail->trail_length;
-       
+
   if (trail_length > 0)
   {
      /* Copy the trail into peer list. */
     struct GNUNET_PeerIdentity peer_list[trail_length];
-    
+
     element = trail->trail_head;
     while (j < trail_length)
     {
@@ -3143,9 +3140,9 @@
       element = element->next;
       j++;
     }
-      
-    GNUNET_assert (NULL != (target_friend = 
-                            GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+
+    GNUNET_assert (NULL != (target_friend =
+                            GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                                
&peer_list[0])));
     GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
                                                   successor->finger_identity,
@@ -3155,8 +3152,8 @@
   }
   else
   {
-    GNUNET_assert (NULL != (target_friend = 
-                            GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+    GNUNET_assert (NULL != (target_friend =
+                            GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                                
&successor->finger_identity)));
     GDS_NEIGHBOURS_send_verify_successor_message (my_identity,
                                                   successor->finger_identity,
@@ -3168,71 +3165,84 @@
 
 
 /**
- * Update the current search finger index. 
+ * Update the current search finger index.
+ *
+ * FIXME document parameters!
  */
 static void
 update_current_search_finger_index (struct GNUNET_PeerIdentity finger_identity,
                                     unsigned int finger_table_index)
 {
   struct FingerInfo *successor;
-  
+
+  /* FIXME correct this: only move current index periodically */
   if (finger_table_index != current_search_finger_index)
     return;
-  
+
   successor = &finger_table[0];
   GNUNET_assert (GNUNET_YES == successor->is_present);
- 
+
   /* We were looking for immediate successor.  */
   if (0 == current_search_finger_index)
   {
     /* Start looking for immediate predecessor. */
     current_search_finger_index = PREDECESSOR_FINGER_ID;
-  
+
     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
     {
-      if (GNUNET_SCHEDULER_NO_TASK == send_verify_successor_task) 
+      if (GNUNET_SCHEDULER_NO_TASK == send_verify_successor_task)
         send_verify_successor_task = GNUNET_SCHEDULER_add_now 
(&send_verify_successor_message, NULL);
     }
-    
+
     return;
   }
-  
+
   current_search_finger_index = current_search_finger_index - 1;
   return;
 }
 
 
 /**
- * Get the first set bit in val. 
+ * Get the least significant bit set in val.
+ *
  * @param val Value
- * @return Position of first bit set.
+ * @return Position of first bit set, 65 in case of error.
  */
 static unsigned int
-find_set_bit(uint64_t val)
+find_set_bit (uint64_t val)
 {
   uint64_t i;
   unsigned int pos;
-  
+
   i = 1;
   pos = 0;
-  
-  while(!(i && val))
+
+  while (!(i & val))
   {
-    i = i << val;
+    i = i << 1;
     pos++;
+    if (pos > 63)
+    {
+      GNUNET_break (0);
+      return 65;
+    }
   }
+
+  if (val/i != 1)
+    return 65; /* Some other bit was set to 1 as well. */
+
   return pos;
 }
 
+
 /**
- * Calculate finger_table_index from initial 64 bit finger identity value that 
- * we send in trail setup message. 
+ * Calculate finger_table_index from initial 64 bit finger identity value that
+ * we send in trail setup message.
  * @param ultimate_destination_finger_value Value that we calculated from our
  *                                          identity and finger_table_index.
  * @param is_predecessor Is the entry for predecessor or not?
  * @return finger_table_index Value between 0 <= finger_table_index <= 64
- *                            finger_table_index > PREDECESSOR_FINGER_ID , 
- *                            if no valid finger_table_index is found. 
+ *         finger_table_index > PREDECESSOR_FINGER_ID, if error occurs.
  */
 static unsigned int
 get_finger_table_index (uint64_t ultimate_destination_finger_value,
@@ -3244,7 +3254,7 @@
 
   memcpy (&my_id64, &my_identity, sizeof (uint64_t));
   my_id64 = GNUNET_ntohll (my_id64);
-  
+
   /* Is this a predecessor finger? */
   if (1 == is_predecessor)
   {
@@ -3253,31 +3263,31 @@
       finger_table_index = PREDECESSOR_FINGER_ID;
     else
       finger_table_index = PREDECESSOR_FINGER_ID + 1; //error value
-    
+
   }
-  else 
+  else
   {
     diff = ultimate_destination_finger_value - my_id64;
-    finger_table_index = find_set_bit(diff);
+    finger_table_index = find_set_bit (diff);
   }
-  
+
   return finger_table_index;
 }
 
 
 /**
- * Remove finger and its associated data structures from finger table. 
+ * Remove finger and its associated data structures from finger table.
  * @param finger Finger to be removed.
  */
 static void
-remove_existing_finger (struct FingerInfo *existing_finger, 
+remove_existing_finger (struct FingerInfo *existing_finger,
                         unsigned int finger_table_index)
 {
   struct FingerInfo *finger;
 
   finger = &finger_table[finger_table_index];
   GNUNET_assert (GNUNET_YES == finger->is_present);
-  
+
   /* If I am my own finger, then we have no trails. */
   if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger->finger_identity,
                                             &my_identity))
@@ -3287,7 +3297,7 @@
             sizeof (finger_table[finger_table_index]));
     return;
   }
-  
+
   /* For all other fingers, send trail teardown across all the trails to reach
    finger, and free the finger. */
   send_all_finger_trails_teardown (finger);
@@ -3300,15 +3310,15 @@
  * Check if there is already an entry in finger_table at finger_table_index.
  * We get the finger_table_index from 64bit finger value we got from the 
network.
  * -- If yes, then select the closest finger.
- *   -- If new and existing finger are same, then check if you can store more 
- *      trails. 
- *      -- If yes then add trail, else keep the best trails to reach to the 
- *         finger. 
+ *   -- If new and existing finger are same, then check if you can store more
+ *      trails.
+ *      -- If yes then add trail, else keep the best trails to reach to the
+ *         finger.
  *   -- If the new finger is closest, remove the existing entry, send trail
  *      teardown message across all the trails to reach the existing entry.
  *      Add the new finger.
  *  -- If new and existing finger are different, and existing finger is closest
- *     then do nothing.  
+ *     then do nothing.
  * -- Update current_search_finger_index.
  * @param finger_identity Peer Identity of new finger
  * @param finger_trail Trail to reach the new finger
@@ -3318,22 +3328,22 @@
  * @param finger_trail_id Unique identifier of @finger_trail.
  */
 static void
-finger_table_add (struct GNUNET_PeerIdentity finger_identity, 
-                  const struct GNUNET_PeerIdentity *finger_trail, 
+finger_table_add (struct GNUNET_PeerIdentity finger_identity,
+                  const struct GNUNET_PeerIdentity *finger_trail,
                   unsigned int finger_trail_length,
                   unsigned int is_predecessor,
                   uint64_t finger_value,
                   struct GNUNET_HashCode finger_trail_id)
 {
   struct FingerInfo *existing_finger;
-  struct GNUNET_PeerIdentity *closest_peer;
+  const struct GNUNET_PeerIdentity *closest_peer;
   struct FingerInfo *successor;
-  int updated_finger_trail_length; 
+  int updated_finger_trail_length;
   unsigned int finger_table_index;
-  
+
   /* Get the finger_table_index corresponding to finger_value we got from 
network.*/
   finger_table_index = get_finger_table_index (finger_value, is_predecessor);
-   
+
   /* Invalid finger_table_index. */
   if ((finger_table_index > PREDECESSOR_FINGER_ID))
   {
@@ -3342,11 +3352,15 @@
   }
 
   /* New entry same as successor. */
-  if ((0 != finger_table_index) && 
-      (PREDECESSOR_FINGER_ID != finger_table_index)) 
+  if ((0 != finger_table_index) &&
+      (PREDECESSOR_FINGER_ID != finger_table_index))
   {
     successor = &finger_table[0];
-    GNUNET_assert (GNUNET_YES == successor->is_present);
+    if (GNUNET_NO == successor->is_present)
+    {
+      GNUNET_break_op (0);
+      return;
+    }
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity,
                                               &successor->finger_identity))
     {
@@ -3354,39 +3368,39 @@
       return;
     }
   }
-  
+
   existing_finger = &finger_table[finger_table_index];
- 
+
   /* No entry present in finger_table for given finger map index. */
   if (GNUNET_NO == existing_finger->is_present)
   {
     struct GNUNET_PeerIdentity *updated_trail;
-     
+
     /* Shorten the trail if possible. */
     updated_finger_trail_length = finger_trail_length;
-    updated_trail =
-         scan_and_compress_trail (finger_identity, finger_trail,
-                                  finger_trail_length, finger_trail_id, 
-                                  &updated_finger_trail_length);
+    updated_trail = scan_and_compress_trail (finger_identity, finger_trail,
+                                             finger_trail_length,
+                                             finger_trail_id,
+                                             &updated_finger_trail_length);
 
-    add_new_finger (finger_identity, updated_trail, 
+    add_new_finger (finger_identity, updated_trail,
                     updated_finger_trail_length,
                     finger_trail_id, finger_table_index);
-    update_current_search_finger_index (finger_identity, 
+    update_current_search_finger_index (finger_identity,
                                         finger_table_index);
     return;
   }
-  
-  
+
+
   /* If existing entry and finger identity are not same. */
   if (0 != GNUNET_CRYPTO_cmp_peer_identity 
(&(existing_finger->finger_identity),
                                             &finger_identity))
   {
     closest_peer = select_closest_peer (&existing_finger->finger_identity,
                                         &finger_identity,
-                                        finger_value, 
+                                        finger_value,
                                         is_predecessor);
-    
+
     /* If the new finger is the closest peer. */
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, closest_peer))
     {
@@ -3395,25 +3409,25 @@
       updated_finger_trail_length = finger_trail_length;
       updated_trail =
         scan_and_compress_trail (finger_identity, finger_trail,
-                                 finger_trail_length, finger_trail_id, 
+                                 finger_trail_length, finger_trail_id,
                                  &updated_finger_trail_length);
       remove_existing_finger (existing_finger, finger_table_index);
       add_new_finger (finger_identity, updated_trail, 
updated_finger_trail_length,
                       finger_trail_id, finger_table_index);
-      
+
     }
     else
     {
-      /* Existing finger is the closest one. We need to send trail teardown  
+      /* Existing finger is the closest one. We need to send trail teardown
          across the trail setup in routing table of all the peers. */
       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger_identity, 
&my_identity))
       {
         if (finger_trail_length > 0)
-          GDS_NEIGHBOURS_send_trail_teardown (finger_trail_id, 
+          GDS_NEIGHBOURS_send_trail_teardown (finger_trail_id,
                                               GDS_ROUTING_SRC_TO_DEST,
                                               finger_trail[0]);
         else
-          GDS_NEIGHBOURS_send_trail_teardown (finger_trail_id, 
+          GDS_NEIGHBOURS_send_trail_teardown (finger_trail_id,
                                               GDS_ROUTING_SRC_TO_DEST,
                                               finger_identity);
       }
@@ -3433,12 +3447,12 @@
                                            &existing_finger->finger_identity))
     {
       struct GNUNET_PeerIdentity *updated_trail;
-      
+
       /* Shorten the trail if possible. */
       updated_finger_trail_length = finger_trail_length;
       updated_trail =
          scan_and_compress_trail (finger_identity, finger_trail,
-                                  finger_trail_length, finger_trail_id, 
+                                  finger_trail_length, finger_trail_id,
                                   &updated_finger_trail_length);
       /* If there is space to store more trails. */
       if (existing_finger->trails_count < MAXIMUM_TRAILS_PER_FINGER)
@@ -3447,7 +3461,7 @@
       else
         select_and_replace_trail (existing_finger, updated_trail,
                                   updated_finger_trail_length, 
finger_trail_id);
-       
+
     }
   }
   update_current_search_finger_index (finger_identity, finger_table_index);
@@ -3478,18 +3492,18 @@
   uint32_t putlen;
   size_t payload_size;
   uint64_t key_value;
-  
+
   msize = ntohs (message->size);
   if (msize < sizeof (struct PeerPutMessage))
-  { 
+  {
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  
+
   put = (struct PeerPutMessage *) message;
   putlen = ntohl (put->put_path_length);
-  
-  
+
+
   if ((msize <
        sizeof (struct PeerPutMessage) +
        putlen * sizeof (struct GNUNET_PeerIdentity)) ||
@@ -3505,10 +3519,10 @@
   payload = &put_path[putlen];
   options = ntohl (put->options);
   intermediate_trail_id = put->intermediate_trail_id;
-  
-  payload_size = msize - (sizeof (struct PeerPutMessage) + 
+
+  payload_size = msize - (sizeof (struct PeerPutMessage) +
                           putlen * sizeof (struct GNUNET_PeerIdentity));
-  
+
   switch (GNUNET_BLOCK_get_key (GDS_block_context, ntohl (put->block_type),
                                 payload, payload_size, &test_key))
   {
@@ -3531,7 +3545,7 @@
       /* cannot verify, good luck */
       break;
   }
-  
+
    if (ntohl (put->block_type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for 
all tpyes */
   {
     switch (GNUNET_BLOCK_evaluate (GDS_block_context,
@@ -3556,7 +3570,7 @@
       return GNUNET_OK;
     }
   }
-  
+
   /* extend 'put path' by sender */
   struct GNUNET_PeerIdentity pp[putlen + 1];
   if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
@@ -3567,11 +3581,11 @@
   }
   else
     putlen = 0;
-  
+
   memcpy (&key_value, &(put->key), sizeof (uint64_t));
   if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
   {
-    next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id, 
+    next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
                                          GDS_ROUTING_SRC_TO_DEST);
     if (NULL == next_hop)
     {
@@ -3587,13 +3601,13 @@
     struct Closest_Peer successor;
     key_value = GNUNET_ntohll (key_value);
     successor = find_successor (key_value, GDS_FINGER_TYPE_NON_PREDECESSOR);
-    
+
     next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
     *next_hop = successor.next_hop;
     intermediate_trail_id = successor.trail_id;
     best_known_dest = successor.best_known_destination;
   }
-  
+
   GDS_CLIENTS_process_put (options,
                            ntohl (put->block_type),
                            ntohl (put->hop_count),
@@ -3603,17 +3617,17 @@
                            &put->key,
                            payload,
                            payload_size);
-  
+
   /* I am the final destination */
-  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &best_known_dest)) 
+  if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &best_known_dest))
   {
     GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
-                              &(put->key),putlen, pp, ntohl (put->block_type), 
+                              &(put->key),putlen, pp, ntohl (put->block_type),
                               payload_size, payload);
   }
   else
   {
-    GDS_NEIGHBOURS_send_put (&put->key,  
+    GDS_NEIGHBOURS_send_put (&put->key,
                              ntohl (put->block_type),ntohl (put->options),
                              ntohl (put->desired_replication_level),
                              best_known_dest, intermediate_trail_id, next_hop,
@@ -3646,7 +3660,7 @@
   uint32_t get_length;
   uint64_t key_value;
   size_t msize;
-  
+
   msize = ntohs (message->size);
   if (msize < sizeof (struct PeerGetMessage))
   {
@@ -3659,7 +3673,7 @@
   best_known_dest = get->best_known_destination;
   intermediate_trail_id = get->intermediate_trail_id;
   get_path = (const struct GNUNET_PeerIdentity *)&get[1];
-  
+
   if ((msize <
        sizeof (struct PeerGetMessage) +
        get_length * sizeof (struct GNUNET_PeerIdentity)) ||
@@ -3667,23 +3681,23 @@
         GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
   {
     GNUNET_break_op (0);
-    return GNUNET_YES; 
+    return GNUNET_YES;
   }
-   
+
   /* Add sender to get path */
   struct GNUNET_PeerIdentity gp[get_length + 1];
   if (get_length > 0)
     memcpy (gp, get_path, get_length * sizeof (struct GNUNET_PeerIdentity));
   gp[get_length] = *peer;
   get_length = get_length + 1;
-  
+
   memcpy (&key_value, &(get->key), sizeof (uint64_t));
   key_value = GNUNET_ntohll (key_value);
 
   /* I am not the final destination. I am part of trail to reach final dest. */
   if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&best_known_dest, &my_identity)))
   {
-    next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id, 
+    next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
                                          GDS_ROUTING_SRC_TO_DEST);
     if (NULL == next_hop)
     {
@@ -3704,7 +3718,7 @@
     best_known_dest = successor.best_known_destination;
     intermediate_trail_id = successor.trail_id;
   }
-  
+
   GDS_CLIENTS_process_get (get->options, get->block_type,get->hop_count,
                            get->desired_replication_level, 
get->get_path_length,
                            gp, &get->key);
@@ -3716,17 +3730,17 @@
     memcpy (final_get_path, gp, get_length * sizeof (struct 
GNUNET_PeerIdentity));
     memcpy (&final_get_path[get_length], &my_identity, sizeof (struct 
GNUNET_PeerIdentity));
     get_length = get_length + 1;
-    
+
     GDS_DATACACHE_handle_get (&(get->key),(get->block_type), NULL, 0, NULL, 0,
-                              get_length, final_get_path, 
+                              get_length, final_get_path,
                               &final_get_path[get_length-2], &my_identity);
   }
   else
   {
-    GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options, 
+    GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
                              get->desired_replication_level, best_known_dest,
                              intermediate_trail_id, next_hop, 0,
-                             get_length, gp); 
+                             get_length, gp);
   }
   return GNUNET_YES;
 }
@@ -3753,7 +3767,7 @@
   unsigned int getlen;
   unsigned int putlen;
   int current_path_index;
-  
+
   msize = ntohs (message->size);
   if (msize < sizeof (struct PeerGetResultMessage))
   {
@@ -3764,10 +3778,10 @@
   get_result = (const struct PeerGetResultMessage *)message;
   getlen = ntohl (get_result->get_path_length);
   putlen = ntohl (get_result->put_path_length);
-  
+
   if ((msize <
        sizeof (struct PeerGetResultMessage) +
-       getlen * sizeof (struct GNUNET_PeerIdentity) + 
+       getlen * sizeof (struct GNUNET_PeerIdentity) +
        putlen * sizeof (struct GNUNET_PeerIdentity)) ||
       (getlen >
        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity) ||
@@ -3777,16 +3791,16 @@
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-   
+
   put_path = (const struct GNUNET_PeerIdentity *) &get_result[1];
   get_path = &put_path[putlen];
   payload = (const void *) &get_path[getlen];
-  payload_size = msize - (sizeof (struct PeerGetResultMessage) + 
+  payload_size = msize - (sizeof (struct PeerGetResultMessage) +
                          (getlen + putlen) * sizeof (struct 
GNUNET_PeerIdentity));
 
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &(get_path[0]))))
   {
-    GDS_CLIENTS_handle_reply (get_result->expiration_time, &(get_result->key), 
+    GDS_CLIENTS_handle_reply (get_result->expiration_time, &(get_result->key),
                               getlen, get_path, putlen,
                               put_path, get_result->type, payload_size, 
payload);
     return GNUNET_YES;
@@ -3805,7 +3819,7 @@
                                     getlen, get_path, 
get_result->expiration_time,
                                     payload, payload_size);
     return GNUNET_YES;
-  }  
+  }
   return GNUNET_SYSERR;
 }
 
@@ -3816,16 +3830,16 @@
  * then get the next hop from routing table. Compare next_hop from routing 
table
  * and local best known hop, and return the closest one to 
final_dest_finger_val
  * @param final_dest_finger_val 64 bit value of finger identity
- * @param intermediate_trail_id If you are part of trail to reach to some 
other 
+ * @param intermediate_trail_id If you are part of trail to reach to some other
  *                              finger, then it is the trail id to reach to
  *                              that finger, else set to 0.
- * @param is_predecessor Are we looking for closest successor or predecessor. 
- * @param current_dest In case you are part of trail, then finger to which 
+ * @param is_predecessor Are we looking for closest successor or predecessor.
+ * @param current_dest In case you are part of trail, then finger to which
  *                     we should forward the message. Else my own identity
  * @return Closest Peer for @a final_dest_finger_val
  */
 static struct Closest_Peer
-get_local_best_known_next_hop (uint64_t final_dest_finger_val, 
+get_local_best_known_next_hop (uint64_t final_dest_finger_val,
                                struct GNUNET_HashCode intermediate_trail_id,
                                unsigned int is_predecessor,
                                 struct GNUNET_PeerIdentity prev_hop,
@@ -3833,48 +3847,42 @@
                                struct GNUNET_PeerIdentity *current_dest)
 {
   struct Closest_Peer peer;
-  
+
   /* Find a local best known peer. */
   peer = find_successor (final_dest_finger_val, is_predecessor);//FIXME: 
chnage to better name
-  
+
   /* Am I just a part of a trail towards a finger (current_destination)? */
-  if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, current_dest)))
+  /* Select best successor among one found locally and current_destination
+   * that we got from network.*/
+  if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, current_dest) &&
+      0 != GNUNET_CRYPTO_cmp_peer_identity (&peer.best_known_destination,
+                                            current_dest))
   {
-    /* Select best successor among one found locally and current_destination 
-     * that we got from network.*/
-    if (0 != GNUNET_CRYPTO_cmp_peer_identity (&peer.best_known_destination,
-                                              current_dest))
-    { 
-      struct GNUNET_PeerIdentity *closest_peer;
-      struct GNUNET_PeerIdentity *local_best_known_dest;
-      local_best_known_dest = GNUNET_new(struct GNUNET_PeerIdentity);
-      memcpy(local_best_known_dest, &peer.best_known_destination, 
sizeof(struct GNUNET_PeerIdentity));
-      
-      closest_peer = select_closest_peer (local_best_known_dest,
-                                          current_dest,
-                                          final_dest_finger_val,
-                                          is_predecessor);
+    const struct GNUNET_PeerIdentity *closest_peer;
 
-      GNUNET_free(local_best_known_dest);
-      /* Is current dest (end point of the trail of which I am a part) 
closest_peer? */
-      if (0 == GNUNET_CRYPTO_cmp_peer_identity (current_dest, closest_peer))
-      {
-        struct GNUNET_PeerIdentity *next_hop;
-        next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
-                                             GDS_ROUTING_SRC_TO_DEST);
-        GNUNET_assert (NULL != next_hop);
+    closest_peer = select_closest_peer (&peer.best_known_destination,
+                                        current_dest,
+                                        final_dest_finger_val,
+                                        is_predecessor);
 
-        peer.next_hop = *next_hop;
-        peer.best_known_destination =  *current_dest;
-        peer.trail_id = intermediate_trail_id;
-      }
-    }  
+    /* Is current dest (end point of the trail of which I am a part) 
closest_peer? */
+    if (0 == GNUNET_CRYPTO_cmp_peer_identity (current_dest, closest_peer))
+    {
+      struct GNUNET_PeerIdentity *next_hop;
+      next_hop = GDS_ROUTING_get_next_hop (intermediate_trail_id,
+                                            GDS_ROUTING_SRC_TO_DEST);
+      GNUNET_assert (NULL != next_hop);
+
+      peer.next_hop = *next_hop;
+      peer.best_known_destination =  *current_dest;
+      peer.trail_id = intermediate_trail_id;
+    }
   }
   return peer;
 }
 
 
-/* 
+/*
  * Core handle for PeerTrailSetupMessage.
  * @param cls closure
  * @param message message
@@ -3907,33 +3915,52 @@
   trail_setup = (const struct PeerTrailSetupMessage *) message;
   trail_length = (msize - sizeof (struct PeerTrailSetupMessage))/
                   sizeof (struct GNUNET_PeerIdentity);
-  if ((msize - sizeof (struct PeerTrailSetupMessage)) % 
+  if ((msize - sizeof (struct PeerTrailSetupMessage)) %
       sizeof (struct GNUNET_PeerIdentity) != 0)
   {
     GNUNET_break_op (0);
-    return GNUNET_OK;      
-  }           
-  
+    return GNUNET_OK;
+  }
+
   trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_setup[1];
   current_dest = trail_setup->best_known_destination;
   trail_id = trail_setup->trail_id;
-  final_dest_finger_val = 
+  final_dest_finger_val =
           GNUNET_ntohll (trail_setup->final_destination_finger_value);
   source = trail_setup->source_peer;
   is_predecessor = ntohl (trail_setup->is_predecessor);
   intermediate_trail_id = trail_setup->intermediate_trail_id;
-  
+
    /* If I was the source and got the message back, then set trail length to 
0.*/
   if (0 == GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &source))
   {
-   trail_length = 0;  
+    /* IF (!) the peers know the destinations of the trails in their routing
+     * table, then:
+     *
+     * This shoud only happen after 1 hop, since the first message is sent
+     * to random friend, and we can happen to be on the best trail to the dest.
+     * If the first friend selects someone else, the request should never come
+     * back to us.
+     *
+     * (TODO)
+     */
+    // GNUNET_break_op (1 == trail_length);
+    trail_length = 0;
   }
-  
+
+  /* Did the friend insert its ID in the trail list? */
+  if (trail_length > 0 &&
+      0 != memcmp (&trail_peer_list[trail_length-1], peer, sizeof (*peer)))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+
   /* Is my routing table full?  */
   if (GNUNET_YES == GDS_ROUTING_threshold_reached())
   {
-    GNUNET_assert (NULL != 
-                  (target_friend = 
+    GNUNET_assert (NULL !=
+                  (target_friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
     GDS_NEIGHBOURS_send_trail_rejection (source, final_dest_finger_val,
                                          my_identity, is_predecessor,
@@ -3942,70 +3969,66 @@
                                          CONGESTION_TIMEOUT);
     return GNUNET_OK;
   }
- 
+
   /* Get the next hop to forward the trail setup request. */
-  struct Closest_Peer next_peer = 
-          get_local_best_known_next_hop (final_dest_finger_val, 
+  struct Closest_Peer next_peer =
+          get_local_best_known_next_hop (final_dest_finger_val,
                                          intermediate_trail_id,
                                          is_predecessor,
                                          *peer,
                                          source,
                                          &current_dest);
- 
+
   /* Am I the final destination? */
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&next_peer.best_known_destination,
                                              &my_identity)))
   {
-    //
     /* If I was not the source of this message for which now I am destination 
*/
     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
     {
       GDS_ROUTING_add (trail_id, *peer, my_identity);
     }
-   
-    if(trail_length > 0)
+
+    if(0 == GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
     {
-      GNUNET_assert (NULL != 
-                    (target_friend = 
-                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
-                                                       
&trail_peer_list[trail_length-1])));
+      finger_table_add (my_identity, NULL, 0, is_predecessor,
+                        final_dest_finger_val, trail_id);
+      return GNUNET_OK;
     }
+
+    if (trail_length > 0)
+      target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
     else
+      target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&source);
+    if (NULL != target_friend)
     {
-      if(0 == GNUNET_CRYPTO_cmp_peer_identity (&source, &my_identity))
-      {
-        finger_table_add (my_identity, NULL, 0, is_predecessor, 
-                          final_dest_finger_val, trail_id);
-        return GNUNET_OK;
-      }
-      GNUNET_assert (NULL != 
-                    (target_friend = 
-                     GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&source)));
+      GNUNET_break_op (0);
+      return GNUNET_SYSERR;
     }
-   
+
     GDS_NEIGHBOURS_send_trail_setup_result (source,
                                             my_identity,
                                             target_friend, trail_length,
                                             trail_peer_list,
-                                            is_predecessor, 
+                                            is_predecessor,
                                             final_dest_finger_val,trail_id);
   }
-  else
+  else /* I'm not the final destination. */
   {
-    GNUNET_assert (NULL != 
-                    (target_friend = 
+    GNUNET_assert (NULL !=
+                    (target_friend =
                       GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                           
&next_peer.next_hop)));
-    
+
     if (0 != GNUNET_CRYPTO_cmp_peer_identity(&my_identity, &source))
     {
       /* Add yourself to list of peers. */
       struct GNUNET_PeerIdentity peer_list[trail_length + 1];
- 
-      memcpy (peer_list, trail_peer_list, 
+
+      memcpy (peer_list, trail_peer_list,
               trail_length * sizeof (struct GNUNET_PeerIdentity));
       peer_list[trail_length] = my_identity;
-     
+
       GDS_NEIGHBOURS_send_trail_setup (source,
                                        final_dest_finger_val,
                                        next_peer.best_known_destination,
@@ -4030,17 +4053,17 @@
 /**
  * FIXME: Should we call this function everywhere in all the handle functions
  * where we have a trail to verify from or a trail id. something like
- * if prev hop is not same then drop the message. 
+ * if prev hop is not same then drop the message.
  * Check if sender_peer and peer from which we should receive the message are
  * same or different.
  * @param trail_peer_list List of peers in trail
  * @param trail_length Total number of peers in @a trail_peer_list
- * @param sender_peer Peer from which we got the message. 
+ * @param sender_peer Peer from which we got the message.
  * @param finger_identity Finger to which trail is setup. It is not part of 
trail.
  * @return #GNUNET_YES if sender_peer and peer from which we should receive the
  *                    message are different.
  *         #GNUNET_NO if sender_peer and peer from which we should receive the
- *                    message are different. 
+ *                    message are different.
  */
 static int
 is_sender_peer_correct (const struct GNUNET_PeerIdentity *trail_peer_list,
@@ -4050,7 +4073,7 @@
                         struct GNUNET_PeerIdentity source_peer)
 {
   int my_index;
-  
+
   /* I am the source peer. */
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
                                              &my_identity)))
@@ -4076,7 +4099,7 @@
     my_index = search_my_index (trail_peer_list, trail_length);
     if (-1 == my_index)
       return GNUNET_NO;
-    
+
     /* I am the last element in the trail. */
     if ((trail_length - 1) == my_index)
     {
@@ -4104,7 +4127,7 @@
  * Core handle for p2p trail setup result messages.
  * @param closure
  * @param message message
- * @param peer sender of this message. 
+ * @param peer sender of this message.
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
@@ -4134,19 +4157,19 @@
   trail_result = (const struct PeerTrailSetupResultMessage *) message;
   trail_length = (msize - sizeof (struct PeerTrailSetupResultMessage))/
                   sizeof (struct GNUNET_PeerIdentity);
-  if ((msize - sizeof (struct PeerTrailSetupResultMessage)) % 
+  if ((msize - sizeof (struct PeerTrailSetupResultMessage)) %
       sizeof (struct GNUNET_PeerIdentity) != 0)
   {
     GNUNET_break_op (0);
-    return GNUNET_OK;      
-  }       
+    return GNUNET_OK;
+  }
 
   is_predecessor = ntohl (trail_result->is_predecessor);
   querying_peer = trail_result->querying_peer;
   finger_identity = trail_result->finger_identity;
   trail_id = trail_result->trail_id;
   trail_peer_list = (const struct GNUNET_PeerIdentity *) &trail_result[1];
-  ulitmate_destination_finger_value = 
+  ulitmate_destination_finger_value =
           GNUNET_ntohll (trail_result->ulitmate_destination_finger_value);
 
   /* FIXME: here we are calculating my_index and comparing also in this 
function.
@@ -4161,21 +4184,22 @@
     return GNUNET_SYSERR;
   }
 #endif
-  
+
   /* Am I the one who initiated the query? */
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
   {
-    /* If I am not my own finger identity, then add routing table entry. */
-    if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
+    /* If I am my own finger identity, error. */
+    if (0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &finger_identity))
     {
-      GDS_ROUTING_add (trail_id, my_identity, *peer);
+      GNUNET_break_op (0);
+      return GNUNET_SYSERR;
     }
-   
+    GDS_ROUTING_add (trail_id, my_identity, *peer);
     finger_table_add (finger_identity, trail_peer_list, trail_length,
                       is_predecessor, ulitmate_destination_finger_value, 
trail_id);
     return GNUNET_YES;
   }
-  
+
   /* Get my location in the trail. */
   my_index = search_my_index (trail_peer_list, trail_length);
   if (-1 == my_index)
@@ -4183,24 +4207,31 @@
     GNUNET_break_op(0);
     return GNUNET_SYSERR;
   }
-  
+
   if (my_index == 0)
     next_hop = trail_result->querying_peer;
   else
     next_hop = trail_peer_list[my_index - 1];
-  
-  if (0 != (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->querying_peer),
+
+  target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop);
+  if (NULL == target_friend)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+
+  if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&(trail_result->querying_peer),
                                              
&(trail_result->finger_identity))))
   {
-    GDS_ROUTING_add (trail_id, next_hop, *peer);
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
   }
 
-  GNUNET_assert (NULL != 
-                (target_friend = 
-                 GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop)));
+  GDS_ROUTING_add (trail_id, next_hop, *peer);
+
   GDS_NEIGHBOURS_send_trail_setup_result (querying_peer, finger_identity,
                                           target_friend, trail_length, 
trail_peer_list,
-                                          is_predecessor, 
+                                          is_predecessor,
                                           ulitmate_destination_finger_value,
                                           trail_id);
   return GNUNET_OK;
@@ -4231,7 +4262,7 @@
     i++;
     j--;
   }
-  
+
   GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
                                                           &inverted_trail[0]));
   return inverted_trail;
@@ -4240,10 +4271,10 @@
 
 /**
  * Return the shortest trail among all the trails to reach to finger from me.
- * @param finger Finger 
+ * @param finger Finger
  * @param shortest_trail_length[out] Trail length of shortest trail from me
  *                                   to @a finger
- * @return Shortest trail. 
+ * @return Shortest trail.
  */
 static struct GNUNET_PeerIdentity *
 get_shortest_trail (struct FingerInfo *finger,
@@ -4256,22 +4287,22 @@
   struct Trail_Element *trail_element;
   struct GNUNET_PeerIdentity *trail_list;
   unsigned int i;
-  
+
   trail = GNUNET_new (struct Trail);
-  
+
   /* Get the shortest trail to reach to current successor. */
   for (i = 0; i < finger->trails_count; i++)
   {
     trail = &finger->trail_list[i];
-   
+
     if (0 == flag)
     {
       shortest_trail_index = i;
-      shortest_trail_length = trail->trail_length; 
+      shortest_trail_length = trail->trail_length;
       flag = 1;
       continue;
     }
-    
+
     if (shortest_trail_length > trail->trail_length)
     {
       shortest_trail_index = i;
@@ -4279,20 +4310,20 @@
     }
     continue;
   }
-  
+
   /* Copy the shortest trail and return. */
   trail = &finger->trail_list[shortest_trail_index];
   trail_element = trail->trail_head;
   trail_list = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)*
                               shortest_trail_length);
-  
+
   for(i = 0; i < shortest_trail_length; i++,trail_element = 
trail_element->next)
   {
-    trail_list[i] = trail_element->peer; 
+    trail_list[i] = trail_element->peer;
   }
-  
+
   GNUNET_assert(shortest_trail_length != -1);
-  
+
   *trail_length = shortest_trail_length;
   return trail_list;
 }
@@ -4300,13 +4331,13 @@
 
 /**
  * Return the trail from source to my current predecessor. Check if source
- * is already part of the this trail, if yes then return the shorten trail. 
+ * is already part of the this trail, if yes then return the shorten trail.
  * @param current_trail Trail from source to me, NOT including the endpoints.
  * @param current_trail_length Number of peers in @a current_trail.
- * @param trail_src_to_curr_pred_length[out] Number of peers in trail from 
+ * @param trail_src_to_curr_pred_length[out] Number of peers in trail from
  *                                           source to my predecessor, NOT 
including
  *                                           the endpoints.
- * @return Trail from source to my predecessor. 
+ * @return Trail from source to my predecessor.
  */
 static struct GNUNET_PeerIdentity *
 get_trail_src_to_curr_pred (struct GNUNET_PeerIdentity source_peer,
@@ -4320,11 +4351,11 @@
   struct FingerInfo *current_predecessor;
   unsigned int i;
   unsigned int j;
-  
+
   current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
   trail_me_to_curr_pred = get_shortest_trail (current_predecessor,
                                               &trail_me_to_curr_pred_length);
-    
+
   /* Check if trail_me_to_curr_pred contains source. */
   if (trail_me_to_curr_pred_length > 0)
   {
@@ -4333,17 +4364,17 @@
       if(0 != GNUNET_CRYPTO_cmp_peer_identity (&source_peer,
                                                &trail_me_to_curr_pred[i]))
         continue;
-       
+
        i = i+1;
-      
-      /* Source is the last element in the trail to reach to my pred. 
+
+      /* Source is the last element in the trail to reach to my pred.
          Source is direct friend of the pred. */
       if (trail_me_to_curr_pred_length == i)
       {
         *trail_src_to_curr_pred_length = 0;
         return NULL;
       }
-      
+
       *trail_src_to_curr_pred_length = trail_me_to_curr_pred_length - i;
       trail_src_to_curr_pred = GNUNET_malloc (sizeof (struct 
GNUNET_PeerIdentity)*
                                               *trail_src_to_curr_pred_length);
@@ -4351,60 +4382,60 @@
       {
         trail_src_to_curr_pred[j] = trail_me_to_curr_pred[i];
       }
-      
+
       return trail_src_to_curr_pred;
     }
   }
-  
+
   /* Append trail from source to me to my current_predecessor. */
   *trail_src_to_curr_pred_length = trail_src_to_me_len +
                                    trail_me_to_curr_pred_length + 1;
 
   trail_src_to_curr_pred = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity)*
                                           *trail_src_to_curr_pred_length);
-  
+
   for (i = 0; i < trail_src_to_me_len; i++)
     trail_src_to_curr_pred[i] = trail_src_to_me[i];
-  
+
   trail_src_to_curr_pred[i] = my_identity;
   i++;
-  
+
   for (j = 0; i < *trail_src_to_curr_pred_length; i++,j++)
     trail_src_to_curr_pred[i] = trail_me_to_curr_pred[j];
- 
+
   return trail_src_to_curr_pred;
 }
 
 
 /**
  * Add finger as your predecessor. To add, first generate a new trail id, 
invert
- * the trail to get the trail from me to finger, add an entry in your routing 
- * table, send add trail message to peers which are part of trail from me to 
+ * the trail to get the trail from me to finger, add an entry in your routing
+ * table, send add trail message to peers which are part of trail from me to
  * finger and add finger in finger table.
  * @param finger
  * @param trail
  * @param trail_length
  */
 static void
-update_predecessor (struct GNUNET_PeerIdentity finger, 
-                    struct GNUNET_PeerIdentity *trail, 
+update_predecessor (struct GNUNET_PeerIdentity finger,
+                    struct GNUNET_PeerIdentity *trail,
                     unsigned int trail_length)
 {
   struct GNUNET_HashCode trail_to_new_predecessor_id;
   struct GNUNET_PeerIdentity *trail_to_new_predecessor;
   struct FriendInfo *target_friend;
-  
+
   /* Generate trail id for trail from me to new predecessor = finger. */
   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
-                              &trail_to_new_predecessor_id, 
+                              &trail_to_new_predecessor_id,
                               sizeof (trail_to_new_predecessor_id));
-  
+
   /* Finger is a friend. */
   if (trail_length == 0)
   {
     trail_to_new_predecessor = NULL;
-    GDS_ROUTING_add (trail_to_new_predecessor_id, my_identity, finger); 
-    GNUNET_assert (NULL != (target_friend = 
+    GDS_ROUTING_add (trail_to_new_predecessor_id, my_identity, finger);
+    GNUNET_assert (NULL != (target_friend =
                             GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                                &finger)));
   }
@@ -4413,23 +4444,23 @@
     /* Invert the trail to get the trail from me to finger, NOT including the
        endpoints.*/
     trail_to_new_predecessor = invert_trail (trail, trail_length);
-  
+
     /* Add an entry in your routing table. */
-    GDS_ROUTING_add (trail_to_new_predecessor_id, 
+    GDS_ROUTING_add (trail_to_new_predecessor_id,
                      my_identity,
                      trail_to_new_predecessor[0]);
-   
-    GNUNET_assert (NULL != (target_friend = 
-                   GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+
+    GNUNET_assert (NULL != (target_friend =
+                   GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                       
&trail_to_new_predecessor[0])));
     GNUNET_assert (NULL != (
-                   GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+                   GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                       &trail[trail_length - 
1])));
   }
-  
+
   /* Add entry in routing table of all peers that are part of trail from me
      to finger, including finger. */
-  GDS_NEIGHBOURS_send_add_trail (my_identity, 
+  GDS_NEIGHBOURS_send_add_trail (my_identity,
                                  finger,
                                  trail_to_new_predecessor_id,
                                  trail_to_new_predecessor,
@@ -4442,32 +4473,32 @@
 }
 
 
-/* 
+/*
  * Check if you already have a predecessor. If not then add finger as your
  * predecessor. If you have predecessor, then compare two peer identites.
  * If finger is correct predecessor, then remove the old entry, add finger in
  * finger table and send add_trail message to add the trail in the routing
- * table of all peers which are part of trail to reach from me to finger. 
- * @param finger New peer which may be our predecessor. 
- * @param trail List of peers to reach from @finger to me. 
+ * table of all peers which are part of trail to reach from me to finger.
+ * @param finger New peer which may be our predecessor.
+ * @param trail List of peers to reach from @finger to me.
  * @param trail_length Total number of peer in @a trail.
  */
 static void
-compare_and_update_predecessor (struct GNUNET_PeerIdentity finger, 
-                                struct GNUNET_PeerIdentity *trail, 
+compare_and_update_predecessor (struct GNUNET_PeerIdentity finger,
+                                struct GNUNET_PeerIdentity *trail,
                                 unsigned int trail_length)
 {
   struct FingerInfo *current_predecessor;
-  struct GNUNET_PeerIdentity *closest_peer;
+  const struct GNUNET_PeerIdentity *closest_peer;
   uint64_t predecessor_value;
   unsigned int is_predecessor = 1;
-  
+
   current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
-  
+
   GNUNET_assert (0 != GNUNET_CRYPTO_cmp_peer_identity (&finger, &my_identity));
-  
+
   /* No predecessor. Add finger as your predecessor. */
-  if (GNUNET_NO == current_predecessor->is_present) 
+  if (GNUNET_NO == current_predecessor->is_present)
   {
     update_predecessor (finger, trail, trail_length);
     return;
@@ -4480,15 +4511,15 @@
   {
     return;
   }
-  
+
   predecessor_value = compute_finger_identity_value (PREDECESSOR_FINGER_ID);
-  closest_peer = select_closest_peer (&finger, 
+  closest_peer = select_closest_peer (&finger,
                                       &current_predecessor->finger_identity,
                                       predecessor_value, is_predecessor);
-  
+
   /* Finger is the closest predecessor. Remove the existing one and add the new
      one. */
-  if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer, &finger))
+  if (closest_peer == &finger)
   {
     remove_existing_finger (current_predecessor, PREDECESSOR_FINGER_ID);
     update_predecessor (finger, trail, trail_length);
@@ -4498,7 +4529,7 @@
 }
 
 
-/* 
+/*
  * Core handle for p2p verify successor messages.
  * @param cls closure
  * @param message message
@@ -4506,7 +4537,7 @@
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
-handle_dht_p2p_verify_successor(void *cls, 
+handle_dht_p2p_verify_successor(void *cls,
                                 const struct GNUNET_PeerIdentity *peer,
                                 const struct GNUNET_MessageHeader *message)
 {
@@ -4522,31 +4553,31 @@
   struct GNUNET_PeerIdentity *trail_src_to_curr_pred;
   size_t msize;
   unsigned int trail_length;
-  
+
   msize = ntohs (message->size);
-  
-  if (msize < sizeof (struct PeerVerifySuccessorMessage)) 
+
+  if (msize < sizeof (struct PeerVerifySuccessorMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   vsm = (const struct PeerVerifySuccessorMessage *) message;
   trail_length = (msize - sizeof (struct PeerVerifySuccessorMessage))/
                   sizeof (struct GNUNET_PeerIdentity);
-  if ((msize - sizeof (struct PeerVerifySuccessorMessage)) % 
+  if ((msize - sizeof (struct PeerVerifySuccessorMessage)) %
       sizeof (struct GNUNET_PeerIdentity) != 0)
   {
     GNUNET_break_op (0);
-    return GNUNET_OK;      
-  } 
-  
+    return GNUNET_OK;
+  }
+
   trail_id = vsm->trail_id;
   source_peer = vsm->source_peer;
   successor = vsm->successor;
   trail = (struct GNUNET_PeerIdentity *)&vsm[1];
-  
- 
+
+
   /* I am NOT the successor of source_peer. Pass the message to next_hop on
    * the trail. */
   if(0 != (GNUNET_CRYPTO_cmp_peer_identity (&successor, &my_identity)))
@@ -4557,8 +4588,8 @@
       GNUNET_break (0);
       return GNUNET_SYSERR;
     }
-    GNUNET_assert (NULL != 
-                  (target_friend = 
+    GNUNET_assert (NULL !=
+                  (target_friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop)));
 
     GDS_NEIGHBOURS_send_verify_successor_message (source_peer, successor,
@@ -4566,14 +4597,14 @@
                                                   target_friend);
     return GNUNET_OK;
   }
-  
+
   /* I am the destination of this message. */
-  
+
   /* Check if the source_peer could be our predecessor and if yes then update
    * it.  */
   compare_and_update_predecessor (source_peer, trail, trail_length);
   current_predecessor = &finger_table[PREDECESSOR_FINGER_ID];
-  
+
   /* Is source of this message NOT my predecessor. */
   if (0 != (GNUNET_CRYPTO_cmp_peer_identity 
(&current_predecessor->finger_identity,
                                              &source_peer)))
@@ -4581,7 +4612,7 @@
     trail_src_to_curr_pred = get_trail_src_to_curr_pred (source_peer,
                                                          trail,
                                                          trail_length,
-                                                         
&trail_src_to_curr_pred_len);    
+                                                         
&trail_src_to_curr_pred_len);
   }
   else
   {
@@ -4594,9 +4625,9 @@
     }
 
   }
-  
-  GNUNET_assert (NULL != 
-                (target_friend = 
+
+  GNUNET_assert (NULL !=
+                (target_friend =
                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
   GDS_NEIGHBOURS_send_verify_successor_result (source_peer, my_identity,
                                                
current_predecessor->finger_identity,
@@ -4613,12 +4644,12 @@
  * If the trail from me to my probable successor contains a friend not
  * at index 0, then we can shorten the trail.
  * @param probable_successor Peer which is our probable successor
- * @param trail_me_to_probable_successor Peers in path from me to my probable 
+ * @param trail_me_to_probable_successor Peers in path from me to my probable
  *                                       successor, NOT including the 
endpoints.
- * @param trail_me_to_probable_successor_len Total number of peers in 
+ * @param trail_me_to_probable_successor_len Total number of peers in
  *                                           @a trail_me_to_probable_succesor.
  * @return Updated trail, if any friend found.
- *         Else the trail_me_to_probable_successor. 
+ *         Else the trail_me_to_probable_successor.
  */
 struct GNUNET_PeerIdentity *
 check_trail_me_to_probable_succ (struct GNUNET_PeerIdentity probable_successor,
@@ -4629,45 +4660,45 @@
   unsigned int i;
   unsigned int j;
   struct GNUNET_PeerIdentity *trail_to_new_successor;
-    
+
   /* Probable successor is  a friend */
-  if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+  if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                  &probable_successor))
   {
     trail_to_new_successor = NULL;
     *trail_to_new_successor_length = 0;
     return trail_to_new_successor;
   }
-  
+
   /* Is there any friend of yours in this trail. */
   if(trail_me_to_probable_successor_len > 1)
   {
     for (i = trail_me_to_probable_successor_len - 1; i > 0; i--)
     {
-      if (NULL == GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+      if (NULL == GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                      
&trail_me_to_probable_successor[i]))
         continue;
-    
+
       j = 0;
       *trail_to_new_successor_length = (trail_me_to_probable_successor_len - 
i);
       trail_to_new_successor = GNUNET_malloc (sizeof(struct 
GNUNET_PeerIdentity)*
                                                 
*trail_to_new_successor_length);
-    
-      for(j = 0;i < trail_me_to_probable_successor_len;i++,j++)    
+
+      for(j = 0;i < trail_me_to_probable_successor_len;i++,j++)
       {
         trail_to_new_successor[j] = trail_me_to_probable_successor[i];
       }
       return trail_to_new_successor;
     }
   }
-  
+
   *trail_to_new_successor_length = trail_me_to_probable_successor_len;
   trail_to_new_successor = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)*
                                           *trail_to_new_successor_length);
-  
+
   for(i = 0; i < *trail_to_new_successor_length; i++)
     trail_to_new_successor[i] = trail_me_to_probable_successor[i];
-  
+
   return trail_to_new_successor;
 }
 
@@ -4677,7 +4708,7 @@
  * successor or not. If not, then compare existing successor and probable 
successor.
  * In case probable successor is the correct successor, remove the existing
  * successor. Add probable successor as new successor. Send notify new 
successor
- * message to new successor. 
+ * message to new successor.
  * @param curr_succ
  * @param probable_successor
  * @param trail
@@ -4690,42 +4721,44 @@
                               unsigned int trail_length)
 {
   struct FingerInfo *current_successor;
-  struct GNUNET_PeerIdentity *closest_peer;
+  const struct GNUNET_PeerIdentity *closest_peer;
   struct GNUNET_HashCode trail_id;
   struct GNUNET_PeerIdentity *trail_me_to_probable_succ;
   struct FriendInfo *target_friend;
   unsigned int trail_me_to_probable_succ_len;
   unsigned int is_predecessor = GNUNET_NO;
   uint64_t successor_value;
-  
+
   current_successor = &finger_table[0];
   successor_value = compute_finger_identity_value(0);
-  
-  /* Have we found some other successor, while waiting for verify successor 
result. */
+
+  /* Have we found some other successor, while waiting for verify successor 
result
+   *
+   * FIXME closest_peer is being overwritten just after the if
+   */
   if(0 != GNUNET_CRYPTO_cmp_peer_identity(&curr_succ, 
&current_successor->finger_identity))
   {
     /* We could have added this new successor, only if it was closer the old 
one. */
     closest_peer = select_closest_peer (&curr_succ,
                                         &current_successor->finger_identity,
                                         successor_value, is_predecessor);
-    
+
     /* FIXME: it may fail in case we have done more number of iterations of
      find _finger_trail_task. */
-    /*GNUNET_assert (0 == 
+    /*GNUNET_assert (0 ==
                    GNUNET_CRYPTO_cmp_peer_identity (closest_peer,
                                                     
&current_successor->finger_identity));*/
-    
+
   }
-  
+
   closest_peer = select_closest_peer (&probable_successor,
                                       &current_successor->finger_identity,
                                       successor_value, is_predecessor);
-  
+
   /* If the current_successor in the finger table is closest, then do nothing. 
*/
-  if (0 == GNUNET_CRYPTO_cmp_peer_identity (closest_peer,
-                                            
&current_successor->finger_identity))
+  if (closest_peer == &current_successor->finger_identity)
     return;
-  
+
   /* Probable successor is the closest peer.*/
   if(trail_length > 0)
     GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
@@ -4733,41 +4766,41 @@
   else
     GNUNET_assert(NULL != GNUNET_CONTAINER_multipeermap_get(friend_peermap,
                                                             
&probable_successor));
-  
+
   trail_me_to_probable_succ_len = 0;
   /* TODO: Check if the path to reach to probable successor contains a friend. 
*/
-  trail_me_to_probable_succ = 
+  trail_me_to_probable_succ =
           check_trail_me_to_probable_succ (probable_successor,
                                            trail, trail_length,
                                            &trail_me_to_probable_succ_len);
 
   /* Remove the existing successor. */
   remove_existing_finger (current_successor, 0);
-  
+
    /* Generate a new trail id to reach to your new successor. */
   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
                               &trail_id, sizeof (trail_id));
-  
+
   if (trail_me_to_probable_succ_len > 0)
   {
     GDS_ROUTING_add (trail_id, my_identity, trail_me_to_probable_succ[0]);
-    GNUNET_assert (NULL != 
+    GNUNET_assert (NULL !=
                   (target_friend =
-                      GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+                      GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                         
&trail_me_to_probable_succ[0])));
   }
   else
   {
     GDS_ROUTING_add (trail_id, my_identity, probable_successor);
-    GNUNET_assert (NULL != 
-                  (target_friend = 
-                   GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+    GNUNET_assert (NULL !=
+                  (target_friend =
+                   GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                        &probable_successor)));
   }
-  
-  add_new_finger (probable_successor, trail_me_to_probable_succ, 
+
+  add_new_finger (probable_successor, trail_me_to_probable_succ,
                   trail_me_to_probable_succ_len, trail_id, 0);
-  
+
   GDS_NEIGHBOURS_send_notify_new_successor (my_identity, probable_successor,
                                             trail_me_to_probable_succ,
                                             trail_me_to_probable_succ_len,
@@ -4785,7 +4818,7 @@
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
-handle_dht_p2p_verify_successor_result(void *cls, 
+handle_dht_p2p_verify_successor_result(void *cls,
                                        const struct GNUNET_PeerIdentity *peer,
                                        const struct GNUNET_MessageHeader 
*message)
 {
@@ -4809,18 +4842,18 @@
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   vsrm = (const struct PeerVerifySuccessorResultMessage *) message;
   trail_length = (msize - sizeof (struct PeerVerifySuccessorResultMessage))/
                       sizeof (struct GNUNET_PeerIdentity);
-  
-  if ((msize - sizeof (struct PeerVerifySuccessorResultMessage)) % 
+
+  if ((msize - sizeof (struct PeerVerifySuccessorResultMessage)) %
       sizeof (struct GNUNET_PeerIdentity) != 0)
   {
     GNUNET_break_op (0);
-    return GNUNET_OK;      
-  }  
-  
+    return GNUNET_OK;
+  }
+
   trail = (const struct GNUNET_PeerIdentity *) &vsrm[1];
   querying_peer = vsrm->querying_peer;
   trail_direction = ntohl (vsrm->trail_direction);
@@ -4835,12 +4868,12 @@
                                   probable_successor, trail, trail_length);
     return GNUNET_OK;
   }
-  
+
   /*If you are not the querying peer then pass on the message */
   GNUNET_assert (NULL != (next_hop =
                          GDS_ROUTING_get_next_hop (trail_id, 
trail_direction)));
-  GNUNET_assert (NULL != 
-                (target_friend = 
+  GNUNET_assert (NULL !=
+                (target_friend =
                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop)));
   GDS_NEIGHBOURS_send_verify_successor_result (querying_peer,
                                                vsrm->current_successor,
@@ -4852,7 +4885,7 @@
 }
 
 
-/* 
+/*
  * Core handle for p2p notify new successor messages.
  * @param cls closure
  * @param message message
@@ -4860,7 +4893,7 @@
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
-handle_dht_p2p_notify_new_successor(void *cls, 
+handle_dht_p2p_notify_new_successor(void *cls,
                                     const struct GNUNET_PeerIdentity *peer,
                                     const struct GNUNET_MessageHeader *message)
 {
@@ -4876,7 +4909,7 @@
   uint32_t trail_length;
 
   msize = ntohs (message->size);
-  
+
   /* We have the trail to reach from source to new successor. */
   if (msize < sizeof (struct PeerNotifyNewSuccessorMessage))
   {
@@ -4887,20 +4920,20 @@
   nsm = (const struct PeerNotifyNewSuccessorMessage *) message;
   trail_length = (msize - sizeof (struct PeerNotifyNewSuccessorMessage))/
                   sizeof (struct GNUNET_PeerIdentity);
-  if ((msize - sizeof (struct PeerNotifyNewSuccessorMessage)) % 
+  if ((msize - sizeof (struct PeerNotifyNewSuccessorMessage)) %
       sizeof (struct GNUNET_PeerIdentity) != 0)
   {
     GNUNET_break_op (0);
-    return GNUNET_OK;      
+    return GNUNET_OK;
   }
-  
+
   trail = (struct GNUNET_PeerIdentity *) &nsm[1];
   source  = nsm->source_peer;
   new_successor = nsm->new_successor;
-  trail_id = nsm->trail_id;  
+  trail_id = nsm->trail_id;
 
-  //FIXME: add a check to make sure peer is correct. 
-  
+  //FIXME: add a check to make sure peer is correct.
+
   /* I am the new_successor to source_peer. */
   if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &new_successor))
   {
@@ -4908,33 +4941,33 @@
     compare_and_update_predecessor (source, trail, trail_length);
     return GNUNET_OK;
   }
-  
+
   GNUNET_assert(trail_length > 0);
   /* I am part of trail to reach to successor. */
   my_index = search_my_index (trail, trail_length);
-  if (-1 == my_index) 
+  if (-1 == my_index)
   {
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
- 
-  if ((trail_length-1) == my_index) 
+
+  if ((trail_length-1) == my_index)
     next_hop = new_successor;
   else
     next_hop = trail[my_index + 1];
- 
 
+
   /* Add an entry in routing table for trail from source to its new successor. 
*/
   GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, *peer, next_hop));
 
-  GNUNET_assert (NULL != 
-                (target_friend = 
+  GNUNET_assert (NULL !=
+                (target_friend =
                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop)));
   GDS_NEIGHBOURS_send_notify_new_successor (source, new_successor, trail,
                                             trail_length,
                                             trail_id, target_friend);
   return GNUNET_OK;
-  
+
 }
 
 
@@ -4970,34 +5003,34 @@
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
-  
+
   trail_rejection = (const struct PeerTrailRejectionMessage *) message;
   trail_length = (msize - sizeof (struct PeerTrailRejectionMessage))/
                   sizeof (struct GNUNET_PeerIdentity);
-  if ((msize - sizeof (struct PeerTrailRejectionMessage)) % 
+  if ((msize - sizeof (struct PeerTrailRejectionMessage)) %
       sizeof (struct GNUNET_PeerIdentity) != 0)
   {
     GNUNET_break_op (0);
-    return GNUNET_OK;      
-  }           
+    return GNUNET_OK;
+  }
 
   trail_peer_list = (const struct GNUNET_PeerIdentity *)&trail_rejection[1];
   is_predecessor = ntohl (trail_rejection->is_predecessor);
   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 =
           GNUNET_ntohll (trail_rejection->ultimate_destination_finger_value);
 
   /* First set the congestion time of the friend that sent you this message. */
-  GNUNET_assert (NULL != 
-                 (target_friend = 
+  GNUNET_assert (NULL !=
+                 (target_friend =
                   GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer)));
-  target_friend->congestion_timestamp = 
+  target_friend->congestion_timestamp =
           GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
                                     congestion_timeout);
 
-  /* I am the source peer which wants to setup the trail. Do nothing. 
+  /* I am the source peer which wants to setup the trail. Do nothing.
    * send_find_finger_trail_task is scheduled periodically.*/
   if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &source)))
     return GNUNET_OK;
@@ -5007,7 +5040,7 @@
   {
     struct GNUNET_PeerIdentity *new_trail;
     unsigned int new_trail_length;
-    
+
     /* Remove yourself from the trail setup so far. */
     if (trail_length == 1)
     {
@@ -5017,17 +5050,17 @@
     }
     else
     {
-      memcpy (&next_hop , &trail_peer_list[trail_length - 2], 
+      memcpy (&next_hop , &trail_peer_list[trail_length - 2],
               sizeof (struct GNUNET_PeerIdentity));
-      
+
       /* Remove myself from the trail. */
       new_trail_length = trail_length -1;
       new_trail = GNUNET_malloc (new_trail_length * sizeof (struct 
GNUNET_PeerIdentity));
       memcpy (new_trail, trail_peer_list, new_trail_length * sizeof (struct 
GNUNET_PeerIdentity));
     }
 
-    GNUNET_assert (NULL != 
-                  (target_friend = 
+    GNUNET_assert (NULL !=
+                  (target_friend =
                     GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop)));
     GDS_NEIGHBOURS_send_trail_rejection (source,
                                          ultimate_destination_finger_value,
@@ -5037,28 +5070,28 @@
     GNUNET_free (new_trail);
     return GNUNET_OK;
   }
-  
+
   struct Closest_Peer successor;
   successor = find_successor (ultimate_destination_finger_value, 
is_predecessor);
-  
+
   /* Am I the final destination? */
-  if (0 == (GNUNET_CRYPTO_cmp_peer_identity 
(&successor.best_known_destination, 
+  if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&successor.best_known_destination,
                                              &my_identity)))
   {
     if (0 == trail_length)
       next_peer = source;
     else
       next_peer = trail_peer_list[trail_length-1];
-    
-    GNUNET_assert (NULL != 
-                  (target_friend = 
+
+    GNUNET_assert (NULL !=
+                  (target_friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_peer)));
-    
+
     GDS_NEIGHBOURS_send_trail_setup_result (source,
                                             my_identity,
                                             target_friend, trail_length,
                                             trail_peer_list,
-                                            is_predecessor, 
+                                            is_predecessor,
                                             ultimate_destination_finger_value,
                                             trail_id);
   }
@@ -5069,8 +5102,8 @@
     memcpy (peer_list, trail_peer_list, trail_length * sizeof (struct 
GNUNET_PeerIdentity));
     peer_list[trail_length] = my_identity;
 
-    GNUNET_assert (NULL != 
-                  (target_friend = 
+    GNUNET_assert (NULL !=
+                  (target_friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop)));
 
     GDS_NEIGHBOURS_send_trail_setup (source,
@@ -5108,40 +5141,40 @@
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  
+
   trail_compression = (const struct PeerTrailCompressionMessage *) message;
   trail_id = trail_compression->trail_id;
- 
+
   /* Am I the new first friend to reach to finger of this trail. */
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity 
(&trail_compression->new_first_friend,
                                              &my_identity)))
   {
-    GNUNET_assert (NULL != 
+    GNUNET_assert (NULL !=
                   (GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                       
&trail_compression->source_peer)));
-    
+
     /* Update your prev hop to source of this message. */
-    GNUNET_assert (GNUNET_SYSERR != 
+    GNUNET_assert (GNUNET_SYSERR !=
                   (GDS_ROUTING_update_trail_prev_hop (trail_id,
                                                       
trail_compression->source_peer)));
     return GNUNET_OK;
   }
-  
+
   /* Pass the message to next hop to finally reach to new_first_friend. */
   next_hop = GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST);
-  
-  if (NULL == next_hop) 
+
+  if (NULL == next_hop)
   {
-    GNUNET_break (0); 
+    GNUNET_break (0);
     return GNUNET_OK;
   }
-  
-  GNUNET_assert (NULL != 
-                (target_friend = 
+
+  GNUNET_assert (NULL !=
+                (target_friend =
                  GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop)));
-  
+
   GDS_ROUTING_remove_trail (trail_id);
-  
+
   GDS_NEIGHBOURS_send_trail_compression (trail_compression->source_peer,
                                          trail_id,
                                          trail_compression->new_first_friend,
@@ -5154,7 +5187,7 @@
  * Core handler for trail teardown message.
  * @param cls closure
  * @param message message
- * @param peer sender of this messsage. 
+ * @param peer sender of this messsage.
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
@@ -5166,24 +5199,24 @@
   struct GNUNET_HashCode trail_id;
   struct GNUNET_PeerIdentity *next_hop;
   size_t msize;
-  
+
   msize = ntohs (message->size);
-  
+
   /* Here we pass only the trail id. */
   if (msize != sizeof (struct PeerTrailTearDownMessage))
   {
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  
+
   trail_teardown = (const struct PeerTrailTearDownMessage *) message;
   trail_direction = ntohl (trail_teardown->trail_direction);
   trail_id = trail_teardown->trail_id;
-  
+
   /* Check if peer is the real peer from which we should get this message.*/
   /* Get the prev_hop for this trail by getting the next hop in opposite 
direction. */
 #if 0
-  GNUNET_assert (NULL != (prev_hop = 
+  GNUNET_assert (NULL != (prev_hop =
                  GDS_ROUTING_get_next_hop (trail_id, !trail_direction)));
   if (0 != GNUNET_CRYPTO_cmp_peer_identity (prev_hop, peer))
   {
@@ -5191,35 +5224,35 @@
     return GNUNET_SYSERR;
   }
 #endif
-  
+
   next_hop = GDS_ROUTING_get_next_hop (trail_id, trail_direction);
-  
+
   if (NULL == next_hop)
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
- 
+
   /* I am the next hop, which means I am the final destination. */
   if (0 == GNUNET_CRYPTO_cmp_peer_identity (next_hop, &my_identity))
   {
     GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
     return GNUNET_OK;
   }
-  else 
+  else
   {
     /* If not final destination, then send a trail teardown message to next 
hop.*/
     GNUNET_assert (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
next_hop));
     GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
     GDS_NEIGHBOURS_send_trail_teardown (trail_id, trail_direction, *next_hop);
   }
-  
+
   return GNUNET_OK;
 }
 
 
 /**
- * Core handle for p2p add trail message. 
+ * Core handle for p2p add trail message.
  * @param cls closure
  * @param message message
  * @param peer peer identity this notification is about
@@ -5251,12 +5284,12 @@
   add_trail = (const struct PeerAddTrailMessage *) message;
   trail_length = (msize - sizeof (struct PeerAddTrailMessage))/
                   sizeof (struct GNUNET_PeerIdentity);
-  if ((msize - sizeof (struct PeerAddTrailMessage)) % 
+  if ((msize - sizeof (struct PeerAddTrailMessage)) %
       sizeof (struct GNUNET_PeerIdentity) != 0)
   {
     GNUNET_break_op (0);
-    return GNUNET_OK;      
-  }           
+    return GNUNET_OK;
+  }
 
   trail = (const struct GNUNET_PeerIdentity *)&add_trail[1];
   destination_peer = add_trail->destination_peer;
@@ -5266,35 +5299,35 @@
   //FIXME: add a check that sender peer is not malicious. Make it a generic
   // function so that it can be used in all other functions where we need the
   // same functionality.
-  
+
   /* I am not the destination of the trail. */
   if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &destination_peer))
   {
     struct FriendInfo *target_friend;
-   
+
     /* Get my location in the trail. */
     my_index = search_my_index (trail, trail_length);
     if (-1 == my_index)
     {
-      
+
       GNUNET_break_op (0);
       return GNUNET_SYSERR;
     }
-    
-    
+
+
     if ((trail_length - 1) == my_index)
-    { 
+    {
       next_hop = destination_peer;
     }
     else
     {
       next_hop = trail[my_index + 1];
     }
-    
+
     /* Add in your routing table. */
     GNUNET_assert (GNUNET_OK == GDS_ROUTING_add (trail_id, next_hop, *peer));
-    GNUNET_assert (NULL != 
-                  (target_friend = 
+    GNUNET_assert (NULL !=
+                  (target_friend =
                    GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
&next_hop)));
     GDS_NEIGHBOURS_send_add_trail (source_peer, destination_peer, trail_id,
                                    trail, trail_length, target_friend);
@@ -5307,11 +5340,11 @@
 
 
 /**
- * Free the finger trail in which the first friend to reach to a finger is 
+ * Free the finger trail in which the first friend to reach to a finger is
  * disconnected_friend. Also remove entry from routing table for that 
particular
- * trail id. 
+ * trail id.
  * @param disconnected_friend PeerIdentity of friend which got disconnected
- * @param remove_finger Finger whose trail we need to check if it has 
+ * @param remove_finger Finger whose trail we need to check if it has
  *                      disconnected_friend as the first hop.
  * @return Total number of trails in which disconnected_friend was the first
  *         hop.
@@ -5322,67 +5355,67 @@
 {
   unsigned int matching_trails_count;
   int i;
-  
+
   /* Number of trails with disconnected_friend as the first hop in the trail
    * to reach from me to remove_finger, NOT including endpoints. */
   matching_trails_count = 0;
-  
+
   /* Iterate over all the trails of finger. */
   for (i = 0; i < remove_finger->trails_count; i++)
   {
     struct Trail *trail;
     trail = &remove_finger->trail_list[i];
-    
-    /* This assertion is ensure that there are no gaps in the trail list. 
+
+    /* This assertion is ensure that there are no gaps in the trail list.
      REMOVE IT AFTERWARDS. */
     GNUNET_assert (GNUNET_YES == trail->is_present);
-    
+
     /* First friend to reach to finger is disconnected_peer. */
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&trail->trail_head->peer,
                                               disconnected_friend))
     {
       struct GNUNET_PeerIdentity *next_hop;
       struct FriendInfo *remove_friend;
-      
-      GNUNET_assert (NULL != 
-                    (remove_friend = 
-                     GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+
+      GNUNET_assert (NULL !=
+                    (remove_friend =
+                     GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                         disconnected_friend)));
       /* FIXME: removing no but check it. */
       //remove_friend->trails_count--;
-      next_hop = GDS_ROUTING_get_next_hop (trail->trail_id, 
+      next_hop = GDS_ROUTING_get_next_hop (trail->trail_id,
                                            GDS_ROUTING_SRC_TO_DEST);
-      
+
       /* Here it may happen that as all the peers got disconnected, the entry 
in
        routing table for that particular trail has been removed, because the
        previously disconnected peer was either a next hop or prev hop of that
        peer. */
       if (NULL == next_hop)
         continue;
-      
+
       GNUNET_assert (0 == (GNUNET_CRYPTO_cmp_peer_identity 
(disconnected_friend,
                                                             next_hop)));
       matching_trails_count++;
       GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail->trail_id));
-      
+
       free_trail (trail);
       trail->is_present = GNUNET_NO;
     }
-  }  
+  }
   return matching_trails_count;
 }
 
 
 /**
- * Iterate over finger_table entries. 
- * 0. Ignore finger which is my_identity or if no valid entry present at 
- *    that finger index. 
+ * Iterate over finger_table entries.
+ * 0. Ignore finger which is my_identity or if no valid entry present at
+ *    that finger index.
  * 1. If disconnected_friend is a finger, then remove the routing entry from
-      your own table. Free the trail. 
+      your own table. Free the trail.
  * 2. Check if disconnected_friend is the first friend in the trail to reach 
to a finger.
- *   2.1 Remove all the trails and entry from routing table in which 
disconnected 
- *       friend is the first friend in the trail. If disconnected_friend is 
the 
- *       first friend in all the trails to reach finger, then remove the 
finger. 
+ *   2.1 Remove all the trails and entry from routing table in which 
disconnected
+ *       friend is the first friend in the trail. If disconnected_friend is the
+ *       first friend in all the trails to reach finger, then remove the 
finger.
  * @param disconnected_friend Peer identity of friend which got disconnected.
  */
 static void
@@ -5392,7 +5425,7 @@
   struct FriendInfo *remove_friend;
   int removed_trails_count;
   int i;
-  
+
   /* Iterate over finger table entries. */
   for (i = 0; i < MAX_FINGERS; i++)
   {
@@ -5401,25 +5434,25 @@
     /* No finger stored at this trail index. */
     if (GNUNET_NO == remove_finger->is_present)
       continue;
-    
+
     /* I am my own finger, then ignore this finger. */
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&remove_finger->finger_identity,
                                               &my_identity))
       continue;
-    
+
     /* Is disconnected_peer a finger? */
     if (0 == GNUNET_CRYPTO_cmp_peer_identity (disconnected_peer,
                                               &remove_finger->finger_identity))
     {
       struct GNUNET_PeerIdentity *next_hop;
       struct GNUNET_HashCode trail_id;
-      
-      
+
+
       GNUNET_assert (GNUNET_YES == (remove_finger->trail_list[0].is_present));
       trail_id = remove_finger->trail_list[0].trail_id;
-     
-      if(NULL != 
-              (next_hop = 
+
+      if(NULL !=
+              (next_hop =
                GDS_ROUTING_get_next_hop (trail_id, GDS_ROUTING_SRC_TO_DEST)))
       {
         GNUNET_assert (0 ==
@@ -5427,11 +5460,11 @@
                                                         
&remove_finger->finger_identity)));
         GNUNET_assert (GNUNET_YES == GDS_ROUTING_remove_trail (trail_id));
         GNUNET_assert (NULL !=
-                       (remove_friend = 
-                        GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+                       (remove_friend =
+                        GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                            
disconnected_peer)));
       }
-      
+
       remove_finger->trail_list[0].is_present = GNUNET_NO;
       //GNUNET_assert (0 != remove_friend->trails_count);
       //remove_friend->trails_count--; //FIXME; CHECK WHY IT FAILS AND THEN 
UNCOMMENT.
@@ -5439,17 +5472,17 @@
       memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
       continue;
     }
-    
+
     /* If finger is a friend but not disconnected_friend, then continue. */
-    if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap, 
+    if (NULL != GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                    
&remove_finger->finger_identity))
       continue;
-    
-    /* Iterate over the list of trails to reach remove_finger. Check if 
+
+    /* Iterate over the list of trails to reach remove_finger. Check if
      * disconnected_friend is the first friend in any of the trail. */
-    removed_trails_count = remove_matching_trails (disconnected_peer, 
+    removed_trails_count = remove_matching_trails (disconnected_peer,
                                                    remove_finger);
-    remove_finger->trails_count = 
+    remove_finger->trails_count =
             remove_finger->trails_count - removed_trails_count;
     /* All the finger trails had disconnected_friend as the first friend,
      * so free the finger. */
@@ -5457,7 +5490,7 @@
     {
       remove_finger->is_present = GNUNET_NO;
       memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
-    }      
+    }
   }
 }
 
@@ -5483,23 +5516,23 @@
 
   /* Remove fingers with peer as first friend or if peer is a finger. */
   remove_matching_fingers (peer);
-  
+
   /* Remove any trail from routing table of which peer is a part of. This 
function
    * internally sends a trail teardown message in the direction of which
    * disconnected peer is not part of. */
   GNUNET_assert (GNUNET_SYSERR != GDS_ROUTING_remove_trail_by_peer (peer));
-  
+
   //GNUNET_assert (0 == remove_friend->trails_count); //FIXME; why should this 
fai.
-  
+
   /* Remove peer from friend_peermap. */
   GNUNET_assert (GNUNET_YES ==
                  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);
@@ -5529,7 +5562,7 @@
   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, 
+  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
                                                             peer_identity))
   {
     GNUNET_break (0);
@@ -5548,8 +5581,8 @@
                                                     
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
 
 
-  /* got a first connection, good time to start with FIND FINGER TRAIL 
requests...*/ 
-  if (GNUNET_SCHEDULER_NO_TASK == find_finger_trail_task) 
+  /* got a first connection, good time to start with FIND FINGER TRAIL 
requests...*/
+  if (GNUNET_SCHEDULER_NO_TASK == find_finger_trail_task)
     find_finger_trail_task = GNUNET_SCHEDULER_add_now 
(&send_find_finger_trail_message, NULL);
 }
 
@@ -5565,7 +5598,7 @@
            const struct GNUNET_PeerIdentity *identity)
 {
   my_identity = *identity;
-  
+
   uint64_t my_id64;
   memcpy (&my_id64, &my_identity, sizeof (uint64_t));
   my_id64 = GNUNET_ntohll (my_id64);
@@ -5581,16 +5614,7 @@
 static void
 finger_table_init ()
 {
-  unsigned int i;
-  unsigned int j;
- 
-  for(i = 0; i < MAX_FINGERS; i++)
-  {
-    finger_table[i].is_present = GNUNET_NO;
-    for (j = 0; j < MAXIMUM_TRAILS_PER_FINGER; j++)
-      finger_table[i].trail_list[j].is_present = GNUNET_NO;
-    memset ((void *)&finger_table[i], 0, sizeof (finger_table[i]));
-  }
+  memset (&finger_table, 0, sizeof (finger_table));
 }
 
 
@@ -5611,9 +5635,9 @@
     {&handle_dht_p2p_verify_successor_result, 
GNUNET_MESSAGE_TYPE_XDHT_P2P_VERIFY_SUCCESSOR_RESULT, 0},
     {&handle_dht_p2p_notify_new_successor, 
GNUNET_MESSAGE_TYPE_XDHT_P2P_NOTIFY_NEW_SUCCESSOR, 0},
     {&handle_dht_p2p_trail_setup_rejection, 
GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_SETUP_REJECTION, 0},
-    {&handle_dht_p2p_trail_compression, 
GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_COMPRESSION, 
+    {&handle_dht_p2p_trail_compression, 
GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_COMPRESSION,
                                         sizeof (struct 
PeerTrailCompressionMessage)},
-    {&handle_dht_p2p_trail_teardown, 
GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN, 
+    {&handle_dht_p2p_trail_teardown, 
GNUNET_MESSAGE_TYPE_XDHT_P2P_TRAIL_TEARDOWN,
                                      sizeof (struct PeerTrailTearDownMessage)},
     {&handle_dht_p2p_add_trail, GNUNET_MESSAGE_TYPE_XDHT_P2P_ADD_TRAIL, 0},
     {NULL, 0, 0}
@@ -5628,7 +5652,7 @@
 
   friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
   finger_table_init ();
-  
+
   return GNUNET_OK;
 }
 
@@ -5655,7 +5679,7 @@
     GNUNET_SCHEDULER_cancel (find_finger_trail_task);
     find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  
+
   if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_task)
   {
     GNUNET_SCHEDULER_cancel (send_verify_successor_task);
@@ -5675,4 +5699,4 @@
   return my_identity;
 }
 
-/* end of gnunet-service-xdht_neighbours.c */
\ No newline at end of file
+/* end of gnunet-service-xdht_neighbours.c */




reply via email to

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