print stat
[oweals/gnunet.git] / src / dht / gnunet-service-dht_neighbours.c
index 7411c964d8650cf884044fbff47b6ff9e816160b..e1491a263d96742cabd9335ba7398969ec78c22e 100644 (file)
 #include "gnunet_hello_lib.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_statistics_service.h"
-#include "gnunet_peerinfo_service.h"
-#include "dht.h"
 #include "gnunet-service-dht.h"
 #include "gnunet-service-dht_clients.h"
 #include "gnunet-service-dht_datacache.h"
+#include "gnunet-service-dht_hello.h"
 #include "gnunet-service-dht_neighbours.h"
 #include "gnunet-service-dht_nse.h"
 #include "gnunet-service-dht_routing.h"
 #include <fenv.h>
+#include "dht.h"
 
 /**
  * How many buckets will we allow total.
 /**
  * What is the maximum number of peers in a given bucket.
  */
-#define DEFAULT_BUCKET_SIZE 4
-
-/**
- * Size of the bloom filter the DHT uses to filter peers.
- */
-#define DHT_BLOOM_SIZE 128
+#define DEFAULT_BUCKET_SIZE 8
 
 /**
  * Desired replication level for FIND PEER requests
 /**
  * How long at least to wait before sending another find peer request.
  */
-#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
+#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
 
 /**
  * How long at most to wait before sending another find peer request.
  */
-#define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
+#define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10)
 
 /**
  * How long at most to wait for transmission of a GET request to another peer?
@@ -328,12 +323,6 @@ struct PeerInfo
    */
   struct GNUNET_CORE_InformationRequestContext *info_ctx;
 
-  /**
-   * HELLO message for the peer, NULL if not known.  FIXME: track
-   * separately? FIXME: free!?
-   */
-  struct GNUNET_HELLO_Message *hello;
-
   /**
    * Task for scheduling preference updates
    */
@@ -422,10 +411,6 @@ static struct GNUNET_PeerIdentity my_identity;
  */
 static struct GNUNET_CORE_Handle *coreAPI;
 
-/**
- * Handle for peerinfo notifications.
- */
-static struct GNUNET_PEERINFO_NotifyContext *pnc;
 
 
 /**
@@ -514,11 +499,14 @@ update_core_preference (void *cls,
                                      &peer->id.hashPubKey);
   if (matching >= 64)
     matching = 63;
-  bucket = find_bucket(&peer->id.hashPubKey);
+  bucket = find_bucket (&peer->id.hashPubKey);
   if (bucket == GNUNET_SYSERR)
     preference = 0;
   else
+  {
+    GNUNET_assert (k_buckets[bucket].peers_size != 0);
     preference = (1LL << matching) / k_buckets[bucket].peers_size;
+  }
   if (preference == 0)
     {
       peer->preference_task
@@ -526,6 +514,9 @@ update_core_preference (void *cls,
                                        &update_core_preference, peer);
       return;
     }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# Preference updates given to core"), 1,
+                           GNUNET_NO);
   peer->info_ctx =
     GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
                                        GNUNET_TIME_UNIT_FOREVER_REL,
@@ -568,6 +559,10 @@ add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
   GNUNET_HashCode mh;
 
   GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding known peer (%s) to bloomfilter for FIND PEER with mutation %u\n",
+             GNUNET_h2s (key),
+             ctx->bf_mutator);
   GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
   return GNUNET_YES;
 }
@@ -587,6 +582,7 @@ send_find_peer_message (void *cls,
 {
   struct GNUNET_TIME_Relative next_send_time;
   struct BloomConstructorContext bcc;
+  struct GNUNET_CONTAINER_BloomFilter *peer_bf;
 
   find_peer_task = GNUNET_SCHEDULER_NO_TASK;
   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
@@ -601,10 +597,16 @@ send_find_peer_message (void *cls,
   }
   bcc.bf_mutator = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
   bcc.bloom =
-    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
+    GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, GNUNET_CONSTANTS_BLOOMFILTER_K);
   GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, 
                                         &add_known_to_bloom,
                                          &bcc);
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# FIND PEER messages initiated"), 1,
+                           GNUNET_NO);
+  peer_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
+                                              DHT_BLOOM_SIZE,
+                                              GNUNET_CONSTANTS_BLOOMFILTER_K);
   // FIXME: pass priority!?
   GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
                             GNUNET_DHT_RO_FIND_PEER,
@@ -612,14 +614,16 @@ send_find_peer_message (void *cls,
                             0,
                             &my_identity.hashPubKey,
                             NULL, 0,
-                            bcc.bloom, bcc.bf_mutator, NULL);
+                            bcc.bloom, bcc.bf_mutator, 
+                            peer_bf);
+  GNUNET_CONTAINER_bloomfilter_free (peer_bf);
   GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
   /* schedule next round */
-  newly_found_peers = 0;
   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);
+    DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
+    GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
+                             DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / (newly_found_peers+1));
+  newly_found_peers = 0;
   find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time, 
                                                 &send_find_peer_message,
                                                 NULL);  
@@ -643,6 +647,10 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
   /* Check for connect to self message */
   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
     return;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Connected %s to %s\n",
+             GNUNET_i2s (&my_identity),
+             GNUNET_h2s (&peer->hashPubKey));
   if (GNUNET_YES ==
       GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
                                               &peer->hashPubKey))
@@ -650,6 +658,9 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_break (0);
     return;
   }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# Peers connected"), 1,
+                           GNUNET_NO);
   peer_bucket = find_bucket (&peer->hashPubKey);
   GNUNET_assert ( (peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS) );
   ret = GNUNET_malloc (sizeof (struct PeerInfo));
@@ -665,8 +676,10 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
                               peer_bucket);
   if ( (peer_bucket > 0) &&
        (k_buckets[peer_bucket].peers_size <= bucket_size) )
+  {
     ret->preference_task = GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
-  newly_found_peers++;
+    newly_found_peers++;
+  }
   GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONTAINER_multihashmap_put (all_known_peers, 
                                                    &peer->hashPubKey, ret,
@@ -692,10 +705,15 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
   struct PeerInfo *to_remove;
   int current_bucket;
   struct P2PPendingMessage *pos;
+  unsigned int discarded;
 
   /* Check for disconnect from self message */
   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
     return;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Disconnected %s from %s\n",
+             GNUNET_i2s (&my_identity),
+             GNUNET_h2s (&peer->hashPubKey));
   to_remove =
       GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
   if (NULL == to_remove)
@@ -703,6 +721,9 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
       GNUNET_break (0);
       return;
     }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# Peers connected"), -1,
+                           GNUNET_NO);
   GNUNET_assert (GNUNET_YES ==
                  GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
                                                        &peer->hashPubKey,
@@ -712,6 +733,11 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
     GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
     to_remove->info_ctx = NULL;
   }
+  if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task)
+  {
+    GNUNET_SCHEDULER_cancel (to_remove->preference_task);
+    to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK;
+  }
   current_bucket = find_bucket (&to_remove->id.hashPubKey);
   GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
                               k_buckets[current_bucket].tail,
@@ -727,13 +753,19 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
     GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
     to_remove->th = NULL;
   }
+  discarded = 0;
   while (NULL != (pos = to_remove->head))
   {
     GNUNET_CONTAINER_DLL_remove (to_remove->head,
                                 to_remove->tail,
                                 pos);
+    discarded++;
     GNUNET_free (pos);
   }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# Queued messages discarded (peer disconnected)"), discarded,
+                           GNUNET_NO);
+  GNUNET_free (to_remove);
 }
 
 
@@ -756,20 +788,36 @@ core_transmit_notify (void *cls, size_t size, void *buf)
   size_t msize;
 
   peer->th = NULL;
-  if (buf == NULL)
+  while ( (NULL != (pending = peer->head)) &&
+         (GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value == 0) )
   {
-    /* client disconnected */
-    return 0;
+    peer->pending_count--;
+    GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
+    GNUNET_free (pending);
   }
-  if (peer->head == NULL)
+  if (pending == NULL)
   {
     /* no messages pending */
     return 0;
   }
+  if (buf == NULL)
+  {
+    peer->th 
+      = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
+                                          pending->importance,
+                                          GNUNET_TIME_absolute_get_remaining (pending->timeout),
+                                          &peer->id, ntohs (pending->msg->size),
+                                          &core_transmit_notify, peer);
+    GNUNET_break (NULL != peer->th);
+    return 0;
+  }
   off = 0;
   while ( (NULL != (pending = peer->head)) &&
          (size - off >= (msize = ntohs (pending->msg->size))) )
   {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# Bytes transmitted to other peers"), msize,
+                             GNUNET_NO);
     memcpy (&cbuf[off], pending->msg, msize);
     off += msize;
     peer->pending_count--;
@@ -777,12 +825,15 @@ core_transmit_notify (void *cls, size_t size, void *buf)
     GNUNET_free (pending);
   }
   if (peer->head != NULL)
+  {
     peer->th 
       = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
-                                           pending->importance,
-                                           GNUNET_TIME_absolute_get_remaining (pending->timeout),
+                                          pending->importance,
+                                          GNUNET_TIME_absolute_get_remaining (pending->timeout),
                                           &peer->id, msize,
-                                           &core_transmit_notify, peer);
+                                          &core_transmit_notify, peer);
+    GNUNET_break (NULL != peer->th);
+  }
   return off;
 }
 
@@ -797,10 +848,14 @@ process_peer_queue (struct PeerInfo *peer)
 {
   struct P2PPendingMessage *pending;
 
-  if (NULL != (pending = peer->head))
+  if (NULL == (pending = peer->head))
     return;
   if (NULL != peer->th)
     return;
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# Bytes of bandwdith requested from core"),
+                           ntohs (pending->msg->size),
+                           GNUNET_NO);
   peer->th 
     = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
                                         pending->importance,
@@ -808,6 +863,7 @@ process_peer_queue (struct PeerInfo *peer)
                                         &peer->id,
                                         ntohs (pending->msg->size),
                                         &core_transmit_notify, peer);
+  GNUNET_break (NULL != peer->th);
 }
 
 
@@ -827,12 +883,12 @@ get_forward_count (uint32_t hop_count,
   uint32_t forward_count;
   float target_value;
 
-  if (hop_count > GDS_NSE_get () * 4.0)
+  if (hop_count > GDS_NSE_get () * 6.0)
   {
     /* forcefully terminate */
     return 0;
   }
-  if (hop_count > GDS_NSE_get () * 2.0)
+  if (hop_count > GDS_NSE_get () * 4.0)
   {
     /* Once we have reached our ideal number of hops, only forward to 1 peer */
     return 1;
@@ -849,7 +905,7 @@ get_forward_count (uint32_t hop_count,
   /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
   target_value = target_value - forward_count;
   random_value =
-    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX); 
+    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); 
   if (random_value < (target_value * UINT32_MAX))
     forward_count++;
   return forward_count;
@@ -934,13 +990,11 @@ am_closest_peer (const GNUNET_HashCode *key,
   int bucket_num;
   int count;
   struct PeerInfo *pos;
-  unsigned int my_distance;
 
   if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
     return GNUNET_YES;
   bucket_num = find_bucket (key);
   bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, key);
-  my_distance = get_distance (&my_identity.hashPubKey, key);
   pos = k_buckets[bucket_num].head;
   count = 0;
   while ((pos != NULL) && (count < bucket_size))
@@ -999,15 +1053,16 @@ select_peer (const GNUNET_HashCode *key,
     /* greedy selection (closest peer that is not in bloomfilter) */
     smallest_distance = UINT_MAX;
     chosen = NULL;
-    for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
+    for (bc = 0; bc < closest_bucket; bc++)
     {
       pos = k_buckets[bc].head;
       count = 0;
       while ((pos != NULL) && (count < bucket_size))
       {
-        if (GNUNET_NO ==
-            GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
-        {
+        if ( (bloom == NULL) ||
+            (GNUNET_NO ==
+             GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
+       {
           dist = get_distance (key, &pos->id.hashPubKey);
           if (dist < smallest_distance)
           {
@@ -1015,10 +1070,24 @@ select_peer (const GNUNET_HashCode *key,
             smallest_distance = dist;
           }
         }
+        else
+       {
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                     "Excluded peer `%s' due to BF match in greedy routing for %s\n",
+                     GNUNET_i2s (&pos->id),
+                     GNUNET_h2s (key));
+         GNUNET_STATISTICS_update (GDS_stats,
+                                   gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
+                                   GNUNET_NO);
+       }
         count++;
         pos = pos->next;
       }
     }
+    if (NULL == chosen)
+      GNUNET_STATISTICS_update (GDS_stats,
+                               gettext_noop ("# Peer selection failed"), 1,
+                               GNUNET_NO);
     return chosen;
   }
 
@@ -1030,9 +1099,17 @@ select_peer (const GNUNET_HashCode *key,
     pos = k_buckets[bc].head;
     while ((pos != NULL) && (count < bucket_size))
     {
-      if (GNUNET_YES ==
-          GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+      if ( (bloom != NULL) &&
+          (GNUNET_YES ==
+           GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
       {
+       GNUNET_STATISTICS_update (GDS_stats,
+                                 gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
+                                 GNUNET_NO);
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                   "Excluded peer `%s' due to BF match in random routing for %s\n",
+                   GNUNET_i2s (&pos->id),
+                   GNUNET_h2s (key));
         pos = pos->next;
         continue;               /* Ignore bloomfiltered peers */
       }
@@ -1042,6 +1119,9 @@ select_peer (const GNUNET_HashCode *key,
   }
   if (count == 0)               /* No peers to select from! */
   {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# Peer selection failed"), 1,
+                             GNUNET_NO);
     return NULL;
   }
   /* Now actually choose a peer */
@@ -1052,8 +1132,9 @@ select_peer (const GNUNET_HashCode *key,
     pos = k_buckets[bc].head;
     while ((pos != NULL) && (count < bucket_size))
     {
-      if (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;               /* Ignore bloomfiltered peers */
@@ -1093,6 +1174,7 @@ get_target_peers (const GNUNET_HashCode *key,
   struct PeerInfo **rtargets;
   struct PeerInfo *nxt;
 
+  GNUNET_assert (NULL != bloom);
   ret = get_forward_count (hop_count, target_replication);
   if (ret == 0)
   {
@@ -1100,15 +1182,23 @@ get_target_peers (const GNUNET_HashCode *key,
     return 0;
   }
   rtargets = GNUNET_malloc (sizeof (struct PeerInfo*) * ret);
-  off = 0;
-  while (ret-- > 0)
+  for (off = 0; off < ret; off++)
   {
     nxt = select_peer (key, bloom, hop_count);
     if (nxt == NULL)
-      break;
-    rtargets[off++] = nxt;
-    GNUNET_CONTAINER_bloomfilter_add (bloom, &nxt->id.hashPubKey);
+      break;      
+    rtargets[off] = nxt;
+    GNUNET_break (GNUNET_NO ==
+                 GNUNET_CONTAINER_bloomfilter_test (bloom, &nxt->id.hashPubKey));
+    GNUNET_CONTAINER_bloomfilter_add (bloom, &rtargets[off]->id.hashPubKey);
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Selected %u/%u peers at hop %u for %s (target was %u)\n",
+             off,
+             GNUNET_CONTAINER_multihashmap_size (all_known_peers),
+             (unsigned int) hop_count,
+             GNUNET_h2s (key),
+             ret);
   if (0 == off)
   {
     GNUNET_free (rtargets);
@@ -1161,11 +1251,27 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
   struct PeerPutMessage *ppm;
   struct GNUNET_PeerIdentity *pp;
   
+  GNUNET_assert (NULL != bf);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding myself (%s) to PUT bloomfilter for %s\n",
+             GNUNET_i2s (&my_identity),
+             GNUNET_h2s (key));
+  GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity.hashPubKey);
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# PUT requests routed"), 1,
+                           GNUNET_NO);
   target_count = get_target_peers (key, bf, hop_count,
                                   desired_replication_level,
                                   &targets);
   if (0 == target_count)
-    return;
+    { 
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Routing PUT for %s terminates after %u hops at %s\n",
+                 GNUNET_h2s (key),
+                 (unsigned int) hop_count,
+                 GNUNET_i2s (&my_identity));
+      return;
+    }
   msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + sizeof (struct PeerPutMessage);
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
   {
@@ -1177,9 +1283,17 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
     GNUNET_break (0);
     return;
   }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# PUT messages queued for transmission"), target_count,
+                           GNUNET_NO);
   for (i=0;i<target_count;i++)
   {
     target = targets[i];
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Routing PUT for %s after %u hops to %s\n",
+               GNUNET_h2s (key),
+               (unsigned int) hop_count,
+               GNUNET_i2s (&target->id));
     pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
     pending->importance = 0; /* FIXME */
     pending->timeout = expiration_time;   
@@ -1193,6 +1307,7 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
     ppm->desired_replication_level = htonl (desired_replication_level);
     ppm->put_path_length = htonl (put_path_length);
     ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
+    GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &target->id.hashPubKey));
     GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
                                                              ppm->bloomfilter,
@@ -1249,12 +1364,28 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
   struct PeerGetMessage *pgm;
   char *xq;
   size_t reply_bf_size;
-  
+
+  GNUNET_assert (NULL != peer_bf);  
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# GET requests routed"), 1,
+                           GNUNET_NO);
   target_count = get_target_peers (key, peer_bf, hop_count,
                                   desired_replication_level,
                                   &targets);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding myself (%s) to GET bloomfilter for %s\n",
+             GNUNET_i2s (&my_identity),
+             GNUNET_h2s (key));
+  GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity.hashPubKey);
   if (0 == target_count)
-    return;
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Routing GET for %s terminates after %u hops at %s\n",
+                 GNUNET_h2s (key),
+                 (unsigned int) hop_count,
+                 GNUNET_i2s (&my_identity));
+      return;
+    }
   reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
   msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
@@ -1262,10 +1393,18 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
     GNUNET_break (0);
     return;
   }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# GET messages queued for transmission"), target_count,
+                           GNUNET_NO);
   /* forward request */
   for (i=0;i<target_count;i++)
   {
     target = targets[i];
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Routing GET for %s after %u hops to %s\n",
+               GNUNET_h2s (key),
+               (unsigned int) hop_count,
+               GNUNET_i2s (&target->id));
     pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); 
     pending->importance = 0; /* FIXME */
     pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
@@ -1279,6 +1418,7 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
     pgm->desired_replication_level = htonl (desired_replication_level);
     pgm->xquery_size = htonl (xquery_size);
     pgm->bf_mutator = reply_bf_mutator; 
+    GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (peer_bf, &target->id.hashPubKey));
     GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
                                                              pgm->bloomfilter,
@@ -1286,10 +1426,11 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
     pgm->key = *key;
     xq = (char *) &pgm[1];
     memcpy (xq, xquery, xquery_size);
-    GNUNET_assert (GNUNET_OK ==
-                  GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
-                                                             &xq[xquery_size],
-                                                             reply_bf_size));
+    if (NULL != reply_bf)
+      GNUNET_assert (GNUNET_OK ==
+                    GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
+                                                               &xq[xquery_size],
+                                                               reply_bf_size));
     GNUNET_CONTAINER_DLL_insert_tail (target->head,
                                      target->tail,
                                      pending);
@@ -1351,6 +1492,9 @@ GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
     /* peer disconnected in the meantime, drop reply */
     return;
   }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# RESULT messages queued for transmission"), 1,
+                           GNUNET_NO);
   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); 
   pending->importance = 0; /* FIXME */
   pending->timeout = expiration_time;
@@ -1437,6 +1581,9 @@ handle_dht_p2p_put (void *cls,
       GNUNET_break_op (0);
       return GNUNET_YES;
     }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# P2P PUT requests received"), 1,
+                           GNUNET_NO);
   put_path = (const struct GNUNET_PeerIdentity*) &put[1];  
   payload = &put_path[putlen];
   options = ntohl (put->options);
@@ -1461,9 +1608,14 @@ handle_dht_p2p_put (void *cls,
     /* cannot verify, good luck */
     break;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "PUT for %s at %s\n",
+             GNUNET_h2s (&put->key),
+             GNUNET_i2s (&my_identity));
   bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
                                          DHT_BLOOM_SIZE,
-                                         DHT_BLOOM_K);
+                                         GNUNET_CONSTANTS_BLOOMFILTER_K);
+  GNUNET_break_op (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &peer->hashPubKey));
   {
     struct GNUNET_PeerIdentity pp[putlen+1];
   
@@ -1478,7 +1630,7 @@ handle_dht_p2p_put (void *cls,
       putlen = 0;
     
     /* give to local clients */
-    GDS_CLIENT_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
+    GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
                             &put->key,
                             0, NULL,
                             putlen,
@@ -1501,7 +1653,7 @@ handle_dht_p2p_put (void *cls,
                               options,
                               ntohl (put->desired_replication_level),
                               GNUNET_TIME_absolute_ntoh (put->expiration_time),
-                              ntohl (put->hop_count) + 1 /* who adds +1? */,
+                              ntohl (put->hop_count),
                               bf,
                               &put->key,
                               putlen, pp,
@@ -1533,13 +1685,15 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
   struct PeerInfo *peer;
   unsigned int choice;
   GNUNET_HashCode mhash;
+  const struct GNUNET_HELLO_Message *hello;
 
   /* first, check about our own HELLO */
   if (NULL != GDS_my_hello)
   {
     GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
-    if (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))
-      
+    if ( (NULL == bf) ||
+        (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) )
+    {
       GDS_NEIGHBOURS_handle_reply (sender,
                                   GNUNET_BLOCK_TYPE_DHT_HELLO,
                                   GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
@@ -1548,22 +1702,39 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
                                   0, NULL,
                                   GDS_my_hello,
                                   GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*) GDS_my_hello));
+    }
+    else
+    {
+      GNUNET_STATISTICS_update (GDS_stats,
+                               gettext_noop ("# FIND PEER requests ignored due to Bloomfilter"), 1,
+                               GNUNET_NO);
+    }
+  }
+  else
+  {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# FIND PEER requests ignored due to lack of HELLO"), 1,
+                             GNUNET_NO);
   }
 
   /* then, also consider sending a random HELLO from the closest bucket */
-  bucket_idx = find_bucket (key);
+  if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
+    bucket_idx = closest_bucket;
+  else
+    bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key));
   if (bucket_idx == GNUNET_SYSERR)
     return;
   bucket = &k_buckets[bucket_idx];
   if (bucket->peers_size == 0)
     return;
-  choice = GNUNET_CRYPTO_random_u32 (bucket->peers_size,
-                                    GNUNET_CRYPTO_QUALITY_WEAK);
+  choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                    bucket->peers_size);
   peer = bucket->head;
   while (choice > 0)
   {
     GNUNET_assert (peer != NULL);
     peer = peer->next;
+    choice--;
   }
   choice = bucket->peers_size;
   do
@@ -1574,8 +1745,9 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
       if (peer == NULL)
        peer = bucket->head;
       GNUNET_BLOCK_mingle_hash (&peer->id.hashPubKey, bf_mutator, &mhash);
+      hello = GDS_HELLO_get (&peer->id);
     }
-  while ( (peer->hello == NULL) ||
+  while ( (hello == NULL) ||
          (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) );
   GDS_NEIGHBOURS_handle_reply (sender,
                               GNUNET_BLOCK_TYPE_DHT_HELLO,
@@ -1583,8 +1755,8 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
                               key,
                               0, NULL,
                               0, NULL,
-                              peer->hello,
-                              GNUNET_HELLO_size (peer->hello));    
+                              hello,
+                              GNUNET_HELLO_size (hello));    
 }
 
 
@@ -1615,7 +1787,8 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct GNUNET_CONTAINER_BloomFilter *reply_bf;
   struct GNUNET_CONTAINER_BloomFilter *peer_bf;
   const char *xquery;
-                      
+
+  GNUNET_break (0 != memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)));
   /* parse and validate message */
   msize = ntohs (message->size);
   if (msize < sizeof (struct PeerGetMessage))
@@ -1630,6 +1803,9 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_break_op (0);
     return GNUNET_YES;
   }
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# P2P GET requests received"), 1,
+                           GNUNET_NO);
   reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
   type = ntohl (get->type);
   options = ntohl (get->options);
@@ -1638,7 +1814,7 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   if (reply_bf_size > 0)
     reply_bf = GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size],
                                                  reply_bf_size,
-                                                 GNUNET_DHT_GET_BLOOMFILTER_K);
+                                                 GNUNET_CONSTANTS_BLOOMFILTER_K);
   eval = GNUNET_BLOCK_evaluate (GDS_block_context,
                                type,
                                &get->key,
@@ -1657,8 +1833,8 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   peer_bf =
     GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, 
                                       DHT_BLOOM_SIZE,
-                                      DHT_BLOOM_K);
-
+                                      GNUNET_CONSTANTS_BLOOMFILTER_K);
+  GNUNET_break_op (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (peer_bf, &peer->hashPubKey));
   /* remember request for routing replies */
   GDS_ROUTING_add (peer,
                   type,
@@ -1666,14 +1842,21 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
                   &get->key,
                   xquery, xquery_size,
                   reply_bf, get->bf_mutator);
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "GET for %s at %s after %u hops\n",
+             GNUNET_h2s (&get->key),
+             GNUNET_i2s (&my_identity),
+             (unsigned int) ntohl (get->hop_count));
   /* local lookup (this may update the reply_bf) */
   if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
        (am_closest_peer (&get->key,
                         peer_bf) ) )
-  {
+    {
     if ( (0 != (options & GNUNET_DHT_RO_FIND_PEER)))
     {
+      GNUNET_STATISTICS_update (GDS_stats,
+                               gettext_noop ("# P2P FIND PEER requests processed"), 1,
+                               GNUNET_NO);
       handle_find_peer (peer,
                        &get->key,
                        reply_bf,
@@ -1688,13 +1871,19 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
                                       get->bf_mutator);
     }
   }
+  else
+  {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# P2P GET requests ONLY routed"), 1,
+                             GNUNET_NO);
+  }
   
   /* P2P forwarding */
   if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
     GDS_NEIGHBOURS_handle_get (type,
                               options,
                               ntohl (get->desired_replication_level),
-                              ntohl (get->hop_count) + 1, /* CHECK: where (else) do we do +1? */
+                              ntohl (get->hop_count),
                               &get->key,
                               xquery, xquery_size,
                               reply_bf,
@@ -1751,6 +1940,9 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_break_op (0);
     return GNUNET_YES;
   } 
+  GNUNET_STATISTICS_update (GDS_stats,
+                           gettext_noop ("# P2P RESULTS received"), 1,
+                           GNUNET_NO);
   put_path = (const struct GNUNET_PeerIdentity*) &prm[1];
   get_path = &put_path[put_path_length];
   type = ntohl (prm->type);
@@ -1784,17 +1976,20 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
       GNUNET_break_op (0);
       return GNUNET_YES;
     }
-    bucket = find_bucket (&pid.hashPubKey);
-    if ( (bucket >= 0) &&
-        (k_buckets[bucket].peers_size < bucket_size) )
-    {    
-      if (NULL != GDS_transport_handle)
-       GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
-                                     h, NULL, NULL);
-      (void) GNUNET_CORE_peer_request_connect (coreAPI,
-                                              &pid, 
-                                              NULL, NULL);
-    }   
+    if (0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
+    {
+      bucket = find_bucket (&pid.hashPubKey);
+      if ( (bucket >= 0) &&
+          (k_buckets[bucket].peers_size < bucket_size) )
+       {    
+         if (NULL != GDS_transport_handle)
+           GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
+                                         h, NULL, NULL);
+         (void) GNUNET_CORE_peer_request_connect (coreAPI,
+                                                  &pid, 
+                                                  NULL, NULL);
+       }   
+    }
   }
 
   /* append 'peer' to 'get_path' */
@@ -1806,7 +2001,7 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
     get_path_length++;
 
     /* forward to local clients */   
-    GDS_CLIENT_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
+    GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
                             &prm->key,
                             get_path_length,
                             xget_path,
@@ -1831,29 +2026,6 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
 }
 
 
-/**
- * Function called for each HELLO known to PEERINFO.
- *
- * @param cls closure
- * @param peer id of the peer, NULL for last call
- * @param hello hello message for the peer (can be NULL)
- * @param error message
- */
-static void
-process_hello (void *cls,
-              const struct GNUNET_PeerIdentity *
-              peer,
-              const struct GNUNET_HELLO_Message *
-              hello, const char *err_msg)
-{
-  // FIXME: consider moving HELLO processing to another file!
-  // FIXME: first, filter HELLOs without addresses (!)
-  // FIXME: track HELLOs (for responding to FIND PEER requests)
-  // FIXME: add code to possibly ask core to establish connections
-  //        (using our own peerinfo is better than using FIND PEER!)
-}
-
-
 /**
  * Initialize neighbours subsystem.
  *
@@ -1887,9 +2059,6 @@ GDS_NEIGHBOURS_init ()
   if (coreAPI == NULL)
     return GNUNET_SYSERR;
   all_known_peers = GNUNET_CONTAINER_multihashmap_create (256);
-  pnc = GNUNET_PEERINFO_notify (GDS_cfg,
-                               &process_hello,
-                               NULL);
   return GNUNET_OK;
 }
 
@@ -1902,11 +2071,6 @@ GDS_NEIGHBOURS_done ()
 {
   if (coreAPI == NULL)
     return;
-  if (NULL != pnc)
-  {
-    GNUNET_PEERINFO_notify_cancel (pnc);
-    pnc = NULL;
-  }
   GNUNET_CORE_disconnect (coreAPI);
   coreAPI = NULL;    
   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (all_known_peers));