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 (struct 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 * Maximum allowed number of pending messages per peer.
75 #define MAXIMUM_PENDING_PER_PEER 64
78 * How often to update our preference levels for peers in our routing tables.
80 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
83 * How long at least to wait before sending another find peer request.
85 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
88 * How long at most to wait before sending another find peer request.
90 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10)
93 * How long at most to wait for transmission of a GET request to another peer?
95 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
98 GNUNET_NETWORK_STRUCT_BEGIN
103 struct PeerPutMessage
106 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
108 struct GNUNET_MessageHeader header;
113 uint32_t options GNUNET_PACKED;
118 uint32_t type GNUNET_PACKED;
123 uint32_t hop_count GNUNET_PACKED;
126 * Replication level for this message
128 uint32_t desired_replication_level GNUNET_PACKED;
131 * Length of the PUT path that follows (if tracked).
133 uint32_t put_path_length GNUNET_PACKED;
136 * When does the content expire?
138 struct GNUNET_TIME_AbsoluteNBO expiration_time;
141 * Bloomfilter (for peer identities) to stop circular routes
143 char bloomfilter[DHT_BLOOM_SIZE];
146 * The key we are storing under.
148 struct GNUNET_HashCode key;
150 /* put path (if tracked) */
160 struct PeerResultMessage
163 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
165 struct GNUNET_MessageHeader header;
170 uint32_t type GNUNET_PACKED;
173 * Length of the PUT path that follows (if tracked).
175 uint32_t put_path_length GNUNET_PACKED;
178 * Length of the GET path that follows (if tracked).
180 uint32_t get_path_length GNUNET_PACKED;
183 * When does the content expire?
185 struct GNUNET_TIME_AbsoluteNBO expiration_time;
188 * The key of the corresponding GET request.
190 struct GNUNET_HashCode key;
192 /* put path (if tracked) */
194 /* get path (if tracked) */
204 struct PeerGetMessage
207 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_GET
209 struct GNUNET_MessageHeader header;
214 uint32_t options GNUNET_PACKED;
217 * Desired content type.
219 uint32_t type GNUNET_PACKED;
224 uint32_t hop_count GNUNET_PACKED;
227 * Desired replication level for this request.
229 uint32_t desired_replication_level GNUNET_PACKED;
232 * Size of the extended query.
234 uint32_t xquery_size;
237 * Bloomfilter mutator.
242 * Bloomfilter (for peer identities) to stop circular routes
244 char bloomfilter[DHT_BLOOM_SIZE];
247 * The key we are looking for.
249 struct GNUNET_HashCode key;
253 /* result bloomfilter */
256 GNUNET_NETWORK_STRUCT_END
259 * Linked list of messages to send to a particular other peer.
261 struct P2PPendingMessage
264 * Pointer to next item in the list
266 struct P2PPendingMessage *next;
269 * Pointer to previous item in the list
271 struct P2PPendingMessage *prev;
274 * Message importance level. FIXME: used? useful?
276 unsigned int importance;
279 * When does this message time out?
281 struct GNUNET_TIME_Absolute timeout;
284 * Actual message to be sent, allocated at the end of the struct:
285 * // msg = (cast) &pm[1];
286 * // memcpy (&pm[1], data, len);
288 const struct GNUNET_MessageHeader *msg;
294 * Entry for a peer in a bucket.
299 * Next peer entry (DLL)
301 struct PeerInfo *next;
304 * Prev peer entry (DLL)
306 struct PeerInfo *prev;
309 * Count of outstanding messages for peer.
311 unsigned int pending_count;
314 * Head of pending messages to be sent to this peer.
316 struct P2PPendingMessage *head;
319 * Tail of pending messages to be sent to this peer.
321 struct P2PPendingMessage *tail;
324 * Core handle for sending messages to this peer.
326 struct GNUNET_CORE_TransmitHandle *th;
329 * Task for scheduling preference updates
331 GNUNET_SCHEDULER_TaskIdentifier preference_task;
334 * What is the identity of the peer?
336 struct GNUNET_PeerIdentity id;
340 * What is the average latency for replies received?
342 struct GNUNET_TIME_Relative latency;
345 * Transport level distance to peer.
347 unsigned int distance;
354 * Peers are grouped into buckets.
361 struct PeerInfo *head;
366 struct PeerInfo *tail;
369 * Number of peers in the bucket.
371 unsigned int peers_size;
376 * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
378 static unsigned int closest_bucket;
381 * How many peers have we added since we sent out our last
384 static unsigned int newly_found_peers;
387 * The buckets. Array of size MAX_BUCKET_SIZE. Offset 0 means 0 bits matching.
389 static struct PeerBucket k_buckets[MAX_BUCKETS];
392 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
394 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
397 * Maximum size for each bucket.
399 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
402 * Task that sends FIND PEER requests.
404 static GNUNET_SCHEDULER_TaskIdentifier find_peer_task;
407 * Identity of this peer.
409 static struct GNUNET_PeerIdentity my_identity;
414 static struct GNUNET_CORE_Handle *coreAPI;
419 static struct GNUNET_ATS_PerformanceHandle *atsAPI;
424 * Find the optimal bucket for this key.
426 * @param hc the hashcode to compare our identity to
427 * @return the proper bucket index, or GNUNET_SYSERR
428 * on error (same hashcode)
431 find_bucket (const struct GNUNET_HashCode * hc)
435 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
436 if (bits == MAX_BUCKETS)
438 /* How can all bits match? Got my own ID? */
440 return GNUNET_SYSERR;
442 return MAX_BUCKETS - bits - 1;
447 * Let GNUnet core know that we like the given peer.
449 * @param cls the 'struct PeerInfo' of the peer
450 * @param tc scheduler context.
453 update_core_preference (void *cls,
454 const struct GNUNET_SCHEDULER_TaskContext *tc)
456 struct PeerInfo *peer = cls;
458 unsigned int matching;
461 peer->preference_task = GNUNET_SCHEDULER_NO_TASK;
462 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
465 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
466 &peer->id.hashPubKey);
469 bucket = find_bucket (&peer->id.hashPubKey);
470 if (bucket == GNUNET_SYSERR)
474 GNUNET_assert (k_buckets[bucket].peers_size != 0);
475 preference = (1LL << matching) / k_buckets[bucket].peers_size;
479 peer->preference_task =
480 GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
481 &update_core_preference, peer);
484 GNUNET_STATISTICS_update (GDS_stats,
485 gettext_noop ("# Preference updates given to core"),
487 GNUNET_ATS_change_preference (atsAPI, &peer->id,
488 GNUNET_ATS_PREFERENCE_BANDWIDTH,
489 (double) preference, GNUNET_ATS_PREFERENCE_END);
490 peer->preference_task =
491 GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
492 &update_core_preference, peer);
499 * Closure for 'add_known_to_bloom'.
501 struct BloomConstructorContext
504 * Bloom filter under construction.
506 struct GNUNET_CONTAINER_BloomFilter *bloom;
516 * Add each of the peers we already know to the bloom filter of
517 * the request so that we don't get duplicate HELLOs.
519 * @param cls the 'struct BloomConstructorContext'.
520 * @param key peer identity to add to the bloom filter
521 * @param value value the peer information (unused)
522 * @return GNUNET_YES (we should continue to iterate)
525 add_known_to_bloom (void *cls, const struct GNUNET_HashCode * key, void *value)
527 struct BloomConstructorContext *ctx = cls;
528 struct GNUNET_HashCode mh;
530 GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 "Adding known peer (%s) to bloomfilter for FIND PEER with mutation %u\n",
533 GNUNET_h2s (key), ctx->bf_mutator);
534 GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
540 * Task to send a find peer message for our own peer identifier
541 * so that we can find the closest peers in the network to ourselves
542 * and attempt to connect to them.
544 * @param cls closure for this task
545 * @param tc the context under which the task is running
548 send_find_peer_message (void *cls,
549 const struct GNUNET_SCHEDULER_TaskContext *tc)
551 struct GNUNET_TIME_Relative next_send_time;
552 struct BloomConstructorContext bcc;
553 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
555 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
556 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
558 if (newly_found_peers > bucket_size)
560 /* If we are finding many peers already, no need to send out our request right now! */
562 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
563 &send_find_peer_message, NULL);
564 newly_found_peers = 0;
568 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
570 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
571 GNUNET_CONSTANTS_BLOOMFILTER_K);
572 GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, &add_known_to_bloom,
574 GNUNET_STATISTICS_update (GDS_stats,
575 gettext_noop ("# FIND PEER messages initiated"), 1,
578 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
579 GNUNET_CONSTANTS_BLOOMFILTER_K);
580 // FIXME: pass priority!?
581 GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
582 GNUNET_DHT_RO_FIND_PEER,
583 FIND_PEER_REPLICATION_LEVEL, 0,
584 &my_identity.hashPubKey, NULL, 0, bcc.bloom,
585 bcc.bf_mutator, peer_bf);
586 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
587 GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
588 /* schedule next round */
589 next_send_time.rel_value =
590 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
591 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
592 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
593 (newly_found_peers + 1));
594 newly_found_peers = 0;
596 GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
602 * Method called whenever a peer connects.
605 * @param peer peer identity this notification is about
606 * @param atsi performance data
607 * @param atsi_count number of records in 'atsi'
610 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
611 const struct GNUNET_ATS_Information *atsi,
612 unsigned int atsi_count)
614 struct PeerInfo *ret;
617 /* Check for connect to self message */
618 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected %s to %s\n",
621 GNUNET_i2s (&my_identity), GNUNET_h2s (&peer->hashPubKey));
623 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
629 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), 1,
631 peer_bucket = find_bucket (&peer->hashPubKey);
632 GNUNET_assert ((peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS));
633 ret = GNUNET_malloc (sizeof (struct PeerInfo));
635 ret->latency = latency;
636 ret->distance = distance;
639 GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head,
640 k_buckets[peer_bucket].tail, ret);
641 k_buckets[peer_bucket].peers_size++;
642 closest_bucket = GNUNET_MAX (closest_bucket, peer_bucket);
643 if ((peer_bucket > 0) && (k_buckets[peer_bucket].peers_size <= bucket_size))
645 ret->preference_task =
646 GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
649 GNUNET_assert (GNUNET_OK ==
650 GNUNET_CONTAINER_multihashmap_put (all_known_peers,
651 &peer->hashPubKey, ret,
652 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
653 if (1 == GNUNET_CONTAINER_multihashmap_size (all_known_peers))
655 /* got a first connection, good time to start with FIND PEER requests... */
656 find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
662 * Method called whenever a peer disconnects.
665 * @param peer peer identity this notification is about
668 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
670 struct PeerInfo *to_remove;
672 struct P2PPendingMessage *pos;
673 unsigned int discarded;
675 /* Check for disconnect from self message */
676 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnected %s from %s\n",
679 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 struct GNUNET_HashCode * target, const struct 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 (struct 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 struct GNUNET_HashCode * key,
942 const struct GNUNET_CONTAINER_BloomFilter *bloom)
948 struct PeerInfo *pos;
950 if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (struct 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 struct 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;
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Excluded peer `%s' due to BF match in greedy routing for %s\n",
1033 GNUNET_i2s (&pos->id), GNUNET_h2s (key));
1034 GNUNET_STATISTICS_update (GDS_stats,
1036 ("# Peers excluded from routing due to Bloomfilter"),
1044 GNUNET_STATISTICS_update (GDS_stats,
1045 gettext_noop ("# Peer selection failed"), 1,
1050 /* select "random" peer */
1051 /* count number of peers that are available and not filtered */
1053 for (bc = 0; bc <= closest_bucket; bc++)
1055 pos = k_buckets[bc].head;
1056 while ((pos != NULL) && (count < bucket_size))
1058 if ((bloom != NULL) &&
1060 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
1062 GNUNET_STATISTICS_update (GDS_stats,
1064 ("# Peers excluded from routing due to Bloomfilter"),
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "Excluded peer `%s' due to BF match in random routing for %s\n",
1068 GNUNET_i2s (&pos->id), GNUNET_h2s (key));
1070 continue; /* Ignore bloomfiltered peers */
1076 if (count == 0) /* No peers to select from! */
1078 GNUNET_STATISTICS_update (GDS_stats,
1079 gettext_noop ("# Peer selection failed"), 1,
1083 /* Now actually choose a peer */
1084 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1086 for (bc = 0; bc <= closest_bucket; bc++)
1088 pos = k_buckets[bc].head;
1089 while ((pos != NULL) && (count < bucket_size))
1091 if ((bloom != NULL) &&
1093 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
1096 continue; /* Ignore bloomfiltered peers */
1098 if (0 == selected--)
1109 * Compute the set of peers that the given request should be
1112 * @param key routing key
1113 * @param bloom bloom filter excluding peers as targets, all selected
1114 * peers will be added to the bloom filter
1115 * @param hop_count number of hops the request has traversed so far
1116 * @param target_replication desired number of replicas
1117 * @param targets where to store an array of target peers (to be
1118 * free'd by the caller)
1119 * @return number of peers returned in 'targets'.
1122 get_target_peers (const struct GNUNET_HashCode * key,
1123 struct GNUNET_CONTAINER_BloomFilter *bloom,
1124 uint32_t hop_count, uint32_t target_replication,
1125 struct PeerInfo ***targets)
1129 struct PeerInfo **rtargets;
1130 struct PeerInfo *nxt;
1132 GNUNET_assert (NULL != bloom);
1133 ret = get_forward_count (hop_count, target_replication);
1139 rtargets = GNUNET_malloc (sizeof (struct PeerInfo *) * ret);
1140 for (off = 0; off < ret; off++)
1142 nxt = select_peer (key, bloom, hop_count);
1145 rtargets[off] = nxt;
1146 GNUNET_break (GNUNET_NO ==
1147 GNUNET_CONTAINER_bloomfilter_test (bloom,
1148 &nxt->id.hashPubKey));
1149 GNUNET_CONTAINER_bloomfilter_add (bloom, &rtargets[off]->id.hashPubKey);
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1152 "Selected %u/%u peers at hop %u for %s (target was %u)\n", off,
1153 GNUNET_CONTAINER_multihashmap_size (all_known_peers),
1154 (unsigned int) hop_count, GNUNET_h2s (key), ret);
1157 GNUNET_free (rtargets);
1161 *targets = rtargets;
1167 * Perform a PUT operation. Forwards the given request to other
1168 * peers. Does not store the data locally. Does not give the
1169 * data to local clients. May do nothing if this is the only
1170 * peer in the network (or if we are the closest peer in the
1173 * @param type type of the block
1174 * @param options routing options
1175 * @param desired_replication_level desired replication count
1176 * @param expiration_time when does the content expire
1177 * @param hop_count how many hops has this message traversed so far
1178 * @param bf Bloom filter of peers this PUT has already traversed
1179 * @param key key for the content
1180 * @param put_path_length number of entries in put_path
1181 * @param put_path peers this request has traversed so far (if tracked)
1182 * @param data payload to store
1183 * @param data_size number of bytes in data
1186 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1187 enum GNUNET_DHT_RouteOption options,
1188 uint32_t desired_replication_level,
1189 struct GNUNET_TIME_Absolute expiration_time,
1191 struct GNUNET_CONTAINER_BloomFilter *bf,
1192 const struct GNUNET_HashCode * key,
1193 unsigned int put_path_length,
1194 struct GNUNET_PeerIdentity *put_path,
1195 const void *data, size_t data_size)
1197 unsigned int target_count;
1199 struct PeerInfo **targets;
1200 struct PeerInfo *target;
1201 struct P2PPendingMessage *pending;
1203 struct PeerPutMessage *ppm;
1204 struct GNUNET_PeerIdentity *pp;
1206 GNUNET_assert (NULL != bf);
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Adding myself (%s) to PUT bloomfilter for %s\n",
1209 GNUNET_i2s (&my_identity), GNUNET_h2s (key));
1210 GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity.hashPubKey);
1211 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# PUT requests routed"),
1214 get_target_peers (key, bf, hop_count, desired_replication_level,
1216 if (0 == target_count)
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "Routing PUT for %s terminates after %u hops at %s\n",
1220 GNUNET_h2s (key), (unsigned int) hop_count,
1221 GNUNET_i2s (&my_identity));
1225 put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size +
1226 sizeof (struct PeerPutMessage);
1227 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1229 put_path_length = 0;
1230 msize = data_size + sizeof (struct PeerPutMessage);
1232 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1235 GNUNET_free (targets);
1238 GNUNET_STATISTICS_update (GDS_stats,
1240 ("# PUT messages queued for transmission"),
1241 target_count, GNUNET_NO);
1242 for (i = 0; i < target_count; i++)
1244 target = targets[i];
1245 if (target->pending_count >= MAXIMUM_PENDING_PER_PEER)
1247 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1249 continue; /* skip */
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 "Routing PUT for %s after %u hops to %s\n", GNUNET_h2s (key),
1253 (unsigned int) hop_count, GNUNET_i2s (&target->id));
1254 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1255 pending->importance = 0; /* FIXME */
1256 pending->timeout = expiration_time;
1257 ppm = (struct PeerPutMessage *) &pending[1];
1258 pending->msg = &ppm->header;
1259 ppm->header.size = htons (msize);
1260 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1261 ppm->options = htonl (options);
1262 ppm->type = htonl (type);
1263 ppm->hop_count = htonl (hop_count + 1);
1264 ppm->desired_replication_level = htonl (desired_replication_level);
1265 ppm->put_path_length = htonl (put_path_length);
1266 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1267 GNUNET_break (GNUNET_YES ==
1268 GNUNET_CONTAINER_bloomfilter_test (bf,
1269 &target->id.hashPubKey));
1270 GNUNET_assert (GNUNET_OK ==
1271 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
1275 pp = (struct GNUNET_PeerIdentity *) &ppm[1];
1276 memcpy (pp, put_path,
1277 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1278 memcpy (&pp[put_path_length], data, data_size);
1279 GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending);
1280 target->pending_count++;
1281 process_peer_queue (target);
1283 GNUNET_free (targets);
1288 * Perform a GET operation. Forwards the given request to other
1289 * peers. Does not lookup the key locally. May do nothing if this is
1290 * the only peer in the network (or if we are the closest peer in the
1293 * @param type type of the block
1294 * @param options routing options
1295 * @param desired_replication_level desired replication count
1296 * @param hop_count how many hops did this request traverse so far?
1297 * @param key key for the content
1298 * @param xquery extended query
1299 * @param xquery_size number of bytes in xquery
1300 * @param reply_bf bloomfilter to filter duplicates
1301 * @param reply_bf_mutator mutator for reply_bf
1302 * @param peer_bf filter for peers not to select (again)
1305 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1306 enum GNUNET_DHT_RouteOption options,
1307 uint32_t desired_replication_level,
1308 uint32_t hop_count, const struct GNUNET_HashCode * key,
1309 const void *xquery, size_t xquery_size,
1310 const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
1311 uint32_t reply_bf_mutator,
1312 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1314 unsigned int target_count;
1316 struct PeerInfo **targets;
1317 struct PeerInfo *target;
1318 struct P2PPendingMessage *pending;
1320 struct PeerGetMessage *pgm;
1322 size_t reply_bf_size;
1324 GNUNET_assert (NULL != peer_bf);
1325 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# GET requests routed"),
1328 get_target_peers (key, peer_bf, hop_count, desired_replication_level,
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "Adding myself (%s) to GET bloomfilter for %s\n",
1332 GNUNET_i2s (&my_identity), GNUNET_h2s (key));
1333 GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity.hashPubKey);
1334 if (0 == target_count)
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 "Routing GET for %s terminates after %u hops at %s\n",
1338 GNUNET_h2s (key), (unsigned int) hop_count,
1339 GNUNET_i2s (&my_identity));
1342 reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
1343 msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
1344 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1347 GNUNET_free (targets);
1350 GNUNET_STATISTICS_update (GDS_stats,
1352 ("# GET messages queued for transmission"),
1353 target_count, GNUNET_NO);
1354 /* forward request */
1355 for (i = 0; i < target_count; i++)
1357 target = targets[i];
1358 if (target->pending_count >= MAXIMUM_PENDING_PER_PEER)
1360 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1362 continue; /* skip */
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365 "Routing GET for %s after %u hops to %s\n", GNUNET_h2s (key),
1366 (unsigned int) hop_count, GNUNET_i2s (&target->id));
1367 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1368 pending->importance = 0; /* FIXME */
1369 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1370 pgm = (struct PeerGetMessage *) &pending[1];
1371 pending->msg = &pgm->header;
1372 pgm->header.size = htons (msize);
1373 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1374 pgm->options = htonl (options);
1375 pgm->type = htonl (type);
1376 pgm->hop_count = htonl (hop_count + 1);
1377 pgm->desired_replication_level = htonl (desired_replication_level);
1378 pgm->xquery_size = htonl (xquery_size);
1379 pgm->bf_mutator = reply_bf_mutator;
1380 GNUNET_break (GNUNET_YES ==
1381 GNUNET_CONTAINER_bloomfilter_test (peer_bf,
1382 &target->id.hashPubKey));
1383 GNUNET_assert (GNUNET_OK ==
1384 GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
1388 xq = (char *) &pgm[1];
1389 memcpy (xq, xquery, xquery_size);
1390 if (NULL != reply_bf)
1391 GNUNET_assert (GNUNET_OK ==
1392 GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
1396 GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending);
1397 target->pending_count++;
1398 process_peer_queue (target);
1400 GNUNET_free (targets);
1405 * Handle a reply (route to origin). Only forwards the reply back to
1406 * the given peer. Does not do local caching or forwarding to local
1409 * @param target neighbour that should receive the block (if still connected)
1410 * @param type type of the block
1411 * @param expiration_time when does the content expire
1412 * @param key key for the content
1413 * @param put_path_length number of entries in put_path
1414 * @param put_path peers the original PUT traversed (if tracked)
1415 * @param get_path_length number of entries in put_path
1416 * @param get_path peers this reply has traversed so far (if tracked)
1417 * @param data payload of the reply
1418 * @param data_size number of bytes in data
1421 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1422 enum GNUNET_BLOCK_Type type,
1423 struct GNUNET_TIME_Absolute expiration_time,
1424 const struct GNUNET_HashCode * key,
1425 unsigned int put_path_length,
1426 const struct GNUNET_PeerIdentity *put_path,
1427 unsigned int get_path_length,
1428 const struct GNUNET_PeerIdentity *get_path,
1429 const void *data, size_t data_size)
1431 struct PeerInfo *pi;
1432 struct P2PPendingMessage *pending;
1434 struct PeerResultMessage *prm;
1435 struct GNUNET_PeerIdentity *paths;
1438 data_size + sizeof (struct PeerResultMessage) + (get_path_length +
1440 sizeof (struct GNUNET_PeerIdentity);
1441 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1443 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1445 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1446 (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE))
1451 pi = GNUNET_CONTAINER_multihashmap_get (all_known_peers, &target->hashPubKey);
1454 /* peer disconnected in the meantime, drop reply */
1457 if (pi->pending_count >= MAXIMUM_PENDING_PER_PEER)
1460 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"),
1465 GNUNET_STATISTICS_update (GDS_stats,
1467 ("# RESULT messages queued for transmission"), 1,
1469 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1470 pending->importance = 0; /* FIXME */
1471 pending->timeout = expiration_time;
1472 prm = (struct PeerResultMessage *) &pending[1];
1473 pending->msg = &prm->header;
1474 prm->header.size = htons (msize);
1475 prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1476 prm->type = htonl (type);
1477 prm->put_path_length = htonl (put_path_length);
1478 prm->get_path_length = htonl (get_path_length);
1479 prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1481 paths = (struct GNUNET_PeerIdentity *) &prm[1];
1482 memcpy (paths, put_path,
1483 put_path_length * sizeof (struct GNUNET_PeerIdentity));
1484 memcpy (&paths[put_path_length], get_path,
1485 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1486 memcpy (&paths[put_path_length + get_path_length], data, data_size);
1487 GNUNET_CONTAINER_DLL_insert (pi->head, pi->tail, pending);
1488 pi->pending_count++;
1489 process_peer_queue (pi);
1494 * To be called on core init/fail.
1496 * @param cls service closure
1497 * @param server handle to the server for this service
1498 * @param identity the public identity of this peer
1501 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1502 const struct GNUNET_PeerIdentity *identity)
1504 GNUNET_assert (server != NULL);
1505 my_identity = *identity;
1510 * Core handler for p2p put requests.
1512 * @param cls closure
1513 * @param peer sender of the request
1514 * @param message message
1515 * @param peer peer identity this notification is about
1516 * @param atsi performance data
1517 * @param atsi_count number of records in 'atsi'
1518 * @return GNUNET_OK to keep the connection open,
1519 * GNUNET_SYSERR to close it (signal serious error)
1522 handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
1523 const struct GNUNET_MessageHeader *message,
1524 const struct GNUNET_ATS_Information *atsi,
1525 unsigned int atsi_count)
1527 const struct PeerPutMessage *put;
1528 const struct GNUNET_PeerIdentity *put_path;
1529 const void *payload;
1532 size_t payload_size;
1533 enum GNUNET_DHT_RouteOption options;
1534 struct GNUNET_CONTAINER_BloomFilter *bf;
1535 struct GNUNET_HashCode test_key;
1537 msize = ntohs (message->size);
1538 if (msize < sizeof (struct PeerPutMessage))
1540 GNUNET_break_op (0);
1543 put = (const struct PeerPutMessage *) message;
1544 putlen = ntohl (put->put_path_length);
1546 sizeof (struct PeerPutMessage) +
1547 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
1549 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
1551 GNUNET_break_op (0);
1554 GNUNET_STATISTICS_update (GDS_stats,
1555 gettext_noop ("# P2P PUT requests received"), 1,
1557 put_path = (const struct GNUNET_PeerIdentity *) &put[1];
1558 payload = &put_path[putlen];
1559 options = ntohl (put->options);
1561 msize - (sizeof (struct PeerPutMessage) +
1562 putlen * sizeof (struct GNUNET_PeerIdentity));
1563 switch (GNUNET_BLOCK_get_key
1564 (GDS_block_context, ntohl (put->type), payload, payload_size,
1568 if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode)))
1570 GNUNET_break_op (0);
1575 GNUNET_break_op (0);
1578 /* cannot verify, good luck */
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PUT for `%s' from %s\n",
1582 GNUNET_h2s (&put->key), GNUNET_i2s (peer));
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);
1618 GDS_CLIENTS_process_put (options,
1620 ntohl (put->hop_count),
1621 ntohl (put->desired_replication_level),
1623 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1632 * We have received a FIND PEER request. Send matching
1635 * @param sender sender of the FIND PEER request
1636 * @param key peers close to this key are desired
1637 * @param bf peers matching this bf are excluded
1638 * @param bf_mutator mutator for bf
1641 handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1642 const struct GNUNET_HashCode * key,
1643 struct GNUNET_CONTAINER_BloomFilter *bf, uint32_t bf_mutator)
1646 struct PeerBucket *bucket;
1647 struct PeerInfo *peer;
1648 unsigned int choice;
1649 struct GNUNET_HashCode mhash;
1650 const struct GNUNET_HELLO_Message *hello;
1652 /* first, check about our own HELLO */
1653 if (NULL != GDS_my_hello)
1655 GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
1657 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)))
1659 GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO,
1660 GNUNET_TIME_relative_to_absolute
1661 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1662 key, 0, NULL, 0, NULL, GDS_my_hello,
1663 GNUNET_HELLO_size ((const struct
1664 GNUNET_HELLO_Message *)
1669 GNUNET_STATISTICS_update (GDS_stats,
1671 ("# FIND PEER requests ignored due to Bloomfilter"),
1677 GNUNET_STATISTICS_update (GDS_stats,
1679 ("# FIND PEER requests ignored due to lack of HELLO"),
1683 /* then, also consider sending a random HELLO from the closest bucket */
1684 if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (struct GNUNET_HashCode)))
1685 bucket_idx = closest_bucket;
1687 bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key));
1688 if (bucket_idx == GNUNET_SYSERR)
1690 bucket = &k_buckets[bucket_idx];
1691 if (bucket->peers_size == 0)
1694 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, bucket->peers_size);
1695 peer = bucket->head;
1698 GNUNET_assert (peer != NULL);
1702 choice = bucket->peers_size;
1707 return; /* no non-masked peer available */
1709 peer = bucket->head;
1710 GNUNET_BLOCK_mingle_hash (&peer->id.hashPubKey, bf_mutator, &mhash);
1711 hello = GDS_HELLO_get (&peer->id);
1713 while ((hello == NULL) ||
1714 (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)));
1715 GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO,
1716 GNUNET_TIME_relative_to_absolute
1717 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION), key,
1718 0, NULL, 0, NULL, hello,
1719 GNUNET_HELLO_size (hello));
1724 * Core handler for p2p get requests.
1726 * @param cls closure
1727 * @param peer sender of the request
1728 * @param message message
1729 * @param peer peer identity this notification is about
1730 * @param atsi performance data
1731 * @param atsi_count number of records in 'atsi'
1732 * @return GNUNET_OK to keep the connection open,
1733 * GNUNET_SYSERR to close it (signal serious error)
1736 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
1737 const struct GNUNET_MessageHeader *message,
1738 const struct GNUNET_ATS_Information *atsi,
1739 unsigned int atsi_count)
1741 struct PeerGetMessage *get;
1742 uint32_t xquery_size;
1743 size_t reply_bf_size;
1745 enum GNUNET_BLOCK_Type type;
1746 enum GNUNET_DHT_RouteOption options;
1747 enum GNUNET_BLOCK_EvaluationResult eval;
1748 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
1749 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
1753 memcmp (peer, &my_identity,
1754 sizeof (struct GNUNET_PeerIdentity)));
1755 /* parse and validate message */
1756 msize = ntohs (message->size);
1757 if (msize < sizeof (struct PeerGetMessage))
1759 GNUNET_break_op (0);
1762 get = (struct PeerGetMessage *) message;
1763 xquery_size = ntohl (get->xquery_size);
1764 if (msize < sizeof (struct PeerGetMessage) + xquery_size)
1766 GNUNET_break_op (0);
1769 GNUNET_STATISTICS_update (GDS_stats,
1770 gettext_noop ("# P2P GET requests received"), 1,
1772 reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
1773 type = ntohl (get->type);
1774 options = ntohl (get->options);
1775 xquery = (const char *) &get[1];
1777 if (reply_bf_size > 0)
1779 GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size], reply_bf_size,
1780 GNUNET_CONSTANTS_BLOOMFILTER_K);
1782 GNUNET_BLOCK_evaluate (GDS_block_context, type, &get->key, &reply_bf,
1783 get->bf_mutator, xquery, xquery_size, NULL, 0);
1784 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
1786 /* request invalid or block type not supported */
1787 GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
1788 if (NULL != reply_bf)
1789 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1793 GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, DHT_BLOOM_SIZE,
1794 GNUNET_CONSTANTS_BLOOMFILTER_K);
1795 GNUNET_break_op (GNUNET_YES ==
1796 GNUNET_CONTAINER_bloomfilter_test (peer_bf,
1797 &peer->hashPubKey));
1798 /* remember request for routing replies */
1799 GDS_ROUTING_add (peer, type, options, &get->key, xquery, xquery_size,
1800 reply_bf, get->bf_mutator);
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GET for %s at %s after %u hops\n",
1802 GNUNET_h2s (&get->key), GNUNET_i2s (&my_identity),
1803 (unsigned int) ntohl (get->hop_count));
1804 /* local lookup (this may update the reply_bf) */
1805 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1806 (am_closest_peer (&get->key, peer_bf)))
1808 if ((0 != (options & GNUNET_DHT_RO_FIND_PEER)))
1810 GNUNET_STATISTICS_update (GDS_stats,
1812 ("# P2P FIND PEER requests processed"), 1,
1814 handle_find_peer (peer, &get->key, reply_bf, get->bf_mutator);
1819 GDS_DATACACHE_handle_get (&get->key, type, xquery, xquery_size,
1820 &reply_bf, get->bf_mutator);
1825 GNUNET_STATISTICS_update (GDS_stats,
1826 gettext_noop ("# P2P GET requests ONLY routed"),
1831 GDS_CLIENTS_process_get (options,
1833 ntohl(get->hop_count),
1834 ntohl(get->desired_replication_level),
1838 /* P2P forwarding */
1839 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1840 GDS_NEIGHBOURS_handle_get (type, options,
1841 ntohl (get->desired_replication_level),
1842 ntohl (get->hop_count), &get->key, xquery,
1843 xquery_size, reply_bf, get->bf_mutator, peer_bf);
1845 if (NULL != reply_bf)
1846 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1847 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
1853 * Core handler for p2p result messages.
1855 * @param cls closure
1856 * @param message message
1857 * @param peer peer identity this notification is about
1858 * @param atsi performance data
1859 * @param atsi_count number of records in 'atsi'
1860 * @return GNUNET_YES (do not cut p2p connection)
1863 handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
1864 const struct GNUNET_MessageHeader *message,
1865 const struct GNUNET_ATS_Information *atsi,
1866 unsigned int atsi_count)
1868 const struct PeerResultMessage *prm;
1869 const struct GNUNET_PeerIdentity *put_path;
1870 const struct GNUNET_PeerIdentity *get_path;
1872 uint32_t get_path_length;
1873 uint32_t put_path_length;
1876 enum GNUNET_BLOCK_Type type;
1878 /* parse and validate message */
1879 msize = ntohs (message->size);
1880 if (msize < sizeof (struct PeerResultMessage))
1882 GNUNET_break_op (0);
1885 prm = (struct PeerResultMessage *) message;
1886 put_path_length = ntohl (prm->put_path_length);
1887 get_path_length = ntohl (prm->get_path_length);
1889 sizeof (struct PeerResultMessage) + (get_path_length +
1891 sizeof (struct GNUNET_PeerIdentity)) ||
1893 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1895 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
1897 GNUNET_break_op (0);
1900 GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P RESULTS received"),
1902 put_path = (const struct GNUNET_PeerIdentity *) &prm[1];
1903 get_path = &put_path[put_path_length];
1904 type = ntohl (prm->type);
1905 data = (const void *) &get_path[get_path_length];
1907 msize - (sizeof (struct PeerResultMessage) +
1909 put_path_length) * sizeof (struct GNUNET_PeerIdentity));
1911 /* if we got a HELLO, consider it for our own routing table */
1912 if (type == GNUNET_BLOCK_TYPE_DHT_HELLO)
1914 const struct GNUNET_MessageHeader *h;
1915 struct GNUNET_PeerIdentity pid;
1918 /* Should be a HELLO, validate and consider using it! */
1919 if (data_size < sizeof (struct GNUNET_MessageHeader))
1921 GNUNET_break_op (0);
1925 if (data_size != ntohs (h->size))
1927 GNUNET_break_op (0);
1931 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h, &pid))
1933 GNUNET_break_op (0);
1936 if (0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
1938 bucket = find_bucket (&pid.hashPubKey);
1939 if ((bucket >= 0) && (k_buckets[bucket].peers_size < bucket_size))
1941 if (NULL != GDS_transport_handle)
1943 GNUNET_TRANSPORT_offer_hello (GDS_transport_handle, h, NULL, NULL);
1944 GNUNET_TRANSPORT_try_connect (GDS_transport_handle, &pid);
1950 /* append 'peer' to 'get_path' */
1952 struct GNUNET_PeerIdentity xget_path[get_path_length + 1];
1954 memcpy (xget_path, get_path,
1955 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1956 xget_path[get_path_length] = *peer;
1959 /* forward to local clients */
1960 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1961 &prm->key, get_path_length, xget_path,
1962 put_path_length, put_path, type, data_size, data);
1964 /* forward to other peers */
1965 GDS_ROUTING_process (type, GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1966 &prm->key, put_path_length, put_path, get_path_length,
1967 xget_path, data, data_size);
1970 GDS_CLIENTS_process_get_resp (type,
1975 GNUNET_TIME_absolute_ntoh (
1976 prm->expiration_time),
1986 * Initialize neighbours subsystem.
1988 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1991 GDS_NEIGHBOURS_init ()
1993 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1994 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
1995 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
1996 {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0},
1999 unsigned long long temp_config_num;
2002 GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size",
2004 bucket_size = (unsigned int) temp_config_num;
2005 atsAPI = GNUNET_ATS_performance_init (GDS_cfg, NULL, NULL);
2007 GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect,
2008 &handle_core_disconnect, NULL, GNUNET_NO, NULL,
2009 GNUNET_NO, core_handlers);
2010 if (coreAPI == NULL)
2011 return GNUNET_SYSERR;
2012 all_known_peers = GNUNET_CONTAINER_multihashmap_create (256);
2018 * Shutdown neighbours subsystem.
2021 GDS_NEIGHBOURS_done ()
2023 if (coreAPI == NULL)
2025 GNUNET_CORE_disconnect (coreAPI);
2027 GNUNET_ATS_performance_done (atsAPI);
2029 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (all_known_peers));
2030 GNUNET_CONTAINER_multihashmap_destroy (all_known_peers);
2031 all_known_peers = NULL;
2032 if (GNUNET_SCHEDULER_NO_TASK != find_peer_task)
2034 GNUNET_SCHEDULER_cancel (find_peer_task);
2035 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
2040 * Get the ID of the local node.
2042 * @return identity of the local node
2044 struct GNUNET_PeerIdentity *
2045 GDS_NEIGHBOURS_get_id ()
2047 return &my_identity;
2051 /* end of gnunet-service-dht_neighbours.c */