fix
[oweals/gnunet.git] / src / dht / gnunet-service-dht.c
index ae57c0ea7236487f39483f9c7bcae95deeb1f841..454dfe5814c638301ec11ac58e59a5ff201593e3 100644 (file)
@@ -20,7 +20,7 @@
 
 /**
  * @file dht/gnunet-service-dht.c
- * @brief main DHT service shell, building block for DHT implementations
+ * @brief GNUnet DHT service
  * @author Christian Grothoff
  * @author Nathan Evans
  */
@@ -71,6 +71,8 @@
  */
 #define DEFAULT_BUCKET_SIZE 4
 
+#define DEFAULT_CORE_QUEUE_SIZE 32
+
 /**
  * Minimum number of peers we need for "good" routing,
  * any less than this and we will allow messages to
@@ -95,7 +97,7 @@
 /**
  * How long to keep recent requests around by default.
  */
-#define DEFAULT_RECENT_REMOVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
+#define DEFAULT_RECENT_REMOVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
 
 /**
  * Default time to wait to send find peer messages sent by the dht service.
  */
 #define DHT_DEFAULT_FIND_PEER_IMPORTANCE 8
 
-/**
- * Default replication parameter for find peer messages sent by the dht service.
- */
-#define DHT_DEFAULT_PUT_REPLICATION 4
-
 /**
  * Default replication parameter for find peer messages sent by the dht service.
  */
 /**
  * How many initial requests to send out (in true Kademlia fashion)
  */
-#define DHT_KADEMLIA_REPLICATION 3
+#define DEFAULT_KADEMLIA_REPLICATION 3
 
 /*
  * Default frequency for sending malicious get messages
  */
-#define DEFAULT_MALICIOUS_GET_FREQUENCY 1000 /* Number of milliseconds */
+#define DEFAULT_MALICIOUS_GET_FREQUENCY 1000    /* Number of milliseconds */
 
 /*
  * Default frequency for sending malicious put messages
  */
-#define DEFAULT_MALICIOUS_PUT_FREQUENCY 1000 /* Default is in milliseconds */
+#define DEFAULT_MALICIOUS_PUT_FREQUENCY 1000    /* Default is in milliseconds */
 
 
 #define DHT_DEFAULT_PING_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1)
@@ -179,32 +176,32 @@ enum ConvergenceOptions
    /**
     * Use the linear method for convergence.
     */
-   DHT_CONVERGE_LINEAR,
+  DHT_CONVERGE_LINEAR,
 
    /**
     * Converge using a fast converging square
     * function.
     */
-   DHT_CONVERGE_SQUARE,
+  DHT_CONVERGE_SQUARE,
 
    /**
     * Converge using a slower exponential
     * function.
     */
-   DHT_CONVERGE_EXPONENTIAL,
+  DHT_CONVERGE_EXPONENTIAL,
 
    /**
     * Don't do any special convergence, allow
     * the algorithm to hopefully route to closer
     * peers more often.
     */
-   DHT_CONVERGE_RANDOM,
+  DHT_CONVERGE_RANDOM,
 
    /**
     * Binary convergence, start routing to closest
     * only after set number of hops.
     */
-   DHT_CONVERGE_BINARY
+  DHT_CONVERGE_BINARY
 };
 
 /**
@@ -240,7 +237,7 @@ struct P2PPendingMessage
   /**
    * Actual message to be sent; // avoid allocation
    */
-  const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
+  const struct GNUNET_MessageHeader *msg;       // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
 
 };
 
@@ -259,6 +256,11 @@ struct PeerInfo
    */
   struct PeerInfo *prev;
 
+  /**
+   * Count of outstanding messages for peer.
+   */
+  unsigned int pending_count;
+
   /**
    * Head of pending messages to be sent to this peer.
    */
@@ -290,19 +292,21 @@ struct PeerInfo
   struct GNUNET_CORE_InformationRequestContext *info_ctx;
 
   /**
-   * What is the average latency for replies received?
+   * What is the identity of the peer?
    */
-  struct GNUNET_TIME_Relative latency;
+  struct GNUNET_PeerIdentity id;
 
+#if 0
   /**
-   * What is the identity of the peer?
+   * What is the average latency for replies received?
    */
-  struct GNUNET_PeerIdentity id;
+  struct GNUNET_TIME_Relative latency;
 
   /**
    * Transport level distance to peer.
    */
   unsigned int distance;
+#endif
 
   /**
    * Holds matching bits from peer to current target,
@@ -310,7 +314,7 @@ struct PeerInfo
    * be considered a really bad idea.
    * FIXME: remove this value (create struct which holds
    *        a single peerinfo and the matching bits, use
-   *        that to pass to comparitor)
+   *        that to pass to comparator)
    */
   unsigned int matching_bits;
 
@@ -359,7 +363,7 @@ struct PendingMessage
   /**
    * Actual message to be sent; // avoid allocation
    */
-  const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
+  const struct GNUNET_MessageHeader *msg;       // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
 
 };
 
@@ -794,7 +798,7 @@ static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
 /**
  * The lowest currently used bucket.
  */
-static unsigned int lowest_bucket; /* Initially equal to MAX_BUCKETS - 1 */
+static unsigned int lowest_bucket;      /* Initially equal to MAX_BUCKETS - 1 */
 
 /**
  * The maximum number of hops before we stop routing messages.
@@ -815,7 +819,7 @@ static int use_max_hops;
  * The buckets (Kademlia routing table, complete with growth).
  * Array of size MAX_BUCKET_SIZE.
  */
-static struct PeerBucket k_buckets[MAX_BUCKETS]; /* From 0 to MAX_BUCKETS - 1 */
+static struct PeerBucket k_buckets[MAX_BUCKETS];        /* From 0 to MAX_BUCKETS - 1 */
 
 /**
  * Hash map of all known peers, for easy removal from k_buckets on disconnect.
@@ -830,7 +834,7 @@ static struct GNUNET_CONTAINER_MultiHashMap *recent_find_peer_requests;
 /**
  * Maximum size for each bucket.
  */
-static unsigned int bucket_size = DEFAULT_BUCKET_SIZE; /* Initially equal to DEFAULT_BUCKET_SIZE */
+static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;  /* Initially equal to DEFAULT_BUCKET_SIZE */
 
 /**
  * List of active clients.
@@ -882,6 +886,11 @@ static unsigned long long malicious_get_frequency;
  */
 static unsigned long long malicious_put_frequency;
 
+/**
+ * Kademlia replication
+ */
+static unsigned long long kademlia_replication;
+
 /**
  * Reply times for requests, if we are busy, don't send any
  * more requests!
@@ -902,14 +911,11 @@ static struct GNUNET_BLOCK_Context *block_context;
 /**
  * Forward declaration.
  */
-static size_t 
-send_generic_reply (void *cls, size_t size, void *buf);
+static size_t send_generic_reply (void *cls, size_t size, void *buf);
 
 
 /** Declare here so retry_core_send is aware of it */
-static size_t 
-core_transmit_notify (void *cls,
-                     size_t size, void *buf);
+static size_t core_transmit_notify (void *cls, size_t size, void *buf);
 
 /**
  * Convert unique ID to hash code.
@@ -918,11 +924,10 @@ core_transmit_notify (void *cls,
  * @param hash set to uid (extended with zeros)
  */
 static void
-hash_from_uid (uint64_t uid,
-               GNUNET_HashCode *hash)
+hash_from_uid (uint64_t uid, GNUNET_HashCode * hash)
 {
-  memset (hash, 0, sizeof(GNUNET_HashCode));
-  *((uint64_t*)hash) = uid;
+  memset (hash, 0, sizeof (GNUNET_HashCode));
+  *((uint64_t *) hash) = uid;
 }
 
 #if AVG
@@ -933,31 +938,31 @@ hash_from_uid (uint64_t uid,
  * @return the average time between asking core to send a message
  *         and when the buffer for copying it is passed
  */
-static struct GNUNET_TIME_Relative get_average_send_delay()
+static struct GNUNET_TIME_Relative
+get_average_send_delay ()
 {
   unsigned int i;
   unsigned int divisor;
   struct GNUNET_TIME_Relative average_time;
-  average_time = GNUNET_TIME_relative_get_zero();
+  average_time = GNUNET_TIME_relative_get_zero ();
   divisor = 0;
   for (i = 0; i < MAX_REPLY_TIMES; i++)
-  {
-    average_time = GNUNET_TIME_relative_add(average_time, reply_times[i]);
-    if (reply_times[i].abs_value == (uint64_t)0)
-      continue;
-    else
-      divisor++;
-  }
+    {
+      average_time = GNUNET_TIME_relative_add (average_time, reply_times[i]);
+      if (reply_times[i].abs_value == (uint64_t) 0)
+        continue;
+      else
+        divisor++;
+    }
   if (divisor == 0)
-  {
-    return average_time;
-  }
-
-  average_time = GNUNET_TIME_relative_divide(average_time, divisor);
-  fprintf(stderr, 
-         "Avg send delay: %u sends is %llu\n",
-         divisor, 
-         (unsigned long long) average_time.abs_value);
+    {
+      return average_time;
+    }
+
+  average_time = GNUNET_TIME_relative_divide (average_time, divisor);
+  fprintf (stderr,
+           "Avg send delay: %u sends is %llu\n",
+           divisor, (unsigned long long) average_time.abs_value);
   return average_time;
 }
 #endif
@@ -967,7 +972,8 @@ static struct GNUNET_TIME_Relative get_average_send_delay()
  * so the next time around we may have a chance at sending
  * again.
  */
-static void decrease_max_send_delay(struct GNUNET_TIME_Relative max_time)
+static void
+decrease_max_send_delay (struct GNUNET_TIME_Relative max_time)
 {
   unsigned int i;
   for (i = 0; i < MAX_REPLY_TIMES; i++)
@@ -986,26 +992,27 @@ static void decrease_max_send_delay(struct GNUNET_TIME_Relative max_time)
  * @return the average time between asking core to send a message
  *         and when the buffer for copying it is passed
  */
-static struct GNUNET_TIME_Relative get_max_send_delay()
+static struct GNUNET_TIME_Relative
+get_max_send_delay ()
 {
   unsigned int i;
   struct GNUNET_TIME_Relative max_time;
-  max_time = GNUNET_TIME_relative_get_zero();
+  max_time = GNUNET_TIME_relative_get_zero ();
 
   for (i = 0; i < MAX_REPLY_TIMES; i++)
-  {
-    if (reply_times[i].rel_value > max_time.rel_value)
-      max_time.rel_value = reply_times[i].rel_value;
-  }
+    {
+      if (reply_times[i].rel_value > max_time.rel_value)
+        max_time.rel_value = reply_times[i].rel_value;
+    }
 
   if (max_time.rel_value > MAX_REQUEST_TIME.rel_value)
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n", 
-              (unsigned long long) max_time.rel_value);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n",
+                (unsigned long long) max_time.rel_value);
   return max_time;
 }
 
 static void
-increment_stats(const char *value)
+increment_stats (const char *value)
 {
   if (stats != NULL)
     {
@@ -1017,8 +1024,7 @@ increment_stats(const char *value)
  *  Try to send another message from our core send list
  */
 static void
-try_core_send (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+try_core_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PeerInfo *peer = cls;
   struct P2PPendingMessage *pending;
@@ -1030,24 +1036,27 @@ try_core_send (void *cls,
     return;
 
   if (peer->th != NULL)
-    return; /* Message send already in progress */
+    return;                     /* Message send already in progress */
 
   pending = peer->head;
   if (pending != NULL)
     {
-      ssize = ntohs(pending->msg->size);
+      ssize = ntohs (pending->msg->size);
 #if DEBUG_DHT > 1
-     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n", my_short_id,
-                "DHT", ssize, GNUNET_i2s(&peer->id));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n",
+                  my_short_id, "DHT", ssize, GNUNET_i2s (&peer->id));
 #endif
-      pending->scheduled = GNUNET_TIME_absolute_get();
+      pending->scheduled = GNUNET_TIME_absolute_get ();
       reply_counter++;
       if (reply_counter >= MAX_REPLY_TIMES)
-       reply_counter = 0;
-      peer->th = GNUNET_CORE_notify_transmit_ready(coreAPI, pending->importance,
-                                                   pending->timeout, &peer->id,
-                                                   ssize, &core_transmit_notify, peer);
+        reply_counter = 0;
+      peer->th =
+        GNUNET_CORE_notify_transmit_ready (coreAPI, pending->importance,
+                                           pending->timeout, &peer->id, ssize,
+                                           &core_transmit_notify, peer);
+      if (peer->th == NULL)
+        increment_stats ("# notify transmit ready failed");
     }
 }
 
@@ -1060,41 +1069,52 @@ try_core_send (void *cls,
  * @param peer the peer to forward the message to
  * @param msg_ctx the context of the message (hop count, bloom, etc.)
  */
-static void 
+static void
 forward_result_message (const struct GNUNET_MessageHeader *msg,
-                       struct PeerInfo *peer,
-                       struct DHT_MessageContext *msg_ctx)
+                        struct PeerInfo *peer,
+                        struct DHT_MessageContext *msg_ctx)
 {
   struct GNUNET_DHT_P2PRouteResultMessage *result_message;
   struct P2PPendingMessage *pending;
   size_t msize;
   size_t psize;
 
-  increment_stats(STAT_RESULT_FORWARDS);
-  msize = sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs(msg->size);
-  GNUNET_assert(msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
-  psize = sizeof(struct P2PPendingMessage) + msize;
-  pending = GNUNET_malloc(psize);
-  pending->msg = (struct GNUNET_MessageHeader *)&pending[1];
+  increment_stats (STAT_RESULT_FORWARDS);
+  msize =
+    sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs (msg->size);
+  GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
+  psize = sizeof (struct P2PPendingMessage) + msize;
+  pending = GNUNET_malloc (psize);
+  pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
   pending->importance = DHT_SEND_PRIORITY;
-  pending->timeout = GNUNET_TIME_relative_get_forever();
-  result_message = (struct GNUNET_DHT_P2PRouteResultMessage *)pending->msg;
-  result_message->header.size = htons(msize);
-  result_message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT);
-  result_message->put_path_length = htons(0); /* FIXME: implement */
-  result_message->get_path_length = htons(0); /* FIXME: implement */
-  result_message->options = htonl(msg_ctx->msg_options);
-  result_message->hop_count = htonl(msg_ctx->hop_count + 1);
-  GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(msg_ctx->bloom, result_message->bloomfilter, DHT_BLOOM_SIZE));
-  result_message->unique_id = GNUNET_htonll(msg_ctx->unique_id);
-  memcpy(&result_message->key, &msg_ctx->key, sizeof(GNUNET_HashCode));
-  memcpy(&result_message[1], msg, ntohs(msg->size));
+  pending->timeout = GNUNET_TIME_relative_get_forever ();
+  result_message = (struct GNUNET_DHT_P2PRouteResultMessage *) pending->msg;
+  result_message->header.size = htons (msize);
+  result_message->header.type =
+    htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT);
+  result_message->put_path_length = htons (0);  /* FIXME: implement */
+  result_message->get_path_length = htons (0);  /* FIXME: implement */
+  result_message->options = htonl (msg_ctx->msg_options);
+  result_message->hop_count = htonl (msg_ctx->hop_count + 1);
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
+                                                            result_message->
+                                                            bloomfilter,
+                                                            DHT_BLOOM_SIZE));
+  result_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
+  memcpy (&result_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
+  memcpy (&result_message[1], msg, ntohs (msg->size));
 #if DEBUG_DHT > 1
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Adding pending message size %d for peer %s\n", my_short_id, "DHT", msize, GNUNET_i2s(&peer->id));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%s:%s Adding pending message size %d for peer %s\n",
+              my_short_id, "DHT", msize, GNUNET_i2s (&peer->id));
 #endif
-  GNUNET_CONTAINER_DLL_insert_after(peer->head, peer->tail, peer->tail, pending);
+  peer->pending_count++;
+  increment_stats ("# pending messages scheduled");
+  GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
+                                     pending);
   if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
-    peer->send_task = GNUNET_SCHEDULER_add_now(&try_core_send, peer);
+    peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
 }
 
 
@@ -1107,9 +1127,8 @@ forward_result_message (const struct GNUNET_MessageHeader *msg,
  * @param buf where the callee should write the message
  * @return number of bytes written to buf
  */
-static size_t 
-core_transmit_notify (void *cls,
-                     size_t size, void *buf)
+static size_t
+core_transmit_notify (void *cls, size_t size, void *buf)
 {
   struct PeerInfo *peer = cls;
   char *cbuf = buf;
@@ -1117,12 +1136,13 @@ core_transmit_notify (void *cls,
 
   size_t off;
   size_t msize;
-
+  peer->th = NULL;
   if (buf == NULL)
     {
       /* client disconnected */
 #if DEBUG_DHT
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n", my_short_id, "DHT");
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n",
+                  my_short_id, "DHT");
 #endif
       return 0;
     }
@@ -1130,44 +1150,39 @@ core_transmit_notify (void *cls,
   if (peer->head == NULL)
     return 0;
 
-  peer->th = NULL;
   off = 0;
   pending = peer->head;
-  reply_times[reply_counter] = GNUNET_TIME_absolute_get_difference(pending->scheduled, GNUNET_TIME_absolute_get());
-  msize = ntohs(pending->msg->size);
+#if DUMB
+  reply_times[reply_counter] =
+    GNUNET_TIME_absolute_get_difference (pending->scheduled,
+                                         GNUNET_TIME_absolute_get ());
+  msize = ntohs (pending->msg->size);
   if (msize <= size)
     {
       off = msize;
       memcpy (cbuf, pending->msg, msize);
-      GNUNET_CONTAINER_DLL_remove (peer->head,
-                                   peer->tail,
-                                   pending);
-#if DEBUG_DHT > 1
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Removing pending message size %d for peer %s\n", my_short_id, "DHT", msize, GNUNET_i2s(&peer->id));
-#endif
+      peer->pending_count--;
+      increment_stats ("# pending messages sent");
+      GNUNET_assert (peer->pending_count >= 0);
+      GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
       GNUNET_free (pending);
     }
-#if SMART
+#else
   while (NULL != pending &&
-          (size - off >= (msize = ntohs (pending->msg->size))))
+         (size - off >= (msize = ntohs (pending->msg->size))))
     {
-#if DEBUG_DHT_ROUTING
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s:%s' : transmit_notify (core) called with size %d, available %d\n", my_short_id, "dht service", msize, size);
-#endif
       memcpy (&cbuf[off], pending->msg, msize);
       off += msize;
-      GNUNET_CONTAINER_DLL_remove (peer->head,
-                                   peer->tail,
-                                   pending);
+      peer->pending_count--;
+      increment_stats ("# pending messages sent");
+      GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
       GNUNET_free (pending);
       pending = peer->head;
     }
 #endif
   if ((peer->head != NULL) && (peer->send_task == GNUNET_SCHEDULER_NO_TASK))
-    peer->send_task = GNUNET_SCHEDULER_add_now(&try_core_send, peer);
-#if DEBUG_DHT > 1
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s:%s' : transmit_notify (core) called with size %d, available %d, returning %d\n", my_short_id, "dht service", msize, size, off);
-#endif
+    peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
+
   return off;
 }
 
@@ -1221,7 +1236,8 @@ distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
   for (i = bucket + 1;
        (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
     {
-      if (GNUNET_CRYPTO_hash_get_bit (target, i) != GNUNET_CRYPTO_hash_get_bit (have, i))
+      if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
+          GNUNET_CRYPTO_hash_get_bit (have, i))
         lsb |= (1 << (bucket + 32 - 9 - i));    /* first bit set will be 10,
                                                    last bit set will be 31 -- if
                                                    i does not reach 512 first... */
@@ -1240,8 +1256,8 @@ static unsigned int
 inverse_distance (const GNUNET_HashCode * target,
                   const GNUNET_HashCode * have)
 {
-  if (GNUNET_CRYPTO_hash_matching_bits(target, have) == 0)
-    return 1; /* Never return 0! */
+  if (GNUNET_CRYPTO_hash_matching_bits (target, have) == 0)
+    return 1;                   /* Never return 0! */
   return ((unsigned int) -1) - distance (target, have);
 }
 
@@ -1254,11 +1270,12 @@ inverse_distance (const GNUNET_HashCode * target,
  * @return the proper bucket index, or GNUNET_SYSERR
  *         on error (same hashcode)
  */
-static int find_bucket(const GNUNET_HashCode *hc)
+static int
+find_bucket (const GNUNET_HashCode * hc)
 {
   unsigned int bits;
 
-  bits = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, hc);
+  bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
   if (bits == MAX_BUCKETS)
     return GNUNET_SYSERR;
   return MAX_BUCKETS - bits - 1;
@@ -1276,14 +1293,15 @@ static int find_bucket(const GNUNET_HashCode *hc)
  * @return the proper bucket index for this key,
  *         or GNUNET_SYSERR on error (same hashcode)
  */
-static int find_current_bucket(const GNUNET_HashCode *hc)
+static int
+find_current_bucket (const GNUNET_HashCode * hc)
 {
   int actual_bucket;
-  actual_bucket = find_bucket(hc);
+  actual_bucket = find_bucket (hc);
 
-  if (actual_bucket == GNUNET_SYSERR) /* hc and our peer identity match! */
+  if (actual_bucket == GNUNET_SYSERR)   /* hc and our peer identity match! */
     return lowest_bucket;
-  else if (actual_bucket < lowest_bucket) /* actual_bucket not yet used */
+  else if (actual_bucket < lowest_bucket)       /* actual_bucket not yet used */
     return lowest_bucket;
   else
     return actual_bucket;
@@ -1298,7 +1316,7 @@ static int find_current_bucket(const GNUNET_HashCode *hc)
  * @return the bucket number holding the peer, GNUNET_SYSERR if not found
  */
 static int
-find_bucket_by_peer(const struct PeerInfo *peer)
+find_bucket_by_peer (const struct PeerInfo *peer)
 {
   int bucket;
   struct PeerInfo *pos;
@@ -1314,7 +1332,7 @@ find_bucket_by_peer(const struct PeerInfo *peer)
         }
     }
 
-  return GNUNET_SYSERR; /* No such peer. */
+  return GNUNET_SYSERR;         /* No such peer. */
 }
 #endif
 
@@ -1329,24 +1347,32 @@ print_routing_table ()
   struct PeerInfo *pos;
   char char_buf[30000];
   int char_pos;
-  memset(char_buf, 0, sizeof(char_buf));
+  memset (char_buf, 0, sizeof (char_buf));
   char_pos = 0;
-  char_pos += sprintf(&char_buf[char_pos], "Printing routing table for peer %s\n", my_short_id);
+  char_pos +=
+    sprintf (&char_buf[char_pos], "Printing routing table for peer %s\n",
+             my_short_id);
   //fprintf(stderr, "Printing routing table for peer %s\n", my_short_id);
   for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
     {
       pos = k_buckets[bucket].head;
-      char_pos += sprintf(&char_buf[char_pos], "Bucket %d:\n", bucket);
+      char_pos += sprintf (&char_buf[char_pos], "Bucket %d:\n", bucket);
       //fprintf(stderr, "Bucket %d:\n", bucket);
       while (pos != NULL)
         {
           //fprintf(stderr, "\tPeer %s, best bucket %d, %d bits match\n", GNUNET_i2s(&pos->id), find_bucket(&pos->id.hashPubKey), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey));
-          char_pos += sprintf(&char_buf[char_pos], "\tPeer %s, best bucket %d, %d bits match\n", GNUNET_i2s(&pos->id), find_bucket(&pos->id.hashPubKey), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey));
+          char_pos +=
+            sprintf (&char_buf[char_pos],
+                     "\tPeer %s, best bucket %d, %d bits match\n",
+                     GNUNET_i2s (&pos->id), find_bucket (&pos->id.hashPubKey),
+                     GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
+                                                       &my_identity.
+                                                       hashPubKey));
           pos = pos->next;
         }
     }
-  fprintf(stderr, "%s", char_buf);
-  fflush(stderr);
+  fprintf (stderr, "%s", char_buf);
+  fflush (stderr);
 }
 #endif
 
@@ -1358,23 +1384,23 @@ print_routing_table ()
  * @return the routing table entry, or NULL if not found
  */
 static struct PeerInfo *
-find_peer_by_id(const struct GNUNET_PeerIdentity *peer)
+find_peer_by_id (const struct GNUNET_PeerIdentity *peer)
 {
   int bucket;
   struct PeerInfo *pos;
-  bucket = find_current_bucket(&peer->hashPubKey);
+  bucket = find_current_bucket (&peer->hashPubKey);
 
-  if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
+  if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
     return NULL;
 
   pos = k_buckets[bucket].head;
   while (pos != NULL)
     {
-      if (0 == memcmp(&pos->id, peer, sizeof(struct GNUNET_PeerIdentity)))
+      if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
         return pos;
       pos = pos->next;
     }
-  return NULL; /* No such peer. */
+  return NULL;                  /* No such peer. */
 }
 
 /* Forward declaration */
@@ -1386,7 +1412,6 @@ update_core_preference (void *cls,
  *
  * @param cls closure
  * @param peer identifies the peer
- * @param bpm_in set to the current bandwidth limit (receiving) for this peer
  * @param bpm_out set to the current bandwidth limit (sending) for this peer
  * @param amount set to the amount that was actually reserved or unreserved;
  *               either the full requested amount or zero (no partial reservations)
@@ -1394,14 +1419,14 @@ update_core_preference (void *cls,
  */
 static void
 update_core_preference_finish (void *cls,
-                               const struct GNUNET_PeerIdentity * peer,
-                               struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
+                               const struct GNUNET_PeerIdentity *peer,
                                struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
                                int amount, uint64_t preference)
 {
   struct PeerInfo *peer_info = cls;
   peer_info->info_ctx = NULL;
-  GNUNET_SCHEDULER_add_delayed(DHT_DEFAULT_PREFERENCE_INTERVAL, &update_core_preference, peer_info);
+  GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
+                                &update_core_preference, peer_info);
 }
 
 static void
@@ -1415,18 +1440,23 @@ update_core_preference (void *cls,
     {
       return;
     }
-  matching = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &peer->id.hashPubKey);
+  matching =
+    GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
+                                      &peer->id.hashPubKey);
   if (matching >= 64)
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer identifier matches by %u bits, only shifting as much as we can!\n", matching);
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "Peer identifier matches by %u bits, only shifting as much as we can!\n",
+                  matching);
       matching = 63;
     }
   preference = 1LL << matching;
-  peer->info_ctx = GNUNET_CORE_peer_change_preference (cfg,
+  peer->info_ctx = GNUNET_CORE_peer_change_preference (coreAPI,
                                                        &peer->id,
-                                                       GNUNET_TIME_relative_get_forever(),
-                                                       GNUNET_BANDWIDTH_value_init (UINT32_MAX),
-                                                       0,
+                                                       GNUNET_TIME_relative_get_forever
+                                                       (),
+                                                       GNUNET_BANDWIDTH_value_init
+                                                       (UINT32_MAX), 0,
                                                        preference,
                                                        &update_core_preference_finish,
                                                        peer);
@@ -1439,36 +1469,38 @@ update_core_preference (void *cls,
  * @param peer GNUNET_PeerIdentity of the peer to add
  * @param bucket the already figured out bucket to add
  *        the peer to
- * @param latency the core reported latency of this peer
- * @param distance the transport level distance to this peer
+ * @param atsi performance information
  *
  * @return the newly added PeerInfo
  */
 static struct PeerInfo *
-add_peer(const struct GNUNET_PeerIdentity *peer,
-         unsigned int bucket,
-         struct GNUNET_TIME_Relative latency,
-         unsigned int distance)
+add_peer (const struct GNUNET_PeerIdentity *peer,
+          unsigned int bucket,
+          const struct GNUNET_TRANSPORT_ATS_Information *atsi)
 {
   struct PeerInfo *new_peer;
-  GNUNET_assert(bucket < MAX_BUCKETS);
-  GNUNET_assert(peer != NULL);
-  new_peer = GNUNET_malloc(sizeof(struct PeerInfo));
+  GNUNET_assert (bucket < MAX_BUCKETS);
+  GNUNET_assert (peer != NULL);
+  new_peer = GNUNET_malloc (sizeof (struct PeerInfo));
+#if 0
   new_peer->latency = latency;
   new_peer->distance = distance;
+#endif
 
-  memcpy(&new_peer->id, peer, sizeof(struct GNUNET_PeerIdentity));
+  memcpy (&new_peer->id, peer, sizeof (struct GNUNET_PeerIdentity));
 
-  GNUNET_CONTAINER_DLL_insert_after(k_buckets[bucket].head,
-                                    k_buckets[bucket].tail,
-                                    k_buckets[bucket].tail,
-                                    new_peer);
+  GNUNET_CONTAINER_DLL_insert_after (k_buckets[bucket].head,
+                                     k_buckets[bucket].tail,
+                                     k_buckets[bucket].tail, new_peer);
   k_buckets[bucket].peers_size++;
 
-  if ((GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &peer->hashPubKey) > 0) && (k_buckets[bucket].peers_size <= bucket_size))
+  if ((GNUNET_CRYPTO_hash_matching_bits
+       (&my_identity.hashPubKey, &peer->hashPubKey) > 0)
+      && (k_buckets[bucket].peers_size <= bucket_size))
     {
 #if DO_UPDATE_PREFERENCE
-      new_peer->preference_task = GNUNET_SCHEDULER_add_now(&update_core_preference, new_peer);
+      new_peer->preference_task =
+        GNUNET_SCHEDULER_add_now (&update_core_preference, new_peer);
 #endif
     }
 
@@ -1485,16 +1517,16 @@ add_peer(const struct GNUNET_PeerIdentity *peer,
  * @param peer the peer to remove
  * @param bucket the bucket the peer belongs to
  */
-static void remove_peer (struct PeerInfo *peer,
-                         unsigned int bucket)
+static void
+remove_peer (struct PeerInfo *peer, unsigned int bucket)
 {
-  GNUNET_assert(k_buckets[bucket].peers_size > 0);
-  GNUNET_CONTAINER_DLL_remove(k_buckets[bucket].head,
-                              k_buckets[bucket].tail,
-                              peer);
+  GNUNET_assert (k_buckets[bucket].peers_size > 0);
+  GNUNET_CONTAINER_DLL_remove (k_buckets[bucket].head,
+                               k_buckets[bucket].tail, peer);
   k_buckets[bucket].peers_size--;
 #if CHANGE_LOWEST
-  if ((bucket == lowest_bucket) && (k_buckets[lowest_bucket].peers_size == 0) && (lowest_bucket < MAX_BUCKETS - 1))
+  if ((bucket == lowest_bucket) && (k_buckets[lowest_bucket].peers_size == 0)
+      && (lowest_bucket < MAX_BUCKETS - 1))
     lowest_bucket++;
 #endif
 }
@@ -1506,8 +1538,8 @@ static void remove_peer (struct PeerInfo *peer,
  * @param peer peer to be removed and freed
  * @param bucket which bucket this peer belongs to
  */
-static void delete_peer (struct PeerInfo *peer,
-                         unsigned int bucket)
+static void
+delete_peer (struct PeerInfo *peer, unsigned int bucket)
 {
   struct P2PPendingMessage *pos;
   struct P2PPendingMessage *next;
@@ -1519,29 +1551,41 @@ static void delete_peer (struct PeerInfo *peer,
     peer_pos = peer_pos->next;
   if (peer_pos == NULL)
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s: Expected peer `%s' in bucket %d\n", my_short_id, "DHT", GNUNET_i2s(&peer->id), bucket);
-      GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s: Lowest bucket: %d, find_current_bucket: %d, peer resides in bucket: %d\n", my_short_id, "DHT", lowest_bucket, find_current_bucket(&peer->id.hashPubKey), find_bucket_by_peer(peer));
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "%s:%s: Expected peer `%s' in bucket %d\n", my_short_id,
+                  "DHT", GNUNET_i2s (&peer->id), bucket);
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "%s:%s: Lowest bucket: %d, find_current_bucket: %d, peer resides in bucket: %d\n",
+                  my_short_id, "DHT", lowest_bucket,
+                  find_current_bucket (&peer->id.hashPubKey),
+                  find_bucket_by_peer (peer));
     }
-  GNUNET_assert(peer_pos != NULL);
+  GNUNET_assert (peer_pos != NULL);
 #endif
-  remove_peer(peer, bucket); /* First remove the peer from its bucket */
+  remove_peer (peer, bucket);   /* First remove the peer from its bucket */
 
   if (peer->send_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel(peer->send_task);
-  if (peer->th != NULL)
-    GNUNET_CORE_notify_transmit_ready_cancel(peer->th);
+    GNUNET_SCHEDULER_cancel (peer->send_task);
+  if ((peer->th != NULL) && (coreAPI != NULL))
+    GNUNET_CORE_notify_transmit_ready_cancel (peer->th);
 
   pos = peer->head;
-  while (pos != NULL) /* Remove any pending messages for this peer */
+  while (pos != NULL)           /* Remove any pending messages for this peer */
     {
+      increment_stats
+        ("# dht pending messages discarded (due to disconnect/shutdown)");
       next = pos->next;
-      GNUNET_free(pos);
+      GNUNET_free (pos);
       pos = next;
     }
 
-  GNUNET_assert(GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->id.hashPubKey));
-  GNUNET_CONTAINER_multihashmap_remove (all_known_peers, &peer->id.hashPubKey, peer);
-  GNUNET_free(peer);
+  GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
+                 (all_known_peers, &peer->id.hashPubKey));
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
+                                                       &peer->id.hashPubKey,
+                                                       peer));
+  GNUNET_free (peer);
 }
 
 
@@ -1555,20 +1599,18 @@ static void delete_peer (struct PeerInfo *peer,
  *         iterate,
  *         GNUNET_NO if not.
  */
-static int move_lowest_bucket (void *cls,
-                               const GNUNET_HashCode * key,
-                               void *value)
+static int
+move_lowest_bucket (void *cls, const GNUNET_HashCode * key, void *value)
 {
   struct PeerInfo *peer = value;
   int new_bucket;
 
-  GNUNET_assert(lowest_bucket > 0);
+  GNUNET_assert (lowest_bucket > 0);
   new_bucket = lowest_bucket - 1;
-  remove_peer(peer, lowest_bucket);
-  GNUNET_CONTAINER_DLL_insert_after(k_buckets[new_bucket].head,
-                                    k_buckets[new_bucket].tail,
-                                    k_buckets[new_bucket].tail,
-                                    peer);
+  remove_peer (peer, lowest_bucket);
+  GNUNET_CONTAINER_DLL_insert_after (k_buckets[new_bucket].head,
+                                     k_buckets[new_bucket].tail,
+                                     k_buckets[new_bucket].tail, peer);
   k_buckets[new_bucket].peers_size++;
   return GNUNET_YES;
 }
@@ -1579,33 +1621,37 @@ static int move_lowest_bucket (void *cls,
  * bucket to the next lower down, and move any appropriate
  * entries in the current lowest bucket to the new bucket.
  */
-static void enable_next_bucket()
+static void
+enable_next_bucket ()
 {
   struct GNUNET_CONTAINER_MultiHashMap *to_remove;
   struct PeerInfo *pos;
-  GNUNET_assert(lowest_bucket > 0);
-  to_remove = GNUNET_CONTAINER_multihashmap_create(bucket_size);
+  GNUNET_assert (lowest_bucket > 0);
+  to_remove = GNUNET_CONTAINER_multihashmap_create (bucket_size);
   pos = k_buckets[lowest_bucket].head;
 
 #if PRINT_TABLES
-  fprintf(stderr, "Printing RT before new bucket\n");
-  print_routing_table();
+  fprintf (stderr, "Printing RT before new bucket\n");
+  print_routing_table ();
 #endif
   /* Populate the array of peers which should be in the next lowest bucket */
   while (pos != NULL)
     {
-      if (find_bucket(&pos->id.hashPubKey) < lowest_bucket)
-        GNUNET_CONTAINER_multihashmap_put(to_remove, &pos->id.hashPubKey, pos, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+      if (find_bucket (&pos->id.hashPubKey) < lowest_bucket)
+        GNUNET_CONTAINER_multihashmap_put (to_remove, &pos->id.hashPubKey,
+                                           pos,
+                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
       pos = pos->next;
     }
 
   /* Remove peers from lowest bucket, insert into next lowest bucket */
-  GNUNET_CONTAINER_multihashmap_iterate(to_remove, &move_lowest_bucket, NULL);
-  GNUNET_CONTAINER_multihashmap_destroy(to_remove);
+  GNUNET_CONTAINER_multihashmap_iterate (to_remove, &move_lowest_bucket,
+                                         NULL);
+  GNUNET_CONTAINER_multihashmap_destroy (to_remove);
   lowest_bucket = lowest_bucket - 1;
 #if PRINT_TABLES
-  fprintf(stderr, "Printing RT after new bucket\n");
-  print_routing_table();
+  fprintf (stderr, "Printing RT after new bucket\n");
+  print_routing_table ();
 #endif
 }
 
@@ -1617,7 +1663,7 @@ static void enable_next_bucket()
  *         key, or NULL on error.
  */
 static struct PeerInfo *
-find_closest_peer (const GNUNET_HashCode *hc)
+find_closest_peer (const GNUNET_HashCode * hc)
 {
   struct PeerInfo *pos;
   struct PeerInfo *current_closest;
@@ -1638,7 +1684,7 @@ find_closest_peer (const GNUNET_HashCode *hc)
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
         {
-          temp_distance = distance(&pos->id.hashPubKey, hc);
+          temp_distance = distance (&pos->id.hashPubKey, hc);
           if (temp_distance <= lowest_distance)
             {
               lowest_distance = temp_distance;
@@ -1648,7 +1694,7 @@ find_closest_peer (const GNUNET_HashCode *hc)
           count++;
         }
     }
-  GNUNET_assert(current_closest != NULL);
+  GNUNET_assert (current_closest != NULL);
   return current_closest;
 }
 
@@ -1662,45 +1708,55 @@ find_closest_peer (const GNUNET_HashCode *hc)
  * @param peer the peer to forward the message to
  * @param msg_ctx the context of the message (hop count, bloom, etc.)
  */
-static void forward_message (const struct GNUNET_MessageHeader *msg,
-                             struct PeerInfo *peer,
-                             struct DHT_MessageContext *msg_ctx)
+static void
+forward_message (const struct GNUNET_MessageHeader *msg,
+                 struct PeerInfo *peer, struct DHT_MessageContext *msg_ctx)
 {
   struct GNUNET_DHT_P2PRouteMessage *route_message;
   struct P2PPendingMessage *pending;
   size_t msize;
   size_t psize;
 
-  increment_stats(STAT_ROUTE_FORWARDS);
-  GNUNET_assert(peer != NULL);
-  if ((msg_ctx->closest != GNUNET_YES) && (peer == find_closest_peer(&msg_ctx->key)))
-    increment_stats(STAT_ROUTE_FORWARDS_CLOSEST);
-
-  msize = sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs(msg->size);
-  GNUNET_assert(msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
-  psize = sizeof(struct P2PPendingMessage) + msize;
-  pending = GNUNET_malloc(psize);
-  pending->msg = (struct GNUNET_MessageHeader *)&pending[1];
+  increment_stats (STAT_ROUTE_FORWARDS);
+  GNUNET_assert (peer != NULL);
+  if ((msg_ctx->closest != GNUNET_YES)
+      && (peer == find_closest_peer (&msg_ctx->key)))
+    increment_stats (STAT_ROUTE_FORWARDS_CLOSEST);
+
+  msize = sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (msg->size);
+  GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
+  psize = sizeof (struct P2PPendingMessage) + msize;
+  pending = GNUNET_malloc (psize);
+  pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
   pending->importance = msg_ctx->importance;
   pending->timeout = msg_ctx->timeout;
-  route_message = (struct GNUNET_DHT_P2PRouteMessage *)pending->msg;
-  route_message->header.size = htons(msize);
-  route_message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE);
-  route_message->options = htonl(msg_ctx->msg_options);
-  route_message->hop_count = htonl(msg_ctx->hop_count + 1);
-  route_message->network_size = htonl(msg_ctx->network_size);
-  route_message->desired_replication_level = htonl(msg_ctx->replication);
-  route_message->unique_id = GNUNET_htonll(msg_ctx->unique_id);
+  route_message = (struct GNUNET_DHT_P2PRouteMessage *) pending->msg;
+  route_message->header.size = htons (msize);
+  route_message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE);
+  route_message->options = htonl (msg_ctx->msg_options);
+  route_message->hop_count = htonl (msg_ctx->hop_count + 1);
+  route_message->network_size = htonl (msg_ctx->network_size);
+  route_message->desired_replication_level = htonl (msg_ctx->replication);
+  route_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
   if (msg_ctx->bloom != NULL)
-    GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(msg_ctx->bloom, route_message->bloomfilter, DHT_BLOOM_SIZE));
-  memcpy(&route_message->key, &msg_ctx->key, sizeof(GNUNET_HashCode));
-  memcpy(&route_message[1], msg, ntohs(msg->size));
+    GNUNET_assert (GNUNET_OK ==
+                   GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
+                                                              route_message->
+                                                              bloomfilter,
+                                                              DHT_BLOOM_SIZE));
+  memcpy (&route_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
+  memcpy (&route_message[1], msg, ntohs (msg->size));
 #if DEBUG_DHT > 1
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Adding pending message size %d for peer %s\n", my_short_id, "DHT", msize, GNUNET_i2s(&peer->id));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%s:%s Adding pending message size %d for peer %s\n",
+              my_short_id, "DHT", msize, GNUNET_i2s (&peer->id));
 #endif
-  GNUNET_CONTAINER_DLL_insert_after(peer->head, peer->tail, peer->tail, pending);
+  peer->pending_count++;
+  increment_stats ("# pending messages scheduled");
+  GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
+                                     pending);
   if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
-    peer->send_task = GNUNET_SCHEDULER_add_now(&try_core_send, peer);
+    peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
 }
 
 #if DO_PING
@@ -1716,27 +1772,32 @@ periodic_ping_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PeerInfo *peer = cls;
   struct GNUNET_MessageHeader ping_message;
-  struct DHT_MessageContext message_context;
+  struct DHT_MessageContext msg_ctx;
 
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
 
-  ping_message.size = htons(sizeof(struct GNUNET_MessageHeader));
-  ping_message.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_PING);
+  ping_message.size = htons (sizeof (struct GNUNET_MessageHeader));
+  ping_message.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PING);
 
-  memset(&message_context, 0, sizeof(struct DHT_MessageContext));
+  memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
 #if DEBUG_PING
-  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Sending periodic ping to %s\n", my_short_id, "DHT", GNUNET_i2s(&peer->id));
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "%s:%s Sending periodic ping to %s\n", my_short_id, "DHT",
+              GNUNET_i2s (&peer->id));
 #endif
-  forward_message(&ping_message, peer, &message_context);
-  peer->ping_task = GNUNET_SCHEDULER_add_delayed(DHT_DEFAULT_PING_DELAY, &periodic_ping_task, peer);
+  forward_message (&ping_message, peer, &msg_ctx);
+  peer->ping_task =
+    GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PING_DELAY, &periodic_ping_task,
+                                  peer);
 }
 
 /**
  * Schedule PING messages for the top X peers in each
  * bucket of the routing table (so core won't disconnect them!)
  */
-void schedule_ping_messages()
+void
+schedule_ping_messages ()
 {
   unsigned int bucket;
   unsigned int count;
@@ -1747,11 +1808,13 @@ void schedule_ping_messages()
       count = 0;
       while (pos != NULL)
         {
-          if ((count < bucket_size) && (pos->ping_task == GNUNET_SCHEDULER_NO_TASK))
-            GNUNET_SCHEDULER_add_now(&periodic_ping_task, pos);
-          else if ((count >= bucket_size) && (pos->ping_task != GNUNET_SCHEDULER_NO_TASK))
+          if ((count < bucket_size)
+              && (pos->ping_task == GNUNET_SCHEDULER_NO_TASK))
+            GNUNET_SCHEDULER_add_now (&periodic_ping_task, pos);
+          else if ((count >= bucket_size)
+                   && (pos->ping_task != GNUNET_SCHEDULER_NO_TASK))
             {
-              GNUNET_SCHEDULER_cancel(pos->ping_task);
+              GNUNET_SCHEDULER_cancel (pos->ping_task);
               pos->ping_task = GNUNET_SCHEDULER_NO_TASK;
             }
           pos = pos->next;
@@ -1766,33 +1829,31 @@ void schedule_ping_messages()
  *
  * @param peer the peer identity of the peer being added
  * @param bucket the bucket that we want this peer to go in
- * @param latency transport latency of this peer
- * @param distance transport distance to this peer
+ * @param atsi transport ATS information
  *
  * @return NULL if the peer was not added,
  *         pointer to PeerInfo for new peer otherwise
  */
 static struct PeerInfo *
-try_add_peer(const struct GNUNET_PeerIdentity *peer,
-             unsigned int bucket,
-             struct GNUNET_TIME_Relative latency,
-             unsigned int distance)
+try_add_peer (const struct GNUNET_PeerIdentity *peer,
+              unsigned int bucket,
+              const struct GNUNET_TRANSPORT_ATS_Information *atsi)
 {
   int peer_bucket;
   struct PeerInfo *new_peer;
 
-  if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
+  if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
     return NULL;
 
-  peer_bucket = find_current_bucket(&peer->hashPubKey);
+  peer_bucket = find_current_bucket (&peer->hashPubKey);
 
-  GNUNET_assert(peer_bucket >= lowest_bucket);
-  new_peer = add_peer(peer, peer_bucket, latency, distance);
+  GNUNET_assert (peer_bucket >= lowest_bucket);
+  new_peer = add_peer (peer, peer_bucket, atsi);
 
   if ((k_buckets[lowest_bucket].peers_size) >= bucket_size)
-    enable_next_bucket();
+    enable_next_bucket ();
 #if DO_PING
-  schedule_ping_messages();
+  schedule_ping_messages ();
 #endif
   return new_peer;
 }
@@ -1805,18 +1866,18 @@ try_add_peer(const struct GNUNET_PeerIdentity *peer,
  */
 static void
 process_pending_messages (struct ClientList *client)
-{ 
-  if (client->pending_head == NULL) 
-    return;    
-  if (client->transmit_handle != NULL) 
+{
+  if (client->pending_head == NULL)
+    return;
+  if (client->transmit_handle != NULL)
     return;
 
   client->transmit_handle =
     GNUNET_SERVER_notify_transmit_ready (client->client_handle,
-                                        ntohs (client->pending_head->msg->
-                                               size),
-                                        GNUNET_TIME_UNIT_FOREVER_REL,
-                                        &send_generic_reply, client);
+                                         ntohs (client->pending_head->
+                                                msg->size),
+                                         GNUNET_TIME_UNIT_FOREVER_REL,
+                                         &send_generic_reply, client);
 }
 
 /**
@@ -1841,18 +1902,17 @@ send_generic_reply (void *cls, size_t size, void *buf)
   size_t msize;
 
   client->transmit_handle = NULL;
-  if (buf == NULL)             
+  if (buf == NULL)
     {
       /* client disconnected */
       return 0;
     }
   off = 0;
-  while ( (NULL != (reply = client->pending_head)) &&
-         (size >= off + (msize = ntohs (reply->msg->size))))
+  while ((NULL != (reply = client->pending_head)) &&
+         (size >= off + (msize = ntohs (reply->msg->size))))
     {
       GNUNET_CONTAINER_DLL_remove (client->pending_head,
-                                  client->pending_tail,
-                                  reply);
+                                   client->pending_tail, reply);
       memcpy (&cbuf[off], reply->msg, msize);
       GNUNET_free (reply);
       off += msize;
@@ -1860,8 +1920,8 @@ send_generic_reply (void *cls, size_t size, void *buf)
   process_pending_messages (client);
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Transmitted %u bytes of replies to client\n",
-             (unsigned int) off);
+              "Transmitted %u bytes of replies to client\n",
+              (unsigned int) off);
 #endif
   return off;
 }
@@ -1878,9 +1938,8 @@ add_pending_message (struct ClientList *client,
                      struct PendingMessage *pending_message)
 {
   GNUNET_CONTAINER_DLL_insert_after (client->pending_head,
-                                    client->pending_tail,
-                                    client->pending_tail,
-                                    pending_message);
+                                     client->pending_tail,
+                                     client->pending_tail, pending_message);
   process_pending_messages (client);
 }
 
@@ -1892,12 +1951,12 @@ add_pending_message (struct ClientList *client,
  * @param client the client to send the reply to
  * @param message the encapsulated message to send
  * @param uid the unique identifier of this request
+ * @param key key
  */
 static void
 send_reply_to_client (struct ClientList *client,
                       const struct GNUNET_MessageHeader *message,
-                      unsigned long long uid,
-                     const GNUNET_HashCode *key)
+                      unsigned long long uid, const GNUNET_HashCode * key)
 {
   struct GNUNET_DHT_RouteResultMessage *reply;
   struct PendingMessage *pending_message;
@@ -1915,12 +1974,12 @@ send_reply_to_client (struct ClientList *client,
       return;
     }
   pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize);
-  pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
-  reply = (struct GNUNET_DHT_RouteResultMessage *)&pending_message[1];
+  pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
+  reply = (struct GNUNET_DHT_RouteResultMessage *) &pending_message[1];
   reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT);
   reply->header.size = htons (tsize);
-  reply->put_path_length = htons(0); /* FIXME: implement */
-  reply->get_path_length = htons(0); /* FIXME: implement */
+  reply->put_path_length = htons (0);   /* FIXME: implement */
+  reply->get_path_length = htons (0);   /* FIXME: implement */
   reply->unique_id = GNUNET_htonll (uid);
   reply->key = *key;
   memcpy (&reply[1], message, msize);
@@ -1939,32 +1998,71 @@ send_reply_to_client (struct ClientList *client,
  * @return GNUNET_YES if we want this peer, GNUNET_NO if not (bucket
  *         already full)
  */
-static int consider_peer (struct GNUNET_PeerIdentity *peer)
+static int
+consider_peer (struct GNUNET_PeerIdentity *peer)
 {
   int bucket;
 
-  if ((GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey)) || (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity))))
-    return GNUNET_NO; /* We already know this peer (are connected even!) */
-  bucket = find_current_bucket(&peer->hashPubKey);
+  if ((GNUNET_YES ==
+       GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
+                                               &peer->hashPubKey))
+      || (0 ==
+          memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))))
+    return GNUNET_NO;           /* We already know this peer (are connected even!) */
+  bucket = find_current_bucket (&peer->hashPubKey);
 
-  if ((k_buckets[bucket].peers_size < bucket_size) || ((bucket == lowest_bucket) && (lowest_bucket > 0)))
+  if ((k_buckets[bucket].peers_size < bucket_size)
+      || ((bucket == lowest_bucket) && (lowest_bucket > 0)))
     return GNUNET_YES;
 
   return GNUNET_NO;
 }
 
+
+/**
+ * Task used to remove forwarding entries, either
+ * after timeout, when full, or on shutdown.
+ *
+ * @param cls the entry to remove
+ * @param tc context, reason, etc.
+ */
+static void
+remove_forward_entry (void *cls,
+                      const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct DHTRouteSource *source_info = cls;
+  struct DHTQueryRecord *record;
+  source_info =
+    GNUNET_CONTAINER_heap_remove_node (forward_list.minHeap,
+                                       source_info->hnode);
+  record = source_info->record;
+  GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
+
+  if (record->head == NULL)     /* No more entries in DLL */
+    {
+      GNUNET_assert (GNUNET_YES ==
+                     GNUNET_CONTAINER_multihashmap_remove
+                     (forward_list.hashmap, &record->key, record));
+      GNUNET_free (record);
+    }
+  if (source_info->find_peers_responded != NULL)
+    GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
+  GNUNET_free (source_info);
+}
+
 /**
  * Main function that handles whether or not to route a result
  * message to other peers, or to send to our local client.
  *
  * @param msg the result message to be routed
- * @param message_context context of the message we are routing
+ * @param msg_ctx context of the message we are routing
  *
  * @return the number of peers the message was routed to,
  *         GNUNET_SYSERR on failure
  */
-static int route_result_message(struct GNUNET_MessageHeader *msg,
-                                struct DHT_MessageContext *message_context)
+static int
+route_result_message (struct GNUNET_MessageHeader *msg,
+                      struct DHT_MessageContext *msg_ctx)
 {
   struct GNUNET_PeerIdentity new_peer;
   struct DHTQueryRecord *record;
@@ -1972,65 +2070,74 @@ static int route_result_message(struct GNUNET_MessageHeader *msg,
   struct PeerInfo *peer_info;
   const struct GNUNET_MessageHeader *hello_msg;
 
-  increment_stats(STAT_RESULTS);
+  increment_stats (STAT_RESULTS);
   /**
    * If a find peer result message is received and contains a valid
    * HELLO for another peer, offer it to the transport service.
    */
-  if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
+  if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
     {
-      if (ntohs(msg->size) <= sizeof(struct GNUNET_MessageHeader))
-        GNUNET_break_op(0);
+      if (ntohs (msg->size) <= sizeof (struct GNUNET_MessageHeader))
+        GNUNET_break_op (0);
 
       hello_msg = &msg[1];
-      if ((ntohs(hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO) || (GNUNET_SYSERR == GNUNET_HELLO_get_id((const struct GNUNET_HELLO_Message *)hello_msg, &new_peer)))
-      {
-        GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Received non-HELLO message type in find peer result message!\n", my_short_id, "DHT");
-        GNUNET_break_op(0);
-        return GNUNET_NO;
-      }
-      else /* We have a valid hello, and peer id stored in new_peer */
-      {
-        find_peer_context.count++;
-        increment_stats(STAT_FIND_PEER_REPLY);
-        if (GNUNET_YES == consider_peer(&new_peer))
+      if ((ntohs (hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO)
+          || (GNUNET_SYSERR ==
+              GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
+                                   hello_msg, &new_peer)))
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                      "%s:%s Received non-HELLO message type in find peer result message!\n",
+                      my_short_id, "DHT");
+          GNUNET_break_op (0);
+          return GNUNET_NO;
+        }
+      else                      /* We have a valid hello, and peer id stored in new_peer */
         {
-          increment_stats(STAT_HELLOS_PROVIDED);
-          GNUNET_TRANSPORT_offer_hello(transport_handle, hello_msg);
-          GNUNET_CORE_peer_request_connect(cfg, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), &new_peer, NULL, NULL);
+          find_peer_context.count++;
+          increment_stats (STAT_FIND_PEER_REPLY);
+          if (GNUNET_YES == consider_peer (&new_peer))
+            {
+              increment_stats (STAT_HELLOS_PROVIDED);
+              GNUNET_TRANSPORT_offer_hello (transport_handle, hello_msg);
+              GNUNET_CORE_peer_request_connect (coreAPI,
+                                                GNUNET_TIME_relative_multiply
+                                                (GNUNET_TIME_UNIT_SECONDS, 5),
+                                                &new_peer, NULL, NULL);
+            }
         }
-      }
     }
 
   if (malicious_dropper == GNUNET_YES)
     record = NULL;
   else
-    record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, &message_context->key);
+    record =
+      GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
 
-  if (record == NULL) /* No record of this message! */
+  if (record == NULL)           /* No record of this message! */
     {
 #if DEBUG_DHT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "`%s:%s': Have no record of response key %s uid %llu\n", my_short_id,
-                "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "`%s:%s': Have no record of response key %s uid %llu\n",
+                  my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
+                  msg_ctx->unique_id);
 #endif
 #if DEBUG_DHT_ROUTING
       if ((debug_routes_extended) && (dhtlog_handle != NULL))
         {
           dhtlog_handle->insert_route (NULL,
-                                       message_context->unique_id,
+                                       msg_ctx->unique_id,
                                        DHTLOG_RESULT,
-                                       message_context->hop_count,
+                                       msg_ctx->hop_count,
                                        GNUNET_SYSERR,
                                        &my_identity,
-                                       &message_context->key,
-                                       message_context->peer, NULL);
+                                       &msg_ctx->key, msg_ctx->peer, NULL);
         }
 #endif
-      if (message_context->bloom != NULL)
+      if (msg_ctx->bloom != NULL)
         {
-          GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
-          message_context->bloom = NULL;
+          GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
+          msg_ctx->bloom = NULL;
         }
       return 0;
     }
@@ -2039,87 +2146,110 @@ static int route_result_message(struct GNUNET_MessageHeader *msg,
   while (pos != NULL)
     {
 #if STRICT_FORWARDING
-      if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) /* If we have already forwarded this peer id, don't do it again! */
+      if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)        /* If we have already forwarded this peer id, don't do it again! */
         {
-          if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pos->find_peers_responded, &new_peer.hashPubKey))
-          {
-            increment_stats("# find peer responses NOT forwarded (bloom match)");
-            pos = pos->next;
-            continue;
-          }
+          if (GNUNET_YES ==
+              GNUNET_CONTAINER_bloomfilter_test (pos->find_peers_responded,
+                                                 &new_peer.hashPubKey))
+            {
+              increment_stats
+                ("# find peer responses NOT forwarded (bloom match)");
+              pos = pos->next;
+              continue;
+            }
           else
-            GNUNET_CONTAINER_bloomfilter_add(pos->find_peers_responded, &new_peer.hashPubKey);
+            GNUNET_CONTAINER_bloomfilter_add (pos->find_peers_responded,
+                                              &new_peer.hashPubKey);
         }
 #endif
 
-      if (0 == memcmp(&pos->source, &my_identity, sizeof(struct GNUNET_PeerIdentity))) /* Local client (or DHT) initiated request! */
+      if (0 == memcmp (&pos->source, &my_identity, sizeof (struct GNUNET_PeerIdentity)))        /* Local client (or DHT) initiated request! */
         {
 #if DEBUG_DHT
           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "`%s:%s': Sending response key %s uid %llu to client\n", my_short_id,
-                      "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id);
+                      "`%s:%s': Sending response key %s uid %llu to client\n",
+                      my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
+                      msg_ctx->unique_id);
 #endif
 #if DEBUG_DHT_ROUTING
           if ((debug_routes_extended) && (dhtlog_handle != NULL))
             {
-              dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_RESULT,
-                                           message_context->hop_count,
-                                           GNUNET_YES, &my_identity, &message_context->key,
-                                           message_context->peer, NULL);
+              dhtlog_handle->insert_route (NULL, msg_ctx->unique_id,
+                                           DHTLOG_RESULT, msg_ctx->hop_count,
+                                           GNUNET_YES, &my_identity,
+                                           &msg_ctx->key, msg_ctx->peer,
+                                           NULL);
             }
 #endif
-          increment_stats(STAT_RESULTS_TO_CLIENT);
-          if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
-            increment_stats(STAT_GET_REPLY);
+          increment_stats (STAT_RESULTS_TO_CLIENT);
+          if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
+            increment_stats (STAT_GET_REPLY);
 
-          send_reply_to_client(pos->client, msg, 
-                              message_context->unique_id,
-                              &message_context->key);
+          send_reply_to_client (pos->client, msg,
+                                msg_ctx->unique_id, &msg_ctx->key);
         }
-      else /* Send to peer */
+      else                      /* Send to peer */
         {
-          peer_info = find_peer_by_id(&pos->source);
-          if (peer_info == NULL) /* Didn't find the peer in our routing table, perhaps peer disconnected! */
+          peer_info = find_peer_by_id (&pos->source);
+          if (peer_info == NULL)        /* Didn't find the peer in our routing table, perhaps peer disconnected! */
             {
               pos = pos->next;
               continue;
             }
 
-          if (message_context->bloom == NULL)
-            message_context->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
-          GNUNET_CONTAINER_bloomfilter_add (message_context->bloom, &my_identity.hashPubKey);
-          if ((GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (message_context->bloom, &peer_info->id.hashPubKey)))
+          if (msg_ctx->bloom == NULL)
+            msg_ctx->bloom =
+              GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
+                                                 DHT_BLOOM_K);
+          GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
+                                            &my_identity.hashPubKey);
+          if ((GNUNET_NO ==
+               GNUNET_CONTAINER_bloomfilter_test (msg_ctx->bloom,
+                                                  &peer_info->id.hashPubKey)))
             {
 #if DEBUG_DHT
               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                          "`%s:%s': Forwarding response key %s uid %llu to peer %s\n", my_short_id,
-                          "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id, GNUNET_i2s(&peer_info->id));
+                          "`%s:%s': Forwarding response key %s uid %llu to peer %s\n",
+                          my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
+                          msg_ctx->unique_id, GNUNET_i2s (&peer_info->id));
 #endif
 #if DEBUG_DHT_ROUTING
               if ((debug_routes_extended) && (dhtlog_handle != NULL))
                 {
-                  dhtlog_handle->insert_route (NULL, message_context->unique_id,
+                  dhtlog_handle->insert_route (NULL, msg_ctx->unique_id,
                                                DHTLOG_RESULT,
-                                               message_context->hop_count,
-                                               GNUNET_NO, &my_identity, &message_context->key,
-                                               message_context->peer, &pos->source);
+                                               msg_ctx->hop_count,
+                                               GNUNET_NO, &my_identity,
+                                               &msg_ctx->key, msg_ctx->peer,
+                                               &pos->source);
                 }
 #endif
-              forward_result_message(msg, peer_info, message_context);
+              forward_result_message (msg, peer_info, msg_ctx);
+              /* Try removing forward entries after sending once, only allows ONE response per request */
+              if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
+                {
+                  GNUNET_SCHEDULER_cancel (pos->delete_task);
+                  pos->delete_task =
+                    GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);
+                }
             }
           else
             {
 #if DEBUG_DHT
               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                          "`%s:%s': NOT Forwarding response (bloom match) key %s uid %llu to peer %s\n", my_short_id,
-                          "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id, GNUNET_i2s(&peer_info->id));
+                          "`%s:%s': NOT Forwarding response (bloom match) key %s uid %llu to peer %s\n",
+                          my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
+                          msg_ctx->unique_id, GNUNET_i2s (&peer_info->id));
 #endif
             }
         }
       pos = pos->next;
     }
-  if (message_context->bloom != NULL)
-    GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
+  if (msg_ctx->bloom != NULL)
+    {
+      GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
+      msg_ctx->bloom = NULL;
+    }
   return 0;
 }
 
@@ -2140,8 +2270,8 @@ static int
 datacache_get_iterator (void *cls,
                         struct GNUNET_TIME_Absolute exp,
                         const GNUNET_HashCode * key,
-                        size_t size, const char *data, 
-                       enum GNUNET_BLOCK_Type type)
+                        size_t size, const char *data,
+                        enum GNUNET_BLOCK_Type type)
 {
   struct DHT_MessageContext *msg_ctx = cls;
   struct DHT_MessageContext *new_msg_ctx;
@@ -2150,40 +2280,40 @@ datacache_get_iterator (void *cls,
 
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "`%s:%s': Received `%s' response from datacache\n", my_short_id, "DHT", "GET");
-#endif  
+              "`%s:%s': Received `%s' response from datacache\n", my_short_id,
+              "DHT", "GET");
+#endif
   eval = GNUNET_BLOCK_evaluate (block_context,
-                               type,
-                               key,
-                               &msg_ctx->reply_bf,
-                               msg_ctx->reply_bf_mutator,
-                               msg_ctx->xquery,
-                               msg_ctx->xquery_size,
-                               data,
-                               size);
+                                type,
+                                key,
+                                &msg_ctx->reply_bf,
+                                msg_ctx->reply_bf_mutator,
+                                msg_ctx->xquery,
+                                msg_ctx->xquery_size, data, size);
   switch (eval)
     {
     case GNUNET_BLOCK_EVALUATION_OK_LAST:
       msg_ctx->do_forward = GNUNET_NO;
     case GNUNET_BLOCK_EVALUATION_OK_MORE:
-      new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext));
-      memcpy(new_msg_ctx, msg_ctx, sizeof(struct DHT_MessageContext));
+      new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
+      memcpy (new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
       get_result =
-       GNUNET_malloc (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
+        GNUNET_malloc (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
       get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
       get_result->header.size =
-       htons (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
-      get_result->expiration = GNUNET_TIME_absolute_hton(exp);
+        htons (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
+      get_result->expiration = GNUNET_TIME_absolute_hton (exp);
       get_result->type = htons (type);
       memcpy (&get_result[1], data, size);
       new_msg_ctx->peer = &my_identity;
-      new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+      new_msg_ctx->bloom =
+        GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
       new_msg_ctx->hop_count = 0;
-      new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE * 2; /* Make result routing a higher priority */
+      new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */
       new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
-      increment_stats(STAT_GET_RESPONSE_START);
-      route_result_message(&get_result->header, new_msg_ctx);
-      GNUNET_free(new_msg_ctx);
+      increment_stats (STAT_GET_RESPONSE_START);
+      route_result_message (&get_result->header, new_msg_ctx);
+      GNUNET_free (new_msg_ctx);
       GNUNET_free (get_result);
       break;
     case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
@@ -2201,7 +2331,8 @@ datacache_get_iterator (void *cls,
     case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
 #if DEBUG_DHT
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "`%s:%s': Valid request, no results.\n", my_short_id, "DHT");
+                  "`%s:%s': Valid request, no results.\n", my_short_id,
+                  "DHT");
 #endif
       GNUNET_break (0);
       break;
@@ -2212,11 +2343,12 @@ datacache_get_iterator (void *cls,
     case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
 #if DEBUG_DHT
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "`%s:%s': Unsupported block type (%u) in response!\n", my_short_id, "DHT", type);
+                  "`%s:%s': Unsupported block type (%u) in response!\n",
+                  my_short_id, "DHT", type);
 #endif
       /* msg_ctx->do_forward = GNUNET_NO;  // not sure... */
       break;
-    }  
+    }
   return GNUNET_OK;
 }
 
@@ -2226,11 +2358,11 @@ datacache_get_iterator (void *cls,
  * peers.
  *
  * @param msg the message to be routed
- * @param message_context the context containing all pertinent information about the message
+ * @param msg_ctx the context containing all pertinent information about the message
  */
 static void
-route_message(const struct GNUNET_MessageHeader *msg,
-              struct DHT_MessageContext *message_context);
+route_message (const struct GNUNET_MessageHeader *msg,
+               struct DHT_MessageContext *msg_ctx);
 
 
 /**
@@ -2238,13 +2370,13 @@ route_message(const struct GNUNET_MessageHeader *msg,
  * if found, send response either to clients or other peers.
  *
  * @param msg the actual get message
- * @param message_context struct containing pertinent information about the get request
+ * @param msg_ctx struct containing pertinent information about the get request
  *
  * @return number of items found for GET request
  */
 static unsigned int
 handle_dht_get (const struct GNUNET_MessageHeader *msg,
-                struct DHT_MessageContext *message_context)
+                struct DHT_MessageContext *msg_ctx)
 {
   const struct GNUNET_DHT_GetMessage *get_msg;
   uint16_t msize;
@@ -2261,80 +2393,78 @@ handle_dht_get (const struct GNUNET_MessageHeader *msg,
     }
   get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
   bf_size = ntohs (get_msg->bf_size);
-  message_context->xquery_size = ntohs (get_msg->xquery_size);
-  message_context->reply_bf_mutator = get_msg->bf_mutator; /* FIXME: ntohl? */
-  if (msize != sizeof (struct GNUNET_DHT_GetMessage) + bf_size + message_context->xquery_size)
+  msg_ctx->xquery_size = ntohs (get_msg->xquery_size);
+  msg_ctx->reply_bf_mutator = get_msg->bf_mutator;      /* FIXME: ntohl? */
+  if (msize !=
+      sizeof (struct GNUNET_DHT_GetMessage) + bf_size + msg_ctx->xquery_size)
     {
       GNUNET_break (0);
       return 0;
     }
-  end = (const char*) &get_msg[1];
-  if (message_context->xquery_size == 0)
+  end = (const char *) &get_msg[1];
+  if (msg_ctx->xquery_size == 0)
     {
-      message_context->xquery = NULL;
+      msg_ctx->xquery = NULL;
     }
   else
     {
-      message_context->xquery = (const void*) end;
-      end += message_context->xquery_size;
+      msg_ctx->xquery = (const void *) end;
+      end += msg_ctx->xquery_size;
     }
   if (bf_size == 0)
     {
-      message_context->reply_bf = NULL;
+      msg_ctx->reply_bf = NULL;
     }
   else
     {
-      message_context->reply_bf = GNUNET_CONTAINER_bloomfilter_init (end,
-                                                                    bf_size,
-                                                                    GNUNET_DHT_GET_BLOOMFILTER_K);
+      msg_ctx->reply_bf = GNUNET_CONTAINER_bloomfilter_init (end,
+                                                             bf_size,
+                                                             GNUNET_DHT_GET_BLOOMFILTER_K);
     }
   type = (enum GNUNET_BLOCK_Type) ntohl (get_msg->type);
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n",
-             my_short_id,
-              "DHT", "GET", 
-             type,
-             GNUNET_h2s (&message_context->key),
-              message_context->unique_id);
+              my_short_id,
+              "DHT", "GET",
+              type, GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id);
 #endif
-  increment_stats(STAT_GETS);
+  increment_stats (STAT_GETS);
   results = 0;
 #if HAVE_MALICIOUS
   if (type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
     {
-      GNUNET_CONTAINER_bloomfilter_free (message_context->reply_bf);
+      GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
       return results;
     }
 #endif
-  message_context->do_forward = GNUNET_YES;
+  msg_ctx->do_forward = GNUNET_YES;
   if (datacache != NULL)
     results
       = GNUNET_DATACACHE_get (datacache,
-                             &message_context->key, type,
-                             &datacache_get_iterator,
-                             message_context);
+                              &msg_ctx->key, type,
+                              &datacache_get_iterator, msg_ctx);
 #if DEBUG_DHT
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "`%s:%s': Found %d results for `%s' request uid %llu\n", my_short_id, "DHT",
-                  results, "GET", message_context->unique_id);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "`%s:%s': Found %d results for `%s' request uid %llu\n",
+              my_short_id, "DHT", results, "GET", msg_ctx->unique_id);
 #endif
   if (results >= 1)
     {
 #if DEBUG_DHT_ROUTING
       if ((debug_routes) && (dhtlog_handle != NULL))
         {
-          dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_GET,
-                                message_context->hop_count, GNUNET_YES, &my_identity,
-                                &message_context->key);
+          dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
+                                       msg_ctx->hop_count, GNUNET_YES,
+                                       &my_identity, &msg_ctx->key);
         }
 
       if ((debug_routes_extended) && (dhtlog_handle != NULL))
         {
-          dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
-                                       message_context->hop_count, GNUNET_YES,
-                                       &my_identity, &message_context->key, message_context->peer,
-                                       NULL);
+          dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
+                                       msg_ctx->hop_count, GNUNET_YES,
+                                       &my_identity, &msg_ctx->key,
+                                       msg_ctx->peer, NULL);
         }
 #endif
     }
@@ -2342,43 +2472,45 @@ handle_dht_get (const struct GNUNET_MessageHeader *msg,
     {
       /* check query valid */
       if (GNUNET_BLOCK_EVALUATION_REQUEST_INVALID
-         == GNUNET_BLOCK_evaluate (block_context,
-                                   type,
-                                   &message_context->key,
-                                   &message_context->reply_bf,
-                                   message_context->reply_bf_mutator,
-                                   message_context->xquery,
-                                   message_context->xquery_size,
-                                   NULL, 0))
-       {
-         GNUNET_break_op (0);
-         message_context->do_forward = GNUNET_NO;
-       }
+          == GNUNET_BLOCK_evaluate (block_context,
+                                    type,
+                                    &msg_ctx->key,
+                                    &msg_ctx->reply_bf,
+                                    msg_ctx->reply_bf_mutator,
+                                    msg_ctx->xquery,
+                                    msg_ctx->xquery_size, NULL, 0))
+        {
+          GNUNET_break_op (0);
+          msg_ctx->do_forward = GNUNET_NO;
+        }
     }
 
-  if (message_context->hop_count == 0) /* Locally initiated request */
+  if (msg_ctx->hop_count == 0)  /* Locally initiated request */
     {
 #if DEBUG_DHT_ROUTING
-    if ((debug_routes) && (dhtlog_handle != NULL))
-      {
-        dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_GET,
-                                      message_context->hop_count, GNUNET_NO, &my_identity,
-                                      &message_context->key);
-      }
+      if ((debug_routes) && (dhtlog_handle != NULL))
+        {
+          dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
+                                       msg_ctx->hop_count, GNUNET_NO,
+                                       &my_identity, &msg_ctx->key);
+        }
 #endif
     }
-  if (message_context->do_forward == GNUNET_YES)
-    route_message (msg, message_context);
-  GNUNET_CONTAINER_bloomfilter_free (message_context->reply_bf);
+  if (msg_ctx->do_forward == GNUNET_YES)
+    route_message (msg, msg_ctx);
+  GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
   return results;
 }
 
 static void
-remove_recent_find_peer(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+remove_recent_find_peer (void *cls,
+                         const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   GNUNET_HashCode *key = cls;
-  
-  GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(recent_find_peer_requests, key, NULL));
+
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multihashmap_remove
+                 (recent_find_peer_requests, key, NULL));
   GNUNET_free (key);
 }
 
@@ -2386,12 +2518,12 @@ remove_recent_find_peer(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
  * Server handler for initiating local dht find peer requests
  *
  * @param find_msg the actual find peer message
- * @param message_context struct containing pertinent information about the request
+ * @param msg_ctx struct containing pertinent information about the request
  *
  */
 static void
 handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
-                      struct DHT_MessageContext *message_context)
+                      struct DHT_MessageContext *msg_ctx)
 {
   struct GNUNET_MessageHeader *find_peer_result;
   struct GNUNET_DHT_FindPeerMessage *find_peer_message;
@@ -2404,83 +2536,95 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
   size_t other_hello_size;
   struct GNUNET_PeerIdentity peer_id;
 
-  find_peer_message = (struct GNUNET_DHT_FindPeerMessage *)find_msg;
-  GNUNET_break_op(ntohs(find_msg->size) >= (sizeof(struct GNUNET_DHT_FindPeerMessage)));
-  if (ntohs(find_msg->size) < sizeof(struct GNUNET_DHT_FindPeerMessage))
+  find_peer_message = (struct GNUNET_DHT_FindPeerMessage *) find_msg;
+  GNUNET_break_op (ntohs (find_msg->size) >=
+                   (sizeof (struct GNUNET_DHT_FindPeerMessage)));
+  if (ntohs (find_msg->size) < sizeof (struct GNUNET_DHT_FindPeerMessage))
     return;
   other_hello = NULL;
   other_hello_size = 0;
-  if (ntohs(find_msg->size) > sizeof(struct GNUNET_DHT_FindPeerMessage))
+  if (ntohs (find_msg->size) > sizeof (struct GNUNET_DHT_FindPeerMessage))
     {
-      other_hello_size = ntohs(find_msg->size) - sizeof(struct GNUNET_DHT_FindPeerMessage);
-      other_hello = GNUNET_malloc(other_hello_size);
-      memcpy(other_hello, &find_peer_message[1], other_hello_size);
-      if ((GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)other_hello) == 0) || (GNUNET_OK != GNUNET_HELLO_get_id((struct GNUNET_HELLO_Message *)other_hello, &peer_id)))
+      other_hello_size =
+        ntohs (find_msg->size) - sizeof (struct GNUNET_DHT_FindPeerMessage);
+      other_hello = GNUNET_malloc (other_hello_size);
+      memcpy (other_hello, &find_peer_message[1], other_hello_size);
+      if ((GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) other_hello) ==
+           0)
+          || (GNUNET_OK !=
+              GNUNET_HELLO_get_id ((struct GNUNET_HELLO_Message *)
+                                   other_hello, &peer_id)))
         {
-          GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Received invalid HELLO message in find peer request!\n");
-          GNUNET_free(other_hello);
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                      "Received invalid HELLO message in find peer request!\n");
+          GNUNET_free (other_hello);
           return;
         }
 #if FIND_PEER_WITH_HELLO
-      if (GNUNET_YES == consider_peer(&peer_id))
+      if (GNUNET_YES == consider_peer (&peer_id))
         {
-          increment_stats(STAT_HELLOS_PROVIDED);
-          GNUNET_TRANSPORT_offer_hello(transport_handle, other_hello);
-          GNUNET_CORE_peer_request_connect(cfg, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), &peer_id, NULL, NULL);
-         route_message (find_msg, message_context);
-         GNUNET_free (other_hello);
+          increment_stats (STAT_HELLOS_PROVIDED);
+          GNUNET_TRANSPORT_offer_hello (transport_handle, other_hello);
+          GNUNET_CORE_peer_request_connect (coreAPI,
+                                            GNUNET_TIME_relative_multiply
+                                            (GNUNET_TIME_UNIT_SECONDS, 5),
+                                            &peer_id, NULL, NULL);
+          route_message (find_msg, msg_ctx);
+          GNUNET_free (other_hello);
+          return;
+        }
+      else                      /* We don't want this peer! */
+        {
+          route_message (find_msg, msg_ctx);
+          GNUNET_free (other_hello);
           return;
         }
-      else /* We don't want this peer! */
-       {
-         route_message (find_msg, message_context);
-         GNUNET_free (other_hello);
-         return;
-       }
 #endif
     }
 
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "`%s:%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
-              my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&message_context->key),
-              ntohs (find_msg->size),
-              sizeof (struct GNUNET_MessageHeader));
+              my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&msg_ctx->key),
+              ntohs (find_msg->size), sizeof (struct GNUNET_MessageHeader));
 #endif
   if (my_hello == NULL)
-  {
+    {
 #if DEBUG_DHT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "`%s': Our HELLO is null, can't return.\n",
-                "DHT");
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "`%s': Our HELLO is null, can't return.\n", "DHT");
 #endif
-    GNUNET_free_non_null (other_hello);
-    route_message (find_msg, message_context);
-    return;
-  }
+      GNUNET_free_non_null (other_hello);
+      route_message (find_msg, msg_ctx);
+      return;
+    }
 
-  incoming_bloom = GNUNET_CONTAINER_bloomfilter_init(find_peer_message->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K);
-  if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(incoming_bloom, &my_identity.hashPubKey))
+  incoming_bloom =
+    GNUNET_CONTAINER_bloomfilter_init (find_peer_message->bloomfilter,
+                                       DHT_BLOOM_SIZE, DHT_BLOOM_K);
+  if (GNUNET_YES ==
+      GNUNET_CONTAINER_bloomfilter_test (incoming_bloom,
+                                         &my_identity.hashPubKey))
     {
-      increment_stats(STAT_BLOOM_FIND_PEER);
-      GNUNET_CONTAINER_bloomfilter_free(incoming_bloom);
-      GNUNET_free_non_null(other_hello);
-      route_message (find_msg, message_context);
-      return; /* We match the bloomfilter, do not send a response to this peer (they likely already know us!)*/
+      increment_stats (STAT_BLOOM_FIND_PEER);
+      GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
+      GNUNET_free_non_null (other_hello);
+      route_message (find_msg, msg_ctx);
+      return;                   /* We match the bloomfilter, do not send a response to this peer (they likely already know us!) */
     }
-  GNUNET_CONTAINER_bloomfilter_free(incoming_bloom);
+  GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
 
 #if RESTRICT_FIND_PEER
 
   /**
    * Ignore any find peer requests from a peer we have seen very recently.
    */
-  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(recent_find_peer_requests, &message_context->key)) /* We have recently responded to a find peer request for this peer! */
-  {
-    increment_stats("# dht find peer requests ignored (recently seen!)");
-    GNUNET_free_non_null(other_hello);
-    return;
-  }
+  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (recent_find_peer_requests, &msg_ctx->key))  /* We have recently responded to a find peer request for this peer! */
+    {
+      increment_stats ("# dht find peer requests ignored (recently seen!)");
+      GNUNET_free_non_null (other_hello);
+      return;
+    }
 
   /**
    * Use this check to only allow the peer to respond to find peer requests if
@@ -2490,41 +2634,46 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
    * the network and has no other peers this is a problem (assume all buckets
    * full, no one will respond!).
    */
-  memcpy(&peer_id.hashPubKey, &message_context->key, sizeof(GNUNET_HashCode));
-  if (GNUNET_NO == consider_peer(&peer_id))
+  memcpy (&peer_id.hashPubKey, &msg_ctx->key, sizeof (GNUNET_HashCode));
+  if (GNUNET_NO == consider_peer (&peer_id))
     {
-      increment_stats("# dht find peer requests ignored (do not need!)");
-      GNUNET_free_non_null(other_hello);
-      route_message (find_msg, message_context);
+      increment_stats ("# dht find peer requests ignored (do not need!)");
+      GNUNET_free_non_null (other_hello);
+      route_message (find_msg, msg_ctx);
       return;
     }
 #endif
 
-  recent_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
-  memcpy(recent_hash, &message_context->key, sizeof(GNUNET_HashCode));
-  if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests,
-                                    &message_context->key, NULL, 
-                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+  recent_hash = GNUNET_malloc (sizeof (GNUNET_HashCode));
+  memcpy (recent_hash, &msg_ctx->key, sizeof (GNUNET_HashCode));
+  if (GNUNET_SYSERR !=
+      GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests,
+                                         &msg_ctx->key, NULL,
+                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Adding recent remove task for key `%s`!\n", GNUNET_h2s(&message_context->key));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Adding recent remove task for key `%s`!\n",
+                  GNUNET_h2s (&msg_ctx->key));
       /* Only add a task if there wasn't one for this key already! */
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
+      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                    (GNUNET_TIME_UNIT_SECONDS, 30),
                                     &remove_recent_find_peer, recent_hash);
     }
   else
     {
-      GNUNET_free(recent_hash);
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received duplicate find peer request too soon!\n");
+      GNUNET_free (recent_hash);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Received duplicate find peer request too soon!\n");
     }
 
   /* Simplistic find_peer functionality, always return our hello */
-  hello_size = ntohs(my_hello->size);
+  hello_size = ntohs (my_hello->size);
   tsize = hello_size + sizeof (struct GNUNET_MessageHeader);
 
   if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
     {
       GNUNET_break_op (0);
-      GNUNET_free_non_null(other_hello);
+      GNUNET_free_non_null (other_hello);
       return;
     }
 
@@ -2537,27 +2686,28 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
               "`%s': Sending hello size %d to requesting peer.\n",
               "DHT", hello_size);
 
-  new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext));
-  memcpy(new_msg_ctx, message_context, sizeof(struct DHT_MessageContext));
+  new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
+  memcpy (new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
   new_msg_ctx->peer = &my_identity;
-  new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+  new_msg_ctx->bloom =
+    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
   new_msg_ctx->hop_count = 0;
-  new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE * 2; /* Make find peer requests a higher priority */
+  new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;     /* Make find peer requests a higher priority */
   new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
-  increment_stats(STAT_FIND_PEER_ANSWER);
-  route_result_message(find_peer_result, new_msg_ctx);
-  GNUNET_free(new_msg_ctx);
+  increment_stats (STAT_FIND_PEER_ANSWER);
+  route_result_message (find_peer_result, new_msg_ctx);
+  GNUNET_free (new_msg_ctx);
 #if DEBUG_DHT_ROUTING
   if ((debug_routes) && (dhtlog_handle != NULL))
     {
-      dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_FIND_PEER,
-                                   message_context->hop_count, GNUNET_YES, &my_identity,
-                                   &message_context->key);
+      dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER,
+                                   msg_ctx->hop_count, GNUNET_YES,
+                                   &my_identity, &msg_ctx->key);
     }
 #endif
-  GNUNET_free_non_null(other_hello);
-  GNUNET_free(find_peer_result);
-  route_message (find_msg, message_context);
+  GNUNET_free_non_null (other_hello);
+  GNUNET_free (find_peer_result);
+  route_message (find_msg, msg_ctx);
 }
 
 /**
@@ -2568,17 +2718,17 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
  * @param tc runtime context for this task
  */
 static void
-republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+republish_content (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 /**
  * Server handler for initiating local dht put requests
  *
  * @param msg the actual put message
- * @param message_context struct containing pertinent information about the request
+ * @param msg_ctx struct containing pertinent information about the request
  */
 static void
 handle_dht_put (const struct GNUNET_MessageHeader *msg,
-                struct DHT_MessageContext *message_context)
+                struct DHT_MessageContext *msg_ctx)
 {
   const struct GNUNET_DHT_PutMessage *put_msg;
   enum GNUNET_BLOCK_Type put_type;
@@ -2587,33 +2737,59 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
   struct RepublishContext *put_context;
   GNUNET_HashCode key;
 
-  GNUNET_assert (ntohs (msg->size) >=
-                 sizeof (struct GNUNET_DHT_PutMessage));
+  GNUNET_assert (ntohs (msg->size) >= sizeof (struct GNUNET_DHT_PutMessage));
 
 
-  put_msg = (const struct GNUNET_DHT_PutMessage *)msg;
+  put_msg = (const struct GNUNET_DHT_PutMessage *) msg;
   put_type = (enum GNUNET_BLOCK_Type) ntohl (put_msg->type);
 #if HAVE_MALICIOUS
   if (put_type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
-    return;
+    {
+#if DEBUG_DHT_ROUTING
+      if ((debug_routes_extended) && (dhtlog_handle != NULL))
+        {
+          /** Log routes that die due to high load! */
+          dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
+                                       msg_ctx->hop_count, GNUNET_SYSERR,
+                                       &my_identity, &msg_ctx->key,
+                                       msg_ctx->peer, NULL);
+        }
+#endif
+      return;
+    }
 #endif
-  data_size = ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
-  ret = GNUNET_BLOCK_get_key (block_context,
-                             put_type,
-                             &put_msg[1],
-                             data_size,
-                             &key);
+  data_size =
+    ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
+  ret =
+    GNUNET_BLOCK_get_key (block_context, put_type, &put_msg[1], data_size,
+                          &key);
   if (GNUNET_NO == ret)
     {
+#if DEBUG_DHT_ROUTING
+      if ((debug_routes_extended) && (dhtlog_handle != NULL))
+        {
+          dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
+                                       msg_ctx->hop_count, GNUNET_SYSERR,
+                                       &my_identity, &msg_ctx->key,
+                                       msg_ctx->peer, NULL);
+        }
+#endif
       /* invalid reply */
       GNUNET_break_op (0);
       return;
     }
-  if ( (GNUNET_YES == ret) &&
-       (0 != memcmp (&key,
-                    &message_context->key,
-                    sizeof (GNUNET_HashCode))) )
+  if ((GNUNET_YES == ret) &&
+      (0 != memcmp (&key, &msg_ctx->key, sizeof (GNUNET_HashCode))))
     {
+#if DEBUG_DHT_ROUTING
+      if ((debug_routes_extended) && (dhtlog_handle != NULL))
+        {
+          dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
+                                       msg_ctx->hop_count, GNUNET_SYSERR,
+                                       &my_identity, &msg_ctx->key,
+                                       msg_ctx->peer, NULL);
+        }
+#endif
       /* invalid wrapper: key mismatch! */
       GNUNET_break_op (0);
       return;
@@ -2623,62 +2799,66 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
-              my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&message_context->key), message_context->unique_id);
+              my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
+              msg_ctx->unique_id);
 #endif
 #if DEBUG_DHT_ROUTING
-  if (message_context->hop_count == 0) /* Locally initiated request */
+  if (msg_ctx->hop_count == 0)  /* Locally initiated request */
     {
       if ((debug_routes) && (dhtlog_handle != NULL))
         {
-          dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_PUT,
-                                       message_context->hop_count, GNUNET_NO, &my_identity,
-                                       &message_context->key);
+          dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
+                                       msg_ctx->hop_count, GNUNET_NO,
+                                       &my_identity, &msg_ctx->key);
         }
     }
 #endif
 
-  if (message_context->closest != GNUNET_YES)
+  if (msg_ctx->closest != GNUNET_YES)
     {
-      route_message (msg, message_context);
+      route_message (msg, msg_ctx);
       return;
     }
 
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
-              my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&message_context->key), message_context->unique_id);
+              my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
+              msg_ctx->unique_id);
 #endif
 
 #if DEBUG_DHT_ROUTING
   if ((debug_routes_extended) && (dhtlog_handle != NULL))
     {
-      dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
-                                   message_context->hop_count, GNUNET_YES,
-                                   &my_identity, &message_context->key, message_context->peer,
+      dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
+                                   msg_ctx->hop_count, GNUNET_YES,
+                                   &my_identity, &msg_ctx->key, msg_ctx->peer,
                                    NULL);
     }
 
   if ((debug_routes) && (dhtlog_handle != NULL))
     {
-      dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_PUT,
-                                   message_context->hop_count, GNUNET_YES, &my_identity,
-                                   &message_context->key);
+      dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
+                                   msg_ctx->hop_count, GNUNET_YES,
+                                   &my_identity, &msg_ctx->key);
     }
 #endif
 
-  increment_stats(STAT_PUTS_INSERTED);
+  increment_stats (STAT_PUTS_INSERTED);
   if (datacache != NULL)
     {
-      ret = GNUNET_DATACACHE_put (datacache, &message_context->key, data_size,
+      ret = GNUNET_DATACACHE_put (datacache, &msg_ctx->key, data_size,
                                   (char *) &put_msg[1], put_type,
-                                  GNUNET_TIME_absolute_ntoh(put_msg->expiration));
+                                  GNUNET_TIME_absolute_ntoh
+                                  (put_msg->expiration));
 
       if ((ret == GNUNET_YES) && (do_republish == GNUNET_YES))
         {
-          put_context = GNUNET_malloc(sizeof(struct RepublishContext));
-          memcpy(&put_context->key, &message_context->key, sizeof(GNUNET_HashCode));
+          put_context = GNUNET_malloc (sizeof (struct RepublishContext));
+          memcpy (&put_context->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
           put_context->type = put_type;
-          GNUNET_SCHEDULER_add_delayed (dht_republish_frequency, &republish_content, put_context);
+          GNUNET_SCHEDULER_add_delayed (dht_republish_frequency,
+                                        &republish_content, put_context);
         }
     }
   else
@@ -2687,7 +2867,7 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
                 my_short_id, "DHT", "PUT");
 
   if (stop_on_closest == GNUNET_NO)
-    route_message (msg, message_context);
+    route_message (msg, msg_ctx);
 }
 
 /**
@@ -2704,7 +2884,8 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
  *
  * @return ballpark diameter figure
  */
-static unsigned int estimate_diameter()
+static unsigned int
+estimate_diameter ()
 {
   return MAX_BUCKETS - lowest_bucket;
 }
@@ -2714,20 +2895,27 @@ static unsigned int estimate_diameter()
  * forward the request to obtain the desired
  * target_replication count (on average).
  *
- * Always 0, 1 or 2 (don't send, send once, split)
+ * returns: target_replication / (est. hops) + (target_replication * hop_count)
+ * where est. hops is typically 2 * the routing table depth
+ *
+ * @param hop_count number of hops the message has traversed
+ * @param target_replication the number of total paths desired
+ *
+ * @return Some number of peers to forward the message to
  */
 static unsigned int
 get_forward_count (unsigned int hop_count, size_t target_replication)
 {
-#if DOUBLE
-  double target_count;
-  double random_probability;
-#else
   uint32_t random_value;
-#endif
-  unsigned int target_value;
+  unsigned int forward_count;
+  float target_value;
   unsigned int diameter;
 
+  diameter = estimate_diameter ();
+
+  if (GNUNET_NO == use_max_hops)
+    max_hops = (diameter + 1) * 2;
+
   /**
    * If we are behaving in strict kademlia mode, send multiple initial requests,
    * but then only send to 1 or 0 peers based strictly on the number of hops.
@@ -2735,7 +2923,7 @@ get_forward_count (unsigned int hop_count, size_t target_replication)
   if (strict_kademlia == GNUNET_YES)
     {
       if (hop_count == 0)
-        return DHT_KADEMLIA_REPLICATION;
+        return kademlia_replication;
       else if (hop_count < max_hops)
         return 1;
       else
@@ -2746,55 +2934,37 @@ get_forward_count (unsigned int hop_count, size_t target_replication)
    * routing right?  The estimation below only works if we think we have reasonably
    * full routing tables, which for our RR topologies may not be the case!
    */
-  diameter = estimate_diameter ();
-  if ((hop_count > (diameter + 1) * 2) && (MINIMUM_PEER_THRESHOLD < estimate_diameter() * bucket_size) && (use_max_hops == GNUNET_NO))
+  if (hop_count > max_hops)
     {
 #if DEBUG_DHT
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "`%s:%s': Hop count too high (est %d, lowest %d), NOT Forwarding request\n", my_short_id,
-                  "DHT", estimate_diameter(), lowest_bucket);
+                  "`%s:%s': Hop count too high (est %d, lowest %d), NOT Forwarding request\n",
+                  my_short_id, "DHT", estimate_diameter (), lowest_bucket);
 #endif
       return 0;
     }
-  else if (hop_count > max_hops)
+
+  random_value = 0;
+  forward_count = 1;
+  target_value =
+    target_replication / (diameter +
+                          ((float) target_replication * hop_count));
+  if (target_value > 1)
     {
-#if DEBUG_DHT
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "`%s:%s': Hop count too high (greater than max)\n", my_short_id,
-                  "DHT");
-#endif
-      return 0;
+      /* Set forward count to floor of target_value */
+      forward_count = (unsigned int) target_value;
+      /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
+      target_value = target_value - forward_count;
     }
+  else
+    random_value =
+      GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
+                                UINT32_MAX);
 
-#if DOUBLE
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Replication %d, hop_count %u, diameter %u\n", target_replication, hop_count, diameter);
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Numerator %f, denominator %f\n", (double)target_replication, ((double)target_replication * (hop_count + 1) + diameter));
-  target_count = /* target_count is ALWAYS < 1 unless replication is < 1 */
-    (double)target_replication / ((double)target_replication * (hop_count + 1) + diameter);
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Target count is %f\n", target_count);
-  random_probability = ((double)GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
-      RAND_MAX)) / RAND_MAX;
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Random is %f\n", random_probability);
-
-  target_value = 0;
-  //while (target_value < target_count)
-  if (target_value < target_count)
-    target_value++; /* target_value is ALWAYS 1 after this "loop", right?  Because target_count is always > 0, right?  Or does it become 0.00000... at some point because the hop count is so high? */
-
-
-  //if ((target_count + 1 - (double)target_value) > random_probability)
-  if ((target_count) > random_probability)
-    target_value++;
-#endif
-
-  random_value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, target_replication * (hop_count + 1) + diameter) + 1;
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "replication %u, at hop %d, will split with probability %f\n", target_replication, hop_count, target_replication / (double)((target_replication * (hop_count + 1) + diameter) + 1));
-  target_value = 1;
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "random %u, target %u, max %u\n", random_value, target_replication, target_replication * (hop_count + 1) + diameter);
-  if (random_value < target_replication)
-    target_value++;
+  if (random_value < (target_value * UINT32_MAX))
+    forward_count += 1;
 
-  return target_value;
+  return forward_count;
 }
 
 /*
@@ -2809,7 +2979,8 @@ get_forward_count (unsigned int hop_count, size_t target_replication)
  * otherwise.
  */
 int
-am_closest_peer (const GNUNET_HashCode * target, struct GNUNET_CONTAINER_BloomFilter *bloom)
+am_closest_peer (const GNUNET_HashCode * target,
+                 struct GNUNET_CONTAINER_BloomFilter *bloom)
 {
   int bits;
   int other_bits;
@@ -2818,31 +2989,34 @@ am_closest_peer (const GNUNET_HashCode * target, struct GNUNET_CONTAINER_BloomFi
   struct PeerInfo *pos;
   unsigned int my_distance;
 
-  if (0 == memcmp(&my_identity.hashPubKey, target, sizeof(GNUNET_HashCode)))
+  if (0 == memcmp (&my_identity.hashPubKey, target, sizeof (GNUNET_HashCode)))
     return GNUNET_YES;
 
-  bucket_num = find_current_bucket(target);
+  bucket_num = find_current_bucket (target);
 
-  bits = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, target);
-  my_distance = distance(&my_identity.hashPubKey, target);
+  bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, target);
+  my_distance = distance (&my_identity.hashPubKey, target);
   pos = k_buckets[bucket_num].head;
   count = 0;
   while ((pos != NULL) && (count < bucket_size))
     {
-      if ((bloom != NULL) && (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(bloom, &pos->id.hashPubKey)))
+      if ((bloom != NULL)
+          && (GNUNET_YES ==
+              GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
         {
           pos = pos->next;
-          continue; /* Skip already checked entries */
+          continue;             /* Skip already checked entries */
         }
 
-      other_bits = GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target);
+      other_bits =
+        GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, target);
       if (other_bits > bits)
         return GNUNET_NO;
-      else if (other_bits == bits) /* We match the same number of bits, do distance comparison */
+      else if (other_bits == bits)      /* We match the same number of bits */
         {
-          if (strict_kademlia != GNUNET_YES) /* Return that we at as close as any other peer */
+          if (strict_kademlia != GNUNET_YES)    /* Return that we at as close as any other peer */
             return GNUNET_YES;
-          else if (distance(&pos->id.hashPubKey, target) < my_distance) /* Check all known peers, only return if we are the true closest */
+          else if (distance (&pos->id.hashPubKey, target) < my_distance)        /* Check all known peers, only return if we are the true closest */
             return GNUNET_NO;
         }
       pos = pos->next;
@@ -2867,13 +3041,12 @@ am_closest_peer (const GNUNET_HashCode * target, struct GNUNET_CONTAINER_BloomFi
  *
  */
 static unsigned long long
-converge_distance (const GNUNET_HashCode *target,
-                   struct PeerInfo *peer,
-                   unsigned int hops)
+converge_distance (const GNUNET_HashCode * target,
+                   struct PeerInfo *peer, unsigned int hops)
 {
   unsigned long long ret;
   unsigned int other_matching_bits;
-  double base_converge_modifier = .1; /* Value that "looks" good (when plotted), have to start somewhere */
+  double base_converge_modifier = .1;   /* Value that "looks" good (when plotted), have to start somewhere */
   double temp_modifier;
   double calc_value;
   double exponent;
@@ -2882,7 +3055,7 @@ converge_distance (const GNUNET_HashCode *target,
   if (use_max_hops)
     curr_max_hops = max_hops;
   else
-    curr_max_hops = (estimate_diameter() + 1) * 2;
+    curr_max_hops = (estimate_diameter () + 1) * 2;
 
   if (converge_modifier > 0)
     temp_modifier = converge_modifier * base_converge_modifier;
@@ -2892,33 +3065,37 @@ converge_distance (const GNUNET_HashCode *target,
       base_converge_modifier = 0.0;
     }
 
-  GNUNET_assert(temp_modifier > 0);
+  GNUNET_assert (temp_modifier > 0);
 
-  other_matching_bits = GNUNET_CRYPTO_hash_matching_bits(target, &peer->id.hashPubKey);
+  other_matching_bits =
+    GNUNET_CRYPTO_hash_matching_bits (target, &peer->id.hashPubKey);
 
   switch (converge_option)
     {
-      case DHT_CONVERGE_RANDOM:
-        return 1; /* Always return 1, choose equally among all peers */
-      case DHT_CONVERGE_LINEAR:
-        calc_value = hops * curr_max_hops * temp_modifier;
-        break;
-      case DHT_CONVERGE_SQUARE:
+    case DHT_CONVERGE_RANDOM:
+      return 1;                 /* Always return 1, choose equally among all peers */
+    case DHT_CONVERGE_LINEAR:
+      calc_value = hops * curr_max_hops * temp_modifier;
+      break;
+    case DHT_CONVERGE_SQUARE:
         /**
          * Simple square based curve.
          */
-        calc_value = (sqrt(hops) / sqrt(curr_max_hops)) * (curr_max_hops / (curr_max_hops * temp_modifier));
-        break;
-      case DHT_CONVERGE_EXPONENTIAL:
+      calc_value =
+        (sqrt (hops) / sqrt (curr_max_hops)) * (curr_max_hops /
+                                                (curr_max_hops *
+                                                 temp_modifier));
+      break;
+    case DHT_CONVERGE_EXPONENTIAL:
         /**
          * Simple exponential curve.
          */
-        if (base_converge_modifier > 0)
-          calc_value = (temp_modifier * hops * hops) / curr_max_hops;
-        else
-          calc_value = (hops * hops) / curr_max_hops;
-        break;
-      case DHT_CONVERGE_BINARY:
+      if (base_converge_modifier > 0)
+        calc_value = (temp_modifier * hops * hops) / curr_max_hops;
+      else
+        calc_value = (hops * hops) / curr_max_hops;
+      break;
+    case DHT_CONVERGE_BINARY:
         /**
          * If below the cutoff, route randomly (return 1),
          * If above the cutoff, return the maximum possible
@@ -2926,17 +3103,17 @@ converge_distance (const GNUNET_HashCode *target,
          * they are sorted.)
          */
 
-        if (hops > converge_modifier) /* Past cutoff */
-          {
-            return ULLONG_MAX;
-          }
-        /* Fall through */
-      default:
-        return 1;
+      if (hops >= converge_modifier)    /* Past cutoff */
+        {
+          return ULLONG_MAX;
+        }
+      /* Fall through */
+    default:
+      return 1;
     }
 
   /* Take the log (base e) of the number of bits matching the other peer */
-  exponent = log(other_matching_bits);
+  exponent = log (other_matching_bits);
 
   /* Check if we would overflow; our largest possible value is 2^64 approx. e^44.361419555836498 */
   if (exponent * calc_value >= 44.361419555836498)
@@ -2944,17 +3121,17 @@ converge_distance (const GNUNET_HashCode *target,
 
   /* Clear errno and all math exceptions */
   errno = 0;
-  feclearexcept(FE_ALL_EXCEPT);
-  ret = (unsigned long long)pow(other_matching_bits, calc_value);
-  if ((errno != 0) || fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
-      FE_UNDERFLOW))
-    {
-      if (0 != fetestexcept(FE_OVERFLOW))
-        GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_OVERFLOW\n");
-      if (0 != fetestexcept(FE_INVALID))
-        GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_INVALID\n");
-      if (0 != fetestexcept(FE_UNDERFLOW))
-        GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_UNDERFLOW\n");
+  feclearexcept (FE_ALL_EXCEPT);
+  ret = (unsigned long long) pow (other_matching_bits, calc_value);
+  if ((errno != 0) || fetestexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
+                                    FE_UNDERFLOW))
+    {
+      if (0 != fetestexcept (FE_OVERFLOW))
+        GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "FE_OVERFLOW\n");
+      if (0 != fetestexcept (FE_INVALID))
+        GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "FE_INVALID\n");
+      if (0 != fetestexcept (FE_UNDERFLOW))
+        GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "FE_UNDERFLOW\n");
       return 0;
     }
   else
@@ -2976,8 +3153,8 @@ converge_distance (const GNUNET_HashCode *target,
 static int
 compare_peers (const void *p1, const void *p2)
 {
-  struct PeerInfo **first = (struct PeerInfo **)p1;
-  struct PeerInfo **second = (struct PeerInfo **)p2;
+  struct PeerInfo **first = (struct PeerInfo **) p1;
+  struct PeerInfo **second = (struct PeerInfo **) p2;
 
   if ((*first)->matching_bits > (*second)->matching_bits)
     return -1;
@@ -2999,6 +3176,7 @@ compare_peers (const void *p1, const void *p2)
  *
  * @param target the key we are selecting a peer to route to
  * @param bloom a bloomfilter containing entries this request has seen already
+ * @param hops how many hops has this message traversed thus far
  *
  * @return Peer to route to, or NULL on error
  */
@@ -3026,10 +3204,14 @@ select_peer (const GNUNET_HashCode * target,
   unsigned int largest_distance;
   struct PeerInfo *chosen;
 
-  my_matching_bits = GNUNET_CRYPTO_hash_matching_bits(target, &my_identity.hashPubKey);
+  my_matching_bits =
+    GNUNET_CRYPTO_hash_matching_bits (target, &my_identity.hashPubKey);
 
   total_distance = 0;
-  if (strict_kademlia == GNUNET_YES)
+  /** If we are doing kademlia routing, or converge is binary (saves some cycles) */
+  if ((strict_kademlia == GNUNET_YES) ||
+      ((converge_option == DHT_CONVERGE_BINARY)
+       && (hops >= converge_modifier)))
     {
       largest_distance = 0;
       chosen = NULL;
@@ -3040,7 +3222,9 @@ select_peer (const GNUNET_HashCode * target,
           while ((pos != NULL) && (count < bucket_size))
             {
               /* If we are doing strict Kademlia routing, then checking the bloomfilter is basically cheating! */
-              if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+              if (GNUNET_NO ==
+                  GNUNET_CONTAINER_bloomfilter_test (bloom,
+                                                     &pos->id.hashPubKey))
                 {
                   distance = inverse_distance (target, &pos->id.hashPubKey);
                   if (distance > largest_distance)
@@ -3056,7 +3240,7 @@ select_peer (const GNUNET_HashCode * target,
 
       if ((largest_distance > 0) && (chosen != NULL))
         {
-          GNUNET_CONTAINER_bloomfilter_add(bloom, &chosen->id.hashPubKey);
+          GNUNET_CONTAINER_bloomfilter_add (bloom, &chosen->id.hashPubKey);
           return chosen;
         }
       else
@@ -3072,22 +3256,24 @@ select_peer (const GNUNET_HashCode * target,
    * Then go over all HIGHER buckets (matching less then we do)
    */
 
-  closest_bucket = find_current_bucket(target);
-  GNUNET_assert(closest_bucket >= lowest_bucket);
+  closest_bucket = find_current_bucket (target);
+  GNUNET_assert (closest_bucket >= lowest_bucket);
   pos = k_buckets[closest_bucket].head;
   count = 0;
-  offset = 0; /* Need offset as well as count in case peers are bloomfiltered */
-  memset(sorted_closest, 0, sizeof(sorted_closest));
+  offset = 0;                   /* Need offset as well as count in case peers are bloomfiltered */
+  memset (sorted_closest, 0, sizeof (sorted_closest));
   /* Put any peers in the closest bucket in the sorting array */
   while ((pos != NULL) && (count < bucket_size))
     {
-      if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+      if (GNUNET_YES ==
+          GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
         {
           count++;
           pos = pos->next;
-          continue; /* Ignore bloomfiltered peers */
+          continue;             /* Ignore bloomfiltered peers */
         }
-      pos->matching_bits = GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target);
+      pos->matching_bits =
+        GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, target);
       sorted_closest[offset] = pos;
       pos = pos->next;
       offset++;
@@ -3095,18 +3281,23 @@ select_peer (const GNUNET_HashCode * target,
     }
 
   /* Sort the peers in descending order */
-  qsort(&sorted_closest[0], offset, sizeof(struct PeerInfo *), &compare_peers);
+  qsort (&sorted_closest[0], offset, sizeof (struct PeerInfo *),
+         &compare_peers);
 
   /* Put the sorted closest peers into the possible bins first, in case of overflow. */
   for (i = 0; i < offset; i++)
     {
-      temp_converge_distance = converge_distance(target, sorted_closest[i], hops);
-      if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey))
-        break; /* Ignore bloomfiltered peers */
-      if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
+      temp_converge_distance =
+        converge_distance (target, sorted_closest[i], hops);
+      if (GNUNET_YES ==
+          GNUNET_CONTAINER_bloomfilter_test (bloom,
+                                             &sorted_closest[i]->id.
+                                             hashPubKey))
+        break;                  /* Ignore bloomfiltered peers */
+      if (total_distance + temp_converge_distance > total_distance) /* Handle largest case and overflow */
         total_distance += temp_converge_distance;
       else
-        break; /* overflow case */
+        break;                  /* overflow case */
     }
 
   /* Now handle peers in lower buckets (matches same # of bits as target) */
@@ -3116,17 +3307,18 @@ select_peer (const GNUNET_HashCode * target,
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
         {
-          if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+          if (GNUNET_YES ==
+              GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
             {
               count++;
               pos = pos->next;
-              continue; /* Ignore bloomfiltered peers */
+              continue;         /* Ignore bloomfiltered peers */
             }
-          temp_converge_distance = converge_distance(target, pos, hops);
-          if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
+          temp_converge_distance = converge_distance (target, pos, hops);
+          if (total_distance + temp_converge_distance > total_distance)     /* Handle largest case and overflow */
             total_distance += temp_converge_distance;
           else
-            break; /* overflow case */
+            break;              /* overflow case */
           pos = pos->next;
           count++;
         }
@@ -3139,25 +3331,26 @@ select_peer (const GNUNET_HashCode * target,
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
         {
-          if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+          if (GNUNET_YES ==
+              GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
             {
               count++;
               pos = pos->next;
-              continue; /* Ignore bloomfiltered peers */
+              continue;         /* Ignore bloomfiltered peers */
             }
-          temp_converge_distance = converge_distance(target, pos, hops);
-          if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
+          temp_converge_distance = converge_distance (target, pos, hops);
+          if (total_distance + temp_converge_distance > total_distance)     /* Handle largest case and overflow */
             total_distance += temp_converge_distance;
           else
-            break; /* overflow case */
+            break;              /* overflow case */
           pos = pos->next;
           count++;
         }
     }
 
-  if (total_distance == 0) /* No peers to select from! */
+  if (total_distance == 0)      /* No peers to select from! */
     {
-      increment_stats("# select_peer, total_distance == 0");
+      increment_stats ("# failed to select peer");
       return NULL;
     }
 
@@ -3168,14 +3361,26 @@ select_peer (const GNUNET_HashCode * target,
   stats_total_distance = 0;
   for (i = 0; i < offset; i++)
     {
-      if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey))
-        break; /* Ignore bloomfiltered peers */
-      temp_converge_distance = converge_distance(target, sorted_closest[i], hops);
-      if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
+      if (GNUNET_YES ==
+          GNUNET_CONTAINER_bloomfilter_test (bloom,
+                                             &sorted_closest[i]->id.
+                                             hashPubKey))
+        break;                  /* Ignore bloomfiltered peers */
+      temp_converge_distance =
+        converge_distance (target, sorted_closest[i], hops);
+      if (stats_total_distance + temp_converge_distance > stats_total_distance)     /* Handle largest case and overflow */
         stats_total_distance += temp_converge_distance;
       else
-        break; /* overflow case */
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&sorted_closest[i]->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&sorted_closest[i]->id.hashPubKey, &my_identity.hashPubKey), (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance);
+        break;                  /* overflow case */
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n",
+                  GNUNET_CRYPTO_hash_matching_bits (&sorted_closest[i]->id.
+                                                    hashPubKey, target),
+                  GNUNET_CRYPTO_hash_matching_bits (&sorted_closest[i]->id.
+                                                    hashPubKey,
+                                                    &my_identity.hashPubKey),
+                  (temp_converge_distance / (double) total_distance) * 100,
+                  temp_converge_distance);
     }
 
   /* Now handle peers in lower buckets (matches same # of bits as target) */
@@ -3185,18 +3390,27 @@ select_peer (const GNUNET_HashCode * target,
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
         {
-          if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+          if (GNUNET_YES ==
+              GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
             {
               count++;
               pos = pos->next;
-              continue; /* Ignore bloomfiltered peers */
+              continue;         /* Ignore bloomfiltered peers */
             }
-          temp_converge_distance = converge_distance(target, pos, hops);
-          if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
+          temp_converge_distance = converge_distance (target, pos, hops);
+          if (stats_total_distance + temp_converge_distance > stats_total_distance) /* Handle largest case and overflow */
             stats_total_distance += temp_converge_distance;
           else
-            break; /* overflow case */
-          GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey), (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance);
+            break;              /* overflow case */
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n",
+                      GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
+                                                        target),
+                      GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
+                                                        &my_identity.
+                                                        hashPubKey),
+                      (temp_converge_distance / (double) total_distance) *
+                      100, temp_converge_distance);
           pos = pos->next;
           count++;
         }
@@ -3209,18 +3423,27 @@ select_peer (const GNUNET_HashCode * target,
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
         {
-          if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+          if (GNUNET_YES ==
+              GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
             {
               count++;
               pos = pos->next;
-              continue; /* Ignore bloomfiltered peers */
+              continue;         /* Ignore bloomfiltered peers */
             }
-          temp_converge_distance = converge_distance(target, pos, hops);
-          if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
+          temp_converge_distance = converge_distance (target, pos, hops);
+          if (stats_total_distance + temp_converge_distance > stats_total_distance) /* Handle largest case and overflow */
             stats_total_distance += temp_converge_distance;
           else
-            break; /* overflow case */
-          GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey),  (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance);
+            break;              /* overflow case */
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n",
+                      GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
+                                                        target),
+                      GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
+                                                        &my_identity.
+                                                        hashPubKey),
+                      (temp_converge_distance / (double) total_distance) *
+                      100, temp_converge_distance);
           pos = pos->next;
           count++;
         }
@@ -3229,14 +3452,19 @@ select_peer (const GNUNET_HashCode * target,
 #endif
 
   /* Now actually choose a peer */
-  selected = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, total_distance);
+  selected =
+    GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, total_distance);
 
   /* Go over closest sorted peers. */
   for (i = 0; i < offset; i++)
     {
-      if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey))
-        break; /* Ignore bloomfiltered peers */
-      temp_converge_distance = converge_distance(target, sorted_closest[i], hops);
+      if (GNUNET_YES ==
+          GNUNET_CONTAINER_bloomfilter_test (bloom,
+                                             &sorted_closest[i]->id.
+                                             hashPubKey))
+        break;                  /* Ignore bloomfiltered peers */
+      temp_converge_distance =
+        converge_distance (target, sorted_closest[i], hops);
       if (temp_converge_distance >= selected)
         return sorted_closest[i];
       else
@@ -3250,13 +3478,14 @@ select_peer (const GNUNET_HashCode * target,
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
         {
-          if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+          if (GNUNET_YES ==
+              GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
             {
               count++;
               pos = pos->next;
-              continue; /* Ignore bloomfiltered peers */
+              continue;         /* Ignore bloomfiltered peers */
             }
-          temp_converge_distance = converge_distance(target, pos, hops);
+          temp_converge_distance = converge_distance (target, pos, hops);
           if (temp_converge_distance >= selected)
             return pos;
           else
@@ -3273,13 +3502,14 @@ select_peer (const GNUNET_HashCode * target,
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
         {
-          if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+          if (GNUNET_YES ==
+              GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
             {
               count++;
               pos = pos->next;
-              continue; /* Ignore bloomfiltered peers */
+              continue;         /* Ignore bloomfiltered peers */
             }
-          temp_converge_distance = converge_distance(target, pos, hops);
+          temp_converge_distance = converge_distance (target, pos, hops);
           if (temp_converge_distance >= selected)
             return pos;
           else
@@ -3289,7 +3519,7 @@ select_peer (const GNUNET_HashCode * target,
         }
     }
 
-  increment_stats("# failed to select peer");
+  increment_stats ("# failed to select peer");
   return NULL;
 }
 
@@ -3307,47 +3537,22 @@ remove_recent (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct RecentRequest *req = cls;
   static GNUNET_HashCode hash;
 
-  GNUNET_assert(req != NULL);
-  hash_from_uid(req->uid, &hash);
-  GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(recent.hashmap, &hash, req));
-  GNUNET_CONTAINER_heap_remove_node(recent.minHeap, req->heap_node);
-  GNUNET_CONTAINER_bloomfilter_free(req->bloom);
-  GNUNET_free(req);
+  GNUNET_assert (req != NULL);
+  hash_from_uid (req->uid, &hash);
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multihashmap_remove (recent.hashmap, &hash,
+                                                       req));
+  GNUNET_CONTAINER_heap_remove_node (recent.minHeap, req->heap_node);
+  GNUNET_CONTAINER_bloomfilter_free (req->bloom);
+  GNUNET_free (req);
 
   /*
-  if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) && (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)) && (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap)))
-  {
-    GNUNET_CONTAINER_multihashmap_destroy(recent.hashmap);
-    GNUNET_CONTAINER_heap_destroy(recent.minHeap);
-  }
-  */
-}
-
-
-/**
- * Task used to remove forwarding entries, either
- * after timeout, when full, or on shutdown.
- *
- * @param cls the entry to remove
- * @param tc context, reason, etc.
- */
-static void
-remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct DHTRouteSource *source_info = cls;
-  struct DHTQueryRecord *record;
-  source_info = GNUNET_CONTAINER_heap_remove_node(forward_list.minHeap, source_info->hnode);
-  record = source_info->record;
-  GNUNET_CONTAINER_DLL_remove(record->head, record->tail, source_info);
-
-  if (record->head == NULL) /* No more entries in DLL */
-    {
-      GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(forward_list.hashmap, &record->key, record));
-      GNUNET_free(record);
-    }
-  if (source_info->find_peers_responded != NULL)
-    GNUNET_CONTAINER_bloomfilter_free(source_info->find_peers_responded);
-  GNUNET_free(source_info);
+     if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) && (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)) && (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap)))
+     {
+     GNUNET_CONTAINER_multihashmap_destroy(recent.hashmap);
+     GNUNET_CONTAINER_heap_destroy(recent.minHeap);
+     }
+   */
 }
 
 /**
@@ -3359,7 +3564,8 @@ remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *
  * @return GNUNET_YES if this response was cached, GNUNET_NO if not
  */
-static int cache_response(struct DHT_MessageContext *msg_ctx)
+static int
+cache_response (struct DHT_MessageContext *msg_ctx)
 {
   struct DHTQueryRecord *record;
   struct DHTRouteSource *source_info;
@@ -3367,65 +3573,93 @@ static int cache_response(struct DHT_MessageContext *msg_ctx)
   struct GNUNET_TIME_Absolute now;
   unsigned int current_size;
 
-  current_size = GNUNET_CONTAINER_multihashmap_size(forward_list.hashmap);
+  current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
+
+#if DELETE_WHEN_FULL
   while (current_size >= MAX_OUTSTANDING_FORWARDS)
     {
-      source_info = GNUNET_CONTAINER_heap_remove_root(forward_list.minHeap);
-      GNUNET_assert(source_info != NULL);
+      source_info = GNUNET_CONTAINER_heap_remove_root (forward_list.minHeap);
+      GNUNET_assert (source_info != NULL);
       record = source_info->record;
-      GNUNET_CONTAINER_DLL_remove(record->head, record->tail, source_info);
+      GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
       if (record->head == NULL) /* No more entries in DLL */
         {
-          GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(forward_list.hashmap, &record->key, record));
-          GNUNET_free(record);
+          GNUNET_assert (GNUNET_YES ==
+                         GNUNET_CONTAINER_multihashmap_remove
+                         (forward_list.hashmap, &record->key, record));
+          GNUNET_free (record);
         }
       if (source_info->delete_task != GNUNET_SCHEDULER_NO_TASK)
-        GNUNET_SCHEDULER_cancel(source_info->delete_task);
+       {
+         GNUNET_SCHEDULER_cancel (source_info->delete_task);
+         source_info->delete_task = GNUNET_SCHEDULER_NO_TASK;
+       }
       if (source_info->find_peers_responded != NULL)
-        GNUNET_CONTAINER_bloomfilter_free(source_info->find_peers_responded);
-      GNUNET_free(source_info);
-      current_size = GNUNET_CONTAINER_multihashmap_size(forward_list.hashmap);
+        GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
+      GNUNET_free (source_info);
+      current_size =
+        GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
     }
-  now = GNUNET_TIME_absolute_get();
-  record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, &msg_ctx->key);
-  if (record != NULL) /* Already know this request! */
+#endif
+  /** Non-local request and have too many outstanding forwards, discard! */
+  if ((current_size >= MAX_OUTSTANDING_FORWARDS) && (msg_ctx->client == NULL))
+    return GNUNET_NO;
+
+  now = GNUNET_TIME_absolute_get ();
+  record =
+    GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
+  if (record != NULL)           /* Already know this request! */
     {
       pos = record->head;
       while (pos != NULL)
         {
-          if (0 == memcmp(msg_ctx->peer, &pos->source, sizeof(struct GNUNET_PeerIdentity)))
-            break; /* Already have this peer in reply list! */
+          if (0 ==
+              memcmp (msg_ctx->peer, &pos->source,
+                      sizeof (struct GNUNET_PeerIdentity)))
+            break;              /* Already have this peer in reply list! */
           pos = pos->next;
         }
-      if ((pos != NULL) && (pos->client == msg_ctx->client)) /* Seen this already */
+      if ((pos != NULL) && (pos->client == msg_ctx->client))    /* Seen this already */
         {
-          GNUNET_CONTAINER_heap_update_cost(forward_list.minHeap, pos->hnode, now.abs_value);
+          GNUNET_CONTAINER_heap_update_cost (forward_list.minHeap, pos->hnode,
+                                             now.abs_value);
           return GNUNET_NO;
         }
     }
   else
     {
-      record = GNUNET_malloc(sizeof (struct DHTQueryRecord));
-      GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(forward_list.hashmap, &msg_ctx->key, record, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-      memcpy(&record->key, &msg_ctx->key, sizeof(GNUNET_HashCode));
+      record = GNUNET_malloc (sizeof (struct DHTQueryRecord));
+      GNUNET_assert (GNUNET_OK ==
+                     GNUNET_CONTAINER_multihashmap_put (forward_list.hashmap,
+                                                        &msg_ctx->key, record,
+                                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+      memcpy (&record->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
     }
 
-  source_info = GNUNET_malloc(sizeof(struct DHTRouteSource));
+  source_info = GNUNET_malloc (sizeof (struct DHTRouteSource));
   source_info->record = record;
-  source_info->delete_task = GNUNET_SCHEDULER_add_delayed(DHT_FORWARD_TIMEOUT, &remove_forward_entry, source_info);
-  source_info->find_peers_responded = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
-  memcpy(&source_info->source, msg_ctx->peer, sizeof(struct GNUNET_PeerIdentity));
-  GNUNET_CONTAINER_DLL_insert_after(record->head, record->tail, record->tail, source_info);
-  if (msg_ctx->client != NULL) /* For local request, set timeout so high it effectively never gets pushed out */
+  source_info->delete_task =
+    GNUNET_SCHEDULER_add_delayed (DHT_FORWARD_TIMEOUT, &remove_forward_entry,
+                                  source_info);
+  source_info->find_peers_responded =
+    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+  memcpy (&source_info->source, msg_ctx->peer,
+          sizeof (struct GNUNET_PeerIdentity));
+  GNUNET_CONTAINER_DLL_insert_after (record->head, record->tail, record->tail,
+                                     source_info);
+  if (msg_ctx->client != NULL)  /* For local request, set timeout so high it effectively never gets pushed out */
     {
       source_info->client = msg_ctx->client;
-      now = GNUNET_TIME_absolute_get_forever();
+      now = GNUNET_TIME_absolute_get_forever ();
     }
-  source_info->hnode = GNUNET_CONTAINER_heap_insert(forward_list.minHeap, source_info, now.abs_value);
+  source_info->hnode =
+    GNUNET_CONTAINER_heap_insert (forward_list.minHeap, source_info,
+                                  now.abs_value);
 #if DEBUG_DHT > 1
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "`%s:%s': Created new forward source info for %s uid %llu\n", my_short_id,
-                  "DHT", GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "`%s:%s': Created new forward source info for %s uid %llu\n",
+              my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
+              msg_ctx->unique_id);
 #endif
   return GNUNET_YES;
 }
@@ -3436,17 +3670,18 @@ static int cache_response(struct DHT_MessageContext *msg_ctx)
  * peers.
  *
  * @param msg the message to be routed
- * @param message_context the context containing all pertinent information about the message
+ * @param msg_ctx the context containing all pertinent information about the message
  */
 static void
-route_message(const struct GNUNET_MessageHeader *msg,
-              struct DHT_MessageContext *message_context)
+route_message (const struct GNUNET_MessageHeader *msg,
+               struct DHT_MessageContext *msg_ctx)
 {
   int i;
   struct PeerInfo *selected;
 #if DEBUG_DHT_ROUTING > 1
   struct PeerInfo *nearest;
 #endif
+  unsigned int target_forward_count;
   unsigned int forward_count;
   struct RecentRequest *recent_req;
   GNUNET_HashCode unique_hash;
@@ -3461,30 +3696,34 @@ route_message(const struct GNUNET_MessageHeader *msg,
 #if DEBUG_DHT_ROUTING
       if ((debug_routes_extended) && (dhtlog_handle != NULL))
         {
-          dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
-                                       message_context->hop_count, GNUNET_SYSERR,
-                                       &my_identity, &message_context->key, message_context->peer,
-                                       NULL);
+          dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
+                                       msg_ctx->hop_count, GNUNET_SYSERR,
+                                       &my_identity, &msg_ctx->key,
+                                       msg_ctx->peer, NULL);
         }
 #endif
-      if (message_context->bloom != NULL)
-        GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
+      if (msg_ctx->bloom != NULL)
+        {
+          GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
+          msg_ctx->bloom = NULL;
+        }
       return;
     }
 
-  increment_stats(STAT_ROUTES);
-  /* Semantics of this call means we find whether we are the closest peer out of those already
-   * routed to on this messages path.
-   */
-  forward_count = get_forward_count(message_context->hop_count, message_context->replication);
-  GNUNET_asprintf(&stat_forward_count, "# forward counts of %d", forward_count);
-  increment_stats(stat_forward_count);
-  GNUNET_free(stat_forward_count);
-  if (message_context->bloom == NULL)
-    message_context->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
-
-  if ((stop_on_closest == GNUNET_YES) && (message_context->closest == GNUNET_YES) && (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
-    forward_count = 0;
+  increment_stats (STAT_ROUTES);
+  target_forward_count =
+    get_forward_count (msg_ctx->hop_count, msg_ctx->replication);
+  GNUNET_asprintf (&stat_forward_count, "# forward counts of %d",
+                   target_forward_count);
+  increment_stats (stat_forward_count);
+  GNUNET_free (stat_forward_count);
+  if (msg_ctx->bloom == NULL)
+    msg_ctx->bloom =
+      GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+
+  if ((stop_on_closest == GNUNET_YES) && (msg_ctx->closest == GNUNET_YES)
+      && (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
+    target_forward_count = 0;
 
   /**
    * NOTICE:  In Kademlia, a find peer request goes no further if the peer doesn't return
@@ -3501,100 +3740,134 @@ route_message(const struct GNUNET_MessageHeader *msg,
    * testing driver) we will ignore this restriction for FIND_PEER messages so that
    * routing tables still get constructed.
    */
-  if ((GNUNET_YES == strict_kademlia) && (message_context->closest == GNUNET_YES) && (message_context->hop_count > 0) && (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER))
-    forward_count = 0;
+  if ((GNUNET_YES == strict_kademlia) && (msg_ctx->closest == GNUNET_YES)
+      && (msg_ctx->hop_count > 0)
+      && (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER))
+    target_forward_count = 0;
 
-#if DEBUG_DHT_ROUTING
-  if (forward_count == 0)
-    ret = GNUNET_SYSERR;
-  else
-    ret = GNUNET_NO;
 
-  if ((debug_routes_extended) && (dhtlog_handle != NULL))
+  GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom, &my_identity.hashPubKey);
+  hash_from_uid (msg_ctx->unique_id, &unique_hash);
+  if (GNUNET_YES ==
+      GNUNET_CONTAINER_multihashmap_contains (recent.hashmap, &unique_hash))
     {
-      dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
-                                   message_context->hop_count, ret,
-                                   &my_identity, &message_context->key, message_context->peer,
-                                   NULL);
-    }
-#endif
-
-
-  GNUNET_CONTAINER_bloomfilter_add (message_context->bloom, &my_identity.hashPubKey);
-  hash_from_uid (message_context->unique_id, &unique_hash);
-  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (recent.hashmap, &unique_hash))
-  {
-    recent_req = GNUNET_CONTAINER_multihashmap_get(recent.hashmap, &unique_hash);
-    GNUNET_assert(recent_req != NULL);
-    if (0 != memcmp(&recent_req->key, &message_context->key, sizeof(GNUNET_HashCode)))
-      increment_stats(STAT_DUPLICATE_UID);
-    else
-      {
-        increment_stats(STAT_RECENT_SEEN);
-        GNUNET_CONTAINER_bloomfilter_or2(message_context->bloom, recent_req->bloom, DHT_BLOOM_SIZE);
-      }
+      recent_req =
+        GNUNET_CONTAINER_multihashmap_get (recent.hashmap, &unique_hash);
+      GNUNET_assert (recent_req != NULL);
+      if (0 !=
+          memcmp (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode)))
+        increment_stats (STAT_DUPLICATE_UID);
+      else
+        {
+          increment_stats (STAT_RECENT_SEEN);
+          GNUNET_CONTAINER_bloomfilter_or2 (msg_ctx->bloom, recent_req->bloom,
+                                            DHT_BLOOM_SIZE);
+        }
     }
   else
     {
-      recent_req = GNUNET_malloc(sizeof(struct RecentRequest));
-      recent_req->uid = message_context->unique_id;
-      memcpy(&recent_req->key, &message_context->key, sizeof(GNUNET_HashCode));
-      recent_req->remove_task = GNUNET_SCHEDULER_add_delayed(DEFAULT_RECENT_REMOVAL, &remove_recent, recent_req);
-      recent_req->heap_node = GNUNET_CONTAINER_heap_insert(recent.minHeap, recent_req, GNUNET_TIME_absolute_get().abs_value);
-      recent_req->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
-      GNUNET_CONTAINER_multihashmap_put(recent.hashmap, &unique_hash, recent_req, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+      recent_req = GNUNET_malloc (sizeof (struct RecentRequest));
+      recent_req->uid = msg_ctx->unique_id;
+      memcpy (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
+      recent_req->remove_task =
+        GNUNET_SCHEDULER_add_delayed (DEFAULT_RECENT_REMOVAL, &remove_recent,
+                                      recent_req);
+      recent_req->heap_node =
+        GNUNET_CONTAINER_heap_insert (recent.minHeap, recent_req,
+                                      GNUNET_TIME_absolute_get ().abs_value);
+      recent_req->bloom =
+        GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+      GNUNET_CONTAINER_multihashmap_put (recent.hashmap, &unique_hash,
+                                         recent_req,
+                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
     }
 
-  if (GNUNET_CONTAINER_multihashmap_size(recent.hashmap) > DHT_MAX_RECENT)
+  if (GNUNET_CONTAINER_multihashmap_size (recent.hashmap) > DHT_MAX_RECENT)
     {
-      recent_req = GNUNET_CONTAINER_heap_peek(recent.minHeap);
-      GNUNET_assert(recent_req != NULL);
-      GNUNET_SCHEDULER_cancel(recent_req->remove_task);
-      GNUNET_SCHEDULER_add_now(&remove_recent, recent_req);
+      recent_req = GNUNET_CONTAINER_heap_peek (recent.minHeap);
+      GNUNET_assert (recent_req != NULL);
+      GNUNET_SCHEDULER_cancel (recent_req->remove_task);
+      GNUNET_SCHEDULER_add_now (&remove_recent, recent_req);
     }
 
-  for (i = 0; i < forward_count; i++)
+  forward_count = 0;
+  for (i = 0; i < target_forward_count; i++)
     {
-      selected = select_peer(&message_context->key, message_context->bloom, message_context->hop_count);
+      selected =
+        select_peer (&msg_ctx->key, msg_ctx->bloom, msg_ctx->hop_count);
 
       if (selected != NULL)
         {
-          if (GNUNET_CRYPTO_hash_matching_bits(&selected->id.hashPubKey, &message_context->key) >= GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &message_context->key))
-            GNUNET_asprintf(&temp_stat_str, "# requests routed to close(r) peer hop %u", message_context->hop_count);
+          forward_count++;
+          if (GNUNET_CRYPTO_hash_matching_bits
+              (&selected->id.hashPubKey,
+               &msg_ctx->key) >=
+              GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
+                                                &msg_ctx->key))
+            GNUNET_asprintf (&temp_stat_str,
+                             "# requests routed to close(r) peer hop %u",
+                             msg_ctx->hop_count);
           else
-            GNUNET_asprintf(&temp_stat_str, "# requests routed to less close peer hop %u", message_context->hop_count);
+            GNUNET_asprintf (&temp_stat_str,
+                             "# requests routed to less close peer hop %u",
+                             msg_ctx->hop_count);
           if (temp_stat_str != NULL)
             {
-              increment_stats(temp_stat_str);
-              GNUNET_free(temp_stat_str);
+              increment_stats (temp_stat_str);
+              GNUNET_free (temp_stat_str);
             }
-          GNUNET_CONTAINER_bloomfilter_add(message_context->bloom, &selected->id.hashPubKey);
+          GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
+                                            &selected->id.hashPubKey);
 #if DEBUG_DHT_ROUTING > 1
-          nearest = find_closest_peer(&message_context->key);
-          nearest_buf = GNUNET_strdup(GNUNET_i2s(&nearest->id));
+          nearest = find_closest_peer (&msg_ctx->key);
+          nearest_buf = GNUNET_strdup (GNUNET_i2s (&nearest->id));
           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "`%s:%s': Forwarding request key %s uid %llu to peer %s (closest %s, bits %d, distance %u)\n", my_short_id,
-                      "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id, GNUNET_i2s(&selected->id), nearest_buf, GNUNET_CRYPTO_hash_matching_bits(&nearest->id.hashPubKey, message_context->key), distance(&nearest->id.hashPubKey, message_context->key));
-          GNUNET_free(nearest_buf);
+                      "`%s:%s': Forwarding request key %s uid %llu to peer %s (closest %s, bits %d, distance %u)\n",
+                      my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
+                      msg_ctx->unique_id, GNUNET_i2s (&selected->id),
+                      nearest_buf,
+                      GNUNET_CRYPTO_hash_matching_bits (&nearest->id.
+                                                        hashPubKey,
+                                                        msg_ctx->key),
+                      distance (&nearest->id.hashPubKey, msg_ctx->key));
+          GNUNET_free (nearest_buf);
 #endif
 #if DEBUG_DHT_ROUTING
           if ((debug_routes_extended) && (dhtlog_handle != NULL))
             {
-              dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
-                                           message_context->hop_count, GNUNET_NO,
-                                           &my_identity, &message_context->key, message_context->peer,
+              dhtlog_handle->insert_route (NULL, msg_ctx->unique_id,
+                                           DHTLOG_ROUTE, msg_ctx->hop_count,
+                                           GNUNET_NO, &my_identity,
+                                           &msg_ctx->key, msg_ctx->peer,
                                            &selected->id);
             }
 #endif
-          forward_message(msg, selected, message_context);
+          forward_message (msg, selected, msg_ctx);
         }
     }
 
-  if (message_context->bloom != NULL)
+  if (msg_ctx->bloom != NULL)
     {
-      GNUNET_CONTAINER_bloomfilter_or2(recent_req->bloom, message_context->bloom, DHT_BLOOM_SIZE);
-      GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
+      GNUNET_CONTAINER_bloomfilter_or2 (recent_req->bloom, msg_ctx->bloom,
+                                        DHT_BLOOM_SIZE);
+      GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
+      msg_ctx->bloom = NULL;
     }
+
+#if DEBUG_DHT_ROUTING
+  if (forward_count == 0)
+    ret = GNUNET_SYSERR;
+  else
+    ret = GNUNET_NO;
+
+  if ((debug_routes_extended) && (dhtlog_handle != NULL))
+    {
+      dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
+                                   msg_ctx->hop_count, ret,
+                                   &my_identity, &msg_ctx->key, msg_ctx->peer,
+                                   NULL);
+    }
+#endif
 }
 
 
@@ -3604,50 +3877,59 @@ route_message(const struct GNUNET_MessageHeader *msg,
  * peers.
  *
  * @param msg the message to be routed
- * @param message_context the context containing all pertinent information about the message
+ * @param msg_ctx the context containing all pertinent information about the message
  */
 static void
-demultiplex_message(const struct GNUNET_MessageHeader *msg,
-                    struct DHT_MessageContext *message_context)
+demultiplex_message (const struct GNUNET_MessageHeader *msg,
+                     struct DHT_MessageContext *msg_ctx)
 {
-  message_context->closest = am_closest_peer(&message_context->key, NULL); 
-  switch (ntohs(msg->type))
+  /* FIXME: Should we use closest excluding those we won't route to (the bloomfilter problem)? */
+  msg_ctx->closest = am_closest_peer (&msg_ctx->key, msg_ctx->bloom);
+  switch (ntohs (msg->type))
     {
-    case GNUNET_MESSAGE_TYPE_DHT_GET: /* Add to hashmap of requests seen, search for data (always) */
-      cache_response (message_context);
-      handle_dht_get (msg, message_context);
+    case GNUNET_MESSAGE_TYPE_DHT_GET:  /* Add to hashmap of requests seen, search for data (always) */
+      cache_response (msg_ctx);
+      handle_dht_get (msg, msg_ctx);
       break;
-    case GNUNET_MESSAGE_TYPE_DHT_PUT: /* Check if closest, if so insert data. */
-      increment_stats(STAT_PUTS);
-      handle_dht_put (msg, message_context);
+    case GNUNET_MESSAGE_TYPE_DHT_PUT:  /* Check if closest, if so insert data. */
+      increment_stats (STAT_PUTS);
+      handle_dht_put (msg, msg_ctx);
       break;
-    case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: /* Check if closest and not started by us, check options, add to requests seen */
-      increment_stats(STAT_FIND_PEER);
-      if (((message_context->hop_count > 0) && (0 != memcmp(message_context->peer, &my_identity, sizeof(struct GNUNET_PeerIdentity)))) || (message_context->client != NULL))
-      {
-        cache_response (message_context);
-        if ((message_context->closest == GNUNET_YES) || (message_context->msg_options == GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE))
-          handle_dht_find_peer (msg, message_context);
-      }
+    case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:    /* Check if closest and not started by us, check options, add to requests seen */
+      increment_stats (STAT_FIND_PEER);
+      if (((msg_ctx->hop_count > 0)
+           && (0 !=
+               memcmp (msg_ctx->peer, &my_identity,
+                       sizeof (struct GNUNET_PeerIdentity))))
+          || (msg_ctx->client != NULL))
+        {
+          cache_response (msg_ctx);
+          if ((msg_ctx->closest == GNUNET_YES)
+              || (msg_ctx->msg_options ==
+                  GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE))
+            handle_dht_find_peer (msg, msg_ctx);
+        }
       else
-        route_message (msg, message_context);
+        route_message (msg, msg_ctx);
 #if DEBUG_DHT_ROUTING
-      if (message_context->hop_count == 0) /* Locally initiated request */
+      if (msg_ctx->hop_count == 0)      /* Locally initiated request */
         {
           if ((debug_routes) && (dhtlog_handle != NULL))
             {
-              dhtlog_handle->insert_dhtkey(NULL, &message_context->key);
-              dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_FIND_PEER,
-                                           message_context->hop_count, GNUNET_NO, &my_identity,
-                                           &message_context->key);
+              dhtlog_handle->insert_dhtkey (NULL, &msg_ctx->key);
+              dhtlog_handle->insert_query (NULL, msg_ctx->unique_id,
+                                           DHTLOG_FIND_PEER,
+                                           msg_ctx->hop_count, GNUNET_NO,
+                                           &my_identity, &msg_ctx->key);
             }
         }
 #endif
       break;
     default:
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  "`%s': Message type (%d) not handled, forwarding anyway!\n", "DHT", ntohs(msg->type));
-      route_message (msg, message_context);
+                  "`%s': Message type (%d) not handled, forwarding anyway!\n",
+                  "DHT", ntohs (msg->type));
+      route_message (msg, msg_ctx);
     }
 }
 
@@ -3678,30 +3960,33 @@ republish_content_iterator (void *cls,
   struct GNUNET_DHT_PutMessage *put_msg;
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "`%s:%s': Received `%s' response from datacache\n", my_short_id, "DHT", "GET");
+              "`%s:%s': Received `%s' response from datacache\n", my_short_id,
+              "DHT", "GET");
 #endif
-  new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext));
+  new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
 
-  put_msg =
-    GNUNET_malloc (sizeof (struct GNUNET_DHT_PutMessage) + size);
+  put_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_PutMessage) + size);
   put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
   put_msg->header.size = htons (sizeof (struct GNUNET_DHT_PutMessage) + size);
-  put_msg->expiration = GNUNET_TIME_absolute_hton(exp);
+  put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
   put_msg->type = htons (type);
   memcpy (&put_msg[1], data, size);
-  new_msg_ctx->unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1));
-  new_msg_ctx->replication = ntohl (DHT_DEFAULT_PUT_REPLICATION);
+  new_msg_ctx->unique_id =
+    GNUNET_ntohll (GNUNET_CRYPTO_random_u64
+                   (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
+  new_msg_ctx->replication = ntohl (DEFAULT_PUT_REPLICATION);
   new_msg_ctx->msg_options = ntohl (0);
-  new_msg_ctx->network_size = estimate_diameter();
+  new_msg_ctx->network_size = estimate_diameter ();
   new_msg_ctx->peer = &my_identity;
-  new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+  new_msg_ctx->bloom =
+    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
   new_msg_ctx->hop_count = 0;
   new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE;
   new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
-  increment_stats(STAT_PUT_START);
-  demultiplex_message(&put_msg->header, new_msg_ctx);
+  increment_stats (STAT_PUT_START);
+  demultiplex_message (&put_msg->header, new_msg_ctx);
 
-  GNUNET_free(new_msg_ctx);
+  GNUNET_free (new_msg_ctx);
   GNUNET_free (put_msg);
   return GNUNET_OK;
 }
@@ -3713,7 +3998,7 @@ republish_content_iterator (void *cls,
  * @param tc runtime context for this task
  */
 static void
-republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+republish_content (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct RepublishContext *put_context = cls;
 
@@ -3721,16 +4006,19 @@ republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     {
-      GNUNET_free(put_context);
+      GNUNET_free (put_context);
       return;
     }
 
-  GNUNET_assert (datacache != NULL); /* If we have no datacache we never should have scheduled this! */
-  results = GNUNET_DATACACHE_get(datacache, &put_context->key, put_context->type, &republish_content_iterator, NULL);
-  if (results == 0) /* Data must have expired */
-    GNUNET_free(put_context);
-  else /* Reschedule task for next time period */
-    GNUNET_SCHEDULER_add_delayed(dht_republish_frequency, &republish_content, put_context);
+  GNUNET_assert (datacache != NULL);    /* If we have no datacache we never should have scheduled this! */
+  results =
+    GNUNET_DATACACHE_get (datacache, &put_context->key, put_context->type,
+                          &republish_content_iterator, NULL);
+  if (results == 0)             /* Data must have expired */
+    GNUNET_free (put_context);
+  else                          /* Reschedule task for next time period */
+    GNUNET_SCHEDULER_add_delayed (dht_republish_frequency, &republish_content,
+                                  put_context);
 
 }
 
@@ -3745,8 +4033,8 @@ republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *         iterate,
  *         GNUNET_NO if not.
  */
-static int find_client_records (void *cls,
-                                const GNUNET_HashCode * key, void *value)
+static int
+find_client_records (void *cls, const GNUNET_HashCode * key, void *value)
 {
   struct ClientList *client = cls;
   struct DHTQueryRecord *record = value;
@@ -3760,19 +4048,23 @@ static int find_client_records (void *cls,
     }
   if (pos != NULL)
     {
-      GNUNET_CONTAINER_DLL_remove(record->head, record->tail, pos);
-      GNUNET_CONTAINER_heap_remove_node(forward_list.minHeap, pos->hnode);
+      GNUNET_CONTAINER_DLL_remove (record->head, record->tail, pos);
+      GNUNET_CONTAINER_heap_remove_node (forward_list.minHeap, pos->hnode);
       if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
-        GNUNET_SCHEDULER_cancel(pos->delete_task);
-
+       {
+         GNUNET_SCHEDULER_cancel (pos->delete_task);
+         pos->delete_task = GNUNET_SCHEDULER_NO_TASK;
+       }
       if (pos->find_peers_responded != NULL)
-        GNUNET_CONTAINER_bloomfilter_free(pos->find_peers_responded);
-      GNUNET_free(pos);
+        GNUNET_CONTAINER_bloomfilter_free (pos->find_peers_responded);
+      GNUNET_free (pos);
     }
-  if (record->head == NULL) /* No more entries in DLL */
+  if (record->head == NULL)     /* No more entries in DLL */
     {
-      GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(forward_list.hashmap, &record->key, record));
-      GNUNET_free(record);
+      GNUNET_assert (GNUNET_YES ==
+                     GNUNET_CONTAINER_multihashmap_remove
+                     (forward_list.hashmap, &record->key, record));
+      GNUNET_free (record);
     }
   return GNUNET_YES;
 }
@@ -3785,8 +4077,8 @@ static int find_client_records (void *cls,
  * @param client identification of the client; NULL
  *        for the last call when the server is destroyed
  */
-static void handle_client_disconnect (void *cls,
-                                      struct GNUNET_SERVER_Client* client)
+static void
+handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
 {
   struct ClientList *pos = client_list;
   struct ClientList *prev;
@@ -3812,13 +4104,19 @@ static void handle_client_disconnect (void *cls,
 
   if (found != NULL)
     {
-      while(NULL != (reply = found->pending_head))
+      if (found->transmit_handle != NULL)
+        GNUNET_CONNECTION_notify_transmit_ready_cancel
+          (found->transmit_handle);
+
+      while (NULL != (reply = found->pending_head))
         {
-          GNUNET_CONTAINER_DLL_remove(found->pending_head, found->pending_tail, reply);
-          GNUNET_free(reply);
+          GNUNET_CONTAINER_DLL_remove (found->pending_head,
+                                       found->pending_tail, reply);
+          GNUNET_free (reply);
         }
-      GNUNET_CONTAINER_multihashmap_iterate(forward_list.hashmap, &find_client_records, found);
-      GNUNET_free(found);
+      GNUNET_CONTAINER_multihashmap_iterate (forward_list.hashmap,
+                                             &find_client_records, found);
+      GNUNET_free (found);
     }
 }
 
@@ -3861,35 +4159,44 @@ static void
 malicious_put_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   static struct GNUNET_DHT_PutMessage put_message;
-  static struct DHT_MessageContext message_context;
+  static struct DHT_MessageContext msg_ctx;
   static GNUNET_HashCode key;
   uint32_t random_key;
 
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
-  put_message.header.size = htons(sizeof(struct GNUNET_DHT_PutMessage));
-  put_message.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_PUT);
-  put_message.type = htonl(GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
-  put_message.expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_forever());
-  memset(&message_context, 0, sizeof(struct DHT_MessageContext));
-  random_key = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t)-1);
-  GNUNET_CRYPTO_hash(&random_key, sizeof(uint32_t), &key);
-  memcpy(&message_context.key, &key, sizeof(GNUNET_HashCode));
-  message_context.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1));
-  message_context.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
-  message_context.msg_options = ntohl (0);
-  message_context.network_size = estimate_diameter();
-  message_context.peer = &my_identity;
-  message_context.importance = DHT_DEFAULT_P2P_IMPORTANCE; /* Make result routing a higher priority */
-  message_context.timeout = DHT_DEFAULT_P2P_TIMEOUT;
+  put_message.header.size = htons (sizeof (struct GNUNET_DHT_PutMessage));
+  put_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
+  put_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
+  put_message.expiration =
+    GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_forever ());
+  memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
+  random_key =
+    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
+  GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
+  memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
+  msg_ctx.unique_id =
+    GNUNET_ntohll (GNUNET_CRYPTO_random_u64
+                   (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
+  msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
+  msg_ctx.msg_options = ntohl (0);
+  msg_ctx.network_size = estimate_diameter ();
+  msg_ctx.peer = &my_identity;
+  msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
+  msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
 #if DEBUG_DHT_ROUTING
   if (dhtlog_handle != NULL)
-    dhtlog_handle->insert_dhtkey(NULL, &key);
+    dhtlog_handle->insert_dhtkey (NULL, &key);
 #endif
-  increment_stats(STAT_PUT_START);
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Sending malicious PUT message with hash %s\n", my_short_id, "DHT", GNUNET_h2s(&key));
-  demultiplex_message(&put_message.header, &message_context);
-  GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, malicious_put_frequency), &malicious_put_task, NULL);
+  increment_stats (STAT_PUT_START);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%s:%s Sending malicious PUT message with hash %s\n",
+              my_short_id, "DHT", GNUNET_h2s (&key));
+  demultiplex_message (&put_message.header, &msg_ctx);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                (GNUNET_TIME_UNIT_MILLISECONDS,
+                                 malicious_put_frequency),
+                                &malicious_put_task, NULL);
 }
 
 
@@ -3903,35 +4210,43 @@ static void
 malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   static struct GNUNET_DHT_GetMessage get_message;
-  struct DHT_MessageContext message_context;
+  struct DHT_MessageContext msg_ctx;
   static GNUNET_HashCode key;
   uint32_t random_key;
 
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
 
-  get_message.header.size = htons(sizeof(struct GNUNET_DHT_GetMessage));
-  get_message.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_GET);
-  get_message.type = htonl(GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
-  memset(&message_context, 0, sizeof(struct DHT_MessageContext));
-  random_key = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t)-1);
-  GNUNET_CRYPTO_hash(&random_key, sizeof(uint32_t), &key);
-  memcpy(&message_context.key, &key, sizeof(GNUNET_HashCode));
-  message_context.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1));
-  message_context.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
-  message_context.msg_options = ntohl (0);
-  message_context.network_size = estimate_diameter();
-  message_context.peer = &my_identity;
-  message_context.importance = DHT_DEFAULT_P2P_IMPORTANCE; /* Make result routing a higher priority */
-  message_context.timeout = DHT_DEFAULT_P2P_TIMEOUT;
+  get_message.header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
+  get_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
+  get_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
+  memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
+  random_key =
+    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
+  GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
+  memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
+  msg_ctx.unique_id =
+    GNUNET_ntohll (GNUNET_CRYPTO_random_u64
+                   (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
+  msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
+  msg_ctx.msg_options = ntohl (0);
+  msg_ctx.network_size = estimate_diameter ();
+  msg_ctx.peer = &my_identity;
+  msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
+  msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
 #if DEBUG_DHT_ROUTING
   if (dhtlog_handle != NULL)
-    dhtlog_handle->insert_dhtkey(NULL, &key);
+    dhtlog_handle->insert_dhtkey (NULL, &key);
 #endif
-  increment_stats(STAT_GET_START);
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Sending malicious GET message with hash %s\n", my_short_id, "DHT", GNUNET_h2s(&key));
-  demultiplex_message (&get_message.header, &message_context);
-  GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, malicious_get_frequency), &malicious_get_task, NULL);
+  increment_stats (STAT_GET_START);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%s:%s Sending malicious GET message with hash %s\n",
+              my_short_id, "DHT", GNUNET_h2s (&key));
+  demultiplex_message (&get_message.header, &msg_ctx);
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                (GNUNET_TIME_UNIT_MILLISECONDS,
+                                 malicious_get_frequency),
+                                &malicious_get_task, NULL);
 }
 #endif
 
@@ -3947,9 +4262,7 @@ malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *         GNUNET_NO if not.
  */
 static int
-add_known_to_bloom (void *cls,
-                    const GNUNET_HashCode * key,
-                    void *value)
+add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
 {
   struct GNUNET_CONTAINER_BloomFilter *bloom = cls;
   GNUNET_CONTAINER_bloomfilter_add (bloom, key);
@@ -3965,10 +4278,11 @@ add_known_to_bloom (void *cls,
  * @param tc the context under which the task is running
  */
 static void
-send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+send_find_peer_message (void *cls,
+                        const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_DHT_FindPeerMessage *find_peer_msg;
-  struct DHT_MessageContext message_context;
+  struct DHT_MessageContext msg_ctx;
   struct GNUNET_TIME_Relative next_send_time;
   struct GNUNET_CONTAINER_BloomFilter *temp_bloom;
 #if COUNT_INTERVAL
@@ -3980,19 +4294,22 @@ send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
 
-  if ((newly_found_peers > bucket_size) && (GNUNET_YES == do_find_peer)) /* If we are finding peers already, no need to send out our request right now! */
+  if ((newly_found_peers > bucket_size) && (GNUNET_YES == do_find_peer))        /* If we are finding peers already, no need to send out our request right now! */
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Have %d newly found peers since last find peer message sent!\n", newly_found_peers);
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "Have %d newly found peers since last find peer message sent!\n",
+                  newly_found_peers);
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
                                     &send_find_peer_message, NULL);
       newly_found_peers = 0;
       return;
     }
-    
-  increment_stats(STAT_FIND_PEER_START);
+
+  increment_stats (STAT_FIND_PEER_START);
 #if COUNT_INTERVAL
-  end = GNUNET_TIME_absolute_get();
-  time_diff = GNUNET_TIME_absolute_get_difference(find_peer_context.start, end);
+  end = GNUNET_TIME_absolute_get ();
+  time_diff =
+    GNUNET_TIME_absolute_get_difference (find_peer_context.start, end);
 
   if (time_diff.abs_value > FIND_PEER_CALC_INTERVAL.abs_value)
     {
@@ -4007,54 +4324,73 @@ send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
 #endif
 
 #if FIND_PEER_WITH_HELLO
-  find_peer_msg = GNUNET_malloc(sizeof(struct GNUNET_DHT_FindPeerMessage) + GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello));
-  find_peer_msg->header.size = htons(sizeof(struct GNUNET_DHT_FindPeerMessage) + GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello));
-  memcpy(&find_peer_msg[1], my_hello, GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello));
+  find_peer_msg =
+    GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage) +
+                   GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *)
+                                      my_hello));
+  find_peer_msg->header.size =
+    htons (sizeof (struct GNUNET_DHT_FindPeerMessage) +
+           GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
+  memcpy (&find_peer_msg[1], my_hello,
+          GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
 #else
-  find_peer_msg = GNUNET_malloc(sizeof(struct GNUNET_DHT_FindPeerMessage));
-  find_peer_msg->header.size = htons(sizeof(struct GNUNET_DHT_FindPeerMessage));
+  find_peer_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage));
+  find_peer_msg->header.size =
+    htons (sizeof (struct GNUNET_DHT_FindPeerMessage));
 #endif
-  find_peer_msg->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
-  temp_bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
-  GNUNET_CONTAINER_multihashmap_iterate(all_known_peers, &add_known_to_bloom, temp_bloom);
-  GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(temp_bloom, find_peer_msg->bloomfilter, DHT_BLOOM_SIZE));
-  memset(&message_context, 0, sizeof(struct DHT_MessageContext));
-  memcpy(&message_context.key, &my_identity.hashPubKey, sizeof(GNUNET_HashCode));
-  message_context.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, (uint64_t)-1));
-  message_context.replication = DHT_DEFAULT_FIND_PEER_REPLICATION;
-  message_context.msg_options = DHT_DEFAULT_FIND_PEER_OPTIONS;
-  message_context.network_size = estimate_diameter();
-  message_context.peer = &my_identity;
-  message_context.importance = DHT_DEFAULT_FIND_PEER_IMPORTANCE;
-  message_context.timeout = DHT_DEFAULT_FIND_PEER_TIMEOUT;
-
-  demultiplex_message(&find_peer_msg->header, &message_context);
-  GNUNET_free(find_peer_msg);
+  find_peer_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
+  temp_bloom =
+    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+  GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, &add_known_to_bloom,
+                                         temp_bloom);
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_CONTAINER_bloomfilter_get_raw_data (temp_bloom,
+                                                            find_peer_msg->
+                                                            bloomfilter,
+                                                            DHT_BLOOM_SIZE));
+  GNUNET_CONTAINER_bloomfilter_free (temp_bloom);
+  memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
+  memcpy (&msg_ctx.key, &my_identity.hashPubKey, sizeof (GNUNET_HashCode));
+  msg_ctx.unique_id =
+    GNUNET_ntohll (GNUNET_CRYPTO_random_u64
+                   (GNUNET_CRYPTO_QUALITY_STRONG, UINT64_MAX));
+  msg_ctx.replication = DHT_DEFAULT_FIND_PEER_REPLICATION;
+  msg_ctx.msg_options = DHT_DEFAULT_FIND_PEER_OPTIONS;
+  msg_ctx.network_size = estimate_diameter ();
+  msg_ctx.peer = &my_identity;
+  msg_ctx.importance = DHT_DEFAULT_FIND_PEER_IMPORTANCE;
+  msg_ctx.timeout = DHT_DEFAULT_FIND_PEER_TIMEOUT;
+
+  demultiplex_message (&find_peer_msg->header, &msg_ctx);
+  GNUNET_free (find_peer_msg);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "`%s:%s': Sent `%s' request to some (?) peers\n", my_short_id, "DHT",
-              "FIND PEER");
+              "`%s:%s': Sent `%s' request to some (?) peers\n", my_short_id,
+              "DHT", "FIND PEER");
   if (newly_found_peers < bucket_size)
     {
-      next_send_time.rel_value = (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
-                              GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
-                                                       DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
+      next_send_time.rel_value =
+        (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
+        GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
+                                  DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
+                                  2);
     }
   else
     {
       next_send_time.rel_value = DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
-                             GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
-                                                      DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value - DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
+        GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
+                                  DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value -
+                                  DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
     }
 
   GNUNET_assert (next_send_time.rel_value != 0);
   find_peer_context.count = 0;
   newly_found_peers = 0;
-  find_peer_context.start = GNUNET_TIME_absolute_get();
+  find_peer_context.start = GNUNET_TIME_absolute_get ();
   if (GNUNET_YES == do_find_peer)
-  {
-    GNUNET_SCHEDULER_add_delayed (next_send_time,
-                                         &send_find_peer_message, NULL);
-  }
+    {
+      GNUNET_SCHEDULER_add_delayed (next_send_time,
+                                    &send_find_peer_message, NULL);
+    }
 }
 
 /**
@@ -4067,47 +4403,79 @@ send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
  * @param message the actual message received
  */
 static void
-handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client,
+handle_dht_local_route_request (void *cls,
+                                struct GNUNET_SERVER_Client *client,
                                 const struct GNUNET_MessageHeader *message)
 {
-  const struct GNUNET_DHT_RouteMessage *dht_msg = (const struct GNUNET_DHT_RouteMessage *) message;
+  const struct GNUNET_DHT_RouteMessage *dht_msg =
+    (const struct GNUNET_DHT_RouteMessage *) message;
   const struct GNUNET_MessageHeader *enc_msg;
-  struct DHT_MessageContext message_context;
+  struct DHT_MessageContext msg_ctx;
 
   enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "`%s:%s': Received `%s' request from client, message type %d, key %s, uid %llu\n",
-              my_short_id, 
-             "DHT",
-             "GENERIC",
-             ntohs (message->type), 
-             GNUNET_h2s (&dht_msg->key),
-              GNUNET_ntohll (dht_msg->unique_id));
+              my_short_id,
+              "DHT",
+              "GENERIC",
+              ntohs (message->type),
+              GNUNET_h2s (&dht_msg->key), GNUNET_ntohll (dht_msg->unique_id));
 #endif
 #if DEBUG_DHT_ROUTING
   if (dhtlog_handle != NULL)
     dhtlog_handle->insert_dhtkey (NULL, &dht_msg->key);
 #endif
-  memset(&message_context, 0, sizeof(struct DHT_MessageContext));
-  message_context.client = find_active_client (client);
-  memcpy(&message_context.key, &dht_msg->key, sizeof(GNUNET_HashCode));
-  message_context.unique_id = GNUNET_ntohll (dht_msg->unique_id);
-  message_context.replication = ntohl (dht_msg->desired_replication_level);
-  message_context.msg_options = ntohl (dht_msg->options);
-  message_context.network_size = estimate_diameter();
-  message_context.peer = &my_identity;
-  message_context.importance = DHT_DEFAULT_P2P_IMPORTANCE * 4; /* Make local routing a higher priority */
-  message_context.timeout = DHT_DEFAULT_P2P_TIMEOUT;
-  if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
-    increment_stats(STAT_GET_START);
-  else if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
-    increment_stats(STAT_PUT_START);
-  else if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER)
-    increment_stats(STAT_FIND_PEER_START);
-
-  demultiplex_message(enc_msg, &message_context);
 
+  memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
+  msg_ctx.client = find_active_client (client);
+  memcpy (&msg_ctx.key, &dht_msg->key, sizeof (GNUNET_HashCode));
+  msg_ctx.unique_id = GNUNET_ntohll (dht_msg->unique_id);
+  msg_ctx.replication = ntohl (dht_msg->desired_replication_level);
+  msg_ctx.msg_options = ntohl (dht_msg->options);
+  msg_ctx.network_size = estimate_diameter ();
+  msg_ctx.peer = &my_identity;
+  msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 4;  /* Make local routing a higher priority */
+  msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
+
+  if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
+    increment_stats (STAT_GET_START);
+  else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
+    increment_stats (STAT_PUT_START);
+  else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER)
+    increment_stats (STAT_FIND_PEER_START);
+
+  if (GNUNET_YES == malicious_dropper)
+    {
+      if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
+        {
+#if DEBUG_DHT_ROUTING
+          if ((debug_routes) && (dhtlog_handle != NULL))
+            {
+              dhtlog_handle->insert_query (NULL, msg_ctx.unique_id,
+                                           DHTLOG_GET, msg_ctx.hop_count,
+                                           GNUNET_NO, &my_identity,
+                                           &msg_ctx.key);
+            }
+#endif
+        }
+      else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
+        {
+#if DEBUG_DHT_ROUTING
+          if ((debug_routes) && (dhtlog_handle != NULL))
+            {
+              dhtlog_handle->insert_query (NULL, msg_ctx.unique_id,
+                                           DHTLOG_PUT, msg_ctx.hop_count,
+                                           GNUNET_NO, &my_identity,
+                                           &msg_ctx.key);
+            }
+#endif
+        }
+      GNUNET_SERVER_receive_done (client, GNUNET_OK);
+      return;
+    }
+
+  demultiplex_message (enc_msg, &msg_ctx);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 
 }
@@ -4126,59 +4494,63 @@ handle_dht_control_message (void *cls, struct GNUNET_SERVER_Client *client,
                             const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_DHT_ControlMessage *dht_control_msg =
-      (const struct GNUNET_DHT_ControlMessage *) message;
+    (const struct GNUNET_DHT_ControlMessage *) message;
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "`%s:%s': Received `%s' request from client, command %d\n", my_short_id, "DHT",
-              "CONTROL", ntohs(dht_control_msg->command));
+              "`%s:%s': Received `%s' request from client, command %d\n",
+              my_short_id, "DHT", "CONTROL",
+              ntohs (dht_control_msg->command));
 #endif
 
-  switch (ntohs(dht_control_msg->command))
-  {
-  case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending self seeking find peer request!\n");
-    GNUNET_SCHEDULER_add_now(&send_find_peer_message, NULL);
-    break;
+  switch (ntohs (dht_control_msg->command))
+    {
+    case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Sending self seeking find peer request!\n");
+      GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
+      break;
 #if HAVE_MALICIOUS
-  case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
-    if (ntohs(dht_control_msg->variable) > 0)
-      malicious_get_frequency = ntohs(dht_control_msg->variable);
-    if (malicious_get_frequency == 0)
-      malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
-    if (malicious_getter != GNUNET_YES)
-      GNUNET_SCHEDULER_add_now(&malicious_get_task, NULL);
-    malicious_getter = GNUNET_YES;
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
-              "%s:%s Initiating malicious GET behavior, frequency %d\n", my_short_id, "DHT", malicious_get_frequency);
-    break;
-  case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
-    if (ntohs(dht_control_msg->variable) > 0)
-      malicious_put_frequency = ntohs(dht_control_msg->variable);
-    if (malicious_put_frequency == 0)
-      malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
-    if (malicious_putter != GNUNET_YES)
-      GNUNET_SCHEDULER_add_now(&malicious_put_task, NULL);
-    malicious_putter = GNUNET_YES;
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-              "%s:%s Initiating malicious PUT behavior, frequency %d\n", my_short_id, "DHT", malicious_put_frequency);
-    break;
-  case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
+    case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
+      if (ntohs (dht_control_msg->variable) > 0)
+        malicious_get_frequency = ntohs (dht_control_msg->variable);
+      if (malicious_get_frequency == 0)
+        malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
+      if (malicious_getter != GNUNET_YES)
+        GNUNET_SCHEDULER_add_now (&malicious_get_task, NULL);
+      malicious_getter = GNUNET_YES;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "%s:%s Initiating malicious GET behavior, frequency %d\n",
+                  my_short_id, "DHT", malicious_get_frequency);
+      break;
+    case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
+      if (ntohs (dht_control_msg->variable) > 0)
+        malicious_put_frequency = ntohs (dht_control_msg->variable);
+      if (malicious_put_frequency == 0)
+        malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
+      if (malicious_putter != GNUNET_YES)
+        GNUNET_SCHEDULER_add_now (&malicious_put_task, NULL);
+      malicious_putter = GNUNET_YES;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "%s:%s Initiating malicious PUT behavior, frequency %d\n",
+                  my_short_id, "DHT", malicious_put_frequency);
+      break;
+    case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
 #if DEBUG_DHT_ROUTING
-    if ((malicious_dropper != GNUNET_YES) && (dhtlog_handle != NULL))
-      dhtlog_handle->set_malicious(&my_identity);
+      if ((malicious_dropper != GNUNET_YES) && (dhtlog_handle != NULL))
+        dhtlog_handle->set_malicious (&my_identity);
 #endif
-    malicious_dropper = GNUNET_YES;
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-              "%s:%s Initiating malicious DROP behavior\n", my_short_id, "DHT");
-    break;
+      malicious_dropper = GNUNET_YES;
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "%s:%s Initiating malicious DROP behavior\n", my_short_id,
+                  "DHT");
+      break;
 #endif
-  default:
-    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
-              "%s:%s Unknown control command type `%d'!\n", 
-              my_short_id, "DHT",
-              ntohs(dht_control_msg->command));
-    break;
-  }
+    default:
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "%s:%s Unknown control command type `%d'!\n",
+                  my_short_id, "DHT", ntohs (dht_control_msg->command));
+      break;
+    }
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -4193,8 +4565,8 @@ handle_dht_control_message (void *cls, struct GNUNET_SERVER_Client *client,
  *
  */
 static void
-handle_dht_local_route_stop(void *cls, struct GNUNET_SERVER_Client *client,
-                            const struct GNUNET_MessageHeader *message)
+handle_dht_local_route_stop (void *cls, struct GNUNET_SERVER_Client *client,
+                             const struct GNUNET_MessageHeader *message)
 {
 
   const struct GNUNET_DHT_StopMessage *dht_stop_msg =
@@ -4203,21 +4575,25 @@ handle_dht_local_route_stop(void *cls, struct GNUNET_SERVER_Client *client,
   struct DHTRouteSource *pos;
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "`%s:%s': Received `%s' request from client, uid %llu\n", my_short_id, "DHT",
-              "GENERIC STOP", GNUNET_ntohll (dht_stop_msg->unique_id));
+              "`%s:%s': Received `%s' request from client, uid %llu\n",
+              my_short_id, "DHT", "GENERIC STOP",
+              GNUNET_ntohll (dht_stop_msg->unique_id));
 #endif
-  record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, &dht_stop_msg->key);
+  record =
+    GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap,
+                                       &dht_stop_msg->key);
   if (record != NULL)
     {
       pos = record->head;
 
       while (pos != NULL)
         {
+          /* If the client is non-null (local request) and the client matches the requesting client, remove the entry. */
           if ((pos->client != NULL) && (pos->client->client_handle == client))
             {
-              GNUNET_SCHEDULER_cancel(pos->delete_task);
-              pos->delete_task = GNUNET_SCHEDULER_NO_TASK;
-              GNUNET_SCHEDULER_add_now(&remove_forward_entry, pos);
+             if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
+               GNUNET_SCHEDULER_cancel (pos->delete_task);
+              pos->delete_task = GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);
             }
           pos = pos->next;
         }
@@ -4229,95 +4605,172 @@ handle_dht_local_route_stop(void *cls, struct GNUNET_SERVER_Client *client,
 
 /**
  * Core handler for p2p route requests.
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ *
  */
 static int
 handle_dht_p2p_route_request (void *cls,
-                             const struct GNUNET_PeerIdentity *peer,
-                             const struct GNUNET_MessageHeader *message,
-                             struct GNUNET_TIME_Relative latency, uint32_t distance)
+                              const struct GNUNET_PeerIdentity *peer,
+                              const struct GNUNET_MessageHeader *message,
+                              const struct GNUNET_TRANSPORT_ATS_Information
+                              *atsi)
 {
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "`%s:%s': Received P2P request from peer %s\n", my_short_id, "DHT", GNUNET_i2s(peer));
+              "`%s:%s': Received P2P request from peer %s\n", my_short_id,
+              "DHT", GNUNET_i2s (peer));
 #endif
-  struct GNUNET_DHT_P2PRouteMessage *incoming = (struct GNUNET_DHT_P2PRouteMessage *)message;
-  struct GNUNET_MessageHeader *enc_msg = (struct GNUNET_MessageHeader *)&incoming[1];
-  struct DHT_MessageContext *message_context;
-
-  if (get_max_send_delay().rel_value > MAX_REQUEST_TIME.rel_value)
-  {
-    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sending of previous replies took too long, backing off!\n");
-    increment_stats("# route requests dropped due to high load");
-    decrease_max_send_delay(get_max_send_delay());
-    return GNUNET_YES;
-  }
+  struct GNUNET_DHT_P2PRouteMessage *incoming =
+    (struct GNUNET_DHT_P2PRouteMessage *) message;
+  struct GNUNET_MessageHeader *enc_msg =
+    (struct GNUNET_MessageHeader *) &incoming[1];
+  struct DHT_MessageContext *msg_ctx;
 
-  if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_P2P_PING) /* Throw these away. FIXME: Don't throw these away? (reply)*/
+  if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_P2P_PING)        /* Throw these away. FIXME: Don't throw these away? (reply) */
     {
 #if DEBUG_PING
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Received P2P Ping message.\n", my_short_id, "DHT");
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "%s:%s Received P2P Ping message.\n", my_short_id, "DHT");
+#endif
+      return GNUNET_YES;
+    }
+
+  if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
+    {
+      GNUNET_break_op (0);
+      return GNUNET_YES;
+    }
+
+  if (malicious_dropper == GNUNET_YES)
+    {
+#if DEBUG_DHT_ROUTING
+      if ((debug_routes_extended) && (dhtlog_handle != NULL))
+        {
+          /** Log routes that die due to high load! */
+          dhtlog_handle->insert_route (NULL,
+                                       GNUNET_ntohll (incoming->unique_id),
+                                       DHTLOG_ROUTE,
+                                       ntohl (incoming->hop_count),
+                                       GNUNET_SYSERR, &my_identity,
+                                       &incoming->key, peer, NULL);
+        }
 #endif
       return GNUNET_YES;
     }
 
-  if (ntohs(enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
+  if (get_max_send_delay ().rel_value > MAX_REQUEST_TIME.rel_value)
     {
-      GNUNET_break_op(0);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Sending of previous replies took too long, backing off!\n");
+      increment_stats ("# route requests dropped due to high load");
+      decrease_max_send_delay (get_max_send_delay ());
+#if DEBUG_DHT_ROUTING
+      if ((debug_routes_extended) && (dhtlog_handle != NULL))
+        {
+        /** Log routes that die due to high load! */
+          dhtlog_handle->insert_route (NULL,
+                                       GNUNET_ntohll (incoming->unique_id),
+                                       DHTLOG_ROUTE,
+                                       ntohl (incoming->hop_count),
+                                       GNUNET_SYSERR, &my_identity,
+                                       &incoming->key, peer, NULL);
+        }
+#endif
       return GNUNET_YES;
     }
-  message_context = GNUNET_malloc(sizeof (struct DHT_MessageContext));
-  message_context->bloom = GNUNET_CONTAINER_bloomfilter_init(incoming->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K);
-  GNUNET_assert(message_context->bloom != NULL);
-  message_context->hop_count = ntohl(incoming->hop_count);
-  memcpy(&message_context->key, &incoming->key, sizeof(GNUNET_HashCode));
-  message_context->replication = ntohl(incoming->desired_replication_level);
-  message_context->unique_id = GNUNET_ntohll(incoming->unique_id);
-  message_context->msg_options = ntohl(incoming->options);
-  message_context->network_size = ntohl(incoming->network_size);
-  message_context->peer = peer;
-  message_context->importance = DHT_DEFAULT_P2P_IMPORTANCE;
-  message_context->timeout = DHT_DEFAULT_P2P_TIMEOUT;
-  demultiplex_message(enc_msg, message_context);
-  GNUNET_free(message_context);
+  msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
+  msg_ctx->bloom =
+    GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
+                                       DHT_BLOOM_K);
+  GNUNET_assert (msg_ctx->bloom != NULL);
+  msg_ctx->hop_count = ntohl (incoming->hop_count);
+  memcpy (&msg_ctx->key, &incoming->key, sizeof (GNUNET_HashCode));
+  msg_ctx->replication = ntohl (incoming->desired_replication_level);
+  msg_ctx->unique_id = GNUNET_ntohll (incoming->unique_id);
+  msg_ctx->msg_options = ntohl (incoming->options);
+  msg_ctx->network_size = ntohl (incoming->network_size);
+  msg_ctx->peer = peer;
+  msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE;
+  msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
+  demultiplex_message (enc_msg, msg_ctx);
+  if (msg_ctx->bloom != NULL)
+    {
+      GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
+      msg_ctx->bloom = NULL;
+    }
+  GNUNET_free (msg_ctx);
   return GNUNET_YES;
 }
 
 
 /**
  * Core handler for p2p route results.
+ *
+ * @param cls closure
+ * @param message message
+ * @param peer peer identity this notification is about
+ * @param atsi performance data
+ *
  */
 static int
 handle_dht_p2p_route_result (void *cls,
-                            const struct GNUNET_PeerIdentity *peer,
-                            const struct GNUNET_MessageHeader *message,
-                            struct GNUNET_TIME_Relative latency, uint32_t distance)
+                             const struct GNUNET_PeerIdentity *peer,
+                             const struct GNUNET_MessageHeader *message,
+                             const struct GNUNET_TRANSPORT_ATS_Information
+                             *atsi)
 {
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "`%s:%s': Received request from peer %s\n", my_short_id, "DHT", GNUNET_i2s(peer));
+              "`%s:%s': Received request from peer %s\n", my_short_id, "DHT",
+              GNUNET_i2s (peer));
 #endif
-  struct GNUNET_DHT_P2PRouteResultMessage *incoming = (struct GNUNET_DHT_P2PRouteResultMessage *)message;
-  struct GNUNET_MessageHeader *enc_msg = (struct GNUNET_MessageHeader *)&incoming[1];
-  struct DHT_MessageContext message_context;
+  struct GNUNET_DHT_P2PRouteResultMessage *incoming =
+    (struct GNUNET_DHT_P2PRouteResultMessage *) message;
+  struct GNUNET_MessageHeader *enc_msg =
+    (struct GNUNET_MessageHeader *) &incoming[1];
+  struct DHT_MessageContext msg_ctx;
+
+  if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
+    {
+      GNUNET_break_op (0);
+      return GNUNET_YES;
+    }
 
-  if (ntohs(enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
+  if (malicious_dropper == GNUNET_YES)
     {
-      GNUNET_break_op(0);
+#if DEBUG_DHT_ROUTING
+      if ((debug_routes_extended) && (dhtlog_handle != NULL))
+        {
+          /** Log routes that die due to high load! */
+          dhtlog_handle->insert_route (NULL,
+                                       GNUNET_ntohll (incoming->unique_id),
+                                       DHTLOG_ROUTE,
+                                       ntohl (incoming->hop_count),
+                                       GNUNET_SYSERR, &my_identity,
+                                       &incoming->key, peer, NULL);
+        }
+#endif
       return GNUNET_YES;
     }
 
-  memset(&message_context, 0, sizeof(struct DHT_MessageContext));
+  memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
   // FIXME: call GNUNET_BLOCK_evaluate (...) -- instead of doing your own bloomfilter!
-  message_context.bloom = GNUNET_CONTAINER_bloomfilter_init(incoming->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K);
-  GNUNET_assert(message_context.bloom != NULL);
-  memcpy(&message_context.key, &incoming->key, sizeof(GNUNET_HashCode));
-  message_context.unique_id = GNUNET_ntohll(incoming->unique_id);
-  message_context.msg_options = ntohl(incoming->options);
-  message_context.hop_count = ntohl(incoming->hop_count);
-  message_context.peer = peer;
-  message_context.importance = DHT_DEFAULT_P2P_IMPORTANCE * 2; /* Make result routing a higher priority */
-  message_context.timeout = DHT_DEFAULT_P2P_TIMEOUT;
-  route_result_message(enc_msg, &message_context);
+  msg_ctx.bloom =
+    GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
+                                       DHT_BLOOM_K);
+  GNUNET_assert (msg_ctx.bloom != NULL);
+  memcpy (&msg_ctx.key, &incoming->key, sizeof (GNUNET_HashCode));
+  msg_ctx.unique_id = GNUNET_ntohll (incoming->unique_id);
+  msg_ctx.msg_options = ntohl (incoming->options);
+  msg_ctx.hop_count = ntohl (incoming->hop_count);
+  msg_ctx.peer = peer;
+  msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;  /* Make result routing a higher priority */
+  msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
+  route_result_message (enc_msg, &msg_ctx);
   return GNUNET_YES;
 }
 
@@ -4334,14 +4787,13 @@ process_hello (void *cls, const struct GNUNET_MessageHeader *message)
 {
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received our `%s' from transport service\n",
-              "HELLO");
+              "Received our `%s' from transport service\n", "HELLO");
 #endif
 
   GNUNET_assert (message != NULL);
-  GNUNET_free_non_null(my_hello);
-  my_hello = GNUNET_malloc(ntohs(message->size));
-  memcpy(my_hello, message, ntohs(message->size));
+  GNUNET_free_non_null (my_hello);
+  my_hello = GNUNET_malloc (ntohs (message->size));
+  memcpy (my_hello, message, ntohs (message->size));
 }
 
 
@@ -4359,20 +4811,23 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   if (transport_handle != NULL)
     {
-      GNUNET_free_non_null(my_hello);
-      GNUNET_TRANSPORT_get_hello_cancel(transport_handle, &process_hello, NULL);
-      GNUNET_TRANSPORT_disconnect(transport_handle);
+      GNUNET_free_non_null (my_hello);
+      GNUNET_TRANSPORT_get_hello_cancel (transport_handle, &process_hello,
+                                         NULL);
+      GNUNET_TRANSPORT_disconnect (transport_handle);
     }
-  for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS; bucket_count++)
+  for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS;
+       bucket_count++)
     {
       while (k_buckets[bucket_count].head != NULL)
         {
           pos = k_buckets[bucket_count].head;
 #if DEBUG_DHT
           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "%s:%s Removing peer %s from bucket %d!\n", my_short_id, "DHT", GNUNET_i2s(&pos->id), bucket_count);
+                      "%s:%s Removing peer %s from bucket %d!\n", my_short_id,
+                      "DHT", GNUNET_i2s (&pos->id), bucket_count);
 #endif
-          delete_peer(pos, bucket_count);
+          delete_peer (pos, bucket_count);
         }
     }
   if (coreAPI != NULL)
@@ -4400,7 +4855,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     }
   if (dhtlog_handle != NULL)
     {
-      GNUNET_DHTLOG_disconnect(dhtlog_handle);
+      GNUNET_DHTLOG_disconnect (dhtlog_handle);
       dhtlog_handle = NULL;
     }
   if (block_context != NULL)
@@ -4408,7 +4863,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_BLOCK_context_destroy (block_context);
       block_context = NULL;
     }
-  GNUNET_free_non_null(my_short_id);
+  GNUNET_free_non_null (my_short_id);
   my_short_id = NULL;
 }
 
@@ -4431,9 +4886,9 @@ core_init (void *cls,
   if (server == NULL)
     {
 #if DEBUG_DHT
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "%s: Connection to core FAILED!\n", "dht",
-              GNUNET_i2s (identity));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "%s: Connection to core FAILED!\n", "dht",
+                  GNUNET_i2s (identity));
 #endif
       GNUNET_SCHEDULER_cancel (cleanup_task);
       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
@@ -4448,8 +4903,10 @@ core_init (void *cls,
   /* Copy our identity so we can use it */
   memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity));
   if (my_short_id != NULL)
-    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s Receive CORE INIT message but have already been initialized! Did CORE fail?\n", "DHT SERVICE");
-  my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "%s Receive CORE INIT message but have already been initialized! Did CORE fail?\n",
+                "DHT SERVICE");
+  my_short_id = GNUNET_strdup (GNUNET_i2s (&my_identity));
   /* Set the server to local variable */
   coreAPI = server;
 
@@ -4459,8 +4916,10 @@ core_init (void *cls,
 
 
 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
-  {&handle_dht_local_route_request, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE, 0},
-  {&handle_dht_local_route_stop, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP, 0},
+  {&handle_dht_local_route_request, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE,
+   0},
+  {&handle_dht_local_route_stop, NULL,
+   GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP, 0},
   {&handle_dht_control_message, NULL, GNUNET_MESSAGE_TYPE_DHT_CONTROL, 0},
   {NULL, NULL, 0, 0}
 };
@@ -4478,41 +4937,52 @@ static struct GNUNET_CORE_MessageHandler core_handlers[] = {
  *
  * @param cls closure
  * @param peer peer identity this notification is about
- * @param latency reported latency of the connection with peer
- * @param distance reported distance (DV) to peer
+ * @param atsi performance data
  */
-void handle_core_connect (void *cls,
-                          const struct GNUNET_PeerIdentity * peer,
-                          struct GNUNET_TIME_Relative latency,
-                          uint32_t distance)
+static void
+handle_core_connect (void *cls,
+                     const struct GNUNET_PeerIdentity *peer,
+                     const struct GNUNET_TRANSPORT_ATS_Information *atsi)
 {
   struct PeerInfo *ret;
 
+  /* Check for connect to self message */
+  if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
+    return;
+
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "%s:%s Receives core connect message for peer %s distance %d!\n", my_short_id, "dht", GNUNET_i2s(peer), distance);
+              "%s:%s Receives core connect message for peer %s distance %d!\n",
+              my_short_id, "dht", GNUNET_i2s (peer), distance);
 #endif
 
-  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey))
+  if (GNUNET_YES ==
+      GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
+                                              &peer->hashPubKey))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Received %s message for peer %s, but already have peer in RT!", my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s(peer));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "%s:%s Received %s message for peer %s, but already have peer in RT!",
+                  my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s (peer));
       return;
     }
 
   if (datacache != NULL)
-    GNUNET_DATACACHE_put(datacache, &peer->hashPubKey, sizeof(struct GNUNET_PeerIdentity), (const char *)peer, GNUNET_BLOCK_TYPE_DHT_HELLO, GNUNET_TIME_absolute_get_forever());
-  ret = try_add_peer(peer,
-                     find_current_bucket(&peer->hashPubKey),
-                     latency,
-                     distance);
+    GNUNET_DATACACHE_put (datacache, &peer->hashPubKey,
+                          sizeof (struct GNUNET_PeerIdentity),
+                          (const char *) peer, GNUNET_BLOCK_TYPE_DHT_HELLO,
+                          GNUNET_TIME_absolute_get_forever ());
+  ret = try_add_peer (peer, find_current_bucket (&peer->hashPubKey), atsi);
   if (ret != NULL)
     {
       newly_found_peers++;
-      GNUNET_CONTAINER_multihashmap_put(all_known_peers, &peer->hashPubKey, ret, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+      GNUNET_CONTAINER_multihashmap_put (all_known_peers, &peer->hashPubKey,
+                                         ret,
+                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
     }
 #if DEBUG_DHT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "%s:%s Adding peer to routing list: %s\n", my_short_id, "DHT", ret == NULL ? "NOT ADDED" : "PEER ADDED");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%s:%s Adding peer to routing list: %s\n", my_short_id, "DHT",
+              ret == NULL ? "NOT ADDED" : "PEER ADDED");
 #endif
 }
 
@@ -4523,27 +4993,40 @@ void handle_core_connect (void *cls,
  * @param cls closure
  * @param peer peer identity this notification is about
  */
-void handle_core_disconnect (void *cls,
-                             const struct
-                             GNUNET_PeerIdentity * peer)
+static void
+handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
   struct PeerInfo *to_remove;
   int current_bucket;
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s: Received peer disconnect message for peer `%s' from %s\n", my_short_id, "DHT", GNUNET_i2s(peer), "CORE");
+  /* Check for disconnect from self message */
+  if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
+    return;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%s:%s: Received peer disconnect message for peer `%s' from %s\n",
+              my_short_id, "DHT", GNUNET_i2s (peer), "CORE");
 
-  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey))
+  if (GNUNET_YES !=
+      GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
+                                              &peer->hashPubKey))
     {
-      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s: do not have peer `%s' in RT, can't disconnect!\n", my_short_id, "DHT", GNUNET_i2s(peer));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "%s:%s: do not have peer `%s' in RT, can't disconnect!\n",
+                  my_short_id, "DHT", GNUNET_i2s (peer));
       return;
     }
-  increment_stats(STAT_DISCONNECTS);
-  GNUNET_assert(GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey));
-  to_remove = GNUNET_CONTAINER_multihashmap_get(all_known_peers, &peer->hashPubKey);
+  increment_stats (STAT_DISCONNECTS);
+  GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
+                 (all_known_peers, &peer->hashPubKey));
+  to_remove =
+    GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
   GNUNET_assert (to_remove != NULL);
-  GNUNET_assert(0 == memcmp(peer, &to_remove->id, sizeof(struct GNUNET_PeerIdentity)));
-  current_bucket = find_current_bucket(&to_remove->id.hashPubKey);
-  delete_peer(to_remove, current_bucket);
+  GNUNET_assert (0 ==
+                 memcmp (peer, &to_remove->id,
+                         sizeof (struct GNUNET_PeerIdentity)));
+  current_bucket = find_current_bucket (&to_remove->id.hashPubKey);
+  delete_peer (to_remove, current_bucket);
 }
 
 
@@ -4568,11 +5051,11 @@ run (void *cls,
   GNUNET_SERVER_add_handlers (server, plugin_handlers);
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
   coreAPI = GNUNET_CORE_connect (cfg,   /* Main configuration */
-                                 GNUNET_TIME_UNIT_FOREVER_REL,
+                                 DEFAULT_CORE_QUEUE_SIZE,       /* queue size */
                                  NULL,  /* Closure passed to DHT functions */
                                  &core_init,    /* Call core_init once connected */
                                  &handle_core_connect,  /* Handle connects */
-                                 &handle_core_disconnect,  /* remove peers on disconnects */
+                                 &handle_core_disconnect,       /* remove peers on disconnects */
                                  NULL,  /* Do we care about "status" updates? */
                                  NULL,  /* Don't want notified about all incoming messages */
                                  GNUNET_NO,     /* For header only inbound notification */
@@ -4582,60 +5065,61 @@ run (void *cls,
 
   if (coreAPI == NULL)
     return;
-  transport_handle = GNUNET_TRANSPORT_connect(cfg,
-                                             NULL, NULL, NULL, NULL, NULL);
+  transport_handle = GNUNET_TRANSPORT_connect (cfg,
+                                               NULL, NULL, NULL, NULL, NULL);
   if (transport_handle != NULL)
     GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
   else
-    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
-              "Failed to connect to transport service!\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "Failed to connect to transport service!\n");
   block_context = GNUNET_BLOCK_context_create (cfg);
   lowest_bucket = MAX_BUCKETS - 1;
-  forward_list.hashmap = GNUNET_CONTAINER_multihashmap_create(MAX_OUTSTANDING_FORWARDS / 10);
-  forward_list.minHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
-  all_known_peers = GNUNET_CONTAINER_multihashmap_create(MAX_BUCKETS / 8);
-  recent_find_peer_requests = GNUNET_CONTAINER_multihashmap_create(MAX_BUCKETS / 8);
-  GNUNET_assert(all_known_peers != NULL);
-  if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging"))
+  forward_list.hashmap =
+    GNUNET_CONTAINER_multihashmap_create (MAX_OUTSTANDING_FORWARDS / 10);
+  forward_list.minHeap =
+    GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
+  all_known_peers = GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
+  recent_find_peer_requests =
+    GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
+  GNUNET_assert (all_known_peers != NULL);
+  if (GNUNET_YES ==
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
+                                            "mysql_logging"))
     {
       debug_routes = GNUNET_YES;
     }
 
   if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                           "strict_kademlia"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "strict_kademlia"))
     {
       strict_kademlia = GNUNET_YES;
     }
 
   if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                           "stop_on_closest"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_on_closest"))
     {
       stop_on_closest = GNUNET_YES;
     }
 
   if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                           "stop_found"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_found"))
     {
       stop_on_found = GNUNET_YES;
     }
 
   if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                           "malicious_getter"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_getter"))
     {
       malicious_getter = GNUNET_YES;
       if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
-                                            "MALICIOUS_GET_FREQUENCY",
-                                            &malicious_get_frequency))
+                                                              "MALICIOUS_GET_FREQUENCY",
+                                                              &malicious_get_frequency))
         malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
     }
 
   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
-                                        "MAX_HOPS",
-                                        &max_hops))
+                                                           "MAX_HOPS",
+                                                           &max_hops))
     {
       max_hops = DEFAULT_MAX_HOPS;
     }
@@ -4647,37 +5131,52 @@ run (void *cls,
     }
 
   if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                           "malicious_putter"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_putter"))
     {
       malicious_putter = GNUNET_YES;
       if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
-                                            "MALICIOUS_PUT_FREQUENCY",
-                                            &malicious_put_frequency))
+                                                              "MALICIOUS_PUT_FREQUENCY",
+                                                              &malicious_put_frequency))
         malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
     }
 
   dht_republish_frequency = GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY;
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "DHT", "REPLICATION_FREQUENCY", &temp_config_num))
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
+                                             "REPLICATION_FREQUENCY",
+                                             &temp_config_num))
+    {
+      dht_republish_frequency =
+        GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES,
+                                       temp_config_num);
+    }
+
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "bucket_size",
+                                             &temp_config_num))
     {
-      dht_republish_frequency = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, temp_config_num);
+      bucket_size = (unsigned int) temp_config_num;
+    }
+
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "kad_alpha",
+                                             &kademlia_replication))
+    {
+      kademlia_replication = DEFAULT_KADEMLIA_REPLICATION;
     }
 
   if (GNUNET_YES ==
-          GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                               "malicious_dropper"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_dropper"))
     {
       malicious_dropper = GNUNET_YES;
     }
 
   if (GNUNET_YES ==
-        GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                             "republish"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "republish"))
     do_republish = GNUNET_NO;
 
   if (GNUNET_NO ==
-        GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                             "do_find_peer"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "do_find_peer"))
     {
       do_find_peer = GNUNET_NO;
     }
@@ -4685,13 +5184,12 @@ run (void *cls,
     do_find_peer = GNUNET_YES;
 
   if (GNUNET_YES ==
-        GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                             "use_real_distance"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "use_real_distance"))
     use_real_distance = GNUNET_YES;
 
   if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing",
-                                           "mysql_logging_extended"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
+                                            "mysql_logging_extended"))
     {
       debug_routes = GNUNET_YES;
       debug_routes_extended = GNUNET_YES;
@@ -4700,7 +5198,7 @@ run (void *cls,
 #if DEBUG_DHT_ROUTING
   if (GNUNET_YES == debug_routes)
     {
-      dhtlog_handle = GNUNET_DHTLOG_connect(cfg);
+      dhtlog_handle = GNUNET_DHTLOG_connect (cfg);
       if (dhtlog_handle == NULL)
         {
           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -4711,77 +5209,85 @@ run (void *cls,
 
   converge_option = DHT_CONVERGE_SQUARE;
   if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                           "converge_linear"))
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "converge_linear"))
     {
       converge_option = DHT_CONVERGE_LINEAR;
     }
   else if (GNUNET_YES ==
-        GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                             "converge_exponential"))
+           GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
+                                                 "converge_exponential"))
     {
       converge_option = DHT_CONVERGE_EXPONENTIAL;
     }
   else if (GNUNET_YES ==
-        GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                             "converge_random"))
+           GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
+                                                 "converge_random"))
     {
       converge_option = DHT_CONVERGE_RANDOM;
     }
   else if (GNUNET_YES ==
-        GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
-                                             "converge_binary"))
+           GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
+                                                 "converge_binary"))
     {
       converge_option = DHT_CONVERGE_BINARY;
     }
 
-  if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "dht_testing", "converge_modifier", &converge_modifier_buf))
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_string (cfg, "dht", "converge_modifier",
+                                             &converge_modifier_buf))
     {
-      if (1 != sscanf(converge_modifier_buf, "%f", &converge_modifier))
+      if (1 != sscanf (converge_modifier_buf, "%f", &converge_modifier))
         {
-          GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read decimal value for %s from `%s'\n", "CONVERGE_MODIFIER", converge_modifier_buf);
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                      "Failed to read decimal value for %s from `%s'\n",
+                      "CONVERGE_MODIFIER", converge_modifier_buf);
           converge_modifier = 0.0;
         }
-      GNUNET_free(converge_modifier_buf);
+      GNUNET_free (converge_modifier_buf);
     }
 
-  stats = GNUNET_STATISTICS_create("dht", cfg);
+  stats = GNUNET_STATISTICS_create ("dht", cfg);
 
   if (stats != NULL)
     {
-      GNUNET_STATISTICS_set(stats, STAT_ROUTES, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_ROUTE_FORWARDS, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_ROUTE_FORWARDS_CLOSEST, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_RESULTS, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_RESULTS_TO_CLIENT, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_RESULT_FORWARDS, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_GETS, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_PUTS, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_PUTS_INSERTED, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_FIND_PEER, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_START, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_GET_START, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_PUT_START, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_REPLY, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_ANSWER, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_BLOOM_FIND_PEER, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_GET_REPLY, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_HELLOS_PROVIDED, 0, GNUNET_NO);
-      GNUNET_STATISTICS_set(stats, STAT_DISCONNECTS, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_ROUTES, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS_CLOSEST, 0,
+                             GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_RESULTS, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_RESULTS_TO_CLIENT, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_RESULT_FORWARDS, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_GETS, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_PUTS, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_PUTS_INSERTED, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_FIND_PEER, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_START, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_GET_START, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_PUT_START, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_REPLY, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_ANSWER, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_BLOOM_FIND_PEER, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_GET_REPLY, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_HELLOS_PROVIDED, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set (stats, STAT_DISCONNECTS, 0, GNUNET_NO);
     }
   /* FIXME: if there are no recent requests then these never get freed, but alternative is _annoying_! */
-  recent.hashmap = GNUNET_CONTAINER_multihashmap_create(DHT_MAX_RECENT / 2);
-  recent.minHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
+  recent.hashmap = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT / 2);
+  recent.minHeap =
+    GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
   if (GNUNET_YES == do_find_peer)
-  {
-    next_send_time.rel_value = DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
-                           GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
-                                                    (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) - DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
-    find_peer_context.start = GNUNET_TIME_absolute_get();
-    GNUNET_SCHEDULER_add_delayed (next_send_time,
-                                  &send_find_peer_message, &find_peer_context);
-  }
+    {
+      next_send_time.rel_value = DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
+        GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
+                                  (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
+                                   2) -
+                                  DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
+      find_peer_context.start = GNUNET_TIME_absolute_get ();
+      GNUNET_SCHEDULER_add_delayed (next_send_time,
+                                    &send_find_peer_message,
+                                    &find_peer_context);
+    }
 
   /* Scheduled the task to clean up when shutdown is called */
   cleanup_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
@@ -4801,13 +5307,12 @@ main (int argc, char *const *argv)
   int ret;
 
   ret = (GNUNET_OK ==
-        GNUNET_SERVICE_run (argc,
-                            argv,
-                            "dht",
-                            GNUNET_SERVICE_OPTION_NONE,
-                            &run, NULL)) ? 0 : 1;
-  GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap));
-  GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap));
+         GNUNET_SERVICE_run (argc,
+                             argv,
+                             "dht",
+                             GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
+  GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent.hashmap));
+  GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent.minHeap));
   GNUNET_CONTAINER_multihashmap_destroy (recent_find_peer_requests);
   GNUNET_CONTAINER_multihashmap_destroy (recent.hashmap);
   GNUNET_CONTAINER_heap_destroy (recent.minHeap);