gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r9730 - GNUnet/src/applications/dv_dht/module


From: gnunet
Subject: [GNUnet-SVN] r9730 - GNUnet/src/applications/dv_dht/module
Date: Fri, 11 Dec 2009 13:22:20 +0100

Author: grothoff
Date: 2009-12-11 13:22:20 +0100 (Fri, 11 Dec 2009)
New Revision: 9730

Modified:
   GNUnet/src/applications/dv_dht/module/cs.c
   GNUnet/src/applications/dv_dht/module/routing.c
   GNUnet/src/applications/dv_dht/module/table.c
Log:
fixing various bugs in DHT, multipeer-test still fails

Modified: GNUnet/src/applications/dv_dht/module/cs.c
===================================================================
--- GNUnet/src/applications/dv_dht/module/cs.c  2009-12-10 22:52:58 UTC (rev 
9729)
+++ GNUnet/src/applications/dv_dht/module/cs.c  2009-12-11 12:22:20 UTC (rev 
9730)
@@ -33,8 +33,6 @@
 #include "gnunet_dv_dht_service.h"
 #include "service.h"
 
-#define DEBUG_CS GNUNET_NO
-
 /**
  * Global core API.
  */

Modified: GNUnet/src/applications/dv_dht/module/routing.c
===================================================================
--- GNUnet/src/applications/dv_dht/module/routing.c     2009-12-10 22:52:58 UTC 
(rev 9729)
+++ GNUnet/src/applications/dv_dht/module/routing.c     2009-12-11 12:22:20 UTC 
(rev 9730)
@@ -26,6 +26,18 @@
  * TODO:
  * - implement extra_get_callback
  * - use "network_size" field to improve our network size estimate(s)
+ *
+ * NATE:
+ * - I am confused about dhtqueryuid vs. queryuid and where those
+ *   are created / set; their use in route_result was totally
+ *   broken (cls could point to a DV_DHT_MESSAGE *or* a
+ *   unsigned long long queryuid and the old code never knew which...),
+ *   so I've fixed that but the use of 'dhtqueryuid' in there is
+ *   likely still totally broken
+ * - I suspect the bloomfilter is too small (4 bytes now, maybe
+ *   use 32 or 64 bytes?); also, we'll likely need to use
+ *   bloomfilter "mutation" for 0.9.x to make all routes at least
+ *   possible
  */
 
 #include "platform.h"
@@ -401,6 +413,13 @@
 }
 
 
+struct RouteResultContext
+{
+  unsigned long long queryuid;
+  const DV_DHT_MESSAGE *rmsg;
+};
+
+
 /**
  * Given a result, lookup in the routing table
  * where to send it next.
@@ -408,8 +427,11 @@
 static int
 route_result (const GNUNET_HashCode * key,
               unsigned int type,
-              unsigned int size, const char *data, void *cls)
+              unsigned int size,
+             const char *data,
+             void *ctx)
 {
+  struct RouteResultContext *rrc = ctx;
   DV_DHTQueryRecord *q;
   GNUNET_HashCode hc;
   DV_DHT_MESSAGE *result;
@@ -427,8 +449,6 @@
 
 #if DEBUG_ROUTING
   GNUNET_EncName enc;
-  unsigned long long queryuid;
-  unsigned long long *dhtqueryuid_ptr = NULL;
   unsigned long long dhtqueryuid;
 #endif
 
@@ -440,43 +460,18 @@
                  _("%s: DV_DHT-Routing of result for key `%s', type %d.\n"),
                  &shortID, &enc, type);
 #endif
-
-  if (cls != NULL)
+  result = NULL;
+  if (rrc->rmsg != NULL)
     {
-      result = cls;
+      result = GNUNET_malloc (ntohs(rrc->rmsg->header.size));
+      memcpy (result, rrc->rmsg, ntohs(rrc->rmsg->header.size));
+      GNUNET_GE_ASSERT (NULL, ntohs (result->header.type) == 
GNUNET_P2P_PROTO_DHT_RESULT);
 #if DEBUG_ROUTING
       result->hop_count = htonl (ntohl (result->hop_count) + 1);
 #endif
     }
-  if ((cls == NULL)
-      || (ntohs (result->header.type) != GNUNET_P2P_PROTO_DHT_RESULT))
+  else
     {
-#if DEBUG_ROUTING
-
-      if ((cls != NULL)
-          && (ntohs (result->header.type) != GNUNET_P2P_PROTO_DHT_RESULT))
-        {
-          dhtqueryuid_ptr = cls;
-          dhtqueryuid = *dhtqueryuid_ptr;
-          GNUNET_GE_LOG (coreAPI->ectx,
-                         GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER
-                         | GNUNET_GE_BULK,
-                         _
-                         ("%s: cls not null and type is wrong! Got dhtqueryuid 
of %llu\n"),
-                         &shortID, dhtqueryuid);
-          GNUNET_GE_LOG (coreAPI->ectx,
-                         GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER
-                         | GNUNET_GE_BULK,
-                         _("%s: got header type of %d or %d, wanted %d"),
-                         &shortID, ntohs (result->header.type),
-                         result->header.type, GNUNET_P2P_PROTO_DHT_RESULT);
-
-        }
-      else
-        {
-          dhtqueryuid = 0;
-        }
-#endif
       result = GNUNET_malloc (sizeof (DV_DHT_MESSAGE) + size);
       result->header.size = htons (sizeof (DV_DHT_MESSAGE) + size);
       result->header.type = htons (GNUNET_P2P_PROTO_DHT_RESULT);
@@ -487,16 +482,17 @@
       result->key = *key;
       memset (&result->bloomfilter, 0, DV_DHT_BLOOM_SIZE);
 #if DEBUG_ROUTING
+      dhtqueryuid = 0; /* FIXME: why have this? */
       if ((debug_routes) && (dhtlog != NULL))
         {
-          dhtlog->insert_query (&queryuid, dhtqueryuid, DHTLOG_RESULT,
+          dhtlog->insert_query (&rrc->queryuid, dhtqueryuid, DHTLOG_RESULT,
                                 ntohl (result->hop_count), GNUNET_NO,
                                 coreAPI->my_identity, key);
         }
       if (dhtqueryuid != 0)
-        result->queryuid = htonl (dhtqueryuid);
+        result->queryuid = GNUNET_htonll (dhtqueryuid);
       else
-        result->queryuid = htonl (queryuid);
+        result->queryuid = GNUNET_htonll (rrc->queryuid);
 #endif
       memcpy (&result[1], data, size);
     }
@@ -596,8 +592,7 @@
 #if DEBUG_ROUTING
               if ((debug_routes_extended) && (dhtlog != NULL))
                 {
-                  queryuid = ntohl (result->queryuid);
-                  dhtlog->insert_route (NULL, queryuid,
+                  dhtlog->insert_route (NULL, rrc->queryuid,
                                         DHTLOG_RESULT,
                                         ntohl (result->hop_count), cost,
                                         GNUNET_NO, coreAPI->my_identity, key,
@@ -622,16 +617,14 @@
 #if DEBUG_ROUTING
               if ((debug_routes) && (dhtlog != NULL))
                 {
-                  queryuid = ntohl (result->queryuid);
-                  dhtlog->insert_query (NULL, queryuid, DHTLOG_RESULT,
+                  dhtlog->insert_query (NULL, rrc->queryuid, DHTLOG_RESULT,
                                         ntohl (result->hop_count), GNUNET_YES,
                                         coreAPI->my_identity, key);
                 }
 
               if ((debug_routes_extended) && (dhtlog != NULL))
                 {
-                  queryuid = ntohl (result->queryuid);
-                  dhtlog->insert_route (NULL, queryuid,
+                  dhtlog->insert_route (NULL, rrc->queryuid,
                                         DHTLOG_RESULT,
                                         ntohl (result->hop_count), 0,
                                         GNUNET_YES, coreAPI->my_identity, key,
@@ -655,8 +648,7 @@
                  &shortID, routed, tracked, sent_other);
 #endif
   GNUNET_bloomfilter_free (bloom);
-  if (cls == NULL)
-    GNUNET_free (result);
+  GNUNET_free (result);
   return GNUNET_OK;
 }
 
@@ -831,6 +823,7 @@
   int i;
   int j;
   int cost;
+  struct RouteResultContext rrc;
 #if DEBUG_ROUTING
   GNUNET_EncName enc;
   GNUNET_EncName henc;
@@ -881,7 +874,7 @@
       if ((debug_routes) && (dhtlog != NULL))
         {
           hop_count = ntohl (get->hop_count);
-          queryuid = ntohl (get->queryuid);
+          queryuid = GNUNET_ntohll (get->queryuid);
           dhtlog->insert_query (NULL, queryuid, DHTLOG_GET,
                                 hop_count, GNUNET_NO, coreAPI->my_identity,
                                 &get->key);
@@ -891,17 +884,20 @@
     }
 
 #if DEBUG_ROUTING
-  queryuid = ntohl (get->queryuid);
+  rrc.queryuid = GNUNET_ntohll (get->queryuid);
+  rrc.rmsg = NULL;
   total =
     dstore->get (&get->key, ntohl (get->type), &route_result,
-                 (void *) &queryuid);
+                 &rrc);
   GNUNET_GE_LOG (coreAPI->ectx,
                  GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
                  GNUNET_GE_BULK,
                  "Found %d local results for query %s, type %d\n", total,
                  (char *) &enc, ntohl (get->type));
 #else
-  total = dstore->get (&get->key, ntohl (get->type), &route_result, NULL);
+  rrc.queryuid = 0;
+  rrc.rmsg = NULL;
+  total = dstore->get (&get->key, ntohl (get->type), &route_result, &rrc);
 #endif
 
 #if DEBUG_ROUTING
@@ -909,7 +905,7 @@
     {
       if ((debug_routes) && (dhtlog != NULL))
         {
-          queryuid = ntohl (get->queryuid);
+          queryuid = GNUNET_ntohll (get->queryuid);
           hop_count = ntohl (get->hop_count);
           dhtlog->insert_query (NULL, queryuid, DHTLOG_GET,
                                 hop_count, GNUNET_YES, coreAPI->my_identity,
@@ -918,8 +914,8 @@
 
       if ((debug_routes_extended) && (dhtlog != NULL))
         {
-          queryuid = ntohl (get->queryuid);
-          dhtlog->insert_route (NULL, ntohl (get->queryuid), DHTLOG_GET,
+          queryuid = GNUNET_ntohll (get->queryuid);
+          dhtlog->insert_route (NULL, queryuid, DHTLOG_GET,
                                 hop_count, 0, GNUNET_YES,
                                 coreAPI->my_identity, &get->key, sender,
                                 NULL);
@@ -1008,8 +1004,8 @@
 #if DEBUG_ROUTING
       if ((debug_routes_extended) && (dhtlog != NULL))
         {
-          queryuid = ntohl (get->queryuid);
-          dhtlog->insert_route (NULL, ntohl (get->queryuid), DHTLOG_GET,
+          queryuid = GNUNET_ntohll (get->queryuid);
+          dhtlog->insert_route (NULL, queryuid, DHTLOG_GET,
                                 hop_count, cost, GNUNET_NO,
                                 coreAPI->my_identity, &get->key, sender,
                                 &next[j]);
@@ -1074,7 +1070,7 @@
 #if DEBUG_ROUTING
       if ((debug_routes_extended) && (dhtlog != NULL))
         {
-          queryuid = ntohl (put->queryuid);
+          queryuid = GNUNET_ntohll (put->queryuid);
           dhtlog->insert_route (NULL, queryuid, DHTLOG_PUT,
                                 hop_count, 0, GNUNET_NO,
                                 coreAPI->my_identity, &put->key, sender,
@@ -1158,7 +1154,7 @@
 #if DEBUG_ROUTING
       if ((debug_routes_extended) && (dhtlog != NULL))
         {
-          queryuid = ntohl (put->queryuid);
+          queryuid = GNUNET_ntohll (put->queryuid);
           dhtlog->insert_route (NULL, queryuid, DHTLOG_PUT,
                                 hop_count, cost, GNUNET_NO,
                                 coreAPI->my_identity, &put->key, sender,
@@ -1181,7 +1177,7 @@
   if ((store == 0) && (target_value == 0) && (debug_routes_extended)
       && (dhtlog != NULL))
     {
-      queryuid = ntohl (put->queryuid);
+      queryuid = GNUNET_ntohll (put->queryuid);
       dhtlog->insert_route (NULL, queryuid, DHTLOG_PUT,
                             hop_count, 0, GNUNET_NO,
                             coreAPI->my_identity, &put->key, sender, NULL);
@@ -1201,7 +1197,7 @@
 
       if ((debug_routes) && (dhtlog != NULL))
         {
-          queryuid = ntohl (put->queryuid);
+          queryuid = GNUNET_ntohll (put->queryuid);
           dhtlog->insert_query (NULL, queryuid, DHTLOG_PUT,
                                 hop_count, GNUNET_YES,
                                 coreAPI->my_identity, &put->key);
@@ -1209,7 +1205,7 @@
 
       if ((debug_routes_extended) && (dhtlog != NULL))
         {
-          queryuid = ntohl (put->queryuid);
+          queryuid = GNUNET_ntohll (put->queryuid);
           dhtlog->insert_route (NULL, queryuid, DHTLOG_PUT,
                                 hop_count, 0, GNUNET_YES,
                                 coreAPI->my_identity, &put->key, sender,
@@ -1262,6 +1258,7 @@
 #if DEBUG_ROUTING
   GNUNET_EncName enc;
 #endif
+  struct RouteResultContext rrc;
 
   if (ntohs (msg->size) < sizeof (DV_DHT_MESSAGE))
     {
@@ -1287,10 +1284,13 @@
       return GNUNET_OK;
     }
 
+  rrc.queryuid = 0;
+  rrc.rmsg = result;
   route_result (&result->key,
                 ntohl (result->type),
                 ntohs (result->header.size) - sizeof (DV_DHT_MESSAGE),
-                (const char *) &result[1], (void *) msg);
+                (const char *) &result[1],
+               &rrc);
   return GNUNET_OK;
 }
 
@@ -1323,7 +1323,7 @@
                             coreAPI->my_identity, key);
     }
 
-  get.queryuid = htonl (queryuid);
+  get.queryuid = GNUNET_htonll (queryuid);
   GNUNET_hash_to_enc (&get.key, &enc);
   GNUNET_GE_LOG (coreAPI->ectx,
                  GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
@@ -1426,7 +1426,7 @@
                      "%s: Inserted dhtkey, uid: %llu, inserted query, uid: 
%llu\n",
                      &shortID, keyuid, queryuid);
     }
-  put->queryuid = htonl (queryuid);
+  put->queryuid = GNUNET_htonll (queryuid);
 #endif
 
   memcpy (&put[1], data, size);

Modified: GNUnet/src/applications/dv_dht/module/table.c
===================================================================
--- GNUnet/src/applications/dv_dht/module/table.c       2009-12-10 22:52:58 UTC 
(rev 9729)
+++ GNUnet/src/applications/dv_dht/module/table.c       2009-12-11 12:22:20 UTC 
(rev 9730)
@@ -272,11 +272,10 @@
 GNUNET_DV_DHT_estimate_network_diameter ()
 {
   unsigned int i;
+
   for (i = bucketCount - 1; i > 0; i--)
-    {
-      if (buckets[i].peers_size > 0)
-        break;
-    }
+    if (buckets[i].peers_size > 0)
+      break;
   return i + 1;
 }
 
@@ -288,14 +287,10 @@
 get_bit_distance (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2)
 {
   unsigned int i;
-  int diff;
 
   for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++)
-    {
-      diff = GNUNET_hash_get_bit (h1, i) - GNUNET_hash_get_bit (h2, i);
-      if (diff != 0)
-        return i;
-    }
+    if (GNUNET_hash_get_bit (h1, i) != GNUNET_hash_get_bit (h2, i))
+      return i;
   return sizeof (GNUNET_HashCode) * 8;
 }
 
@@ -312,27 +307,9 @@
     return NULL;                /* myself! */
   index = get_bit_distance (&peer->hashPubKey,
                             &coreAPI->my_identity->hashPubKey);
-/* Why are we not returning the bit distance as the bucket we'd like to store
- * this peer in?  If the first bit differs (none match) then it goes in bucket
- * 0.  If more match than we have buckets for it goes in the highest numbered
- * bucket.  Why do all this nonsense?  Also why break in error when we have
- * a peer that matches lots of our bits?  We WANT that peer in our table,
- * but returning NULL and erroring out keeps it far far away from us!
- *
- * ANSWER:
- * A bucket is for more than one bit distance; a bucket can be for a range
- * of bit distances.  I.e., we may have one bucket for 0 bits shared,
- * one bucket for 1 bit shared, one bucket for 2 bits shared, one bucket
- * for 2 or 3 bits shared and one bucket for 4 to N bits shared.
- * Finally, if there are more bits shared than we have buckets for,
- * our bucket initialization was wrong (the entire range was not covered!),
- * so we throw an assertion failure and return NULL --- after all, how
- * can another peer have all the same bits as we do but still be different?
- */
   i = bucketCount - 1;
   while ((buckets[i].bstart > index) && (i > 0))
     i--;
-
   if ((buckets[i].bstart <= index) && (buckets[i].bend >= index))
     return &buckets[i];
   GNUNET_GE_BREAK (NULL, 0);
@@ -351,8 +328,9 @@
   if (bucket == NULL)
     return NULL;
   for (i = 0; i < bucket->peers_size; i++)
-    if (0 ==
-        memcmp (peer, &bucket->peers[i]->id, sizeof (GNUNET_PeerIdentity)))
+    if (0 == memcmp (peer, 
+                    &bucket->peers[i]->id, 
+                    sizeof (GNUNET_PeerIdentity)))
       return bucket->peers[i];
   return NULL;
 }
@@ -367,13 +345,68 @@
   return findPeerEntryInBucket (findBucketFor (peer), peer);
 }
 
+
+
 /**
+ * Compute the distance between have and target as a 32-bit value.
+ * Differences in the lower bits must count stronger than differences
+ * in the higher bits.
+ *
+ * @return 0 if have==target, otherwise a number
+ *           that is larger as the distance between
+ *           the two hash codes inceases
+ */
+static unsigned int
+distance (const GNUNET_HashCode * target,
+         const GNUNET_HashCode * have)
+{
+  unsigned int bucket;
+  unsigned int msb;
+  unsigned int lsb;
+  unsigned int i;
+
+  /* We have to represent the distance between two 2^9 (=512)-bit
+     numbers as a 2^5 (=32)-bit number with "0" being used for the
+     two numbers being identical; furthermore, we need to
+     guarantee that a difference in the number of matching
+     bits is always represented in the result.
+
+     We use 2^32/2^9 numerical values to distinguish between
+     hash codes that have the same LSB bit distance and
+     use the highest 2^9 bits of the result to signify the
+     number of (miss)matching LSB bits; if we have 0 matching
+     and hence 512 missmatching LSB bits we return -1 (since
+     512 itself cannot be represented with 9 bits) */
+
+  /* first, calculate the most significant 9 bits of our
+     result, aka the number of LSBs */
+  bucket = get_bit_distance (target, have);
+  /* bucket is now a value between 0 and 512 */
+  if (bucket == 512)
+    return 0; /* perfect match */
+  if (bucket == 0)
+    return (unsigned int) -1; /* LSB differs; use max (if we did the 
bit-shifting
+                                below, we'd end up with max+1 (overflow)) */
+
+  /* calculate the most significant bits of the final result */
+  msb = (512 - bucket) << (32-9);
+  /* calculate the 32-9 least significant bits of the final result by
+     looking at the differences in the 32-9 bits following the
+     mismatching bit at 'bucket' */
+  lsb = 0;
+  for (i=bucket+1; (i<sizeof(GNUNET_HashCode)*8) && (i<bucket+1+32-9);i++)
+    {
+      if (GNUNET_hash_get_bit (target, i) != GNUNET_hash_get_bit (have, i))
+       lsb |= (1 << (bucket+32-9-i)); /* first bit set will be 10, 
+                                         last bit set will be 31 -- if
+                                         i does not reach 512 first... */
+    }
+  return msb | lsb;
+}
+
+/**
  * Return a number that is the larger the closer the
- * "have" GNUNET_hash code is to the "target".  The basic
- * idea is that if "have" would be in the n-th lowest
- * bucket of "target", the returned value should be
- * 2^n.  However, the largest number we can return
- * is 2^31, so this number may have to be scaled.
+ * "have" GNUNET_hash code is to the "target".  
  *
  * @return inverse distance metric, non-zero.
  */
@@ -381,24 +414,7 @@
 inverse_distance (const GNUNET_HashCode * target,
                   const GNUNET_HashCode * have)
 {
-  unsigned int bucket;
-  double d;
-
-  bucket = get_bit_distance (target, have);
-  /*d = bucket * 32;
-     d = exp2 (d / (sizeof (GNUNET_HashCode) * 8));
-     I can't understand this code.  Why multiply bucket by 32?
-     I say if we want a scaled value, assume we have a bucket
-     for each bit.  Obviously we can't get to 2^512, but this
-     will appropriately tell us whether one loc is closer than
-     another.  I also don't get why we want 2^(d/512)!!  Say we
-     should be in bucket 5 (5 matching bits), then we get 1
-     as a return value, just as if we have 15 matching bits!!!
-     15 matching should be closer than 5!!!!!!! */
-  d = exp2 (bucket);
-  if (d > ((unsigned int) -1))
-    return -1;
-  return (unsigned int) d;
+  return ((unsigned int) -1)-distance (target, have);
 }
 
 /**
@@ -430,11 +446,14 @@
   int match;
   const PeerBucket *bucket;
   const PeerInfo *pi;
+#if NATE_WHAT_IS_THIS
   const PeerInfo *chosen;
+#endif
 
-  GNUNET_mutex_lock (lock);
   largest_distance = 0;
   total_distance = 0;
+#if NATE_WHAT_IS_THIS
+  GNUNET_mutex_lock (lock);
   for (bc = 0; bc < bucketCount; bc++)
     {
       bucket = &buckets[bc];
@@ -476,7 +495,10 @@
       return GNUNET_OK;
     }
   else
-    return GNUNET_SYSERR;
+    {
+      return GNUNET_SYSERR;
+    }
+#endif
 
   GNUNET_mutex_lock (lock);
   if (stats != NULL)
@@ -559,92 +581,75 @@
 
 /*
  * Find the actual, closest peer in our buckets to target
+ *
+ * @return GNUNET_SYSERR if there are no peers known, GNUNET_OK if
+ *         we found a peer
  */
-int
+static int
 find_closest_peer (GNUNET_PeerIdentity * set, const GNUNET_HashCode * target)
 {
-  unsigned int largest_distance;
-  unsigned int total_distance;
+  unsigned int largest_inv_distance;
+  unsigned int inv_dist;
   unsigned int bc;
   unsigned int ec;
-
   const PeerBucket *bucket;
   const PeerInfo *pi;
   const PeerInfo *chosen;
+
   chosen = NULL;
+  largest_inv_distance = 0;
   GNUNET_mutex_lock (lock);
-  largest_distance = 0;
-  total_distance = 0;
   for (bc = 0; bc < bucketCount; bc++)
     {
       bucket = &buckets[bc];
       for (ec = 0; ec < bucket->peers_size; ec++)
         {
           pi = bucket->peers[ec];
-          if (inverse_distance (target, &pi->id.hashPubKey) >
-              largest_distance)
+         inv_dist = inverse_distance (target, &pi->id.hashPubKey);
+          if (inv_dist > largest_inv_distance)
             {
               chosen = bucket->peers[ec];
-              largest_distance =
-                inverse_distance (target, &pi->id.hashPubKey);
+              largest_inv_distance = inv_dist;
             }
         }
     }
-
   GNUNET_mutex_unlock (lock);
-  if ((largest_distance > 0) && (chosen != NULL))
+  if (chosen != NULL)
     {
       *set = chosen->id;
       return GNUNET_OK;
     }
-  else
-    return GNUNET_SYSERR;
+  return GNUNET_SYSERR;
 }
 
-void
-printPeerBits (GNUNET_PeerIdentity * peer)
+#if DEBUG_TABLE
+static void
+printKeyBits (const GNUNET_HashCode * key)
 {
   unsigned int i;
-  char loc[513];
-  loc[512] = '\0';
+  char loc[sizeof(GNUNET_HashCode)*8+1];
+
+  loc[sizeof(loc)-1] = '\0';
   for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++)
     {
-      if (GNUNET_hash_get_bit (&peer->hashPubKey, i) == 0)
-        {
-          loc[i] = '0';
-        }
+      if (GNUNET_hash_get_bit (key, i) == 0)
+       loc[i] = '0';        
       else
-        {
-          loc[i] = '1';
-        }
+       loc[i] = '1';
     }
   GNUNET_GE_LOG (coreAPI->ectx,
                  GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
                  GNUNET_GE_BULK, "%s\n", &loc);
 }
 
-void
-printKeyBits (const GNUNET_HashCode * key)
+static void
+printPeerBits (GNUNET_PeerIdentity * peer)
 {
-  unsigned int i;
-  char loc[513];
-  loc[512] = '\0';
-  for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++)
-    {
-      if (GNUNET_hash_get_bit (key, i) == 0)
-        {
-          loc[i] = '0';
-        }
-      else
-        {
-          loc[i] = '1';
-        }
-    }
-  GNUNET_GE_LOG (coreAPI->ectx,
-                 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                 GNUNET_GE_BULK, "%s\n", &loc);
+  printKeyBits (&peer->hashPubKey);
 }
 
+#endif
+
 /*
  * Check whether my identity is closer than any known peers.
  *
@@ -656,11 +661,13 @@
 {
 
   GNUNET_PeerIdentity closest;
+
   memset (&closest, 0, sizeof (GNUNET_PeerIdentity));
-  find_closest_peer (&closest, target);
-  if (&closest == NULL)
-    return GNUNET_SYSERR;
-
+  if (GNUNET_OK != find_closest_peer (&closest, target))
+    {
+      /* no other peers known; hence we must be the closest! */
+      return GNUNET_YES;
+    }
 #if DEBUG_TABLE
   GNUNET_GE_LOG (coreAPI->ectx,
                  GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
@@ -682,18 +689,14 @@
                  inverse_distance (target,
                                    &coreAPI->my_identity->hashPubKey));
 #endif
-  if (inverse_distance (target, &coreAPI->my_identity->hashPubKey) >=
-      inverse_distance (target, &closest.hashPubKey)
-      && (inverse_distance (target, &coreAPI->my_identity->hashPubKey) > 0))
-    {
-      return GNUNET_YES;
-    }
+  if (distance (target, &coreAPI->my_identity->hashPubKey) <=
+      distance (target, &closest.hashPubKey))
+    return GNUNET_YES;    
   return GNUNET_NO;
 }
 
 /**
- * We have received a pong from a peer and know it is still
- * there.
+ * We have received a pong from a peer and know it is still there.
  */
 static void
 pongNotify (void *cls)
@@ -905,7 +908,7 @@
   GNUNET_mutex_unlock (lock);
 }
 
-
+#if DEBUG_TABLE
 void
 print_buckets ()
 {
@@ -929,6 +932,7 @@
         }
     }
 }
+#endif
 
 
 /**
@@ -967,11 +971,11 @@
                      debug_file_name, strerror (errno));
       return GNUNET_SYSERR;
     }
-#endif
   GNUNET_GE_LOG (coreAPI->ectx,
                  GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
                  GNUNET_GE_BULK, "My bit location:\n");
   printPeerBits (coreAPI->my_identity);
+#endif
   /* use less than 50% of peer's ideal number of
      connections for DV_DHT table size */
   i = coreAPI->core_slots_count () / MAINTAIN_BUCKET_SIZE / 2;





reply via email to

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