#include "gnunet_hello_lib.h"
#include "gnunet_dht_service.h"
#include "gnunet_statistics_service.h"
-#include "gnunet_peerinfo_service.h"
-#include "dht.h"
#include "gnunet-service-dht.h"
#include "gnunet-service-dht_clients.h"
#include "gnunet-service-dht_datacache.h"
+#include "gnunet-service-dht_hello.h"
+#include "gnunet-service-dht_neighbours.h"
#include "gnunet-service-dht_nse.h"
#include "gnunet-service-dht_routing.h"
#include <fenv.h>
+#include "dht.h"
/**
* How many buckets will we allow total.
/**
* What is the maximum number of peers in a given bucket.
*/
-#define DEFAULT_BUCKET_SIZE 4
-
-/**
- * Size of the bloom filter the DHT uses to filter peers.
- */
-#define DHT_BLOOM_SIZE 128
+#define DEFAULT_BUCKET_SIZE 8
/**
* Desired replication level for FIND PEER requests
/**
* How long at least to wait before sending another find peer request.
*/
-#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
+#define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
/**
* How long at most to wait before sending another find peer request.
*/
-#define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
+#define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10)
/**
* How long at most to wait for transmission of a GET request to another peer?
*/
struct GNUNET_CORE_InformationRequestContext *info_ctx;
- /**
- * HELLO message for the peer, NULL if not known. FIXME: track
- * separately? FIXME: free!?
- */
- struct GNUNET_HELLO_Message *hello;
-
/**
* Task for scheduling preference updates
*/
*/
static struct GNUNET_CORE_Handle *coreAPI;
-/**
- * Handle for peerinfo notifications.
- */
-static struct GNUNET_PEERINFO_NotifyContext *pnc;
/**
&peer->id.hashPubKey);
if (matching >= 64)
matching = 63;
- bucket = find_bucket(&peer->id.hashPubKey);
+ bucket = find_bucket (&peer->id.hashPubKey);
if (bucket == GNUNET_SYSERR)
preference = 0;
else
+ {
+ GNUNET_assert (k_buckets[bucket].peers_size != 0);
preference = (1LL << matching) / k_buckets[bucket].peers_size;
+ }
if (preference == 0)
{
peer->preference_task
&update_core_preference, peer);
return;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Preference updates given to core"), 1,
+ GNUNET_NO);
peer->info_ctx =
GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
GNUNET_TIME_UNIT_FOREVER_REL,
}
+/**
+ * Closure for 'add_known_to_bloom'.
+ */
+struct BloomConstructorContext
+{
+ /**
+ * Bloom filter under construction.
+ */
+ struct GNUNET_CONTAINER_BloomFilter *bloom;
+
+ /**
+ * Mutator to use.
+ */
+ uint32_t bf_mutator;
+};
+
+
+/**
+ * Add each of the peers we already know to the bloom filter of
+ * the request so that we don't get duplicate HELLOs.
+ *
+ * @param cls the 'struct BloomConstructorContext'.
+ * @param key peer identity to add to the bloom filter
+ * @param value value the peer information (unused)
+ * @return GNUNET_YES (we should continue to iterate)
+ */
+static int
+add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct BloomConstructorContext *ctx = cls;
+ GNUNET_HashCode mh;
+
+ GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding known peer (%s) to bloomfilter for FIND PEER with mutation %u\n",
+ GNUNET_h2s (key),
+ ctx->bf_mutator);
+ GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
+ return GNUNET_YES;
+}
+
+
+/**
+ * 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
+ * and attempt to connect to them.
+ *
+ * @param cls closure for this task
+ * @param tc the context under which the task is running
+ */
+static void
+send_find_peer_message (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_TIME_Relative next_send_time;
+ struct BloomConstructorContext bcc;
+ struct GNUNET_CONTAINER_BloomFilter *peer_bf;
+
+ find_peer_task = GNUNET_SCHEDULER_NO_TASK;
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+ return;
+ if (newly_found_peers > bucket_size)
+ {
+ /* If we are finding many peers already, no need to send out our request right now! */
+ find_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
+ &send_find_peer_message, NULL);
+ newly_found_peers = 0;
+ return;
+ }
+ bcc.bf_mutator = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
+ bcc.bloom =
+ GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, GNUNET_CONSTANTS_BLOOMFILTER_K);
+ GNUNET_CONTAINER_multihashmap_iterate (all_known_peers,
+ &add_known_to_bloom,
+ &bcc);
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# FIND PEER messages initiated"), 1,
+ GNUNET_NO);
+ peer_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
+ DHT_BLOOM_SIZE,
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ // FIXME: pass priority!?
+ GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
+ GNUNET_DHT_RO_FIND_PEER,
+ FIND_PEER_REPLICATION_LEVEL,
+ 0,
+ &my_identity.hashPubKey,
+ NULL, 0,
+ bcc.bloom, bcc.bf_mutator,
+ peer_bf);
+ GNUNET_CONTAINER_bloomfilter_free (peer_bf);
+ GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
+ /* schedule next round */
+ next_send_time.rel_value =
+ DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
+ GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
+ DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / (newly_found_peers+1));
+ newly_found_peers = 0;
+ find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time,
+ &send_find_peer_message,
+ NULL);
+}
+
+
/**
* Method called whenever a peer connects.
*
/* Check for connect to self message */
if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
return;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Connected %s to %s\n",
+ GNUNET_i2s (&my_identity),
+ GNUNET_h2s (&peer->hashPubKey));
if (GNUNET_YES ==
GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
&peer->hashPubKey))
GNUNET_break (0);
return;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Peers connected"), 1,
+ GNUNET_NO);
peer_bucket = find_bucket (&peer->hashPubKey);
GNUNET_assert ( (peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS) );
ret = GNUNET_malloc (sizeof (struct PeerInfo));
peer_bucket);
if ( (peer_bucket > 0) &&
(k_buckets[peer_bucket].peers_size <= bucket_size) )
+ {
ret->preference_task = GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
- newly_found_peers++;
+ newly_found_peers++;
+ }
GNUNET_assert (GNUNET_OK ==
GNUNET_CONTAINER_multihashmap_put (all_known_peers,
&peer->hashPubKey, ret,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ if (1 == GNUNET_CONTAINER_multihashmap_size (all_known_peers))
+ {
+ /* got a first connection, good time to start with FIND PEER requests... */
+ find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message,
+ NULL);
+ }
}
struct PeerInfo *to_remove;
int current_bucket;
struct P2PPendingMessage *pos;
+ unsigned int discarded;
/* Check for disconnect from self message */
if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
return;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnected %s from %s\n",
+ GNUNET_i2s (&my_identity),
+ GNUNET_h2s (&peer->hashPubKey));
to_remove =
GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
if (NULL == to_remove)
GNUNET_break (0);
return;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Peers connected"), -1,
+ GNUNET_NO);
GNUNET_assert (GNUNET_YES ==
GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
&peer->hashPubKey,
GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
to_remove->info_ctx = NULL;
}
+ if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task)
+ {
+ GNUNET_SCHEDULER_cancel (to_remove->preference_task);
+ to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK;
+ }
current_bucket = find_bucket (&to_remove->id.hashPubKey);
GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
k_buckets[current_bucket].tail,
GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
to_remove->th = NULL;
}
+ discarded = 0;
while (NULL != (pos = to_remove->head))
{
GNUNET_CONTAINER_DLL_remove (to_remove->head,
to_remove->tail,
pos);
+ discarded++;
GNUNET_free (pos);
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Queued messages discarded (peer disconnected)"), discarded,
+ GNUNET_NO);
+ GNUNET_free (to_remove);
}
size_t msize;
peer->th = NULL;
- if (buf == NULL)
+ while ( (NULL != (pending = peer->head)) &&
+ (GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value == 0) )
{
- /* client disconnected */
- return 0;
+ peer->pending_count--;
+ GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
+ GNUNET_free (pending);
}
- if (peer->head == NULL)
+ if (pending == NULL)
{
/* no messages pending */
return 0;
}
+ if (buf == NULL)
+ {
+ peer->th
+ = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
+ pending->importance,
+ GNUNET_TIME_absolute_get_remaining (pending->timeout),
+ &peer->id, ntohs (pending->msg->size),
+ &core_transmit_notify, peer);
+ GNUNET_break (NULL != peer->th);
+ return 0;
+ }
off = 0;
while ( (NULL != (pending = peer->head)) &&
(size - off >= (msize = ntohs (pending->msg->size))) )
{
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Bytes transmitted to other peers"), msize,
+ GNUNET_NO);
memcpy (&cbuf[off], pending->msg, msize);
off += msize;
peer->pending_count--;
GNUNET_free (pending);
}
if (peer->head != NULL)
+ {
peer->th
= GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
- pending->importance,
- GNUNET_TIME_absolute_get_remaining (pending->timeout),
+ pending->importance,
+ GNUNET_TIME_absolute_get_remaining (pending->timeout),
&peer->id, msize,
- &core_transmit_notify, peer);
+ &core_transmit_notify, peer);
+ GNUNET_break (NULL != peer->th);
+ }
return off;
}
{
struct P2PPendingMessage *pending;
- if (NULL != (pending = peer->head))
+ if (NULL == (pending = peer->head))
return;
if (NULL != peer->th)
return;
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Bytes of bandwdith requested from core"),
+ ntohs (pending->msg->size),
+ GNUNET_NO);
peer->th
= GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
pending->importance,
&peer->id,
ntohs (pending->msg->size),
&core_transmit_notify, peer);
+ GNUNET_break (NULL != peer->th);
}
uint32_t forward_count;
float target_value;
- if (hop_count > GDS_NSE_get () * 4.0)
+ if (hop_count > GDS_NSE_get () * 6.0)
{
/* forcefully terminate */
return 0;
}
- if (hop_count > GDS_NSE_get () * 2.0)
+ if (hop_count > GDS_NSE_get () * 4.0)
{
/* Once we have reached our ideal number of hops, only forward to 1 peer */
return 1;
/* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
target_value = target_value - forward_count;
random_value =
- GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
+ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
if (random_value < (target_value * UINT32_MAX))
forward_count++;
return forward_count;
int bucket_num;
int count;
struct PeerInfo *pos;
- unsigned int my_distance;
if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
return GNUNET_YES;
bucket_num = find_bucket (key);
bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, key);
- my_distance = get_distance (&my_identity.hashPubKey, key);
pos = k_buckets[bucket_num].head;
count = 0;
while ((pos != NULL) && (count < bucket_size))
/* greedy selection (closest peer that is not in bloomfilter) */
smallest_distance = UINT_MAX;
chosen = NULL;
- for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
+ for (bc = 0; bc < closest_bucket; bc++)
{
pos = k_buckets[bc].head;
count = 0;
while ((pos != NULL) && (count < bucket_size))
{
- if (GNUNET_NO ==
- GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
- {
+ if ( (bloom == NULL) ||
+ (GNUNET_NO ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
+ {
dist = get_distance (key, &pos->id.hashPubKey);
if (dist < smallest_distance)
{
smallest_distance = dist;
}
}
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Excluded peer `%s' due to BF match in greedy routing for %s\n",
+ GNUNET_i2s (&pos->id),
+ GNUNET_h2s (key));
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
+ GNUNET_NO);
+ }
count++;
pos = pos->next;
}
}
+ if (NULL == chosen)
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Peer selection failed"), 1,
+ GNUNET_NO);
return chosen;
}
pos = k_buckets[bc].head;
while ((pos != NULL) && (count < bucket_size))
{
- if (GNUNET_YES ==
- GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+ if ( (bloom != NULL) &&
+ (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
{
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
+ GNUNET_NO);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Excluded peer `%s' due to BF match in random routing for %s\n",
+ GNUNET_i2s (&pos->id),
+ GNUNET_h2s (key));
pos = pos->next;
continue; /* Ignore bloomfiltered peers */
}
}
if (count == 0) /* No peers to select from! */
{
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# Peer selection failed"), 1,
+ GNUNET_NO);
return NULL;
}
/* Now actually choose a peer */
pos = k_buckets[bc].head;
while ((pos != NULL) && (count < bucket_size))
{
- if (GNUNET_YES ==
- GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
+ if ( (bloom != NULL) &&
+ (GNUNET_YES ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
{
pos = pos->next;
continue; /* Ignore bloomfiltered peers */
struct PeerInfo **rtargets;
struct PeerInfo *nxt;
+ GNUNET_assert (NULL != bloom);
ret = get_forward_count (hop_count, target_replication);
if (ret == 0)
{
return 0;
}
rtargets = GNUNET_malloc (sizeof (struct PeerInfo*) * ret);
- off = 0;
- while (ret-- > 0)
+ for (off = 0; off < ret; off++)
{
nxt = select_peer (key, bloom, hop_count);
if (nxt == NULL)
- break;
- rtargets[off++] = nxt;
- GNUNET_CONTAINER_bloomfilter_add (bloom, &nxt->id.hashPubKey);
+ break;
+ rtargets[off] = nxt;
+ GNUNET_break (GNUNET_NO ==
+ GNUNET_CONTAINER_bloomfilter_test (bloom, &nxt->id.hashPubKey));
+ GNUNET_CONTAINER_bloomfilter_add (bloom, &rtargets[off]->id.hashPubKey);
}
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Selected %u/%u peers at hop %u for %s (target was %u)\n",
+ off,
+ GNUNET_CONTAINER_multihashmap_size (all_known_peers),
+ (unsigned int) hop_count,
+ GNUNET_h2s (key),
+ ret);
if (0 == off)
{
GNUNET_free (rtargets);
struct PeerPutMessage *ppm;
struct GNUNET_PeerIdentity *pp;
+ GNUNET_assert (NULL != bf);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding myself (%s) to PUT bloomfilter for %s\n",
+ GNUNET_i2s (&my_identity),
+ GNUNET_h2s (key));
+ GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity.hashPubKey);
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# PUT requests routed"), 1,
+ GNUNET_NO);
target_count = get_target_peers (key, bf, hop_count,
desired_replication_level,
&targets);
if (0 == target_count)
- return;
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Routing PUT for %s terminates after %u hops at %s\n",
+ GNUNET_h2s (key),
+ (unsigned int) hop_count,
+ GNUNET_i2s (&my_identity));
+ return;
+ }
msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + sizeof (struct PeerPutMessage);
if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
{
GNUNET_break (0);
return;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# PUT messages queued for transmission"), target_count,
+ GNUNET_NO);
for (i=0;i<target_count;i++)
{
target = targets[i];
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Routing PUT for %s after %u hops to %s\n",
+ GNUNET_h2s (key),
+ (unsigned int) hop_count,
+ GNUNET_i2s (&target->id));
pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
pending->importance = 0; /* FIXME */
pending->timeout = expiration_time;
ppm->desired_replication_level = htonl (desired_replication_level);
ppm->put_path_length = htonl (put_path_length);
ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
+ GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &target->id.hashPubKey));
GNUNET_assert (GNUNET_OK ==
GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
ppm->bloomfilter,
struct PeerGetMessage *pgm;
char *xq;
size_t reply_bf_size;
-
+
+ GNUNET_assert (NULL != peer_bf);
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# GET requests routed"), 1,
+ GNUNET_NO);
target_count = get_target_peers (key, peer_bf, hop_count,
desired_replication_level,
&targets);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding myself (%s) to GET bloomfilter for %s\n",
+ GNUNET_i2s (&my_identity),
+ GNUNET_h2s (key));
+ GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity.hashPubKey);
if (0 == target_count)
- return;
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Routing GET for %s terminates after %u hops at %s\n",
+ GNUNET_h2s (key),
+ (unsigned int) hop_count,
+ GNUNET_i2s (&my_identity));
+ return;
+ }
reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
GNUNET_break (0);
return;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# GET messages queued for transmission"), target_count,
+ GNUNET_NO);
/* forward request */
for (i=0;i<target_count;i++)
{
target = targets[i];
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Routing GET for %s after %u hops to %s\n",
+ GNUNET_h2s (key),
+ (unsigned int) hop_count,
+ GNUNET_i2s (&target->id));
pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
pending->importance = 0; /* FIXME */
pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
pgm->desired_replication_level = htonl (desired_replication_level);
pgm->xquery_size = htonl (xquery_size);
pgm->bf_mutator = reply_bf_mutator;
+ GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (peer_bf, &target->id.hashPubKey));
GNUNET_assert (GNUNET_OK ==
GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
pgm->bloomfilter,
pgm->key = *key;
xq = (char *) &pgm[1];
memcpy (xq, xquery, xquery_size);
- GNUNET_assert (GNUNET_OK ==
- GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
- &xq[xquery_size],
- reply_bf_size));
+ if (NULL != reply_bf)
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
+ &xq[xquery_size],
+ reply_bf_size));
GNUNET_CONTAINER_DLL_insert_tail (target->head,
target->tail,
pending);
struct GNUNET_TIME_Absolute expiration_time,
const GNUNET_HashCode *key,
unsigned int put_path_length,
- struct GNUNET_PeerIdentity *put_path,
+ const struct GNUNET_PeerIdentity *put_path,
unsigned int get_path_length,
- struct GNUNET_PeerIdentity *get_path,
+ const struct GNUNET_PeerIdentity *get_path,
const void *data,
size_t data_size)
{
/* peer disconnected in the meantime, drop reply */
return;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# RESULT messages queued for transmission"), 1,
+ GNUNET_NO);
pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
pending->importance = 0; /* FIXME */
pending->timeout = expiration_time;
}
-/**
- * Closure for 'add_known_to_bloom'.
- */
-struct BloomConstructorContext
-{
- /**
- * Bloom filter under construction.
- */
- struct GNUNET_CONTAINER_BloomFilter *bloom;
-
- /**
- * Mutator to use.
- */
- uint32_t bf_mutator;
-};
-
-
-/**
- * Add each of the peers we already know to the bloom filter of
- * the request so that we don't get duplicate HELLOs.
- *
- * @param cls the 'struct BloomConstructorContext'.
- * @param key peer identity to add to the bloom filter
- * @param value value the peer information (unused)
- * @return GNUNET_YES (we should continue to iterate)
- */
-static int
-add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
-{
- struct BloomConstructorContext *ctx = cls;
- GNUNET_HashCode mh;
-
- GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
- GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
- return GNUNET_YES;
-}
-
-
-/**
- * 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
- * and attempt to connect to them.
- *
- * @param cls closure for this task
- * @param tc the context under which the task is running
- */
-static void
-send_find_peer_message (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct GNUNET_TIME_Relative next_send_time;
- struct BloomConstructorContext bcc;
-
- find_peer_task = GNUNET_SCHEDULER_NO_TASK;
- if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
- return;
- if (newly_found_peers > bucket_size)
- {
- /* If we are finding many peers already, no need to send out our request right now! */
- find_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
- &send_find_peer_message, NULL);
- newly_found_peers = 0;
- return;
- }
- bcc.bf_mutator = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
- bcc.bloom =
- GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
- GNUNET_CONTAINER_multihashmap_iterate (all_known_peers,
- &add_known_to_bloom,
- &bcc);
- // FIXME: pass priority!?
- GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
- GNUNET_DHT_RO_FIND_PEER,
- FIND_PEER_REPLICATION_LEVEL,
- 0,
- &my_identity.hashPubKey,
- NULL, 0,
- bcc.bloom, bcc.bf_mutator, NULL);
- GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
- /* schedule next round */
- newly_found_peers = 0;
- next_send_time.rel_value =
- (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
- GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
- DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
- find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time,
- &send_find_peer_message,
- NULL);
-}
-
-
/**
* To be called on core init/fail.
*
const struct GNUNET_PeerIdentity *identity,
const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
{
- struct GNUNET_TIME_Relative next_send_time;
-
GNUNET_assert (server != NULL);
my_identity = *identity;
- /* FIXME: do upon 1st connect instead! */
- next_send_time.rel_value =
- DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
- GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
- (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
- 2) -
- DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
- find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time,
- &send_find_peer_message,
- NULL);
}
GNUNET_break_op (0);
return GNUNET_YES;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P PUT requests received"), 1,
+ GNUNET_NO);
put_path = (const struct GNUNET_PeerIdentity*) &put[1];
payload = &put_path[putlen];
options = ntohl (put->options);
/* cannot verify, good luck */
break;
}
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "PUT for %s at %s\n",
+ GNUNET_h2s (&put->key),
+ GNUNET_i2s (&my_identity));
bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
DHT_BLOOM_SIZE,
- DHT_BLOOM_K);
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ GNUNET_break_op (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &peer->hashPubKey));
{
struct GNUNET_PeerIdentity pp[putlen+1];
putlen = 0;
/* give to local clients */
- GDS_CLIENT_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
+ GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
&put->key,
0, NULL,
putlen,
options,
ntohl (put->desired_replication_level),
GNUNET_TIME_absolute_ntoh (put->expiration_time),
- ntohl (put->hop_count) + 1 /* who adds +1? */,
+ ntohl (put->hop_count),
bf,
&put->key,
putlen, pp,
struct PeerInfo *peer;
unsigned int choice;
GNUNET_HashCode mhash;
+ const struct GNUNET_HELLO_Message *hello;
/* first, check about our own HELLO */
if (NULL != GDS_my_hello)
{
GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
- if (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))
-
+ if ( (NULL == bf) ||
+ (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) )
+ {
GDS_NEIGHBOURS_handle_reply (sender,
GNUNET_BLOCK_TYPE_DHT_HELLO,
GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
0, NULL,
GDS_my_hello,
GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*) GDS_my_hello));
+ }
+ else
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# FIND PEER requests ignored due to Bloomfilter"), 1,
+ GNUNET_NO);
+ }
+ }
+ else
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# FIND PEER requests ignored due to lack of HELLO"), 1,
+ GNUNET_NO);
}
/* then, also consider sending a random HELLO from the closest bucket */
- bucket_idx = find_bucket (key);
+ if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
+ bucket_idx = closest_bucket;
+ else
+ bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key));
if (bucket_idx == GNUNET_SYSERR)
return;
bucket = &k_buckets[bucket_idx];
if (bucket->peers_size == 0)
return;
- choice = GNUNET_CRYPTO_random_u32 (bucket->peers_size,
- GNUNET_CRYPTO_QUALITY_WEAK);
+ choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+ bucket->peers_size);
peer = bucket->head;
while (choice > 0)
{
GNUNET_assert (peer != NULL);
peer = peer->next;
+ choice--;
}
choice = bucket->peers_size;
do
if (peer == NULL)
peer = bucket->head;
GNUNET_BLOCK_mingle_hash (&peer->id.hashPubKey, bf_mutator, &mhash);
+ hello = GDS_HELLO_get (&peer->id);
}
- while ( (peer->hello == NULL) ||
+ while ( (hello == NULL) ||
(GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) );
GDS_NEIGHBOURS_handle_reply (sender,
GNUNET_BLOCK_TYPE_DHT_HELLO,
key,
0, NULL,
0, NULL,
- peer->hello,
- GNUNET_HELLO_size (peer->hello));
+ hello,
+ GNUNET_HELLO_size (hello));
}
struct GNUNET_CONTAINER_BloomFilter *reply_bf;
struct GNUNET_CONTAINER_BloomFilter *peer_bf;
const char *xquery;
-
+
+ GNUNET_break (0 != memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)));
/* parse and validate message */
msize = ntohs (message->size);
if (msize < sizeof (struct PeerGetMessage))
GNUNET_break_op (0);
return GNUNET_YES;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P GET requests received"), 1,
+ GNUNET_NO);
reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
type = ntohl (get->type);
options = ntohl (get->options);
if (reply_bf_size > 0)
reply_bf = GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size],
reply_bf_size,
- GNUNET_DHT_GET_BLOOMFILTER_K);
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
eval = GNUNET_BLOCK_evaluate (GDS_block_context,
type,
&get->key,
peer_bf =
GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
DHT_BLOOM_SIZE,
- DHT_BLOOM_K);
-
+ GNUNET_CONSTANTS_BLOOMFILTER_K);
+ GNUNET_break_op (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (peer_bf, &peer->hashPubKey));
/* remember request for routing replies */
GDS_ROUTING_add (peer,
type,
&get->key,
xquery, xquery_size,
reply_bf, get->bf_mutator);
-
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "GET for %s at %s after %u hops\n",
+ GNUNET_h2s (&get->key),
+ GNUNET_i2s (&my_identity),
+ (unsigned int) ntohl (get->hop_count));
/* local lookup (this may update the reply_bf) */
if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
(am_closest_peer (&get->key,
peer_bf) ) )
- {
+ {
if ( (0 != (options & GNUNET_DHT_RO_FIND_PEER)))
{
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P FIND PEER requests processed"), 1,
+ GNUNET_NO);
handle_find_peer (peer,
&get->key,
reply_bf,
}
else
{
- GDS_DATACACHE_handle_get (&get->key,
- type,
- xquery, xquery_size,
- &reply_bf,
- get->bf_mutator);
- /* FIXME: should track if the local lookup resulted in a
- definitive result and then NOT do P2P forwarding */
+ eval = GDS_DATACACHE_handle_get (&get->key,
+ type,
+ xquery, xquery_size,
+ &reply_bf,
+ get->bf_mutator);
}
}
+ else
+ {
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P GET requests ONLY routed"), 1,
+ GNUNET_NO);
+ }
- /* P2P forwarding */
- GDS_NEIGHBOURS_handle_get (type,
- options,
- ntohl (get->desired_replication_level),
- ntohl (get->hop_count) + 1, /* CHECK: where (else) do we do +1? */
- &get->key,
- xquery, xquery_size,
- reply_bf,
- get->bf_mutator,
- peer_bf);
+ /* 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);
GNUNET_break_op (0);
return GNUNET_YES;
}
+ GNUNET_STATISTICS_update (GDS_stats,
+ gettext_noop ("# P2P RESULTS received"), 1,
+ GNUNET_NO);
put_path = (const struct GNUNET_PeerIdentity*) &prm[1];
get_path = &put_path[put_path_length];
type = ntohl (prm->type);
GNUNET_break_op (0);
return GNUNET_YES;
}
- bucket = find_bucket (&pid.hashPubKey);
- if ( (bucket >= 0) &&
- (k_buckets[bucket].peers_size < bucket_size) )
- {
- if (NULL != GDS_transport_handle)
- GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
- h, NULL, NULL);
- (void) GNUNET_CORE_peer_request_connect (coreAPI,
- &pid,
- NULL, NULL);
- }
+ if (0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
+ {
+ bucket = find_bucket (&pid.hashPubKey);
+ if ( (bucket >= 0) &&
+ (k_buckets[bucket].peers_size < bucket_size) )
+ {
+ if (NULL != GDS_transport_handle)
+ GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
+ h, NULL, NULL);
+ (void) GNUNET_CORE_peer_request_connect (coreAPI,
+ &pid,
+ NULL, NULL);
+ }
+ }
}
/* append 'peer' to 'get_path' */
get_path_length++;
/* forward to local clients */
- GDS_CLIENT_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
+ GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
&prm->key,
get_path_length,
xget_path,
}
-/**
- * Function called for each HELLO known to PEERINFO.
- *
- * @param cls closure
- * @param peer id of the peer, NULL for last call
- * @param hello hello message for the peer (can be NULL)
- * @param error message
- */
-static void
-process_hello (void *cls,
- const struct GNUNET_PeerIdentity *
- peer,
- const struct GNUNET_HELLO_Message *
- hello, const char *err_msg)
-{
- // FIXME: consider moving HELLO processing to another file!
- // FIXME: first, filter HELLOs without addresses (!)
- // FIXME: track HELLOs (for responding to FIND PEER requests)
- // FIXME: add code to possibly ask core to establish connections
- // (using our own peerinfo is better than using FIND PEER!)
-}
-
-
/**
* Initialize neighbours subsystem.
*
if (coreAPI == NULL)
return GNUNET_SYSERR;
all_known_peers = GNUNET_CONTAINER_multihashmap_create (256);
- pnc = GNUNET_PEERINFO_notify (GDS_cfg,
- &process_hello,
- NULL);
return GNUNET_OK;
}
{
if (coreAPI == NULL)
return;
- if (NULL != pnc)
- {
- GNUNET_PEERINFO_notify_cancel (pnc);
- pnc = NULL;
- }
GNUNET_CORE_disconnect (coreAPI);
coreAPI = NULL;
GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (all_known_peers));