hopefull stop strict kademlia from being breadth first search of network
[oweals/gnunet.git] / src / dht / gnunet-service-dht.c
index 1201b5a2f34659d1d115bd7050f68b3166f4d020..fd9694fee4119529fb968af014c769f0b42f51ce 100644 (file)
@@ -38,6 +38,7 @@
 #include "gnunet_transport_service.h"
 #include "gnunet_hello_lib.h"
 #include "gnunet_dht_service.h"
+#include "gnunet_statistics_service.h"
 #include "dhtlog.h"
 #include "dht.h"
 
  */
 #define MINIMUM_PEER_THRESHOLD 20
 
-#define DHT_DEFAULT_FIND_PEER_REPLICATION 20
+#define DHT_DEFAULT_FIND_PEER_REPLICATION 10
 
 #define DHT_DEFAULT_FIND_PEER_OPTIONS GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
 
-#define DHT_DEFAULT_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1)
+#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1)
+#define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
+
+/**
+ * How many initial requests to send out (in true Kademlia fashion)
+ */
+#define DHT_KADEMLIA_REPLICATION 3
+
+/*
+ * Default frequency for sending malicious get messages
+ */
+#define DEFAULT_MALICIOUS_GET_FREQUENCY 1000 /* Number of milliseconds */
+
+/**
+ * Type for a malicious request, so we can ignore it during testing
+ */
+#define DHT_MALICIOUS_MESSAGE_TYPE 42
+/*
+ * Default frequency for sending malicious put messages
+ */
+#define DEFAULT_MALICIOUS_PUT_FREQUENCY 1000 /* Default is in milliseconds */
 
 #define DHT_DEFAULT_PING_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1)
 
@@ -112,6 +133,7 @@ struct P2PPendingMessage
 
 };
 
+
 /**
  * Per-peer information.
  */
@@ -416,6 +438,13 @@ struct DHTResults
 
 };
 
+/**
+ * Don't use our routing algorithm, always route
+ * to closest peer; initially send requests to 3
+ * peers.
+ */
+static int strict_kademlia;
+
 /**
  * Routing option to end routing when closest peer found.
  */
@@ -436,6 +465,11 @@ static struct DHTResults forward_list;
  */
 static struct GNUNET_DATACACHE_Handle *datacache;
 
+/**
+ * Handle for the statistics service.
+ */
+struct GNUNET_STATISTICS_Handle *stats;
+
 /**
  * The main scheduler to use for the DHT service
  */
@@ -519,6 +553,28 @@ static unsigned int debug_routes;
  */
 static unsigned int debug_routes_extended;
 
+/*
+ * GNUNET_YES or GNUNET_NO, whether or not to act as
+ * a malicious node which drops all messages
+ */
+static unsigned int malicious_dropper;
+
+/*
+ * GNUNET_YES or GNUNET_NO, whether or not to act as
+ * a malicious node which sends out lots of GETS
+ */
+static unsigned int malicious_getter;
+
+/*
+ * GNUNET_YES or GNUNET_NO, whether or not to act as
+ * a malicious node which sends out lots of PUTS
+ */
+static unsigned int malicious_putter;
+
+static unsigned long long malicious_get_frequency;
+
+static unsigned long long malicious_put_frequency;
+
 /**
  * Forward declaration.
  */
@@ -528,6 +584,15 @@ static size_t send_generic_reply (void *cls, size_t size, void *buf);
 size_t core_transmit_notify (void *cls,
                              size_t size, void *buf);
 
+static void
+increment_stats(const char *value)
+{
+  if (stats != NULL)
+    {
+      GNUNET_STATISTICS_update (stats, value, 1, GNUNET_NO);
+    }
+}
+
 /**
  *  Try to send another message from our core send list
  */
@@ -582,6 +647,7 @@ static void forward_result_message (void *cls,
   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;
@@ -1087,6 +1153,7 @@ static void forward_message (void *cls,
   size_t msize;
   size_t psize;
 
+  increment_stats(STAT_ROUTE_FORWARDS);
   msize = sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs(msg->size);
   GNUNET_assert(msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
   psize = sizeof(struct P2PPendingMessage) + msize;
@@ -1348,6 +1415,8 @@ static int consider_peer (struct GNUNET_PeerIdentity *peer)
 {
   int bucket;
 
+  if (GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey))
+    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)))
     return GNUNET_YES;
@@ -1373,6 +1442,7 @@ static int route_result_message(void *cls,
   struct PeerInfo *peer_info;
   const struct GNUNET_MessageHeader *hello_msg;
 
+  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.
@@ -1390,6 +1460,7 @@ static int route_result_message(void *cls,
       }
       else /* We have a valid hello, and peer id stored in new_peer */
       {
+        increment_stats(STAT_FIND_PEER_REPLY);
         if (GNUNET_YES == consider_peer(&new_peer))
         {
           GNUNET_TRANSPORT_offer_hello(transport_handle, hello_msg);
@@ -1398,9 +1469,13 @@ static int route_result_message(void *cls,
           /* FIXME: Do we need this (peer_request_connect call)??? */
         }
       }
-
     }
-  record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, message_context->key);
+
+  if (malicious_dropper == GNUNET_YES)
+    record = NULL;
+  else
+    record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, message_context->key);
+
   if (record == NULL) /* No record of this message! */
     {
 #if DEBUG_DHT
@@ -1449,6 +1524,10 @@ static int route_result_message(void *cls,
                                            message_context->peer, NULL);
             }
 #endif
+          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);
         }
       else /* Send to peer */
@@ -1537,6 +1616,7 @@ datacache_get_iterator (void *cls,
   new_msg_ctx->peer = &my_identity;
   new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
   new_msg_ctx->hop_count = 0;
+  increment_stats(STAT_GET_RESPONSE_START);
   route_result_message(cls, &get_result->header, new_msg_ctx);
   GNUNET_free(new_msg_ctx);
   //send_reply_to_client (datacache_get_ctx->client, &get_result->header,
@@ -1575,12 +1655,15 @@ handle_dht_get (void *cls,
   get_type = ntohs (get_msg->type);
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "`%s:%s': Received `%s' request from client, message type %u, key %s, uid %llu\n", my_short_id,
+              "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n", my_short_id,
               "DHT", "GET", get_type, GNUNET_h2s (message_context->key),
               message_context->unique_id);
 #endif
-
+  increment_stats(STAT_GETS);
   results = 0;
+  if (get_type == DHT_MALICIOUS_MESSAGE_TYPE)
+    return results;
+
   if (datacache != NULL)
     results =
       GNUNET_DATACACHE_get (datacache, message_context->key, get_type,
@@ -1685,6 +1768,7 @@ handle_dht_find_peer (void *cls,
   new_msg_ctx->peer = &my_identity;
   new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
   new_msg_ctx->hop_count = 0;
+  increment_stats(STAT_FIND_PEER_ANSWER);
   route_result_message(cls, find_peer_result, new_msg_ctx);
   GNUNET_free(new_msg_ctx);
 #if DEBUG_DHT_ROUTING
@@ -1718,8 +1802,14 @@ handle_dht_put (void *cls,
 
   GNUNET_assert (ntohs (msg->size) >=
                  sizeof (struct GNUNET_DHT_PutMessage));
+
+
   put_msg = (struct GNUNET_DHT_PutMessage *)msg;
   put_type = ntohs (put_msg->type);
+
+  if (put_type == DHT_MALICIOUS_MESSAGE_TYPE)
+    return;
+
   data_size = ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
 #if DEBUG_DHT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1727,6 +1817,28 @@ handle_dht_put (void *cls,
               my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (message_context->key), message_context->unique_id);
 #endif
 #if DEBUG_DHT_ROUTING
+  if (message_context->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);
+        }
+    }
+#endif
+
+  if (message_context->closest != GNUNET_YES)
+    return;
+
+#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,
+                                   NULL);
+    }
 
   if ((debug_routes) && (dhtlog_handle != NULL))
     {
@@ -1736,6 +1848,7 @@ handle_dht_put (void *cls,
     }
 #endif
 
+  increment_stats(STAT_PUTS_INSERTED);
   if (datacache != NULL)
     GNUNET_DATACACHE_put (datacache, message_context->key, data_size,
                           (char *) &put_msg[1], put_type,
@@ -1779,6 +1892,20 @@ get_forward_count (unsigned int hop_count, size_t target_replication)
   unsigned int target_value;
   unsigned int diameter;
 
+  /**
+   * If we are behaving in strict kademlia mode, send multiple initial requests,
+   * but then only send to 1 or 0 peers.
+   */
+  if (strict_kademlia == GNUNET_YES)
+    {
+      if (hop_count == 0)
+        return DHT_KADEMLIA_REPLICATION;
+      else if (hop_count < MAX_HOPS)
+        return 1;
+      else
+        return 0;
+    }
+
   /* FIXME: the smaller we think the network is the more lenient we should be for
    * 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!
@@ -1954,102 +2081,104 @@ select_peer (const GNUNET_HashCode * target,
   unsigned int bc;
   unsigned int count;
   struct PeerInfo *pos;
-#if USE_KADEMLIA
-  const struct PeerInfo *chosen;
+  struct PeerInfo *chosen;
   unsigned long long largest_distance;
-#else
   unsigned long long total_distance;
   unsigned long long selected;
-#endif
 
-#if USE_KADEMLIA
-  largest_distance = 0;
-  chosen = NULL;
-  for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
-    {
-      pos = k_buckets[bc].head;
-      while (pos != NULL)
-        {
-          if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
-            {
-              distance = inverse_distance (target, &pos->id.hashPubKey);
-              if (distance > largest_distance)
-                {
-                  chosen = pos;
-                  largest_distance = distance;
-                }
-            }
-          pos = pos->next;
-        }
-    }
+if (strict_kademlia == GNUNET_YES)
+  {
+    largest_distance = 0;
+    chosen = NULL;
+    for (bc = lowest_bucket; bc < MAX_BUCKETS; 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))
+              {
+                distance = inverse_distance (target, &pos->id.hashPubKey);
+                if (distance > largest_distance)
+                  {
+                    chosen = pos;
+                    largest_distance = distance;
+                  }
+              }
+            count++;
+            pos = pos->next;
+          }
+      }
 
-  if ((largest_distance > 0) && (chosen != NULL))
-    {
-      GNUNET_CONTAINER_bloomfilter_add(bloom, &chosen->id.hashPubKey);
-      return chosen;
-    }
+    if ((largest_distance > 0) && (chosen != NULL))
+      {
+        GNUNET_CONTAINER_bloomfilter_add(bloom, &chosen->id.hashPubKey);
+        return chosen;
+      }
+    else
+      {
+        return NULL;
+      }
+  }
   else
-    {
-      return NULL;
-    }
-#else
-  /* GNUnet-style */
-  total_distance = 0;
-  for (bc = lowest_bucket; bc < MAX_BUCKETS; 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))
-            total_distance += (unsigned long long)inverse_distance (target, &pos->id.hashPubKey);
+  {
+    /* GNUnet-style */
+    total_distance = 0;
+    for (bc = lowest_bucket; bc < MAX_BUCKETS; 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))
+              total_distance += (unsigned long long)inverse_distance (target, &pos->id.hashPubKey);
 #if DEBUG_DHT > 1
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "`%s:%s': Total distance is %llu, distance from %s to %s is %u\n",
-                      my_short_id, "DHT", total_distance, GNUNET_i2s(&pos->id), GNUNET_h2s(target) , inverse_distance(target, &pos->id.hashPubKey));
+            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                        "`%s:%s': Total distance is %llu, distance from %s to %s is %u\n",
+                        my_short_id, "DHT", total_distance, GNUNET_i2s(&pos->id), GNUNET_h2s(target) , inverse_distance(target, &pos->id.hashPubKey));
 #endif
-          pos = pos->next;
-          count++;
-        }
-    }
-  if (total_distance == 0)
-    {
-      return NULL;
-    }
+            pos = pos->next;
+            count++;
+          }
+      }
+    if (total_distance == 0)
+      {
+        return NULL;
+      }
 
-  selected = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, total_distance);
-  for (bc = lowest_bucket; bc < MAX_BUCKETS; 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))
-            {
-              distance = inverse_distance (target, &pos->id.hashPubKey);
-              if (distance > selected)
-                return pos;
-              selected -= distance;
-            }
-          else
-            {
+    selected = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, total_distance);
+    for (bc = lowest_bucket; bc < MAX_BUCKETS; 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))
+              {
+                distance = inverse_distance (target, &pos->id.hashPubKey);
+                if (distance > selected)
+                  return pos;
+                selected -= distance;
+              }
+            else
+              {
 #if DEBUG_DHT
-              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                          "`%s:%s': peer %s matches bloomfilter.\n",
-                          my_short_id, "DHT", GNUNET_i2s(&pos->id));
+                GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                            "`%s:%s': peer %s matches bloomfilter.\n",
+                            my_short_id, "DHT", GNUNET_i2s(&pos->id));
 #endif
-            }
-          pos = pos->next;
-          count++;
-        }
-    }
+              }
+            pos = pos->next;
+            count++;
+          }
+      }
 #if DEBUG_DHT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "`%s:%s': peer %s matches bloomfilter.\n",
-                my_short_id, "DHT", GNUNET_i2s(&pos->id));
-#endif
-  return NULL;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "`%s:%s': peer %s matches bloomfilter.\n",
+                  my_short_id, "DHT", GNUNET_i2s(&pos->id));
 #endif
+    return NULL;
+  }
 }
 
 
@@ -2178,6 +2307,23 @@ static int route_message(void *cls,
   int ret;
 #endif
 
+  if (malicious_dropper == GNUNET_YES)
+    {
+#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);
+        }
+#endif
+      if (message_context->bloom != NULL)
+        GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
+      return 0;
+    }
+
+  increment_stats(STAT_ROUTES);
   message_context->closest = am_closest_peer(message_context->key);
   forward_count = get_forward_count(message_context->hop_count, message_context->replication);
   nearest = find_closest_peer(message_context->key);
@@ -2186,7 +2332,8 @@ static int route_message(void *cls,
     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 ((stop_on_closest == GNUNET_YES) && (message_context->closest == GNUNET_YES) && (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
+  if (((stop_on_closest == GNUNET_YES) && (message_context->closest == GNUNET_YES) && (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
+      || ((strict_kademlia == GNUNET_YES) && (message_context->closest == GNUNET_YES)))
     forward_count = 0;
 
 #if DEBUG_DHT_ROUTING
@@ -2212,32 +2359,11 @@ static int route_message(void *cls,
         forward_count = 0;
       break;
     case GNUNET_MESSAGE_TYPE_DHT_PUT: /* Check if closest, if so insert data. FIXME: thresholding to reduce complexity?*/
-      if (message_context->closest == GNUNET_YES)
-        {
-#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,
-                                           NULL);
-            }
-#endif
-          handle_dht_put (cls, msg, message_context);
-        }
-#if DEBUG_DHT_ROUTING
-        if (message_context->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);
-              }
-          }
-#endif
+      increment_stats(STAT_PUTS);
+      handle_dht_put (cls, msg, message_context);
       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 (cls, message_context);
@@ -2265,7 +2391,7 @@ static int route_message(void *cls,
   for (i = 0; i < forward_count; i++)
     {
       selected = select_peer(message_context->key, message_context->bloom);
-      /* FIXME: either log to sql or log to stats or both when selected is NULL at this point! */
+
       if (selected != NULL)
         {
           GNUNET_CONTAINER_bloomfilter_add(message_context->bloom, &selected->id.hashPubKey);
@@ -2276,7 +2402,6 @@ static int route_message(void *cls,
                       "DHT", GNUNET_h2s (message_context->key), message_context->unique_id, GNUNET_i2s(&selected->id), nearest_buf, matching_bits(&nearest->id.hashPubKey, message_context->key), distance(&nearest->id.hashPubKey, message_context->key));
           GNUNET_free(nearest_buf);
 #endif
-          /* FIXME: statistics */
           if ((debug_routes_extended) && (dhtlog_handle != NULL))
             {
               dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
@@ -2337,6 +2462,76 @@ find_active_client (struct GNUNET_SERVER_Client *client)
   return ret;
 }
 
+/**
+ * Task to send a malicious put message across the network.
+ *
+ * @param cls closure for this task
+ * @param tc the context under which the task is running
+ */
+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 GNUNET_HashCode key;
+  unsigned int mcsize;
+  uint32_t random_key;
+  put_message.header.size = htons(sizeof(struct GNUNET_DHT_GetMessage));
+  put_message.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_PUT);
+  put_message.type = htons(DHT_MALICIOUS_MESSAGE_TYPE);
+  put_message.expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_forever());
+  mcsize = sizeof(struct DHT_MessageContext) + sizeof(GNUNET_HashCode);
+  memset(&message_context, 0, sizeof(struct DHT_MessageContext));
+  message_context.client = NULL;
+  random_key = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t)-1);
+  GNUNET_CRYPTO_hash(&random_key, sizeof(uint32_t), &key);
+  message_context.key = &key;
+  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;
+  increment_stats(STAT_PUT_START);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Sending malicious PUT message with hash %s", my_short_id, "DHT", GNUNET_h2s(&key));
+  route_message(NULL, &put_message.header, &message_context);
+  GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, malicious_put_frequency), &malicious_put_task, NULL);
+
+}
+
+/**
+ * Task to send a malicious put message across the network.
+ *
+ * @param cls closure for this task
+ * @param tc the context under which the task is running
+ */
+static void
+malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  static struct GNUNET_DHT_GetMessage get_message;
+  static struct DHT_MessageContext message_context;
+  static GNUNET_HashCode key;
+  unsigned int mcsize;
+  uint32_t random_key;
+  get_message.header.size = htons(sizeof(struct GNUNET_DHT_GetMessage));
+  get_message.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_GET);
+  get_message.type = htons(DHT_MALICIOUS_MESSAGE_TYPE);
+  mcsize = sizeof(struct DHT_MessageContext) + sizeof(GNUNET_HashCode);
+  memset(&message_context, 0, sizeof(struct DHT_MessageContext));
+  message_context.client = NULL;
+  random_key = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t)-1);
+  GNUNET_CRYPTO_hash(&random_key, sizeof(uint32_t), &key);
+  message_context.key = &key;
+  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;
+  increment_stats(STAT_GET_START);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Sending malicious GET message with hash %s", my_short_id, "DHT", GNUNET_h2s(&key));
+  route_message(NULL, &get_message.header, &message_context);
+  GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, malicious_get_frequency), &malicious_get_task, NULL);
+}
+
 /**
  * Task to send a find peer message for our own peer identifier
  * so that we can find the closest peers in the network to ourselves
@@ -2356,6 +2551,8 @@ send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
 
+  increment_stats(STAT_FIND_PEER_START);
+
   find_peer_msg = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
   find_peer_msg->size = htons(sizeof(struct GNUNET_MessageHeader));
   find_peer_msg->type = htons(GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
@@ -2372,8 +2569,9 @@ send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "`%s:%s': Sent `%s' request to %d peers\n", my_short_id, "DHT",
               "FIND PEER", ret);
-  next_send_time = DHT_DEFAULT_FIND_PEER_INTERVAL;
-  next_send_time.value = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, next_send_time.value * 3);
+  next_send_time.value = DHT_MINIMUM_FIND_PEER_INTERVAL.value +
+                         GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
+                                                  DHT_MAXIMUM_FIND_PEER_INTERVAL.value - DHT_MINIMUM_FIND_PEER_INTERVAL.value);
   GNUNET_SCHEDULER_add_delayed (sched,
                                 next_send_time,
                                 &send_find_peer_message, NULL);
@@ -2419,12 +2617,69 @@ handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client,
   message_context.network_size = estimate_diameter();
   message_context.peer = &my_identity;
 
+  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);
+
   route_message(cls, enc_msg, &message_context);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 
 }
 
+/**
+ * Handler for any locally received DHT control messages,
+ * sets malicious flags mostly for now.
+ *
+ * @param cls closure for the service
+ * @param client the client we received this message from
+ * @param message the actual message received
+ *
+ */
+static void
+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;
+#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));
+#endif
+
+  switch (ntohs(dht_control_msg->command))
+  {
+  case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
+    if (ntohs(dht_control_msg->variable) > 0)
+      malicious_get_frequency = ntohs(dht_control_msg->variable);
+    if (malicious_getter != GNUNET_YES)
+      GNUNET_SCHEDULER_add_now(sched, &malicious_get_task, NULL);
+    malicious_getter = GNUNET_YES;
+    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%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_putter != GNUNET_YES)
+      GNUNET_SCHEDULER_add_now(sched, &malicious_put_task, NULL);
+    malicious_putter = GNUNET_YES;
+    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Initiating malicious PUT behavior, frequency %d\n", my_short_id, "DHT", malicious_put_frequency);
+    break;
+  case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
+    malicious_dropper = GNUNET_YES;
+    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Initiating malicious DROP behavior\n", my_short_id, "DHT");
+    break;
+  default:
+    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Unknown control command type `%d'!\n", ntohs(dht_control_msg->command));
+  }
+
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
 /**
  * Handler for any generic DHT stop messages, calls the appropriate handler
  * depending on message type (if processed locally)
@@ -2541,6 +2796,7 @@ handle_dht_p2p_route_result (void *cls,
       GNUNET_break_op(0);
       return GNUNET_YES;
     }
+
   memset(&message_context, 0, 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);
@@ -2576,6 +2832,7 @@ process_hello (void *cls, const struct GNUNET_MessageHeader *message)
   memcpy(my_hello, message, ntohs(message->size));
 }
 
+
 /**
  * Task run during shutdown.
  *
@@ -2587,7 +2844,6 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   int bucket_count;
   struct PeerInfo *pos;
-
   if (transport_handle != NULL)
   {
     GNUNET_free_non_null(my_hello);
@@ -2624,6 +2880,11 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_DATACACHE_destroy (datacache);
     }
 
+  if (stats != NULL)
+    {
+      GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
+    }
+
   if (dhtlog_handle != NULL)
     GNUNET_DHTLOG_disconnect(dhtlog_handle);
 
@@ -2662,8 +2923,11 @@ core_init (void *cls,
               "%s: Core connection initialized, I am peer: %s\n", "dht",
               GNUNET_i2s (identity));
 #endif
+
   /* 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));
   /* Set the server to local variable */
   coreAPI = server;
@@ -2676,6 +2940,7 @@ 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_control_message, NULL, GNUNET_MESSAGE_TYPE_DHT_CONTROL, 0},
   {NULL, NULL, 0, 0}
 };
 
@@ -2705,6 +2970,13 @@ void handle_core_connect (void *cls,
   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);
 #endif
+
+  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));
+      return;
+    }
+
   if (datacache != NULL)
     GNUNET_DATACACHE_put(datacache, &peer->hashPubKey, sizeof(struct GNUNET_PeerIdentity), (const char *)peer, 0, GNUNET_TIME_absolute_get_forever());
   ret = try_add_peer(peer,
@@ -2736,6 +3008,11 @@ void handle_core_disconnect (void *cls,
 
   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))
+    {
+      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;
+    }
   GNUNET_assert(GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey));
   to_remove = GNUNET_CONTAINER_multihashmap_get(all_known_peers, &peer->hashPubKey);
   GNUNET_assert(0 == memcmp(peer, &to_remove->id, sizeof(struct GNUNET_PeerIdentity)));
@@ -2768,7 +3045,7 @@ run (void *cls,
                                  NULL,  /* Closure passed to DHT functionas around? */
                                  &core_init,    /* Call core_init once connected */
                                  &handle_core_connect,  /* Handle connects */
-                                 &handle_core_disconnect,  /* FIXME: 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 */
@@ -2795,6 +3072,13 @@ run (void *cls,
       debug_routes = GNUNET_YES;
     }
 
+  if (GNUNET_YES ==
+      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"))
@@ -2809,6 +3093,35 @@ run (void *cls,
       stop_on_found = GNUNET_YES;
     }
 
+  if (GNUNET_YES ==
+      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 = DEFAULT_MALICIOUS_GET_FREQUENCY;
+    }
+
+  if (GNUNET_YES ==
+      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 = DEFAULT_MALICIOUS_PUT_FREQUENCY;
+    }
+
+  if (GNUNET_YES ==
+          GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
+                                               "malicious_dropper"))
+    {
+      malicious_dropper = GNUNET_YES;
+    }
+
   if (GNUNET_YES ==
       GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing",
                                            "mysql_logging_extended"))
@@ -2827,10 +3140,31 @@ run (void *cls,
         }
     }
 
+  stats = GNUNET_STATISTICS_create(sched, "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_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_GET_REPLY, 0, GNUNET_NO);
+      GNUNET_STATISTICS_set(stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO);
+    }
 #if DO_FIND_PEER
   random_seconds = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 180);
   GNUNET_SCHEDULER_add_delayed (sched,
-                                GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
+                                GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, random_seconds),
                                 &send_find_peer_message, NULL);
 #endif