2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dht/gnunet-service-dht_neighbours.c
23 * @brief GNUnet DHT service's bucket and neighbour management code
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_block_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_nse_service.h"
35 #include "gnunet_ats_service.h"
36 #include "gnunet_core_service.h"
37 #include "gnunet_datacache_lib.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_hello_lib.h"
40 #include "gnunet_dht_service.h"
41 #include "gnunet_statistics_service.h"
42 #include "gnunet-service-dht.h"
43 #include "gnunet-service-dht_clients.h"
44 #include "gnunet-service-dht_datacache.h"
45 #include "gnunet-service-dht_hello.h"
46 #include "gnunet-service-dht_neighbours.h"
47 #include "gnunet-service-dht_nse.h"
48 #include "gnunet-service-dht_routing.h"
53 * How many buckets will we allow total.
55 #define MAX_BUCKETS sizeof (GNUNET_HashCode) * 8
58 * What is the maximum number of peers in a given bucket.
60 #define DEFAULT_BUCKET_SIZE 8
63 * Desired replication level for FIND PEER requests
65 #define FIND_PEER_REPLICATION_LEVEL 4
68 * Maximum allowed replication level for all requests.
70 #define MAXIMUM_REPLICATION_LEVEL 16
73 * How often to update our preference levels for peers in our routing tables.
75 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
78 * How long at least to wait before sending another find peer request.
80 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
83 * How long at most to wait before sending another find peer request.
85 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10)
88 * How long at most to wait for transmission of a GET request to another peer?
90 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
99 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
101 struct GNUNET_MessageHeader header;
106 uint32_t options GNUNET_PACKED;
111 uint32_t type GNUNET_PACKED;
116 uint32_t hop_count GNUNET_PACKED;
119 * Replication level for this message
121 uint32_t desired_replication_level GNUNET_PACKED;
124 * Length of the PUT path that follows (if tracked).
126 uint32_t put_path_length GNUNET_PACKED;
129 * When does the content expire?
131 struct GNUNET_TIME_AbsoluteNBO expiration_time;
134 * Bloomfilter (for peer identities) to stop circular routes
136 char bloomfilter[DHT_BLOOM_SIZE];
139 * The key we are storing under.
143 /* put path (if tracked) */
153 struct PeerResultMessage
156 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
158 struct GNUNET_MessageHeader header;
163 uint32_t type GNUNET_PACKED;
166 * Length of the PUT path that follows (if tracked).
168 uint32_t put_path_length GNUNET_PACKED;
171 * Length of the GET path that follows (if tracked).
173 uint32_t get_path_length GNUNET_PACKED;
176 * When does the content expire?
178 struct GNUNET_TIME_AbsoluteNBO expiration_time;
181 * The key of the corresponding GET request.
185 /* put path (if tracked) */
187 /* get path (if tracked) */
197 struct PeerGetMessage
200 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_GET
202 struct GNUNET_MessageHeader header;
207 uint32_t options GNUNET_PACKED;
210 * Desired content type.
212 uint32_t type GNUNET_PACKED;
217 uint32_t hop_count GNUNET_PACKED;
220 * Desired replication level for this request.
222 uint32_t desired_replication_level GNUNET_PACKED;
225 * Size of the extended query.
227 uint32_t xquery_size;
230 * Bloomfilter mutator.
235 * Bloomfilter (for peer identities) to stop circular routes
237 char bloomfilter[DHT_BLOOM_SIZE];
240 * The key we are looking for.
246 /* result bloomfilter */
252 * Linked list of messages to send to a particular other peer.
254 struct P2PPendingMessage
257 * Pointer to next item in the list
259 struct P2PPendingMessage *next;
262 * Pointer to previous item in the list
264 struct P2PPendingMessage *prev;
267 * Message importance level. FIXME: used? useful?
269 unsigned int importance;
272 * When does this message time out?
274 struct GNUNET_TIME_Absolute timeout;
277 * Actual message to be sent, allocated at the end of the struct:
278 * // msg = (cast) &pm[1];
279 * // memcpy (&pm[1], data, len);
281 const struct GNUNET_MessageHeader *msg;
287 * Entry for a peer in a bucket.
292 * Next peer entry (DLL)
294 struct PeerInfo *next;
297 * Prev peer entry (DLL)
299 struct PeerInfo *prev;
302 * Count of outstanding messages for peer. FIXME: NEEDED?
303 * FIXME: bound queue size!?
305 unsigned int pending_count;
308 * Head of pending messages to be sent to this peer.
310 struct P2PPendingMessage *head;
313 * Tail of pending messages to be sent to this peer.
315 struct P2PPendingMessage *tail;
318 * Core handle for sending messages to this peer.
320 struct GNUNET_CORE_TransmitHandle *th;
323 * Task for scheduling preference updates
325 GNUNET_SCHEDULER_TaskIdentifier preference_task;
328 * What is the identity of the peer?
330 struct GNUNET_PeerIdentity id;
334 * What is the average latency for replies received?
336 struct GNUNET_TIME_Relative latency;
339 * Transport level distance to peer.
341 unsigned int distance;
348 * Peers are grouped into buckets.
355 struct PeerInfo *head;
360 struct PeerInfo *tail;
363 * Number of peers in the bucket.
365 unsigned int peers_size;
370 * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
372 static unsigned int closest_bucket;
375 * How many peers have we added since we sent out our last
378 static unsigned int newly_found_peers;
381 * The buckets. Array of size MAX_BUCKET_SIZE. Offset 0 means 0 bits matching.
383 static struct PeerBucket k_buckets[MAX_BUCKETS];
386 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
388 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
391 * Maximum size for each bucket.
393 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
396 * Task that sends FIND PEER requests.
398 static GNUNET_SCHEDULER_TaskIdentifier find_peer_task;
401 * Identity of this peer.
403 static struct GNUNET_PeerIdentity my_identity;
408 static struct GNUNET_CORE_Handle *coreAPI;
413 static struct GNUNET_ATS_PerformanceHandle *atsAPI;
418 * Find the optimal bucket for this key.
420 * @param hc the hashcode to compare our identity to
421 * @return the proper bucket index, or GNUNET_SYSERR
422 * on error (same hashcode)
425 find_bucket (const GNUNET_HashCode * hc)
429 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
430 if (bits == MAX_BUCKETS)
432 /* How can all bits match? Got my own ID? */
434 return GNUNET_SYSERR;
436 return MAX_BUCKETS - bits - 1;
441 * Let GNUnet core know that we like the given peer.
443 * @param cls the 'struct PeerInfo' of the peer
444 * @param tc scheduler context.
447 update_core_preference (void *cls,
448 const struct GNUNET_SCHEDULER_TaskContext *tc)
450 struct PeerInfo *peer = cls;
452 unsigned int matching;
455 peer->preference_task = GNUNET_SCHEDULER_NO_TASK;
456 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
459 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
460 &peer->id.hashPubKey);
463 bucket = find_bucket (&peer->id.hashPubKey);
464 if (bucket == GNUNET_SYSERR)
468 GNUNET_assert (k_buckets[bucket].peers_size != 0);
469 preference = (1LL << matching) / k_buckets[bucket].peers_size;
473 peer->preference_task =
474 GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
475 &update_core_preference, peer);
478 GNUNET_STATISTICS_update (GDS_stats,
479 gettext_noop ("# Preference updates given to core"),
481 GNUNET_ATS_change_preference (atsAPI, &peer->id,
482 GNUNET_ATS_PREFERENCE_BANDWIDTH,
483 (double) preference, GNUNET_ATS_PREFERENCE_END);
484 peer->preference_task =
485 GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
486 &update_core_preference, peer);
493 * Closure for 'add_known_to_bloom'.
495 struct BloomConstructorContext
498 * Bloom filter under construction.
500 struct GNUNET_CONTAINER_BloomFilter *bloom;
510 * Add each of the peers we already know to the bloom filter of
511 * the request so that we don't get duplicate HELLOs.
513 * @param cls the 'struct BloomConstructorContext'.
514 * @param key peer identity to add to the bloom filter
515 * @param value value the peer information (unused)
516 * @return GNUNET_YES (we should continue to iterate)
519 add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
521 struct BloomConstructorContext *ctx = cls;
524 GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Adding known peer (%s) to bloomfilter for FIND PEER with mutation %u\n",
528 GNUNET_h2s (key), ctx->bf_mutator);
530 GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
536 * Task to send a find peer message for our own peer identifier
537 * so that we can find the closest peers in the network to ourselves
538 * and attempt to connect to them.
540 * @param cls closure for this task
541 * @param tc the context under which the task is running
544 send_find_peer_message (void *cls,
545 const struct GNUNET_SCHEDULER_TaskContext *tc)
547 struct GNUNET_TIME_Relative next_send_time;
548 struct BloomConstructorContext bcc;
549 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
551 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
552 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
554 if (newly_found_peers > bucket_size)
556 /* If we are finding many peers already, no need to send out our request right now! */
558 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
559 &send_find_peer_message, NULL);
560 newly_found_peers = 0;
564 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
566 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
567 GNUNET_CONSTANTS_BLOOMFILTER_K);
568 GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, &add_known_to_bloom,
570 GNUNET_STATISTICS_update (GDS_stats,
571 gettext_noop ("# FIND PEER messages initiated"), 1,
574 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
575 GNUNET_CONSTANTS_BLOOMFILTER_K);
576 // FIXME: pass priority!?
577 GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
578 GNUNET_DHT_RO_FIND_PEER,
579 FIND_PEER_REPLICATION_LEVEL, 0,
580 &my_identity.hashPubKey, NULL, 0, bcc.bloom,
581 bcc.bf_mutator, peer_bf);
582 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
583 GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
584 /* schedule next round */
585 next_send_time.rel_value =
586 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
587 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
588 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
589 (newly_found_peers + 1));
590 newly_found_peers = 0;
592 GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
598 * Method called whenever a peer connects.
601 * @param peer peer identity this notification is about
602 * @param atsi performance data
603 * @param atsi_count number of records in 'atsi'
606 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
607 const struct GNUNET_ATS_Information *atsi,
608 unsigned int atsi_count)
610 struct PeerInfo *ret;
613 /* Check for connect to self message */
614 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected %s to %s\n",
618 GNUNET_i2s (&my_identity), GNUNET_h2s (&peer->hashPubKey));
621 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
627 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# Peers connected"), 1,
629 peer_bucket = find_bucket (&peer->hashPubKey);
630 GNUNET_assert ((peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS));
631 ret = GNUNET_malloc (sizeof (struct PeerInfo));
633 ret->latency = latency;
634 ret->distance = distance;
637 GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head,
638 k_buckets[peer_bucket].tail, ret);
639 k_buckets[peer_bucket].peers_size++;
640 closest_bucket = GNUNET_MAX (closest_bucket, peer_bucket);
641 if ((peer_bucket > 0) && (k_buckets[peer_bucket].peers_size <= bucket_size))
643 ret->preference_task =
644 GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
647 GNUNET_assert (GNUNET_OK ==
648 GNUNET_CONTAINER_multihashmap_put (all_known_peers,
649 &peer->hashPubKey, ret,
650 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
651 if (1 == GNUNET_CONTAINER_multihashmap_size (all_known_peers))
653 /* got a first connection, good time to start with FIND PEER requests... */
654 find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
660 * Method called whenever a peer disconnects.
663 * @param peer peer identity this notification is about
666 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
668 struct PeerInfo *to_remove;
670 struct P2PPendingMessage *pos;
671 unsigned int discarded;
673 /* Check for disconnect from self message */
674 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnected %s from %s\n",
678 GNUNET_i2s (&my_identity), GNUNET_h2s (&peer->hashPubKey));
681 GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
682 if (NULL == to_remove)
687 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# Peers connected"), -1,
689 GNUNET_assert (GNUNET_YES ==
690 GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
693 if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task)
695 GNUNET_SCHEDULER_cancel (to_remove->preference_task);
696 to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK;
698 current_bucket = find_bucket (&to_remove->id.hashPubKey);
699 GNUNET_assert (current_bucket >= 0);
700 GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
701 k_buckets[current_bucket].tail, to_remove);
702 GNUNET_assert (k_buckets[current_bucket].peers_size > 0);
703 k_buckets[current_bucket].peers_size--;
704 while ((closest_bucket > 0) && (k_buckets[closest_bucket].peers_size == 0))
707 if (to_remove->th != NULL)
709 GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
710 to_remove->th = NULL;
713 while (NULL != (pos = to_remove->head))
715 GNUNET_CONTAINER_DLL_remove (to_remove->head, to_remove->tail, pos);
719 GNUNET_STATISTICS_update (GDS_stats,
721 ("# Queued messages discarded (peer disconnected)"),
722 discarded, GNUNET_NO);
723 GNUNET_free (to_remove);
728 * Called when core is ready to send a message we asked for
729 * out to the destination.
731 * @param cls the 'struct PeerInfo' of the target peer
732 * @param size number of bytes available in buf
733 * @param buf where the callee should write the message
734 * @return number of bytes written to buf
737 core_transmit_notify (void *cls, size_t size, void *buf)
739 struct PeerInfo *peer = cls;
741 struct P2PPendingMessage *pending;
746 while ((NULL != (pending = peer->head)) &&
747 (GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value == 0))
749 peer->pending_count--;
750 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
751 GNUNET_free (pending);
755 /* no messages pending */
761 GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
763 GNUNET_TIME_absolute_get_remaining
764 (pending->timeout), &peer->id,
765 ntohs (pending->msg->size),
766 &core_transmit_notify, peer);
767 GNUNET_break (NULL != peer->th);
771 while ((NULL != (pending = peer->head)) &&
772 (size - off >= (msize = ntohs (pending->msg->size))))
774 GNUNET_STATISTICS_update (GDS_stats,
776 ("# Bytes transmitted to other peers"), msize,
778 memcpy (&cbuf[off], pending->msg, msize);
780 peer->pending_count--;
781 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
782 GNUNET_free (pending);
784 if (peer->head != NULL)
787 GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
789 GNUNET_TIME_absolute_get_remaining
790 (pending->timeout), &peer->id, msize,
791 &core_transmit_notify, peer);
792 GNUNET_break (NULL != peer->th);
799 * Transmit all messages in the peer's message queue.
801 * @param peer message queue to process
804 process_peer_queue (struct PeerInfo *peer)
806 struct P2PPendingMessage *pending;
808 if (NULL == (pending = peer->head))
810 if (NULL != peer->th)
812 GNUNET_STATISTICS_update (GDS_stats,
814 ("# Bytes of bandwdith requested from core"),
815 ntohs (pending->msg->size), GNUNET_NO);
817 GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
819 GNUNET_TIME_absolute_get_remaining
820 (pending->timeout), &peer->id,
821 ntohs (pending->msg->size),
822 &core_transmit_notify, peer);
823 GNUNET_break (NULL != peer->th);
828 * To how many peers should we (on average) forward the request to
829 * obtain the desired target_replication count (on average).
831 * @param hop_count number of hops the message has traversed
832 * @param target_replication the number of total paths desired
833 * @return Some number of peers to forward the message to
836 get_forward_count (uint32_t hop_count, uint32_t target_replication)
838 uint32_t random_value;
839 uint32_t forward_count;
842 if (hop_count > GDS_NSE_get () * 6.0)
844 /* forcefully terminate */
847 if (hop_count > GDS_NSE_get () * 4.0)
849 /* Once we have reached our ideal number of hops, only forward to 1 peer */
852 /* bound by system-wide maximum */
854 GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL, target_replication);
856 1 + (target_replication - 1.0) / (GDS_NSE_get () +
857 ((float) (target_replication - 1.0) *
859 /* Set forward count to floor of target_value */
860 forward_count = (uint32_t) target_value;
861 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
862 target_value = target_value - forward_count;
864 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
865 if (random_value < (target_value * UINT32_MAX))
867 return forward_count;
872 * Compute the distance between have and target as a 32-bit value.
873 * Differences in the lower bits must count stronger than differences
874 * in the higher bits.
876 * @return 0 if have==target, otherwise a number
877 * that is larger as the distance between
878 * the two hash codes increases
881 get_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
888 /* We have to represent the distance between two 2^9 (=512)-bit
889 * numbers as a 2^5 (=32)-bit number with "0" being used for the
890 * two numbers being identical; furthermore, we need to
891 * guarantee that a difference in the number of matching
892 * bits is always represented in the result.
894 * We use 2^32/2^9 numerical values to distinguish between
895 * hash codes that have the same LSB bit distance and
896 * use the highest 2^9 bits of the result to signify the
897 * number of (mis)matching LSB bits; if we have 0 matching
898 * and hence 512 mismatching LSB bits we return -1 (since
899 * 512 itself cannot be represented with 9 bits) */
901 /* first, calculate the most significant 9 bits of our
902 * result, aka the number of LSBs */
903 bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
904 /* bucket is now a value between 0 and 512 */
906 return 0; /* perfect match */
908 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
909 * below, we'd end up with max+1 (overflow)) */
911 /* calculate the most significant bits of the final result */
912 msb = (512 - bucket) << (32 - 9);
913 /* calculate the 32-9 least significant bits of the final result by
914 * looking at the differences in the 32-9 bits following the
915 * mismatching bit at 'bucket' */
918 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
920 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
921 GNUNET_CRYPTO_hash_get_bit (have, i))
922 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
923 * last bit set will be 31 -- if
924 * i does not reach 512 first... */
931 * Check whether my identity is closer than any known peers. If a
932 * non-null bloomfilter is given, check if this is the closest peer
933 * that hasn't already been routed to.
935 * @param key hash code to check closeness to
936 * @param bloom bloomfilter, exclude these entries from the decision
937 * @return GNUNET_YES if node location is closest,
938 * GNUNET_NO otherwise.
941 am_closest_peer (const GNUNET_HashCode * key,
942 const struct GNUNET_CONTAINER_BloomFilter *bloom)
948 struct PeerInfo *pos;
950 if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
952 bucket_num = find_bucket (key);
953 GNUNET_assert (bucket_num >= 0);
954 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, key);
955 pos = k_buckets[bucket_num].head;
957 while ((pos != NULL) && (count < bucket_size))
959 if ((bloom != NULL) &&
961 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
964 continue; /* Skip already checked entries */
966 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, key);
967 if (other_bits > bits)
969 if (other_bits == bits) /* We match the same number of bits */
973 /* No peers closer, we are the closest! */
979 * Select a peer from the routing table that would be a good routing
980 * destination for sending a message for "key". The resulting peer
981 * must not be in the set of blocked peers.<p>
983 * Note that we should not ALWAYS select the closest peer to the
984 * target, peers further away from the target should be chosen with
985 * exponentially declining probability.
987 * FIXME: double-check that this is fine
990 * @param key the key we are selecting a peer to route to
991 * @param bloom a bloomfilter containing entries this request has seen already
992 * @param hops how many hops has this message traversed thus far
993 * @return Peer to route to, or NULL on error
995 static struct PeerInfo *
996 select_peer (const GNUNET_HashCode * key,
997 const struct GNUNET_CONTAINER_BloomFilter *bloom, uint32_t hops)
1001 unsigned int selected;
1002 struct PeerInfo *pos;
1004 unsigned int smallest_distance;
1005 struct PeerInfo *chosen;
1007 if (hops >= GDS_NSE_get ())
1009 /* greedy selection (closest peer that is not in bloomfilter) */
1010 smallest_distance = UINT_MAX;
1012 for (bc = 0; bc <= closest_bucket; bc++)
1014 pos = k_buckets[bc].head;
1016 while ((pos != NULL) && (count < bucket_size))
1018 if ((bloom == NULL) ||
1020 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
1022 dist = get_distance (key, &pos->id.hashPubKey);
1023 if (dist < smallest_distance)
1026 smallest_distance = dist;
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Excluded peer `%s' due to BF match in greedy routing for %s\n",
1034 GNUNET_i2s (&pos->id), GNUNET_h2s (key));
1036 GNUNET_STATISTICS_update (GDS_stats,
1038 ("# Peers excluded from routing due to Bloomfilter"),
1046 GNUNET_STATISTICS_update (GDS_stats,
1047 gettext_noop ("# Peer selection failed"), 1,
1052 /* select "random" peer */
1053 /* count number of peers that are available and not filtered */
1055 for (bc = 0; bc <= closest_bucket; bc++)
1057 pos = k_buckets[bc].head;
1058 while ((pos != NULL) && (count < bucket_size))
1060 if ((bloom != NULL) &&
1062 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
1064 GNUNET_STATISTICS_update (GDS_stats,
1066 ("# Peers excluded from routing due to Bloomfilter"),
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1070 "Excluded peer `%s' due to BF match in random routing for %s\n",
1071 GNUNET_i2s (&pos->id), GNUNET_h2s (key));
1074 continue; /* Ignore bloomfiltered peers */
1080 if (count == 0) /* No peers to select from! */
1082 GNUNET_STATISTICS_update (GDS_stats,
1083 gettext_noop ("# Peer selection failed"), 1,
1087 /* Now actually choose a peer */
1088 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1090 for (bc = 0; bc <= closest_bucket; bc++)
1092 pos = k_buckets[bc].head;
1093 while ((pos != NULL) && (count < bucket_size))
1095 if ((bloom != NULL) &&
1097 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
1100 continue; /* Ignore bloomfiltered peers */
1102 if (0 == selected--)
1113 * Compute the set of peers that the given request should be
1116 * @param key routing key
1117 * @param bloom bloom filter excluding peers as targets, all selected
1118 * peers will be added to the bloom filter
1119 * @param hop_count number of hops the request has traversed so far
1120 * @param target_replication desired number of replicas
1121 * @param targets where to store an array of target peers (to be
1122 * free'd by the caller)
1123 * @return number of peers returned in 'targets'.
1126 get_target_peers (const GNUNET_HashCode * key,
1127 struct GNUNET_CONTAINER_BloomFilter *bloom,
1128 uint32_t hop_count, uint32_t target_replication,
1129 struct PeerInfo ***targets)
1133 struct PeerInfo **rtargets;
1134 struct PeerInfo *nxt;
1136 GNUNET_assert (NULL != bloom);
1137 ret = get_forward_count (hop_count, target_replication);
1143 rtargets = GNUNET_malloc (sizeof (struct PeerInfo *) * ret);
1144 for (off = 0; off < ret; off++)
1146 nxt = select_peer (key, bloom, hop_count);
1149 rtargets[off] = nxt;
1150 GNUNET_break (GNUNET_NO ==
1151 GNUNET_CONTAINER_bloomfilter_test (bloom,
1152 &nxt->id.hashPubKey));
1153 GNUNET_CONTAINER_bloomfilter_add (bloom, &rtargets[off]->id.hashPubKey);
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "Selected %u/%u peers at hop %u for %s (target was %u)\n", off,
1158 GNUNET_CONTAINER_multihashmap_size (all_known_peers),
1159 (unsigned int) hop_count, GNUNET_h2s (key), ret);
1163 GNUNET_free (rtargets);
1167 *targets = rtargets;
1173 * Perform a PUT operation. Forwards the given request to other
1174 * peers. Does not store the data locally. Does not give the
1175 * data to local clients. May do nothing if this is the only
1176 * peer in the network (or if we are the closest peer in the
1179 * @param type type of the block
1180 * @param options routing options
1181 * @param desired_replication_level desired replication count
1182 * @param expiration_time when does the content expire
1183 * @param hop_count how many hops has this message traversed so far
1184 * @param bf Bloom filter of peers this PUT has already traversed
1185 * @param key key for the content
1186 * @param put_path_length number of entries in put_path
1187 * @param put_path peers this request has traversed so far (if tracked)
1188 * @param data payload to store
1189 * @param data_size number of bytes in data
1192 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1193 enum GNUNET_DHT_RouteOption options,
1194 uint32_t desired_replication_level,
1195 struct GNUNET_TIME_Absolute expiration_time,
1197 struct GNUNET_CONTAINER_BloomFilter *bf,
1198 const GNUNET_HashCode * key,
1199 unsigned int put_path_length,
1200 struct GNUNET_PeerIdentity *put_path,
1201 const void *data, size_t data_size)
1203 unsigned int target_count;
1205 struct PeerInfo **targets;
1206 struct PeerInfo *target;
1207 struct P2PPendingMessage *pending;
1209 struct PeerPutMessage *ppm;
1210 struct GNUNET_PeerIdentity *pp;
1212 GNUNET_assert (NULL != bf);
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "Adding myself (%s) to PUT bloomfilter for %s\n",
1216 GNUNET_i2s (&my_identity), GNUNET_h2s (key));
1218 GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity.hashPubKey);
1219 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# PUT requests routed"),
1222 get_target_peers (key, bf, hop_count, desired_replication_level,
1224 if (0 == target_count)
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Routing PUT for %s terminates after %u hops at %s\n",
1229 GNUNET_h2s (key), (unsigned int) hop_count,
1230 GNUNET_i2s (&my_identity));
1235 put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
1236 sizeof (struct PeerPutMessage);
1237 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1239 put_path_length = 0;
1240 msize = data_size + sizeof (struct PeerPutMessage);
1242 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1245 GNUNET_free (targets);
1248 GNUNET_STATISTICS_update (GDS_stats,
1250 ("# PUT messages queued for transmission"),
1251 target_count, GNUNET_NO);
1252 for (i = 0; i < target_count; i++)
1254 target = targets[i];
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Routing PUT for %s after %u hops to %s\n", GNUNET_h2s (key),
1258 (unsigned int) hop_count, GNUNET_i2s (&target->id));
1260 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1261 pending->importance = 0; /* FIXME */
1262 pending->timeout = expiration_time;
1263 ppm = (struct PeerPutMessage *) &pending[1];
1264 pending->msg = &ppm->header;
1265 ppm->header.size = htons (msize);
1266 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1267 ppm->options = htonl (options);
1268 ppm->type = htonl (type);
1269 ppm->hop_count = htonl (hop_count + 1);
1270 ppm->desired_replication_level = htonl (desired_replication_level);
1271 ppm->put_path_length = htonl (put_path_length);
1272 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1273 GNUNET_break (GNUNET_YES ==
1274 GNUNET_CONTAINER_bloomfilter_test (bf,
1275 &target->id.hashPubKey));
1276 GNUNET_assert (GNUNET_OK ==
1277 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
1281 pp = (struct GNUNET_PeerIdentity *) &ppm[1];
1282 memcpy (pp, put_path,
1283 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1284 memcpy (&pp[put_path_length], data, data_size);
1285 GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending);
1286 target->pending_count++;
1287 process_peer_queue (target);
1289 GNUNET_free (targets);
1294 * Perform a GET operation. Forwards the given request to other
1295 * peers. Does not lookup the key locally. May do nothing if this is
1296 * the only peer in the network (or if we are the closest peer in the
1299 * @param type type of the block
1300 * @param options routing options
1301 * @param desired_replication_level desired replication count
1302 * @param hop_count how many hops did this request traverse so far?
1303 * @param key key for the content
1304 * @param xquery extended query
1305 * @param xquery_size number of bytes in xquery
1306 * @param reply_bf bloomfilter to filter duplicates
1307 * @param reply_bf_mutator mutator for reply_bf
1308 * @param peer_bf filter for peers not to select (again)
1311 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1312 enum GNUNET_DHT_RouteOption options,
1313 uint32_t desired_replication_level,
1314 uint32_t hop_count, const GNUNET_HashCode * key,
1315 const void *xquery, size_t xquery_size,
1316 const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
1317 uint32_t reply_bf_mutator,
1318 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1320 unsigned int target_count;
1322 struct PeerInfo **targets;
1323 struct PeerInfo *target;
1324 struct P2PPendingMessage *pending;
1326 struct PeerGetMessage *pgm;
1328 size_t reply_bf_size;
1330 GNUNET_assert (NULL != peer_bf);
1331 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# GET requests routed"),
1334 get_target_peers (key, peer_bf, hop_count, desired_replication_level,
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Adding myself (%s) to GET bloomfilter for %s\n",
1339 GNUNET_i2s (&my_identity), GNUNET_h2s (key));
1341 GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity.hashPubKey);
1342 if (0 == target_count)
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 "Routing GET for %s terminates after %u hops at %s\n",
1347 GNUNET_h2s (key), (unsigned int) hop_count,
1348 GNUNET_i2s (&my_identity));
1352 reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
1353 msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
1354 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1357 GNUNET_free (targets);
1360 GNUNET_STATISTICS_update (GDS_stats,
1362 ("# GET messages queued for transmission"),
1363 target_count, GNUNET_NO);
1364 /* forward request */
1365 for (i = 0; i < target_count; i++)
1367 target = targets[i];
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "Routing GET for %s after %u hops to %s\n", GNUNET_h2s (key),
1371 (unsigned int) hop_count, GNUNET_i2s (&target->id));
1373 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1374 pending->importance = 0; /* FIXME */
1375 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1376 pgm = (struct PeerGetMessage *) &pending[1];
1377 pending->msg = &pgm->header;
1378 pgm->header.size = htons (msize);
1379 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1380 pgm->options = htonl (options);
1381 pgm->type = htonl (type);
1382 pgm->hop_count = htonl (hop_count + 1);
1383 pgm->desired_replication_level = htonl (desired_replication_level);
1384 pgm->xquery_size = htonl (xquery_size);
1385 pgm->bf_mutator = reply_bf_mutator;
1386 GNUNET_break (GNUNET_YES ==
1387 GNUNET_CONTAINER_bloomfilter_test (peer_bf,
1388 &target->id.hashPubKey));
1389 GNUNET_assert (GNUNET_OK ==
1390 GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
1394 xq = (char *) &pgm[1];
1395 memcpy (xq, xquery, xquery_size);
1396 if (NULL != reply_bf)
1397 GNUNET_assert (GNUNET_OK ==
1398 GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
1402 GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending);
1403 target->pending_count++;
1404 process_peer_queue (target);
1406 GNUNET_free (targets);
1411 * Handle a reply (route to origin). Only forwards the reply back to
1412 * the given peer. Does not do local caching or forwarding to local
1415 * @param target neighbour that should receive the block (if still connected)
1416 * @param type type of the block
1417 * @param expiration_time when does the content expire
1418 * @param key key for the content
1419 * @param put_path_length number of entries in put_path
1420 * @param put_path peers the original PUT traversed (if tracked)
1421 * @param get_path_length number of entries in put_path
1422 * @param get_path peers this reply has traversed so far (if tracked)
1423 * @param data payload of the reply
1424 * @param data_size number of bytes in data
1427 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1428 enum GNUNET_BLOCK_Type type,
1429 struct GNUNET_TIME_Absolute expiration_time,
1430 const GNUNET_HashCode * key,
1431 unsigned int put_path_length,
1432 const struct GNUNET_PeerIdentity *put_path,
1433 unsigned int get_path_length,
1434 const struct GNUNET_PeerIdentity *get_path,
1435 const void *data, size_t data_size)
1437 struct PeerInfo *pi;
1438 struct P2PPendingMessage *pending;
1440 struct PeerResultMessage *prm;
1441 struct GNUNET_PeerIdentity *paths;
1444 data_size + sizeof (struct PeerResultMessage) + (get_path_length +
1446 sizeof (struct GNUNET_PeerIdentity);
1447 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1449 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1451 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1452 (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE))
1457 pi = GNUNET_CONTAINER_multihashmap_get (all_known_peers, &target->hashPubKey);
1460 /* peer disconnected in the meantime, drop reply */
1463 GNUNET_STATISTICS_update (GDS_stats,
1465 ("# RESULT messages queued for transmission"), 1,
1467 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1468 pending->importance = 0; /* FIXME */
1469 pending->timeout = expiration_time;
1470 prm = (struct PeerResultMessage *) &pending[1];
1471 pending->msg = &prm->header;
1472 prm->header.size = htons (msize);
1473 prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1474 prm->type = htonl (type);
1475 prm->put_path_length = htonl (put_path_length);
1476 prm->get_path_length = htonl (get_path_length);
1477 prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1479 paths = (struct GNUNET_PeerIdentity *) &prm[1];
1480 memcpy (paths, put_path,
1481 put_path_length * sizeof (struct GNUNET_PeerIdentity));
1482 memcpy (&paths[put_path_length], get_path,
1483 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1484 memcpy (&paths[put_path_length + get_path_length], data, data_size);
1485 GNUNET_CONTAINER_DLL_insert (pi->head, pi->tail, pending);
1486 pi->pending_count++;
1487 process_peer_queue (pi);
1492 * To be called on core init/fail.
1494 * @param cls service closure
1495 * @param server handle to the server for this service
1496 * @param identity the public identity of this peer
1499 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1500 const struct GNUNET_PeerIdentity *identity)
1502 GNUNET_assert (server != NULL);
1503 my_identity = *identity;
1508 * Core handler for p2p put requests.
1510 * @param cls closure
1511 * @param peer sender of the request
1512 * @param message message
1513 * @param peer peer identity this notification is about
1514 * @param atsi performance data
1515 * @param atsi_count number of records in 'atsi'
1516 * @return GNUNET_OK to keep the connection open,
1517 * GNUNET_SYSERR to close it (signal serious error)
1520 handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
1521 const struct GNUNET_MessageHeader *message,
1522 const struct GNUNET_ATS_Information *atsi,
1523 unsigned int atsi_count)
1525 const struct PeerPutMessage *put;
1526 const struct GNUNET_PeerIdentity *put_path;
1527 const void *payload;
1530 size_t payload_size;
1531 enum GNUNET_DHT_RouteOption options;
1532 struct GNUNET_CONTAINER_BloomFilter *bf;
1533 GNUNET_HashCode test_key;
1535 msize = ntohs (message->size);
1536 if (msize < sizeof (struct PeerPutMessage))
1538 GNUNET_break_op (0);
1541 put = (const struct PeerPutMessage *) message;
1542 putlen = ntohl (put->put_path_length);
1544 sizeof (struct PeerPutMessage) +
1545 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
1547 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
1549 GNUNET_break_op (0);
1552 GNUNET_STATISTICS_update (GDS_stats,
1553 gettext_noop ("# P2P PUT requests received"), 1,
1555 put_path = (const struct GNUNET_PeerIdentity *) &put[1];
1556 payload = &put_path[putlen];
1557 options = ntohl (put->options);
1559 msize - (sizeof (struct PeerPutMessage) +
1560 putlen * sizeof (struct GNUNET_PeerIdentity));
1561 switch (GNUNET_BLOCK_get_key
1562 (GDS_block_context, ntohl (put->type), payload, payload_size,
1566 if (0 != memcmp (&test_key, &put->key, sizeof (GNUNET_HashCode)))
1568 GNUNET_break_op (0);
1573 GNUNET_break_op (0);
1576 /* cannot verify, good luck */
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PUT for %s at %s\n",
1581 GNUNET_h2s (&put->key), GNUNET_i2s (&my_identity));
1583 bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter, DHT_BLOOM_SIZE,
1584 GNUNET_CONSTANTS_BLOOMFILTER_K);
1585 GNUNET_break_op (GNUNET_YES ==
1586 GNUNET_CONTAINER_bloomfilter_test (bf, &peer->hashPubKey));
1588 struct GNUNET_PeerIdentity pp[putlen + 1];
1590 /* extend 'put path' by sender */
1591 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
1593 memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
1600 /* give to local clients */
1601 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1602 &put->key, 0, NULL, putlen, pp, ntohl (put->type),
1603 payload_size, payload);
1605 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1606 (am_closest_peer (&put->key, bf)))
1607 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh
1608 (put->expiration_time), &put->key, putlen, pp,
1609 ntohl (put->type), payload_size, payload);
1610 /* route to other peers */
1611 GDS_NEIGHBOURS_handle_put (ntohl (put->type), options,
1612 ntohl (put->desired_replication_level),
1613 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1614 ntohl (put->hop_count), bf, &put->key, putlen,
1615 pp, payload, payload_size);
1617 GNUNET_CONTAINER_bloomfilter_free (bf);
1623 * We have received a FIND PEER request. Send matching
1626 * @param sender sender of the FIND PEER request
1627 * @param key peers close to this key are desired
1628 * @param bf peers matching this bf are excluded
1629 * @param bf_mutator mutator for bf
1632 handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1633 const GNUNET_HashCode * key,
1634 struct GNUNET_CONTAINER_BloomFilter *bf, uint32_t bf_mutator)
1637 struct PeerBucket *bucket;
1638 struct PeerInfo *peer;
1639 unsigned int choice;
1640 GNUNET_HashCode mhash;
1641 const struct GNUNET_HELLO_Message *hello;
1643 /* first, check about our own HELLO */
1644 if (NULL != GDS_my_hello)
1646 GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
1648 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)))
1650 GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO,
1651 GNUNET_TIME_relative_to_absolute
1652 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1653 key, 0, NULL, 0, NULL, GDS_my_hello,
1654 GNUNET_HELLO_size ((const struct
1655 GNUNET_HELLO_Message *)
1660 GNUNET_STATISTICS_update (GDS_stats,
1662 ("# FIND PEER requests ignored due to Bloomfilter"),
1668 GNUNET_STATISTICS_update (GDS_stats,
1670 ("# FIND PEER requests ignored due to lack of HELLO"),
1674 /* then, also consider sending a random HELLO from the closest bucket */
1675 if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
1676 bucket_idx = closest_bucket;
1678 bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key));
1679 if (bucket_idx == GNUNET_SYSERR)
1681 bucket = &k_buckets[bucket_idx];
1682 if (bucket->peers_size == 0)
1685 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, bucket->peers_size);
1686 peer = bucket->head;
1689 GNUNET_assert (peer != NULL);
1693 choice = bucket->peers_size;
1698 return; /* no non-masked peer available */
1700 peer = bucket->head;
1701 GNUNET_BLOCK_mingle_hash (&peer->id.hashPubKey, bf_mutator, &mhash);
1702 hello = GDS_HELLO_get (&peer->id);
1704 while ((hello == NULL) ||
1705 (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)));
1706 GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO,
1707 GNUNET_TIME_relative_to_absolute
1708 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION), key,
1709 0, NULL, 0, NULL, hello,
1710 GNUNET_HELLO_size (hello));
1715 * Core handler for p2p get requests.
1717 * @param cls closure
1718 * @param peer sender of the request
1719 * @param message message
1720 * @param peer peer identity this notification is about
1721 * @param atsi performance data
1722 * @param atsi_count number of records in 'atsi'
1723 * @return GNUNET_OK to keep the connection open,
1724 * GNUNET_SYSERR to close it (signal serious error)
1727 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
1728 const struct GNUNET_MessageHeader *message,
1729 const struct GNUNET_ATS_Information *atsi,
1730 unsigned int atsi_count)
1732 struct PeerGetMessage *get;
1733 uint32_t xquery_size;
1734 size_t reply_bf_size;
1736 enum GNUNET_BLOCK_Type type;
1737 enum GNUNET_DHT_RouteOption options;
1738 enum GNUNET_BLOCK_EvaluationResult eval;
1739 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
1740 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
1744 memcmp (peer, &my_identity,
1745 sizeof (struct GNUNET_PeerIdentity)));
1746 /* parse and validate message */
1747 msize = ntohs (message->size);
1748 if (msize < sizeof (struct PeerGetMessage))
1750 GNUNET_break_op (0);
1753 get = (struct PeerGetMessage *) message;
1754 xquery_size = ntohl (get->xquery_size);
1755 if (msize < sizeof (struct PeerGetMessage) + xquery_size)
1757 GNUNET_break_op (0);
1760 GNUNET_STATISTICS_update (GDS_stats,
1761 gettext_noop ("# P2P GET requests received"), 1,
1763 reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
1764 type = ntohl (get->type);
1765 options = ntohl (get->options);
1766 xquery = (const char *) &get[1];
1768 if (reply_bf_size > 0)
1770 GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size], reply_bf_size,
1771 GNUNET_CONSTANTS_BLOOMFILTER_K);
1773 GNUNET_BLOCK_evaluate (GDS_block_context, type, &get->key, &reply_bf,
1774 get->bf_mutator, xquery, xquery_size, NULL, 0);
1775 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
1777 /* request invalid or block type not supported */
1778 GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
1779 if (NULL != reply_bf)
1780 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1784 GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, DHT_BLOOM_SIZE,
1785 GNUNET_CONSTANTS_BLOOMFILTER_K);
1786 GNUNET_break_op (GNUNET_YES ==
1787 GNUNET_CONTAINER_bloomfilter_test (peer_bf,
1788 &peer->hashPubKey));
1789 /* remember request for routing replies */
1790 GDS_ROUTING_add (peer, type, options, &get->key, xquery, xquery_size,
1791 reply_bf, get->bf_mutator);
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GET for %s at %s after %u hops\n",
1794 GNUNET_h2s (&get->key), GNUNET_i2s (&my_identity),
1795 (unsigned int) ntohl (get->hop_count));
1797 /* local lookup (this may update the reply_bf) */
1798 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1799 (am_closest_peer (&get->key, peer_bf)))
1801 if ((0 != (options & GNUNET_DHT_RO_FIND_PEER)))
1803 GNUNET_STATISTICS_update (GDS_stats,
1805 ("# P2P FIND PEER requests processed"), 1,
1807 handle_find_peer (peer, &get->key, reply_bf, get->bf_mutator);
1812 GDS_DATACACHE_handle_get (&get->key, type, xquery, xquery_size,
1813 &reply_bf, get->bf_mutator);
1818 GNUNET_STATISTICS_update (GDS_stats,
1819 gettext_noop ("# P2P GET requests ONLY routed"),
1823 /* P2P forwarding */
1824 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1825 GDS_NEIGHBOURS_handle_get (type, options,
1826 ntohl (get->desired_replication_level),
1827 ntohl (get->hop_count), &get->key, xquery,
1828 xquery_size, reply_bf, get->bf_mutator, peer_bf);
1830 if (NULL != reply_bf)
1831 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1832 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
1838 * Core handler for p2p result messages.
1840 * @param cls closure
1841 * @param message message
1842 * @param peer peer identity this notification is about
1843 * @param atsi performance data
1844 * @param atsi_count number of records in 'atsi'
1845 * @return GNUNET_YES (do not cut p2p connection)
1848 handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
1849 const struct GNUNET_MessageHeader *message,
1850 const struct GNUNET_ATS_Information *atsi,
1851 unsigned int atsi_count)
1853 const struct PeerResultMessage *prm;
1854 const struct GNUNET_PeerIdentity *put_path;
1855 const struct GNUNET_PeerIdentity *get_path;
1857 uint32_t get_path_length;
1858 uint32_t put_path_length;
1861 enum GNUNET_BLOCK_Type type;
1863 /* parse and validate message */
1864 msize = ntohs (message->size);
1865 if (msize < sizeof (struct PeerResultMessage))
1867 GNUNET_break_op (0);
1870 prm = (struct PeerResultMessage *) message;
1871 put_path_length = ntohl (prm->put_path_length);
1872 get_path_length = ntohl (prm->get_path_length);
1874 sizeof (struct PeerResultMessage) + (get_path_length +
1876 sizeof (struct GNUNET_PeerIdentity)) ||
1878 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1880 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
1882 GNUNET_break_op (0);
1885 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P RESULTS received"),
1887 put_path = (const struct GNUNET_PeerIdentity *) &prm[1];
1888 get_path = &put_path[put_path_length];
1889 type = ntohl (prm->type);
1890 data = (const void *) &get_path[get_path_length];
1892 msize - (sizeof (struct PeerResultMessage) +
1894 put_path_length) * sizeof (struct GNUNET_PeerIdentity));
1896 /* if we got a HELLO, consider it for our own routing table */
1897 if (type == GNUNET_BLOCK_TYPE_DHT_HELLO)
1899 const struct GNUNET_MessageHeader *h;
1900 struct GNUNET_PeerIdentity pid;
1903 /* Should be a HELLO, validate and consider using it! */
1904 if (data_size < sizeof (struct GNUNET_MessageHeader))
1906 GNUNET_break_op (0);
1910 if (data_size != ntohs (h->size))
1912 GNUNET_break_op (0);
1916 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h, &pid))
1918 GNUNET_break_op (0);
1921 if (0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
1923 bucket = find_bucket (&pid.hashPubKey);
1924 if ((bucket >= 0) && (k_buckets[bucket].peers_size < bucket_size))
1926 if (NULL != GDS_transport_handle)
1928 GNUNET_TRANSPORT_offer_hello (GDS_transport_handle, h, NULL, NULL);
1929 GNUNET_TRANSPORT_try_connect (GDS_transport_handle, &pid);
1935 /* append 'peer' to 'get_path' */
1937 struct GNUNET_PeerIdentity xget_path[get_path_length + 1];
1939 memcpy (xget_path, get_path,
1940 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1941 xget_path[get_path_length] = *peer;
1944 /* forward to local clients */
1945 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1946 &prm->key, get_path_length, xget_path,
1947 put_path_length, put_path, type, data_size, data);
1949 /* forward to other peers */
1950 GDS_ROUTING_process (type, GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1951 &prm->key, put_path_length, put_path, get_path_length,
1952 xget_path, data, data_size);
1959 * Initialize neighbours subsystem.
1961 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1964 GDS_NEIGHBOURS_init ()
1966 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1967 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
1968 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
1969 {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0},
1972 unsigned long long temp_config_num;
1975 GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size",
1977 bucket_size = (unsigned int) temp_config_num;
1978 atsAPI = GNUNET_ATS_performance_init (GDS_cfg, NULL, NULL);
1980 GNUNET_CORE_connect (GDS_cfg, 1, NULL, &core_init, &handle_core_connect,
1981 &handle_core_disconnect, NULL, GNUNET_NO, NULL,
1982 GNUNET_NO, core_handlers);
1983 if (coreAPI == NULL)
1984 return GNUNET_SYSERR;
1985 all_known_peers = GNUNET_CONTAINER_multihashmap_create (256);
1991 * Shutdown neighbours subsystem.
1994 GDS_NEIGHBOURS_done ()
1996 if (coreAPI == NULL)
1998 GNUNET_CORE_disconnect (coreAPI);
2000 GNUNET_ATS_performance_done (atsAPI);
2002 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (all_known_peers));
2003 GNUNET_CONTAINER_multihashmap_destroy (all_known_peers);
2004 all_known_peers = NULL;
2005 if (GNUNET_SCHEDULER_NO_TASK != find_peer_task)
2007 GNUNET_SCHEDULER_cancel (find_peer_task);
2008 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
2013 /* end of gnunet-service-dht_neighbours.c */