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_core_service.h"
36 #include "gnunet_datacache_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_dht_service.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet-service-dht.h"
42 #include "gnunet-service-dht_clients.h"
43 #include "gnunet-service-dht_datacache.h"
44 #include "gnunet-service-dht_hello.h"
45 #include "gnunet-service-dht_neighbours.h"
46 #include "gnunet-service-dht_nse.h"
47 #include "gnunet-service-dht_routing.h"
51 * How many buckets will we allow total.
53 #define MAX_BUCKETS sizeof (GNUNET_HashCode) * 8
56 * What is the maximum number of peers in a given bucket.
58 #define DEFAULT_BUCKET_SIZE 4
61 * Size of the bloom filter the DHT uses to filter peers.
63 #define DHT_BLOOM_SIZE 128
66 * Desired replication level for FIND PEER requests
68 #define FIND_PEER_REPLICATION_LEVEL 4
71 * Maximum allowed replication level for all requests.
73 #define MAXIMUM_REPLICATION_LEVEL 16
76 * How often to update our preference levels for peers in our routing tables.
78 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
81 * How long at least to wait before sending another find peer request.
83 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
86 * How long at most to wait before sending another find peer request.
88 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
91 * How long at most to wait for transmission of a GET request to another peer?
93 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
102 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
104 struct GNUNET_MessageHeader header;
109 uint32_t options GNUNET_PACKED;
114 uint32_t type GNUNET_PACKED;
119 uint32_t hop_count GNUNET_PACKED;
122 * Replication level for this message
124 uint32_t desired_replication_level GNUNET_PACKED;
127 * Length of the PUT path that follows (if tracked).
129 uint32_t put_path_length GNUNET_PACKED;
132 * When does the content expire?
134 struct GNUNET_TIME_AbsoluteNBO expiration_time;
137 * Bloomfilter (for peer identities) to stop circular routes
139 char bloomfilter[DHT_BLOOM_SIZE];
142 * The key we are storing under.
146 /* put path (if tracked) */
156 struct PeerResultMessage
159 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
161 struct GNUNET_MessageHeader header;
166 uint32_t type GNUNET_PACKED;
169 * Length of the PUT path that follows (if tracked).
171 uint32_t put_path_length GNUNET_PACKED;
174 * Length of the GET path that follows (if tracked).
176 uint32_t get_path_length GNUNET_PACKED;
179 * When does the content expire?
181 struct GNUNET_TIME_AbsoluteNBO expiration_time;
184 * The key of the corresponding GET request.
188 /* put path (if tracked) */
190 /* get path (if tracked) */
200 struct PeerGetMessage
203 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_GET
205 struct GNUNET_MessageHeader header;
210 uint32_t options GNUNET_PACKED;
213 * Desired content type.
215 uint32_t type GNUNET_PACKED;
220 uint32_t hop_count GNUNET_PACKED;
223 * Desired replication level for this request.
225 uint32_t desired_replication_level GNUNET_PACKED;
228 * Size of the extended query.
230 uint32_t xquery_size;
233 * Bloomfilter mutator.
238 * Bloomfilter (for peer identities) to stop circular routes
240 char bloomfilter[DHT_BLOOM_SIZE];
243 * The key we are looking for.
249 /* result bloomfilter */
255 * Linked list of messages to send to a particular other peer.
257 struct P2PPendingMessage
260 * Pointer to next item in the list
262 struct P2PPendingMessage *next;
265 * Pointer to previous item in the list
267 struct P2PPendingMessage *prev;
270 * Message importance level. FIXME: used? useful?
272 unsigned int importance;
275 * When does this message time out?
277 struct GNUNET_TIME_Absolute timeout;
280 * Actual message to be sent, allocated at the end of the struct:
281 * // msg = (cast) &pm[1];
282 * // memcpy (&pm[1], data, len);
284 const struct GNUNET_MessageHeader *msg;
290 * Entry for a peer in a bucket.
295 * Next peer entry (DLL)
297 struct PeerInfo *next;
300 * Prev peer entry (DLL)
302 struct PeerInfo *prev;
305 * Count of outstanding messages for peer. FIXME: NEEDED?
306 * FIXME: bound queue size!?
308 unsigned int pending_count;
311 * Head of pending messages to be sent to this peer.
313 struct P2PPendingMessage *head;
316 * Tail of pending messages to be sent to this peer.
318 struct P2PPendingMessage *tail;
321 * Core handle for sending messages to this peer.
323 struct GNUNET_CORE_TransmitHandle *th;
326 * Preference update context
328 struct GNUNET_CORE_InformationRequestContext *info_ctx;
331 * Task for scheduling preference updates
333 GNUNET_SCHEDULER_TaskIdentifier preference_task;
336 * What is the identity of the peer?
338 struct GNUNET_PeerIdentity id;
342 * What is the average latency for replies received?
344 struct GNUNET_TIME_Relative latency;
347 * Transport level distance to peer.
349 unsigned int distance;
356 * Peers are grouped into buckets.
363 struct PeerInfo *head;
368 struct PeerInfo *tail;
371 * Number of peers in the bucket.
373 unsigned int peers_size;
378 * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
380 static unsigned int closest_bucket;
383 * How many peers have we added since we sent out our last
386 static unsigned int newly_found_peers;
389 * The buckets. Array of size MAX_BUCKET_SIZE. Offset 0 means 0 bits matching.
391 static struct PeerBucket k_buckets[MAX_BUCKETS];
394 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
396 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
399 * Maximum size for each bucket.
401 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
404 * Task that sends FIND PEER requests.
406 static GNUNET_SCHEDULER_TaskIdentifier find_peer_task;
409 * Identity of this peer.
411 static struct GNUNET_PeerIdentity my_identity;
414 * Handle to GNUnet core.
416 static struct GNUNET_CORE_Handle *coreAPI;
420 * Find the optimal bucket for this key.
422 * @param hc the hashcode to compare our identity to
423 * @return the proper bucket index, or GNUNET_SYSERR
424 * on error (same hashcode)
427 find_bucket (const GNUNET_HashCode * hc)
431 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
432 if (bits == MAX_BUCKETS)
434 /* How can all bits match? Got my own ID? */
436 return GNUNET_SYSERR;
438 return MAX_BUCKETS - bits - 1;
443 * Let GNUnet core know that we like the given peer.
445 * @param cls the 'struct PeerInfo' of the peer
446 * @param tc scheduler context.
449 update_core_preference (void *cls,
450 const struct GNUNET_SCHEDULER_TaskContext *tc);
454 * Function called with statistics about the given peer.
457 * @param peer identifies the peer
458 * @param bpm_out set to the current bandwidth limit (sending) for this peer
459 * @param amount set to the amount that was actually reserved or unreserved;
460 * either the full requested amount or zero (no partial reservations)
461 * @param res_delay if the reservation could not be satisfied (amount was 0), how
462 * long should the client wait until re-trying?
463 * @param preference current traffic preference for the given peer
466 update_core_preference_finish (void *cls,
467 const struct GNUNET_PeerIdentity *peer,
468 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
470 struct GNUNET_TIME_Relative res_delay,
473 struct PeerInfo *peer_info = cls;
475 peer_info->info_ctx = NULL;
476 peer_info->preference_task
477 = GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
478 &update_core_preference, peer_info);
483 * Let GNUnet core know that we like the given peer.
485 * @param cls the 'struct PeerInfo' of the peer
486 * @param tc scheduler context.
489 update_core_preference (void *cls,
490 const struct GNUNET_SCHEDULER_TaskContext *tc)
492 struct PeerInfo *peer = cls;
494 unsigned int matching;
497 peer->preference_task = GNUNET_SCHEDULER_NO_TASK;
498 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
501 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
502 &peer->id.hashPubKey);
505 bucket = find_bucket (&peer->id.hashPubKey);
506 if (bucket == GNUNET_SYSERR)
509 preference = (1LL << matching) / k_buckets[bucket].peers_size;
512 peer->preference_task
513 = GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
514 &update_core_preference, peer);
517 GNUNET_STATISTICS_update (GDS_stats,
518 gettext_noop ("# Preference updates given to core"), 1,
521 GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
522 GNUNET_TIME_UNIT_FOREVER_REL,
523 GNUNET_BANDWIDTH_VALUE_MAX, 0,
525 &update_core_preference_finish, peer);
530 * Closure for 'add_known_to_bloom'.
532 struct BloomConstructorContext
535 * Bloom filter under construction.
537 struct GNUNET_CONTAINER_BloomFilter *bloom;
547 * Add each of the peers we already know to the bloom filter of
548 * the request so that we don't get duplicate HELLOs.
550 * @param cls the 'struct BloomConstructorContext'.
551 * @param key peer identity to add to the bloom filter
552 * @param value value the peer information (unused)
553 * @return GNUNET_YES (we should continue to iterate)
556 add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
558 struct BloomConstructorContext *ctx = cls;
561 GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
562 GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
568 * Task to send a find peer message for our own peer identifier
569 * so that we can find the closest peers in the network to ourselves
570 * and attempt to connect to them.
572 * @param cls closure for this task
573 * @param tc the context under which the task is running
576 send_find_peer_message (void *cls,
577 const struct GNUNET_SCHEDULER_TaskContext *tc)
579 struct GNUNET_TIME_Relative next_send_time;
580 struct BloomConstructorContext bcc;
582 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
583 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
585 if (newly_found_peers > bucket_size)
587 /* If we are finding many peers already, no need to send out our request right now! */
588 find_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
589 &send_find_peer_message, NULL);
590 newly_found_peers = 0;
593 bcc.bf_mutator = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
595 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, GNUNET_CONSTANTS_BLOOMFILTER_K);
596 GNUNET_CONTAINER_multihashmap_iterate (all_known_peers,
599 GNUNET_STATISTICS_update (GDS_stats,
600 gettext_noop ("# FIND PEER messages initiated"), 1,
602 // FIXME: pass priority!?
603 GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
604 GNUNET_DHT_RO_FIND_PEER,
605 FIND_PEER_REPLICATION_LEVEL,
607 &my_identity.hashPubKey,
609 bcc.bloom, bcc.bf_mutator, NULL);
610 GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
611 /* schedule next round */
612 newly_found_peers = 0;
613 next_send_time.rel_value =
614 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
615 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
616 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
617 find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time,
618 &send_find_peer_message,
624 * Method called whenever a peer connects.
627 * @param peer peer identity this notification is about
628 * @param atsi performance data
631 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
632 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
634 struct PeerInfo *ret;
637 /* Check for connect to self message */
638 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
641 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
647 GNUNET_STATISTICS_update (GDS_stats,
648 gettext_noop ("# Peers connected"), 1,
650 peer_bucket = find_bucket (&peer->hashPubKey);
651 GNUNET_assert ( (peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS) );
652 ret = GNUNET_malloc (sizeof (struct PeerInfo));
654 ret->latency = latency;
655 ret->distance = distance;
658 GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head,
659 k_buckets[peer_bucket].tail, ret);
660 k_buckets[peer_bucket].peers_size++;
661 closest_bucket = GNUNET_MAX (closest_bucket,
663 if ( (peer_bucket > 0) &&
664 (k_buckets[peer_bucket].peers_size <= bucket_size) )
665 ret->preference_task = GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
667 GNUNET_assert (GNUNET_OK ==
668 GNUNET_CONTAINER_multihashmap_put (all_known_peers,
669 &peer->hashPubKey, ret,
670 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
671 if (1 == GNUNET_CONTAINER_multihashmap_size (all_known_peers))
673 /* got a first connection, good time to start with FIND PEER requests... */
674 find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message,
681 * Method called whenever a peer disconnects.
684 * @param peer peer identity this notification is about
687 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
689 struct PeerInfo *to_remove;
691 struct P2PPendingMessage *pos;
693 /* Check for disconnect from self message */
694 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
697 GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
698 if (NULL == to_remove)
703 GNUNET_STATISTICS_update (GDS_stats,
704 gettext_noop ("# Peers connected"), -1,
706 GNUNET_assert (GNUNET_YES ==
707 GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
710 if (NULL != to_remove->info_ctx)
712 GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
713 to_remove->info_ctx = NULL;
715 current_bucket = find_bucket (&to_remove->id.hashPubKey);
716 GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
717 k_buckets[current_bucket].tail,
719 GNUNET_assert (k_buckets[current_bucket].peers_size > 0);
720 k_buckets[current_bucket].peers_size--;
721 while ( (closest_bucket > 0) &&
722 (k_buckets[closest_bucket].peers_size == 0) )
725 if (to_remove->th != NULL)
727 GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
728 to_remove->th = NULL;
730 while (NULL != (pos = to_remove->head))
732 GNUNET_CONTAINER_DLL_remove (to_remove->head,
741 * Called when core is ready to send a message we asked for
742 * out to the destination.
744 * @param cls the 'struct PeerInfo' of the target peer
745 * @param size number of bytes available in buf
746 * @param buf where the callee should write the message
747 * @return number of bytes written to buf
750 core_transmit_notify (void *cls, size_t size, void *buf)
752 struct PeerInfo *peer = cls;
754 struct P2PPendingMessage *pending;
761 /* client disconnected */
764 if (peer->head == NULL)
766 /* no messages pending */
770 while ( (NULL != (pending = peer->head)) &&
771 (size - off >= (msize = ntohs (pending->msg->size))) )
773 GNUNET_STATISTICS_update (GDS_stats,
774 gettext_noop ("# Bytes transmitted to other peers"), msize,
776 memcpy (&cbuf[off], pending->msg, msize);
778 peer->pending_count--;
779 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
780 GNUNET_free (pending);
782 if (peer->head != NULL)
784 = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
786 GNUNET_TIME_absolute_get_remaining (pending->timeout),
788 &core_transmit_notify, peer);
794 * Transmit all messages in the peer's message queue.
796 * @param peer message queue to process
799 process_peer_queue (struct PeerInfo *peer)
801 struct P2PPendingMessage *pending;
803 if (NULL == (pending = peer->head))
805 if (NULL != peer->th)
807 GNUNET_STATISTICS_update (GDS_stats,
808 gettext_noop ("# Bytes of bandwdith requested from core"),
809 ntohs (pending->msg->size),
812 = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
814 GNUNET_TIME_absolute_get_remaining (pending->timeout),
816 ntohs (pending->msg->size),
817 &core_transmit_notify, peer);
822 * To how many peers should we (on average) forward the request to
823 * obtain the desired target_replication count (on average).
825 * @param hop_count number of hops the message has traversed
826 * @param target_replication the number of total paths desired
827 * @return Some number of peers to forward the message to
830 get_forward_count (uint32_t hop_count,
831 uint32_t target_replication)
833 uint32_t random_value;
834 uint32_t forward_count;
837 if (hop_count > GDS_NSE_get () * 4.0)
839 /* forcefully terminate */
842 if (hop_count > GDS_NSE_get () * 2.0)
844 /* Once we have reached our ideal number of hops, only forward to 1 peer */
847 /* bound by system-wide maximum */
848 target_replication = GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL,
851 1 + (target_replication - 1.0) / (GDS_NSE_get () +
852 ((float) (target_replication - 1.0) *
854 /* Set forward count to floor of target_value */
855 forward_count = (uint32_t) target_value;
856 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
857 target_value = target_value - forward_count;
859 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
860 if (random_value < (target_value * UINT32_MAX))
862 return forward_count;
867 * Compute the distance between have and target as a 32-bit value.
868 * Differences in the lower bits must count stronger than differences
869 * in the higher bits.
871 * @return 0 if have==target, otherwise a number
872 * that is larger as the distance between
873 * the two hash codes increases
876 get_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
883 /* We have to represent the distance between two 2^9 (=512)-bit
884 * numbers as a 2^5 (=32)-bit number with "0" being used for the
885 * two numbers being identical; furthermore, we need to
886 * guarantee that a difference in the number of matching
887 * bits is always represented in the result.
889 * We use 2^32/2^9 numerical values to distinguish between
890 * hash codes that have the same LSB bit distance and
891 * use the highest 2^9 bits of the result to signify the
892 * number of (mis)matching LSB bits; if we have 0 matching
893 * and hence 512 mismatching LSB bits we return -1 (since
894 * 512 itself cannot be represented with 9 bits) */
896 /* first, calculate the most significant 9 bits of our
897 * result, aka the number of LSBs */
898 bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
899 /* bucket is now a value between 0 and 512 */
901 return 0; /* perfect match */
903 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
904 * below, we'd end up with max+1 (overflow)) */
906 /* calculate the most significant bits of the final result */
907 msb = (512 - bucket) << (32 - 9);
908 /* calculate the 32-9 least significant bits of the final result by
909 * looking at the differences in the 32-9 bits following the
910 * mismatching bit at 'bucket' */
913 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
915 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
916 GNUNET_CRYPTO_hash_get_bit (have, i))
917 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
918 * last bit set will be 31 -- if
919 * i does not reach 512 first... */
926 * Check whether my identity is closer than any known peers. If a
927 * non-null bloomfilter is given, check if this is the closest peer
928 * that hasn't already been routed to.
930 * @param key hash code to check closeness to
931 * @param bloom bloomfilter, exclude these entries from the decision
932 * @return GNUNET_YES if node location is closest,
933 * GNUNET_NO otherwise.
936 am_closest_peer (const GNUNET_HashCode *key,
937 const struct GNUNET_CONTAINER_BloomFilter *bloom)
943 struct PeerInfo *pos;
945 if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
947 bucket_num = find_bucket (key);
948 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, key);
949 pos = k_buckets[bucket_num].head;
951 while ((pos != NULL) && (count < bucket_size))
953 if ((bloom != NULL) &&
955 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
958 continue; /* Skip already checked entries */
960 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, key);
961 if (other_bits > bits)
963 if (other_bits == bits) /* We match the same number of bits */
967 /* No peers closer, we are the closest! */
973 * Select a peer from the routing table that would be a good routing
974 * destination for sending a message for "key". The resulting peer
975 * must not be in the set of blocked peers.<p>
977 * Note that we should not ALWAYS select the closest peer to the
978 * target, peers further away from the target should be chosen with
979 * exponentially declining probability.
981 * FIXME: double-check that this is fine
984 * @param key the key we are selecting a peer to route to
985 * @param bloom a bloomfilter containing entries this request has seen already
986 * @param hops how many hops has this message traversed thus far
987 * @return Peer to route to, or NULL on error
989 static struct PeerInfo *
990 select_peer (const GNUNET_HashCode *key,
991 const struct GNUNET_CONTAINER_BloomFilter *bloom,
996 unsigned int selected;
997 struct PeerInfo *pos;
999 unsigned int smallest_distance;
1000 struct PeerInfo *chosen;
1002 if (hops >= GDS_NSE_get ())
1004 /* greedy selection (closest peer that is not in bloomfilter) */
1005 smallest_distance = UINT_MAX;
1007 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1009 pos = k_buckets[bc].head;
1011 while ((pos != NULL) && (count < bucket_size))
1014 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
1016 dist = get_distance (key, &pos->id.hashPubKey);
1017 if (dist < smallest_distance)
1020 smallest_distance = dist;
1025 GNUNET_STATISTICS_update (GDS_stats,
1026 gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
1034 GNUNET_STATISTICS_update (GDS_stats,
1035 gettext_noop ("# Peer selection failed"), 1,
1040 /* select "random" peer */
1041 /* count number of peers that are available and not filtered */
1043 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1045 pos = k_buckets[bc].head;
1046 while ((pos != NULL) && (count < bucket_size))
1048 if ( (bloom != NULL) &&
1050 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
1052 GNUNET_STATISTICS_update (GDS_stats,
1053 gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
1056 continue; /* Ignore bloomfiltered peers */
1062 if (count == 0) /* No peers to select from! */
1064 GNUNET_STATISTICS_update (GDS_stats,
1065 gettext_noop ("# Peer selection failed"), 1,
1069 /* Now actually choose a peer */
1070 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1072 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1074 pos = k_buckets[bc].head;
1075 while ((pos != NULL) && (count < bucket_size))
1077 if ( (bloom != NULL) &&
1079 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
1082 continue; /* Ignore bloomfiltered peers */
1084 if (0 == selected--)
1095 * Compute the set of peers that the given request should be
1098 * @param key routing key
1099 * @param bloom bloom filter excluding peers as targets, all selected
1100 * peers will be added to the bloom filter
1101 * @param hop_count number of hops the request has traversed so far
1102 * @param target_replication desired number of replicas
1103 * @param targets where to store an array of target peers (to be
1104 * free'd by the caller)
1105 * @return number of peers returned in 'targets'.
1108 get_target_peers (const GNUNET_HashCode *key,
1109 struct GNUNET_CONTAINER_BloomFilter *bloom,
1111 uint32_t target_replication,
1112 struct PeerInfo ***targets)
1116 struct PeerInfo **rtargets;
1117 struct PeerInfo *nxt;
1119 ret = get_forward_count (hop_count, target_replication);
1125 rtargets = GNUNET_malloc (sizeof (struct PeerInfo*) * ret);
1129 nxt = select_peer (key, bloom, hop_count);
1132 rtargets[off++] = nxt;
1133 GNUNET_CONTAINER_bloomfilter_add (bloom, &nxt->id.hashPubKey);
1137 GNUNET_free (rtargets);
1141 *targets = rtargets;
1147 * Perform a PUT operation. Forwards the given request to other
1148 * peers. Does not store the data locally. Does not give the
1149 * data to local clients. May do nothing if this is the only
1150 * peer in the network (or if we are the closest peer in the
1153 * @param type type of the block
1154 * @param options routing options
1155 * @param desired_replication_level desired replication count
1156 * @param expiration_time when does the content expire
1157 * @param hop_count how many hops has this message traversed so far
1158 * @param bf Bloom filter of peers this PUT has already traversed
1159 * @param key key for the content
1160 * @param put_path_length number of entries in put_path
1161 * @param put_path peers this request has traversed so far (if tracked)
1162 * @param data payload to store
1163 * @param data_size number of bytes in data
1166 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1167 enum GNUNET_DHT_RouteOption options,
1168 uint32_t desired_replication_level,
1169 struct GNUNET_TIME_Absolute expiration_time,
1171 struct GNUNET_CONTAINER_BloomFilter *bf,
1172 const GNUNET_HashCode *key,
1173 unsigned int put_path_length,
1174 struct GNUNET_PeerIdentity *put_path,
1178 unsigned int target_count;
1180 struct PeerInfo **targets;
1181 struct PeerInfo *target;
1182 struct P2PPendingMessage *pending;
1184 struct PeerPutMessage *ppm;
1185 struct GNUNET_PeerIdentity *pp;
1187 GNUNET_STATISTICS_update (GDS_stats,
1188 gettext_noop ("# PUT requests routed"), 1,
1190 target_count = get_target_peers (key, bf, hop_count,
1191 desired_replication_level,
1193 if (0 == target_count)
1195 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + sizeof (struct PeerPutMessage);
1196 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1198 put_path_length = 0;
1199 msize = data_size + sizeof (struct PeerPutMessage);
1201 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1206 GNUNET_STATISTICS_update (GDS_stats,
1207 gettext_noop ("# Peers selected as targets for PUT requests"), target_count,
1209 for (i=0;i<target_count;i++)
1211 target = targets[i];
1212 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1213 pending->importance = 0; /* FIXME */
1214 pending->timeout = expiration_time;
1215 ppm = (struct PeerPutMessage*) &pending[1];
1216 pending->msg = &ppm->header;
1217 ppm->header.size = htons (msize);
1218 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1219 ppm->options = htonl (options);
1220 ppm->type = htonl (type);
1221 ppm->hop_count = htonl (hop_count + 1);
1222 ppm->desired_replication_level = htonl (desired_replication_level);
1223 ppm->put_path_length = htonl (put_path_length);
1224 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1226 GNUNET_assert (GNUNET_OK ==
1227 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
1231 pp = (struct GNUNET_PeerIdentity*) &ppm[1];
1232 memcpy (pp, put_path, sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1233 memcpy (&pp[put_path_length], data, data_size);
1234 GNUNET_CONTAINER_DLL_insert_tail (target->head,
1237 target->pending_count++;
1238 process_peer_queue (target);
1240 GNUNET_free (targets);
1245 * Perform a GET operation. Forwards the given request to other
1246 * peers. Does not lookup the key locally. May do nothing if this is
1247 * the only peer in the network (or if we are the closest peer in the
1250 * @param type type of the block
1251 * @param options routing options
1252 * @param desired_replication_level desired replication count
1253 * @param hop_count how many hops did this request traverse so far?
1254 * @param key key for the content
1255 * @param xquery extended query
1256 * @param xquery_size number of bytes in xquery
1257 * @param reply_bf bloomfilter to filter duplicates
1258 * @param reply_bf_mutator mutator for reply_bf
1259 * @param peer_bf filter for peers not to select (again)
1262 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1263 enum GNUNET_DHT_RouteOption options,
1264 uint32_t desired_replication_level,
1266 const GNUNET_HashCode *key,
1269 const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
1270 uint32_t reply_bf_mutator,
1271 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1273 unsigned int target_count;
1275 struct PeerInfo **targets;
1276 struct PeerInfo *target;
1277 struct P2PPendingMessage *pending;
1279 struct PeerGetMessage *pgm;
1281 size_t reply_bf_size;
1283 GNUNET_STATISTICS_update (GDS_stats,
1284 gettext_noop ("# GET requests routed"), 1,
1286 target_count = get_target_peers (key, peer_bf, hop_count,
1287 desired_replication_level,
1289 if (0 == target_count)
1291 reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
1292 msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
1293 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1298 GNUNET_STATISTICS_update (GDS_stats,
1299 gettext_noop ("# Peers selected as targets for GET requests"), target_count,
1301 /* forward request */
1302 for (i=0;i<target_count;i++)
1304 target = targets[i];
1305 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1306 pending->importance = 0; /* FIXME */
1307 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1308 pgm = (struct PeerGetMessage*) &pending[1];
1309 pending->msg = &pgm->header;
1310 pgm->header.size = htons (msize);
1311 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1312 pgm->options = htonl (options);
1313 pgm->type = htonl (type);
1314 pgm->hop_count = htonl (hop_count + 1);
1315 pgm->desired_replication_level = htonl (desired_replication_level);
1316 pgm->xquery_size = htonl (xquery_size);
1317 pgm->bf_mutator = reply_bf_mutator;
1318 if (NULL != peer_bf)
1319 GNUNET_assert (GNUNET_OK ==
1320 GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
1324 xq = (char *) &pgm[1];
1325 memcpy (xq, xquery, xquery_size);
1326 if (NULL != reply_bf)
1327 GNUNET_assert (GNUNET_OK ==
1328 GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
1331 GNUNET_CONTAINER_DLL_insert_tail (target->head,
1334 target->pending_count++;
1335 process_peer_queue (target);
1337 GNUNET_free (targets);
1342 * Handle a reply (route to origin). Only forwards the reply back to
1343 * the given peer. Does not do local caching or forwarding to local
1346 * @param target neighbour that should receive the block (if still connected)
1347 * @param type type of the block
1348 * @param expiration_time when does the content expire
1349 * @param key key for the content
1350 * @param put_path_length number of entries in put_path
1351 * @param put_path peers the original PUT traversed (if tracked)
1352 * @param get_path_length number of entries in put_path
1353 * @param get_path peers this reply has traversed so far (if tracked)
1354 * @param data payload of the reply
1355 * @param data_size number of bytes in data
1358 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1359 enum GNUNET_BLOCK_Type type,
1360 struct GNUNET_TIME_Absolute expiration_time,
1361 const GNUNET_HashCode *key,
1362 unsigned int put_path_length,
1363 const struct GNUNET_PeerIdentity *put_path,
1364 unsigned int get_path_length,
1365 const struct GNUNET_PeerIdentity *get_path,
1369 struct PeerInfo *pi;
1370 struct P2PPendingMessage *pending;
1372 struct PeerResultMessage *prm;
1373 struct GNUNET_PeerIdentity *paths;
1375 msize = data_size + sizeof (struct PeerResultMessage) +
1376 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
1377 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1378 (get_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1379 (put_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1380 (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE) )
1385 pi = GNUNET_CONTAINER_multihashmap_get (all_known_peers,
1386 &target->hashPubKey);
1389 /* peer disconnected in the meantime, drop reply */
1392 GNUNET_STATISTICS_update (GDS_stats,
1393 gettext_noop ("# REPLIES routed"), 1,
1395 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1396 pending->importance = 0; /* FIXME */
1397 pending->timeout = expiration_time;
1398 prm = (struct PeerResultMessage*) &pending[1];
1399 pending->msg = &prm->header;
1400 prm->header.size = htons (msize);
1401 prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1402 prm->type = htonl (type);
1403 prm->put_path_length = htonl (put_path_length);
1404 prm->get_path_length = htonl (get_path_length);
1405 prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1407 paths = (struct GNUNET_PeerIdentity*) &prm[1];
1408 memcpy (paths, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity));
1409 memcpy (&paths[put_path_length],
1410 get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
1411 memcpy (&paths[put_path_length + get_path_length],
1413 GNUNET_CONTAINER_DLL_insert (pi->head,
1416 pi->pending_count++;
1417 process_peer_queue (pi);
1422 * To be called on core init/fail.
1424 * @param cls service closure
1425 * @param server handle to the server for this service
1426 * @param identity the public identity of this peer
1427 * @param publicKey the public key of this peer
1430 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1431 const struct GNUNET_PeerIdentity *identity,
1432 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1434 GNUNET_assert (server != NULL);
1435 my_identity = *identity;
1440 * Core handler for p2p put requests.
1442 * @param cls closure
1443 * @param peer sender of the request
1444 * @param message message
1445 * @param peer peer identity this notification is about
1446 * @param atsi performance data
1447 * @return GNUNET_OK to keep the connection open,
1448 * GNUNET_SYSERR to close it (signal serious error)
1451 handle_dht_p2p_put (void *cls,
1452 const struct GNUNET_PeerIdentity *peer,
1453 const struct GNUNET_MessageHeader *message,
1454 const struct GNUNET_TRANSPORT_ATS_Information
1457 const struct PeerPutMessage *put;
1458 const struct GNUNET_PeerIdentity *put_path;
1459 const void *payload;
1462 size_t payload_size;
1463 enum GNUNET_DHT_RouteOption options;
1464 struct GNUNET_CONTAINER_BloomFilter *bf;
1465 GNUNET_HashCode test_key;
1467 msize = ntohs (message->size);
1468 if (msize < sizeof (struct PeerPutMessage))
1470 GNUNET_break_op (0);
1473 put = (const struct PeerPutMessage*) message;
1474 putlen = ntohl (put->put_path_length);
1475 if ( (msize < sizeof (struct PeerPutMessage) + putlen * sizeof (struct GNUNET_PeerIdentity)) ||
1476 (putlen > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) )
1478 GNUNET_break_op (0);
1481 GNUNET_STATISTICS_update (GDS_stats,
1482 gettext_noop ("# P2P PUT requests received"), 1,
1484 put_path = (const struct GNUNET_PeerIdentity*) &put[1];
1485 payload = &put_path[putlen];
1486 options = ntohl (put->options);
1487 payload_size = msize - (sizeof (struct PeerPutMessage) +
1488 putlen * sizeof (struct GNUNET_PeerIdentity));
1489 switch (GNUNET_BLOCK_get_key (GDS_block_context,
1491 payload, payload_size,
1495 if (0 != memcmp (&test_key, &put->key, sizeof (GNUNET_HashCode)))
1497 GNUNET_break_op (0);
1502 GNUNET_break_op (0);
1505 /* cannot verify, good luck */
1508 bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
1510 GNUNET_CONSTANTS_BLOOMFILTER_K);
1512 struct GNUNET_PeerIdentity pp[putlen+1];
1514 /* extend 'put path' by sender */
1515 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
1517 memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
1524 /* give to local clients */
1525 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1534 if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1535 (am_closest_peer (&put->key,
1537 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1543 /* route to other peers */
1544 GDS_NEIGHBOURS_handle_put (ntohl (put->type),
1546 ntohl (put->desired_replication_level),
1547 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1548 ntohl (put->hop_count) + 1 /* who adds +1? */,
1555 GNUNET_CONTAINER_bloomfilter_free (bf);
1561 * We have received a FIND PEER request. Send matching
1564 * @param sender sender of the FIND PEER request
1565 * @param key peers close to this key are desired
1566 * @param bf peers matching this bf are excluded
1567 * @param bf_mutator mutator for bf
1570 handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1571 const GNUNET_HashCode *key,
1572 struct GNUNET_CONTAINER_BloomFilter *bf,
1573 uint32_t bf_mutator)
1576 struct PeerBucket *bucket;
1577 struct PeerInfo *peer;
1578 unsigned int choice;
1579 GNUNET_HashCode mhash;
1580 const struct GNUNET_HELLO_Message *hello;
1582 /* first, check about our own HELLO */
1583 if (NULL != GDS_my_hello)
1585 GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
1586 if ( (NULL == bf) ||
1587 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) )
1589 GDS_NEIGHBOURS_handle_reply (sender,
1590 GNUNET_BLOCK_TYPE_DHT_HELLO,
1591 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1596 GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*) GDS_my_hello));
1600 GNUNET_STATISTICS_update (GDS_stats,
1601 gettext_noop ("# FIND PEER requests ignored due to Bloomfilter"), 1,
1607 GNUNET_STATISTICS_update (GDS_stats,
1608 gettext_noop ("# FIND PEER requests ignored due to lack of HELLO"), 1,
1612 /* then, also consider sending a random HELLO from the closest bucket */
1613 if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
1614 bucket_idx = closest_bucket;
1616 bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key));
1617 if (bucket_idx == GNUNET_SYSERR)
1619 bucket = &k_buckets[bucket_idx];
1620 if (bucket->peers_size == 0)
1622 choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1623 bucket->peers_size);
1624 peer = bucket->head;
1627 GNUNET_assert (peer != NULL);
1630 choice = bucket->peers_size;
1635 return; /* no non-masked peer available */
1637 peer = bucket->head;
1638 GNUNET_BLOCK_mingle_hash (&peer->id.hashPubKey, bf_mutator, &mhash);
1639 hello = GDS_HELLO_get (&peer->id);
1641 while ( (hello == NULL) ||
1642 (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) );
1643 GDS_NEIGHBOURS_handle_reply (sender,
1644 GNUNET_BLOCK_TYPE_DHT_HELLO,
1645 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1650 GNUNET_HELLO_size (hello));
1655 * Core handler for p2p get requests.
1657 * @param cls closure
1658 * @param peer sender of the request
1659 * @param message message
1660 * @param peer peer identity this notification is about
1661 * @param atsi performance data
1662 * @return GNUNET_OK to keep the connection open,
1663 * GNUNET_SYSERR to close it (signal serious error)
1666 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
1667 const struct GNUNET_MessageHeader *message,
1668 const struct GNUNET_TRANSPORT_ATS_Information
1671 struct PeerGetMessage *get;
1672 uint32_t xquery_size;
1673 size_t reply_bf_size;
1675 enum GNUNET_BLOCK_Type type;
1676 enum GNUNET_DHT_RouteOption options;
1677 enum GNUNET_BLOCK_EvaluationResult eval;
1678 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
1679 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
1682 /* parse and validate message */
1683 msize = ntohs (message->size);
1684 if (msize < sizeof (struct PeerGetMessage))
1686 GNUNET_break_op (0);
1689 get = (struct PeerGetMessage *) message;
1690 xquery_size = ntohl (get->xquery_size);
1691 if (msize < sizeof (struct PeerGetMessage) + xquery_size)
1693 GNUNET_break_op (0);
1696 GNUNET_STATISTICS_update (GDS_stats,
1697 gettext_noop ("# P2P GET requests received"), 1,
1699 reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
1700 type = ntohl (get->type);
1701 options = ntohl (get->options);
1702 xquery = (const char*) &get[1];
1704 if (reply_bf_size > 0)
1705 reply_bf = GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size],
1707 GNUNET_CONSTANTS_BLOOMFILTER_K);
1708 eval = GNUNET_BLOCK_evaluate (GDS_block_context,
1713 xquery, xquery_size,
1715 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
1717 /* request invalid or block type not supported */
1718 GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
1719 if (NULL != reply_bf)
1720 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1724 GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
1726 GNUNET_CONSTANTS_BLOOMFILTER_K);
1728 /* remember request for routing replies */
1729 GDS_ROUTING_add (peer,
1733 xquery, xquery_size,
1734 reply_bf, get->bf_mutator);
1736 /* local lookup (this may update the reply_bf) */
1737 if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1738 (am_closest_peer (&get->key,
1741 if ( (0 != (options & GNUNET_DHT_RO_FIND_PEER)))
1743 GNUNET_STATISTICS_update (GDS_stats,
1744 gettext_noop ("# P2P FIND PEER requests processed"), 1,
1746 handle_find_peer (peer,
1753 eval = GDS_DATACACHE_handle_get (&get->key,
1755 xquery, xquery_size,
1762 GNUNET_STATISTICS_update (GDS_stats,
1763 gettext_noop ("# P2P GET requests ONLY routed"), 1,
1767 /* P2P forwarding */
1768 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1769 GDS_NEIGHBOURS_handle_get (type,
1771 ntohl (get->desired_replication_level),
1772 ntohl (get->hop_count) + 1, /* CHECK: where (else) do we do +1? */
1774 xquery, xquery_size,
1779 if (NULL != reply_bf)
1780 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1781 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
1787 * Core handler for p2p result messages.
1789 * @param cls closure
1790 * @param message message
1791 * @param peer peer identity this notification is about
1792 * @param atsi performance data
1793 * @return GNUNET_YES (do not cut p2p connection)
1796 handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
1797 const struct GNUNET_MessageHeader *message,
1798 const struct GNUNET_TRANSPORT_ATS_Information
1801 const struct PeerResultMessage *prm;
1802 const struct GNUNET_PeerIdentity *put_path;
1803 const struct GNUNET_PeerIdentity *get_path;
1805 uint32_t get_path_length;
1806 uint32_t put_path_length;
1809 enum GNUNET_BLOCK_Type type;
1811 /* parse and validate message */
1812 msize = ntohs (message->size);
1813 if (msize < sizeof (struct PeerResultMessage))
1815 GNUNET_break_op (0);
1818 prm = (struct PeerResultMessage *) message;
1819 put_path_length = ntohl (prm->put_path_length);
1820 get_path_length = ntohl (prm->get_path_length);
1821 if ( (msize < sizeof (struct PeerResultMessage) +
1822 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity)) ||
1823 (get_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1824 (put_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) )
1826 GNUNET_break_op (0);
1829 GNUNET_STATISTICS_update (GDS_stats,
1830 gettext_noop ("# P2P RESULTS received"), 1,
1832 put_path = (const struct GNUNET_PeerIdentity*) &prm[1];
1833 get_path = &put_path[put_path_length];
1834 type = ntohl (prm->type);
1835 data = (const void*) &get_path[get_path_length];
1836 data_size = msize - (sizeof (struct PeerResultMessage) +
1837 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity));
1839 /* if we got a HELLO, consider it for our own routing table */
1840 if (type == GNUNET_BLOCK_TYPE_DHT_HELLO)
1842 const struct GNUNET_MessageHeader *h;
1843 struct GNUNET_PeerIdentity pid;
1846 /* Should be a HELLO, validate and consider using it! */
1847 if (data_size < sizeof (struct GNUNET_MessageHeader))
1849 GNUNET_break_op (0);
1853 if (data_size != ntohs (h->size))
1855 GNUNET_break_op (0);
1859 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) h,
1862 GNUNET_break_op (0);
1865 if (0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
1867 bucket = find_bucket (&pid.hashPubKey);
1868 if ( (bucket >= 0) &&
1869 (k_buckets[bucket].peers_size < bucket_size) )
1871 if (NULL != GDS_transport_handle)
1872 GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
1874 (void) GNUNET_CORE_peer_request_connect (coreAPI,
1881 /* append 'peer' to 'get_path' */
1883 struct GNUNET_PeerIdentity xget_path[get_path_length+1];
1885 memcpy (xget_path, get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
1886 xget_path[get_path_length] = *peer;
1889 /* forward to local clients */
1890 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1900 /* forward to other peers */
1901 GDS_ROUTING_process (type,
1902 GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1916 * Initialize neighbours subsystem.
1918 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1921 GDS_NEIGHBOURS_init ()
1923 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1924 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
1925 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
1926 {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0},
1929 unsigned long long temp_config_num;
1932 GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size",
1934 bucket_size = (unsigned int) temp_config_num;
1935 coreAPI = GNUNET_CORE_connect (GDS_cfg,
1939 &handle_core_connect,
1940 &handle_core_disconnect,
1941 NULL, /* Do we care about "status" updates? */
1945 if (coreAPI == NULL)
1946 return GNUNET_SYSERR;
1947 all_known_peers = GNUNET_CONTAINER_multihashmap_create (256);
1953 * Shutdown neighbours subsystem.
1956 GDS_NEIGHBOURS_done ()
1958 if (coreAPI == NULL)
1960 GNUNET_CORE_disconnect (coreAPI);
1962 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (all_known_peers));
1963 GNUNET_CONTAINER_multihashmap_destroy (all_known_peers);
1964 all_known_peers = NULL;
1965 if (GNUNET_SCHEDULER_NO_TASK != find_peer_task)
1967 GNUNET_SCHEDULER_cancel (find_peer_task);
1968 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
1973 /* end of gnunet-service-dht_neighbours.c */