rename all_known_peers to all_connected_peers
[oweals/gnunet.git] / src / dht / gnunet-service-dht_neighbours.c
index 7344abd3519afe879114a4c8c39a4e2505ea3d5c..f4d817223b3b9a4a4cc10438f7908c1229d516b9 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2015 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -24,7 +24,6 @@
  * @author Christian Grothoff
  * @author Nathan Evans
  */
-
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_block_lib.h"
@@ -46,7 +45,6 @@
 #include "gnunet-service-dht_neighbours.h"
 #include "gnunet-service-dht_nse.h"
 #include "gnunet-service-dht_routing.h"
-#include <fenv.h>
 #include "dht.h"
 
 #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
@@ -335,7 +333,7 @@ struct PeerInfo
   /**
    * Task for scheduling preference updates
    */
-  GNUNET_SCHEDULER_TaskIdentifier preference_task;
+  struct GNUNET_SCHEDULER_Task *preference_task;
 
   /**
    * What is the identity of the peer?
@@ -406,14 +404,15 @@ static unsigned int newly_found_peers;
 static int disable_try_connect;
 
 /**
- * The buckets.  Array of size MAX_BUCKET_SIZE.  Offset 0 means 0 bits matching.
+ * The buckets.  Array of size #MAX_BUCKETS.  Offset 0 means 0 bits matching.
  */
 static struct PeerBucket k_buckets[MAX_BUCKETS];
 
 /**
- * Hash map of all known peers, for easy removal from k_buckets on disconnect.
+ * Hash map of all CORE-connected peers, for easy removal from
+ * #k_buckets on disconnect.
  */
-static struct GNUNET_CONTAINER_MultiPeerMap *all_known_peers;
+static struct GNUNET_CONTAINER_MultiPeerMap *all_connected_peers;
 
 /**
  * Maximum size for each bucket.
@@ -423,7 +422,7 @@ static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
 /**
  * Task that sends FIND PEER requests.
  */
-static GNUNET_SCHEDULER_TaskIdentifier find_peer_task;
+static struct GNUNET_SCHEDULER_Task *find_peer_task;
 
 /**
  * Identity of this peer.
@@ -441,9 +440,14 @@ static struct GNUNET_HashCode my_identity_hash;
 static struct GNUNET_CORE_Handle *core_api;
 
 /**
- * Handle to ATS.
+ * Handle to ATS performance monitoring.
  */
-static struct GNUNET_ATS_PerformanceHandle *atsAPI;
+static struct GNUNET_ATS_PerformanceHandle *ats_perf;
+
+/**
+ * Handle to ATS connectivity.
+ */
+static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
 
 
 
@@ -486,7 +490,7 @@ update_core_preference (void *cls,
   int bucket;
   struct GNUNET_HashCode phash;
 
-  peer->preference_task = GNUNET_SCHEDULER_NO_TASK;
+  peer->preference_task = NULL;
   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
     return;
   GNUNET_CRYPTO_hash (&peer->id,
@@ -515,9 +519,11 @@ update_core_preference (void *cls,
   GNUNET_STATISTICS_update (GDS_stats,
                             gettext_noop ("# Preference updates given to core"),
                             1, GNUNET_NO);
-  GNUNET_ATS_performance_change_preference (atsAPI, &peer->id,
-                                GNUNET_ATS_PREFERENCE_BANDWIDTH,
-                                (double) preference, GNUNET_ATS_PREFERENCE_END);
+  GNUNET_ATS_performance_change_preference (ats_perf,
+                                            &peer->id,
+                                            GNUNET_ATS_PREFERENCE_BANDWIDTH,
+                                            (double) preference,
+                                            GNUNET_ATS_PREFERENCE_END);
   peer->preference_task =
       GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
                                     &update_core_preference, peer);
@@ -587,7 +593,7 @@ send_find_peer_message (void *cls,
   struct BloomConstructorContext bcc;
   struct GNUNET_CONTAINER_BloomFilter *peer_bf;
 
-  find_peer_task = GNUNET_SCHEDULER_NO_TASK;
+  find_peer_task = NULL;
   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
     return;
   if (newly_found_peers > bucket_size)
@@ -604,7 +610,8 @@ send_find_peer_message (void *cls,
   bcc.bloom =
       GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
                                          GNUNET_CONSTANTS_BLOOMFILTER_K);
-  GNUNET_CONTAINER_multipeermap_iterate (all_known_peers, &add_known_to_bloom,
+  GNUNET_CONTAINER_multipeermap_iterate (all_connected_peers,
+                                         &add_known_to_bloom,
                                          &bcc);
   GNUNET_STATISTICS_update (GDS_stats,
                             gettext_noop ("# FIND PEER messages initiated"), 1,
@@ -640,7 +647,8 @@ send_find_peer_message (void *cls,
  * @param peer peer identity this notification is about
  */
 static void
-handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
+handle_core_connect (void *cls,
+                     const struct GNUNET_PeerIdentity *peer)
 {
   struct PeerInfo *ret;
   struct GNUNET_HashCode phash;
@@ -653,7 +661,7 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
              "Connected to %s\n",
               GNUNET_i2s (peer));
   if (GNUNET_YES ==
-      GNUNET_CONTAINER_multipeermap_contains (all_known_peers,
+      GNUNET_CONTAINER_multipeermap_contains (all_connected_peers,
                                               peer))
   {
     GNUNET_break (0);
@@ -673,7 +681,8 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
 #endif
   ret->id = *peer;
   GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head,
-                                    k_buckets[peer_bucket].tail, ret);
+                                    k_buckets[peer_bucket].tail,
+                                    ret);
   k_buckets[peer_bucket].peers_size++;
   closest_bucket = GNUNET_MAX (closest_bucket, peer_bucket);
   if ((peer_bucket > 0) && (k_buckets[peer_bucket].peers_size <= bucket_size))
@@ -683,14 +692,16 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
     newly_found_peers++;
   }
   GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONTAINER_multipeermap_put (all_known_peers,
-                                                    peer, ret,
+                 GNUNET_CONTAINER_multipeermap_put (all_connected_peers,
+                                                    peer,
+                                                    ret,
                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-  if (1 == GNUNET_CONTAINER_multipeermap_size (all_known_peers) &&
+  if (1 == GNUNET_CONTAINER_multipeermap_size (all_connected_peers) &&
       (GNUNET_YES != disable_try_connect))
   {
     /* got a first connection, good time to start with FIND PEER requests... */
-    find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
+    find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message,
+                                               NULL);
   }
 }
 
@@ -718,7 +729,8 @@ handle_core_disconnect (void *cls,
              "Disconnected %s\n",
               GNUNET_i2s (peer));
   to_remove =
-      GNUNET_CONTAINER_multipeermap_get (all_known_peers, peer);
+      GNUNET_CONTAINER_multipeermap_get (all_connected_peers,
+                                         peer);
   if (NULL == to_remove)
   {
     GNUNET_break (0);
@@ -727,13 +739,13 @@ handle_core_disconnect (void *cls,
   GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), -1,
                             GNUNET_NO);
   GNUNET_assert (GNUNET_YES ==
-                 GNUNET_CONTAINER_multipeermap_remove (all_known_peers,
+                 GNUNET_CONTAINER_multipeermap_remove (all_connected_peers,
                                                        peer,
                                                        to_remove));
-  if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task)
+  if (NULL != to_remove->preference_task)
   {
     GNUNET_SCHEDULER_cancel (to_remove->preference_task);
-    to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK;
+    to_remove->preference_task = NULL;
   }
   GNUNET_CRYPTO_hash (peer,
                      sizeof (struct GNUNET_PeerIdentity),
@@ -789,6 +801,11 @@ core_transmit_notify (void *cls, size_t size, void *buf)
   while ((NULL != (pending = peer->head)) &&
          (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us))
   {
+    GNUNET_STATISTICS_update (GDS_stats,
+                              gettext_noop
+                              ("# Messages dropped (CORE timeout)"),
+                              1,
+                              GNUNET_NO);
     peer->pending_count--;
     GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
     GNUNET_free (pending);
@@ -821,17 +838,25 @@ core_transmit_notify (void *cls, size_t size, void *buf)
     memcpy (&cbuf[off], pending->msg, msize);
     off += msize;
     peer->pending_count--;
-    GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
+    GNUNET_CONTAINER_DLL_remove (peer->head,
+                                peer->tail,
+                                pending);
     GNUNET_free (pending);
   }
-  if (peer->head != NULL)
+  if (NULL != (pending = peer->head))
   {
+    /* technically redundant, but easier to read and
+       avoids bogus gcc warning... */
+    msize = ntohs (pending->msg->size);
     peer->th =
-        GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO,
-                                           GNUNET_CORE_PRIO_BEST_EFFORT,
-                                           GNUNET_TIME_absolute_get_remaining
-                                           (pending->timeout), &peer->id, msize,
-                                           &core_transmit_notify, peer);
+      GNUNET_CORE_notify_transmit_ready (core_api,
+                                        GNUNET_NO,
+                                        GNUNET_CORE_PRIO_BEST_EFFORT,
+                                        GNUNET_TIME_absolute_get_remaining (pending->timeout),
+                                        &peer->id,
+                                        msize,
+                                        &core_transmit_notify,
+                                        peer);
     GNUNET_break (NULL != peer->th);
   }
   return off;
@@ -1219,9 +1244,12 @@ get_target_peers (const struct GNUNET_HashCode *key,
     GNUNET_CONTAINER_bloomfilter_add (bloom, &nhash);
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Selected %u/%u peers at hop %u for %s (target was %u)\n", off,
-              GNUNET_CONTAINER_multipeermap_size (all_known_peers),
-              (unsigned int) hop_count, GNUNET_h2s (key), ret);
+              "Selected %u/%u peers at hop %u for %s (target was %u)\n",
+              off,
+              GNUNET_CONTAINER_multipeermap_size (all_connected_peers),
+              (unsigned int) hop_count,
+              GNUNET_h2s (key),
+              ret);
   if (0 == off)
   {
     GNUNET_free (rtargets);
@@ -1256,8 +1284,9 @@ get_target_peers (const struct GNUNET_HashCode *key,
  * @param put_path peers this request has traversed so far (if tracked)
  * @param data payload to store
  * @param data_size number of bytes in @a data
+ * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not
  */
-void
+int
 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
                            enum GNUNET_DHT_RouteOption options,
                            uint32_t desired_replication_level,
@@ -1278,6 +1307,7 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
   struct PeerPutMessage *ppm;
   struct GNUNET_PeerIdentity *pp;
   struct GNUNET_HashCode thash;
+  unsigned int skip_count;
 
   GNUNET_assert (NULL != bf);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1295,34 +1325,38 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
                 "Routing PUT for %s terminates after %u hops at %s\n",
                 GNUNET_h2s (key), (unsigned int) hop_count,
                 GNUNET_i2s (&my_identity));
-    return;
+    return GNUNET_NO;
   }
   msize =
       put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
       sizeof (struct PeerPutMessage);
-  if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
   {
     put_path_length = 0;
     msize = data_size + sizeof (struct PeerPutMessage);
   }
-  if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+  if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
   {
     GNUNET_break (0);
     GNUNET_free (targets);
-    return;
+    return GNUNET_NO;
   }
   GNUNET_STATISTICS_update (GDS_stats,
                             gettext_noop
                             ("# PUT messages queued for transmission"),
                             target_count, GNUNET_NO);
+  skip_count = 0;
   for (i = 0; i < target_count; i++)
   {
     target = targets[i];
     if (target->pending_count >= MAXIMUM_PENDING_PER_PEER)
     {
-      GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
+      /* skip */
+      GNUNET_STATISTICS_update (GDS_stats,
+                                gettext_noop ("# P2P messages dropped due to full queue"),
                                1, GNUNET_NO);
-      continue; /* skip */
+      skip_count++;
+      continue;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Routing PUT for %s after %u hops to %s\n", GNUNET_h2s (key),
@@ -1360,6 +1394,7 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
     process_peer_queue (target);
   }
   GNUNET_free (targets);
+  return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO;
 }
 
 
@@ -1379,8 +1414,9 @@ GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
  * @param reply_bf bloomfilter to filter duplicates
  * @param reply_bf_mutator mutator for @a reply_bf
  * @param peer_bf filter for peers not to select (again)
+ * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not
  */
-void
+int
 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
                            enum GNUNET_DHT_RouteOption options,
                            uint32_t desired_replication_level,
@@ -1400,6 +1436,7 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
   char *xq;
   size_t reply_bf_size;
   struct GNUNET_HashCode thash;
+  unsigned int skip_count;
 
   GNUNET_assert (NULL != peer_bf);
   GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# GET requests routed"),
@@ -1417,7 +1454,7 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
                 "Routing GET for %s terminates after %u hops at %s\n",
                 GNUNET_h2s (key), (unsigned int) hop_count,
                 GNUNET_i2s (&my_identity));
-    return;
+    return GNUNET_NO;
   }
   reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
   msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
@@ -1425,21 +1462,25 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
   {
     GNUNET_break (0);
     GNUNET_free (targets);
-    return;
+    return GNUNET_NO;
   }
   GNUNET_STATISTICS_update (GDS_stats,
                             gettext_noop
                             ("# GET messages queued for transmission"),
                             target_count, GNUNET_NO);
   /* forward request */
+  skip_count = 0;
   for (i = 0; i < target_count; i++)
   {
     target = targets[i];
     if (target->pending_count >= MAXIMUM_PENDING_PER_PEER)
     {
-      GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
+      /* skip */
+      GNUNET_STATISTICS_update (GDS_stats,
+                                gettext_noop ("# P2P messages dropped due to full queue"),
                                1, GNUNET_NO);
-      continue; /* skip */
+      skip_count++;
+      continue;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Routing GET for %s after %u hops to %s\n", GNUNET_h2s (key),
@@ -1481,6 +1522,7 @@ GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
     process_peer_queue (target);
   }
   GNUNET_free (targets);
+  return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO;
 }
 
 
@@ -1504,12 +1546,13 @@ void
 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
                              enum GNUNET_BLOCK_Type type,
                              struct GNUNET_TIME_Absolute expiration_time,
-                             const struct GNUNET_HashCode * key,
+                             const struct GNUNET_HashCode *key,
                              unsigned int put_path_length,
                              const struct GNUNET_PeerIdentity *put_path,
                              unsigned int get_path_length,
                              const struct GNUNET_PeerIdentity *get_path,
-                             const void *data, size_t data_size)
+                             const void *data,
+                             size_t data_size)
 {
   struct PeerInfo *pi;
   struct P2PPendingMessage *pending;
@@ -1531,7 +1574,8 @@ GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
     GNUNET_break (0);
     return;
   }
-  pi = GNUNET_CONTAINER_multipeermap_get (all_known_peers, target);
+  pi = GNUNET_CONTAINER_multipeermap_get (all_connected_peers,
+                                          target);
   if (NULL == pi)
   {
     /* peer disconnected in the meantime, drop reply */
@@ -1601,8 +1645,7 @@ core_init (void *cls,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
-handle_dht_p2p_put (void *cls,
-                   const struct GNUNET_PeerIdentity *peer,
+handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
                     const struct GNUNET_MessageHeader *message)
 {
   const struct PeerPutMessage *put;
@@ -1615,6 +1658,7 @@ handle_dht_p2p_put (void *cls,
   struct GNUNET_CONTAINER_BloomFilter *bf;
   struct GNUNET_HashCode test_key;
   struct GNUNET_HashCode phash;
+  int forwarded;
 
   msize = ntohs (message->size);
   if (msize < sizeof (struct PeerPutMessage))
@@ -1645,6 +1689,7 @@ handle_dht_p2p_put (void *cls,
   payload_size =
       msize - (sizeof (struct PeerPutMessage) +
                putlen * sizeof (struct GNUNET_PeerIdentity));
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PUT for `%s' from %s\n",
               GNUNET_h2s (&put->key), GNUNET_i2s (peer));
   GNUNET_CRYPTO_hash (peer, sizeof (struct GNUNET_PeerIdentity), &phash);
@@ -1654,7 +1699,7 @@ handle_dht_p2p_put (void *cls,
 
     tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
     LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
-                 "XDHT PUT %s: %s->%s (%u, %u=>%u)\n",
+                 "R5N PUT %s: %s->%s (%u, %u=>%u)\n",
                  GNUNET_h2s (&put->key), GNUNET_i2s (peer), tmp,
                  ntohl(put->hop_count),
                  GNUNET_CRYPTO_hash_matching_bits (&phash, &put->key),
@@ -1689,6 +1734,7 @@ handle_dht_p2p_put (void *cls,
   {
     switch (GNUNET_BLOCK_evaluate (GDS_block_context,
                                    ntohl (put->type),
+                                   GNUNET_BLOCK_EO_NONE,
                                    NULL,    /* query */
                                    NULL, 0, /* bloom filer */
                                    NULL, 0, /* xquery */
@@ -1738,13 +1784,16 @@ handle_dht_p2p_put (void *cls,
                                 (put->expiration_time), &put->key, putlen, pp,
                                 ntohl (put->type), payload_size, payload);
     /* route to other peers */
-    GDS_NEIGHBOURS_handle_put (ntohl (put->type), options,
-                               ntohl (put->desired_replication_level),
-                               GNUNET_TIME_absolute_ntoh (put->expiration_time),
-                               ntohl (put->hop_count), bf, &put->key, putlen,
-                               pp, payload, payload_size);
+    forwarded = GDS_NEIGHBOURS_handle_put (ntohl (put->type), options,
+                                           ntohl (put->desired_replication_level),
+                                           GNUNET_TIME_absolute_ntoh (put->expiration_time),
+                                           ntohl (put->hop_count), bf,
+                                           &put->key, putlen,
+                                           pp, payload, payload_size);
     /* notify monitoring clients */
-    GDS_CLIENTS_process_put (options,
+    GDS_CLIENTS_process_put (options
+                             | (GNUNET_OK == forwarded)
+                             ? GNUNET_DHT_RO_LAST_HOP : 0,
                              ntohl (put->type),
                              ntohl (put->hop_count),
                              ntohl (put->desired_replication_level),
@@ -1863,7 +1912,8 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
-handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
+handle_dht_p2p_get (void *cls,
+                    const struct GNUNET_PeerIdentity *peer,
                     const struct GNUNET_MessageHeader *message)
 {
   struct PeerGetMessage *get;
@@ -1877,6 +1927,7 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct GNUNET_CONTAINER_BloomFilter *peer_bf;
   const char *xquery;
   struct GNUNET_HashCode phash;
+  int forwarded;
 
   GNUNET_break (0 !=
                 memcmp (peer, &my_identity,
@@ -1906,20 +1957,21 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_STATISTICS_update (GDS_stats,
                             gettext_noop ("# P2P GET bytes received"), msize,
                             GNUNET_NO);
-  GNUNET_CRYPTO_hash (peer, sizeof (struct GNUNET_PeerIdentity), &phash);
+  GNUNET_CRYPTO_hash (peer,
+                      sizeof (struct GNUNET_PeerIdentity),
+                      &phash);
   if (GNUNET_YES == log_route_details_stderr)
   {
     char *tmp;
 
     tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
     LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
-                 "XDHT GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n",
+                 "R5N GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n",
                  GNUNET_h2s (&get->key), GNUNET_i2s (peer), tmp,
                  ntohl(get->hop_count),
                  GNUNET_CRYPTO_hash_matching_bits (&phash, &get->key),
                  GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, &get->key),
-                 ntohl(get->xquery_size), xquery
-                );
+                 ntohl(get->xquery_size), xquery);
     GNUNET_free (tmp);
   }
 
@@ -1928,8 +1980,16 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
         GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size], reply_bf_size,
                                            GNUNET_CONSTANTS_BLOOMFILTER_K);
   eval =
-      GNUNET_BLOCK_evaluate (GDS_block_context, type, &get->key, &reply_bf,
-                             get->bf_mutator, xquery, xquery_size, NULL, 0);
+      GNUNET_BLOCK_evaluate (GDS_block_context,
+                             type,
+                             GNUNET_BLOCK_EO_NONE,
+                             &get->key,
+                             &reply_bf,
+                             get->bf_mutator,
+                             xquery,
+                             xquery_size,
+                             NULL,
+                             0);
   if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
   {
     /* request invalid or block type not supported */
@@ -1947,8 +2007,10 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   /* remember request for routing replies */
   GDS_ROUTING_add (peer, type, options, &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),
+  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)) ||
@@ -1976,19 +2038,27 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
                               1, GNUNET_NO);
   }
 
-  GDS_CLIENTS_process_get (options,
+  /* P2P forwarding */
+  forwarded = GNUNET_NO;
+  if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
+    forwarded = GDS_NEIGHBOURS_handle_get (type, options,
+                                           ntohl (get->desired_replication_level),
+                                           ntohl (get->hop_count),
+                                           &get->key,
+                                           xquery,
+                                           xquery_size,
+                                           reply_bf,
+                                           get->bf_mutator, peer_bf);
+  GDS_CLIENTS_process_get (options
+                           | (GNUNET_OK == forwarded)
+                           ? GNUNET_DHT_RO_LAST_HOP : 0,
                            type,
-                           ntohl(get->hop_count),
-                           ntohl(get->desired_replication_level),
+                           ntohl (get->hop_count),
+                           ntohl (get->desired_replication_level),
                            0, NULL,
                            &get->key);
 
-  /* P2P forwarding */
-  if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
-    GDS_NEIGHBOURS_handle_get (type, options,
-                               ntohl (get->desired_replication_level),
-                               ntohl (get->hop_count), &get->key, xquery,
-                               xquery_size, reply_bf, get->bf_mutator, peer_bf);
+
   /* clean up */
   if (NULL != reply_bf)
     GNUNET_CONTAINER_bloomfilter_free (reply_bf);
@@ -2006,7 +2076,8 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @return #GNUNET_YES (do not cut p2p connection)
  */
 static int
-handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
+handle_dht_p2p_result (void *cls,
+                       const struct GNUNET_PeerIdentity *peer,
                        const struct GNUNET_MessageHeader *message)
 {
   const struct PeerResultMessage *prm;
@@ -2059,13 +2130,13 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
     char *tmp;
 
     tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
-    LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "XDHT RESULT %s: %s->%s (%u)\n",
+    LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "R5N RESULT %s: %s->%s (%u)\n",
                  GNUNET_h2s (&prm->key), GNUNET_i2s (peer), tmp,
                  get_path_length + 1);
     GNUNET_free (tmp);
   }
   /* if we got a HELLO, consider it for our own routing table */
-  if (type == GNUNET_BLOCK_TYPE_DHT_HELLO)
+  if (GNUNET_BLOCK_TYPE_DHT_HELLO == type)
   {
     const struct GNUNET_MessageHeader *h;
     struct GNUNET_PeerIdentity pid;
@@ -2084,24 +2155,35 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
       return GNUNET_YES;
     }
     if (GNUNET_OK !=
-        GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h, &pid))
+        GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h,
+                             &pid))
     {
       GNUNET_break_op (0);
       return GNUNET_YES;
     }
-    if ((GNUNET_YES != disable_try_connect) &&
-        0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
+    if ( (GNUNET_YES != disable_try_connect) &&
+         (0 != memcmp (&my_identity,
+                       &pid,
+                       sizeof (struct GNUNET_PeerIdentity))) )
     {
       struct GNUNET_HashCode pid_hash;
 
-      GNUNET_CRYPTO_hash (&pid, sizeof (struct GNUNET_PeerIdentity), &pid_hash);
+      GNUNET_CRYPTO_hash (&pid,
+                          sizeof (struct GNUNET_PeerIdentity),
+                          &pid_hash);
       bucket = find_bucket (&pid_hash);
-      if ((bucket >= 0) &&
-          (k_buckets[bucket].peers_size < bucket_size) &&
-          (NULL != GDS_transport_handle))
+      if ( (bucket >= 0) &&
+           (k_buckets[bucket].peers_size < bucket_size) &&
+           (NULL != GDS_transport_handle) )
       {
-        GNUNET_TRANSPORT_offer_hello (GDS_transport_handle, h, NULL, NULL);
-        GNUNET_TRANSPORT_try_connect (GDS_transport_handle, &pid, NULL, NULL); /*FIXME TRY_CONNECT change */
+        GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
+                                      h,
+                                      NULL,
+                                      NULL);
+        GNUNET_TRANSPORT_try_connect (GDS_transport_handle,
+                                      &pid,
+                                      NULL,
+                                      NULL); /*FIXME TRY_CONNECT change */
       }
     }
   }
@@ -2178,14 +2260,18 @@ GDS_NEIGHBOURS_init ()
 
   log_route_details_stderr =
     (NULL != getenv("GNUNET_DHT_ROUTE_DEBUG")) ? GNUNET_YES : GNUNET_NO;
-  atsAPI = GNUNET_ATS_performance_init (GDS_cfg, NULL, NULL);
+  ats_perf = GNUNET_ATS_performance_init (GDS_cfg,
+                                          NULL,
+                                          NULL);
+  ats_ch = GNUNET_ATS_connectivity_init (GDS_cfg);
   core_api =
       GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
                            &handle_core_disconnect, NULL, GNUNET_NO, NULL,
                            GNUNET_NO, core_handlers);
   if (core_api == NULL)
     return GNUNET_SYSERR;
-  all_known_peers = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
+  all_connected_peers = GNUNET_CONTAINER_multipeermap_create (256,
+                                                          GNUNET_NO);
   return GNUNET_OK;
 }
 
@@ -2200,15 +2286,17 @@ GDS_NEIGHBOURS_done ()
     return;
   GNUNET_CORE_disconnect (core_api);
   core_api = NULL;
-  GNUNET_ATS_performance_done (atsAPI);
-  atsAPI = NULL;
-  GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (all_known_peers));
-  GNUNET_CONTAINER_multipeermap_destroy (all_known_peers);
-  all_known_peers = NULL;
-  if (GNUNET_SCHEDULER_NO_TASK != find_peer_task)
+  GNUNET_ATS_performance_done (ats_perf);
+  ats_perf = NULL;
+  GNUNET_ATS_connectivity_done (ats_ch);
+  ats_ch = NULL;
+  GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (all_connected_peers));
+  GNUNET_CONTAINER_multipeermap_destroy (all_connected_peers);
+  all_connected_peers = NULL;
+  if (NULL != find_peer_task)
   {
     GNUNET_SCHEDULER_cancel (find_peer_task);
-    find_peer_task = GNUNET_SCHEDULER_NO_TASK;
+    find_peer_task = NULL;
   }
 }