2 This file is part of GNUnet.
3 Copyright (C) 2009-2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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
28 #include "gnunet_util_lib.h"
29 #include "gnunet_block_lib.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_nse_service.h"
34 #include "gnunet_ats_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_datacache.h"
43 #include "gnunet-service-dht_hello.h"
44 #include "gnunet-service-dht_neighbours.h"
45 #include "gnunet-service-dht_nse.h"
46 #include "gnunet-service-dht_routing.h"
49 #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
52 * Enable slow sanity checks to debug issues.
54 #define SANITY_CHECKS 1
57 * How many buckets will we allow total.
59 #define MAX_BUCKETS sizeof (struct GNUNET_HashCode) * 8
62 * What is the maximum number of peers in a given bucket.
64 #define DEFAULT_BUCKET_SIZE 8
67 * Desired replication level for FIND PEER requests
69 #define FIND_PEER_REPLICATION_LEVEL 4
72 * Maximum allowed replication level for all requests.
74 #define MAXIMUM_REPLICATION_LEVEL 16
77 * Maximum allowed number of pending messages per peer.
79 #define MAXIMUM_PENDING_PER_PEER 64
82 * How long at least to wait before sending another find peer request.
84 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
87 * How long at most to wait before sending another find peer request.
89 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10)
92 * How long at most to wait for transmission of a GET request to another peer?
94 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
97 * Hello address expiration
99 extern struct GNUNET_TIME_Relative hello_expiration;
102 GNUNET_NETWORK_STRUCT_BEGIN
107 struct PeerPutMessage
110 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
112 struct GNUNET_MessageHeader header;
117 uint32_t options GNUNET_PACKED;
122 uint32_t type GNUNET_PACKED;
127 uint32_t hop_count GNUNET_PACKED;
130 * Replication level for this message
132 uint32_t desired_replication_level GNUNET_PACKED;
135 * Length of the PUT path that follows (if tracked).
137 uint32_t put_path_length GNUNET_PACKED;
140 * When does the content expire?
142 struct GNUNET_TIME_AbsoluteNBO expiration_time;
145 * Bloomfilter (for peer identities) to stop circular routes
147 char bloomfilter[DHT_BLOOM_SIZE];
150 * The key we are storing under.
152 struct GNUNET_HashCode key;
154 /* put path (if tracked) */
164 struct PeerResultMessage
167 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
169 struct GNUNET_MessageHeader header;
174 uint32_t type GNUNET_PACKED;
177 * Length of the PUT path that follows (if tracked).
179 uint32_t put_path_length GNUNET_PACKED;
182 * Length of the GET path that follows (if tracked).
184 uint32_t get_path_length GNUNET_PACKED;
187 * When does the content expire?
189 struct GNUNET_TIME_AbsoluteNBO expiration_time;
192 * The key of the corresponding GET request.
194 struct GNUNET_HashCode key;
196 /* put path (if tracked) */
198 /* get path (if tracked) */
208 struct PeerGetMessage
211 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET
213 struct GNUNET_MessageHeader header;
218 uint32_t options GNUNET_PACKED;
221 * Desired content type.
223 uint32_t type GNUNET_PACKED;
228 uint32_t hop_count GNUNET_PACKED;
231 * Desired replication level for this request.
233 uint32_t desired_replication_level GNUNET_PACKED;
236 * Size of the extended query.
238 uint32_t xquery_size;
241 * Bloomfilter mutator.
246 * Bloomfilter (for peer identities) to stop circular routes
248 char bloomfilter[DHT_BLOOM_SIZE];
251 * The key we are looking for.
253 struct GNUNET_HashCode key;
257 /* result bloomfilter */
260 GNUNET_NETWORK_STRUCT_END
264 * Entry for a peer in a bucket.
269 * Next peer entry (DLL)
271 struct PeerInfo *next;
274 * Prev peer entry (DLL)
276 struct PeerInfo *prev;
279 * Handle for sending messages to this peer.
281 struct GNUNET_MQ_Handle *mq;
284 * What is the identity of the peer?
286 const struct GNUNET_PeerIdentity *id;
291 struct GNUNET_HashCode phash;
294 * Which bucket is this peer in?
302 * Peers are grouped into buckets.
309 struct PeerInfo *head;
314 struct PeerInfo *tail;
317 * Number of peers in the bucket.
319 unsigned int peers_size;
324 * Information about a peer that we would like to connect to.
330 * Handle to active HELLO offer operation, or NULL.
332 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
335 * Handle to active connectivity suggestion operation, or NULL.
337 struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
340 * How much would we like to connect to this peer?
347 * Do we cache all results that we are routing in the local datacache?
349 static int cache_results;
352 * Should routing details be logged to stderr (for debugging)?
354 static int log_route_details_stderr;
357 * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
359 static unsigned int closest_bucket;
362 * How many peers have we added since we sent out our last
365 static unsigned int newly_found_peers;
368 * Option for testing that disables the 'connect' function of the DHT.
370 static int disable_try_connect;
373 * The buckets. Array of size #MAX_BUCKETS. Offset 0 means 0 bits matching.
375 static struct PeerBucket k_buckets[MAX_BUCKETS];
378 * Hash map of all CORE-connected peers, for easy removal from
379 * #k_buckets on disconnect. Values are of type `struct PeerInfo`.
381 static struct GNUNET_CONTAINER_MultiPeerMap *all_connected_peers;
384 * Hash map of all peers we would like to be connected to.
385 * Values are of type `struct ConnectInfo`.
387 static struct GNUNET_CONTAINER_MultiPeerMap *all_desired_peers;
390 * Maximum size for each bucket.
392 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
395 * Task that sends FIND PEER requests.
397 static struct GNUNET_SCHEDULER_Task *find_peer_task;
400 * Identity of this peer.
402 static struct GNUNET_PeerIdentity my_identity;
405 * Hash of the identity of this peer.
407 static struct GNUNET_HashCode my_identity_hash;
412 static struct GNUNET_CORE_Handle *core_api;
415 * Handle to ATS connectivity.
417 static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
421 * Find the optimal bucket for this key.
423 * @param hc the hashcode to compare our identity to
424 * @return the proper bucket index, or GNUNET_SYSERR
425 * on error (same hashcode)
428 find_bucket (const struct GNUNET_HashCode *hc)
432 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, hc);
433 if (bits == MAX_BUCKETS)
435 /* How can all bits match? Got my own ID? */
437 return GNUNET_SYSERR;
439 return MAX_BUCKETS - bits - 1;
444 * Function called when #GNUNET_TRANSPORT_offer_hello() is done.
445 * Clean up the "oh" field in the @a cls
447 * @param cls a `struct ConnectInfo`
450 offer_hello_done (void *cls)
452 struct ConnectInfo *ci = cls;
459 * Function called for all entries in #all_desired_peers to clean up.
462 * @param peer peer the entry is for
463 * @param value the value to remove
464 * @return #GNUNET_YES
467 free_connect_info (void *cls,
468 const struct GNUNET_PeerIdentity *peer,
471 struct ConnectInfo *ci = value;
473 GNUNET_assert (GNUNET_YES ==
474 GNUNET_CONTAINER_multipeermap_remove (all_desired_peers,
479 GNUNET_ATS_connectivity_suggest_cancel (ci->sh);
484 GNUNET_TRANSPORT_offer_hello_cancel (ci->oh);
493 * Consider if we want to connect to a given peer, and if so
494 * let ATS know. If applicable, the HELLO is offered to the
497 * @param pid peer to consider connectivity requirements for
498 * @param h a HELLO message, or NULL
501 try_connect (const struct GNUNET_PeerIdentity *pid,
502 const struct GNUNET_MessageHeader *h)
505 struct GNUNET_HashCode pid_hash;
506 struct ConnectInfo *ci;
509 GNUNET_CRYPTO_hash (pid,
510 sizeof (struct GNUNET_PeerIdentity),
512 bucket = find_bucket (&pid_hash);
515 ci = GNUNET_CONTAINER_multipeermap_get (all_desired_peers,
518 if (k_buckets[bucket].peers_size < bucket_size)
519 strength = (bucket_size - k_buckets[bucket].peers_size) * bucket;
521 strength = bucket; /* minimum value of connectivity */
523 GNUNET_CONTAINER_multipeermap_contains (all_connected_peers,
525 strength *= 2; /* double for connected peers */
526 else if (k_buckets[bucket].peers_size > bucket_size)
527 strength = 0; /* bucket full, we really do not care about more */
529 if ( (0 == strength) &&
532 /* release request */
533 GNUNET_assert (GNUNET_YES ==
534 free_connect_info (NULL,
541 ci = GNUNET_new (struct ConnectInfo);
542 GNUNET_assert (GNUNET_OK ==
543 GNUNET_CONTAINER_multipeermap_put (all_desired_peers,
546 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
548 if ( (NULL != ci->oh) &&
550 GNUNET_TRANSPORT_offer_hello_cancel (ci->oh);
552 ci->oh = GNUNET_TRANSPORT_offer_hello (GDS_cfg,
556 if ( (NULL != ci->sh) &&
557 (ci->strength != strength) )
558 GNUNET_ATS_connectivity_suggest_cancel (ci->sh);
559 if (ci->strength != strength)
560 ci->sh = GNUNET_ATS_connectivity_suggest (ats_ch,
563 ci->strength = strength;
568 * Function called for each peer in #all_desired_peers during
569 * #update_connect_preferences() if we have reason to adjust
570 * the strength of our desire to keep connections to certain
571 * peers. Calls #try_connect() to update the calculations for
575 * @param pid peer to update
576 * @param value unused
577 * @return #GNUNET_YES (continue to iterate)
580 update_desire_strength (void *cls,
581 const struct GNUNET_PeerIdentity *pid,
584 try_connect (pid, NULL);
590 * Update our preferences for connectivity as given to ATS.
592 * @param cls the `struct PeerInfo` of the peer
593 * @param tc scheduler context.
596 update_connect_preferences ()
598 GNUNET_CONTAINER_multipeermap_iterate (all_desired_peers,
599 &update_desire_strength,
605 * Add each of the peers we already know to the bloom filter of
606 * the request so that we don't get duplicate HELLOs.
608 * @param cls the `struct GNUNET_BLOCK_Group`
609 * @param key peer identity to add to the bloom filter
610 * @param value value the peer information (unused)
611 * @return #GNUNET_YES (we should continue to iterate)
614 add_known_to_bloom (void *cls,
615 const struct GNUNET_PeerIdentity *key,
618 struct GNUNET_BLOCK_Group *bg = cls;
619 struct GNUNET_HashCode key_hash;
621 GNUNET_CRYPTO_hash (key,
622 sizeof (struct GNUNET_PeerIdentity),
624 GNUNET_BLOCK_group_set_seen (bg,
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Adding known peer (%s) to bloomfilter for FIND PEER\n",
635 * Task to send a find peer message for our own peer identifier
636 * so that we can find the closest peers in the network to ourselves
637 * and attempt to connect to them.
639 * @param cls closure for this task
642 send_find_peer_message (void *cls)
644 struct GNUNET_TIME_Relative next_send_time;
645 struct GNUNET_BLOCK_Group *bg;
646 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
648 find_peer_task = NULL;
649 if (newly_found_peers > bucket_size)
651 /* If we are finding many peers already, no need to send out our request right now! */
653 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
654 &send_find_peer_message,
656 newly_found_peers = 0;
659 bg = GNUNET_BLOCK_group_create (GDS_block_context,
660 GNUNET_BLOCK_TYPE_DHT_HELLO,
661 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
668 GNUNET_CONTAINER_multipeermap_iterate (all_connected_peers,
671 GNUNET_STATISTICS_update (GDS_stats,
672 gettext_noop ("# FIND PEER messages initiated"),
676 = GNUNET_CONTAINER_bloomfilter_init (NULL,
678 GNUNET_CONSTANTS_BLOOMFILTER_K);
679 // FIXME: pass priority!?
680 GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
681 GNUNET_DHT_RO_FIND_PEER,
682 FIND_PEER_REPLICATION_LEVEL,
689 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
690 GNUNET_BLOCK_group_destroy (bg);
691 /* schedule next round */
692 next_send_time.rel_value_us =
693 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value_us +
694 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
695 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value_us /
696 (newly_found_peers + 1));
697 newly_found_peers = 0;
698 GNUNET_assert (NULL == find_peer_task);
700 GNUNET_SCHEDULER_add_delayed (next_send_time,
701 &send_find_peer_message,
707 * Method called whenever a peer connects.
710 * @param peer peer identity this notification is about
711 * @param mq message queue for sending messages to @a peer
712 * @return our `struct PeerInfo` for @a peer
715 handle_core_connect (void *cls,
716 const struct GNUNET_PeerIdentity *peer,
717 struct GNUNET_MQ_Handle *mq)
721 /* Check for connect to self message */
722 if (0 == memcmp (&my_identity,
724 sizeof (struct GNUNET_PeerIdentity)))
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 GNUNET_assert (GNUNET_NO ==
730 GNUNET_CONTAINER_multipeermap_get (all_connected_peers,
732 GNUNET_STATISTICS_update (GDS_stats,
733 gettext_noop ("# peers connected"),
736 pi = GNUNET_new (struct PeerInfo);
739 GNUNET_CRYPTO_hash (peer,
740 sizeof (struct GNUNET_PeerIdentity),
742 pi->peer_bucket = find_bucket (&pi->phash);
743 GNUNET_assert ( (pi->peer_bucket >= 0) &&
744 (pi->peer_bucket < MAX_BUCKETS) );
745 GNUNET_CONTAINER_DLL_insert_tail (k_buckets[pi->peer_bucket].head,
746 k_buckets[pi->peer_bucket].tail,
748 k_buckets[pi->peer_bucket].peers_size++;
749 closest_bucket = GNUNET_MAX (closest_bucket,
751 GNUNET_assert (GNUNET_OK ==
752 GNUNET_CONTAINER_multipeermap_put (all_connected_peers,
755 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
756 if ( (pi->peer_bucket > 0) &&
757 (k_buckets[pi->peer_bucket].peers_size <= bucket_size))
759 update_connect_preferences ();
762 if ( (1 == GNUNET_CONTAINER_multipeermap_size (all_connected_peers)) &&
763 (GNUNET_YES != disable_try_connect))
765 /* got a first connection, good time to start with FIND PEER requests... */
766 GNUNET_assert (NULL == find_peer_task);
767 find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message,
775 * Method called whenever a peer disconnects.
778 * @param peer peer identity this notification is about
779 * @param internal_cls our `struct PeerInfo` for @a peer
782 handle_core_disconnect (void *cls,
783 const struct GNUNET_PeerIdentity *peer,
786 struct PeerInfo *to_remove = internal_cls;
788 /* Check for disconnect from self message */
789 if (NULL == to_remove)
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
794 GNUNET_STATISTICS_update (GDS_stats,
795 gettext_noop ("# peers connected"),
798 GNUNET_assert (GNUNET_YES ==
799 GNUNET_CONTAINER_multipeermap_remove (all_connected_peers,
802 if ( (0 == GNUNET_CONTAINER_multipeermap_size (all_connected_peers)) &&
803 (GNUNET_YES != disable_try_connect) )
805 GNUNET_SCHEDULER_cancel (find_peer_task);
806 find_peer_task = NULL;
808 GNUNET_assert (to_remove->peer_bucket >= 0);
809 GNUNET_CONTAINER_DLL_remove (k_buckets[to_remove->peer_bucket].head,
810 k_buckets[to_remove->peer_bucket].tail,
812 GNUNET_assert (k_buckets[to_remove->peer_bucket].peers_size > 0);
813 k_buckets[to_remove->peer_bucket].peers_size--;
814 while ( (closest_bucket > 0) &&
815 (0 == k_buckets[to_remove->peer_bucket].peers_size) )
817 if (k_buckets[to_remove->peer_bucket].peers_size < bucket_size)
818 update_connect_preferences ();
819 GNUNET_free (to_remove);
824 * To how many peers should we (on average) forward the request to
825 * obtain the desired target_replication count (on average).
827 * @param hop_count number of hops the message has traversed
828 * @param target_replication the number of total paths desired
829 * @return Some number of peers to forward the message to
832 get_forward_count (uint32_t hop_count,
833 uint32_t target_replication)
835 uint32_t random_value;
836 uint32_t forward_count;
839 if (hop_count > GDS_NSE_get () * 4.0)
841 /* forcefully terminate */
842 GNUNET_STATISTICS_update (GDS_stats,
843 gettext_noop ("# requests TTL-dropped"),
847 if (hop_count > GDS_NSE_get () * 2.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.
878 * @return 0 if have==target, otherwise a number
879 * that is larger as the distance between
880 * the two hash codes increases
883 get_distance (const struct GNUNET_HashCode *target,
884 const struct GNUNET_HashCode *have)
891 /* We have to represent the distance between two 2^9 (=512)-bit
892 * numbers as a 2^5 (=32)-bit number with "0" being used for the
893 * two numbers being identical; furthermore, we need to
894 * guarantee that a difference in the number of matching
895 * bits is always represented in the result.
897 * We use 2^32/2^9 numerical values to distinguish between
898 * hash codes that have the same LSB bit distance and
899 * use the highest 2^9 bits of the result to signify the
900 * number of (mis)matching LSB bits; if we have 0 matching
901 * and hence 512 mismatching LSB bits we return -1 (since
902 * 512 itself cannot be represented with 9 bits) */
904 /* first, calculate the most significant 9 bits of our
905 * result, aka the number of LSBs */
906 bucket = GNUNET_CRYPTO_hash_matching_bits (target,
908 /* bucket is now a value between 0 and 512 */
910 return 0; /* perfect match */
912 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
913 * below, we'd end up with max+1 (overflow)) */
915 /* calculate the most significant bits of the final result */
916 msb = (512 - bucket) << (32 - 9);
917 /* calculate the 32-9 least significant bits of the final result by
918 * looking at the differences in the 32-9 bits following the
919 * mismatching bit at 'bucket' */
922 (i < sizeof (struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
924 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
925 GNUNET_CRYPTO_hash_get_bit (have, i))
926 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
927 * last bit set will be 31 -- if
928 * i does not reach 512 first... */
935 * Check whether my identity is closer than any known peers. If a
936 * non-null bloomfilter is given, check if this is the closest peer
937 * that hasn't already been routed to.
939 * @param key hash code to check closeness to
940 * @param bloom bloomfilter, exclude these entries from the decision
941 * @return #GNUNET_YES if node location is closest,
942 * #GNUNET_NO otherwise.
945 am_closest_peer (const struct GNUNET_HashCode *key,
946 const struct GNUNET_CONTAINER_BloomFilter *bloom)
952 struct PeerInfo *pos;
954 if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode)))
956 bucket_num = find_bucket (key);
957 GNUNET_assert (bucket_num >= 0);
958 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
960 pos = k_buckets[bucket_num].head;
962 while ((NULL != pos) && (count < bucket_size))
964 if ((NULL != bloom) &&
966 GNUNET_CONTAINER_bloomfilter_test (bloom,
970 continue; /* Skip already checked entries */
972 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->phash,
974 if (other_bits > bits)
976 if (other_bits == bits) /* We match the same number of bits */
980 /* No peers closer, we are the closest! */
986 * Select a peer from the routing table that would be a good routing
987 * destination for sending a message for "key". The resulting peer
988 * must not be in the set of blocked peers.<p>
990 * Note that we should not ALWAYS select the closest peer to the
991 * target, peers further away from the target should be chosen with
992 * exponentially declining probability.
994 * FIXME: double-check that this is fine
997 * @param key the key we are selecting a peer to route to
998 * @param bloom a bloomfilter containing entries this request has seen already
999 * @param hops how many hops has this message traversed thus far
1000 * @return Peer to route to, or NULL on error
1002 static struct PeerInfo *
1003 select_peer (const struct GNUNET_HashCode *key,
1004 const struct GNUNET_CONTAINER_BloomFilter *bloom,
1009 unsigned int selected;
1010 struct PeerInfo *pos;
1012 unsigned int smallest_distance;
1013 struct PeerInfo *chosen;
1015 if (hops >= GDS_NSE_get ())
1017 /* greedy selection (closest peer that is not in bloomfilter) */
1018 smallest_distance = UINT_MAX;
1020 for (bc = 0; bc <= closest_bucket; bc++)
1022 pos = k_buckets[bc].head;
1024 while ((pos != NULL) && (count < bucket_size))
1026 if ((bloom == NULL) ||
1028 GNUNET_CONTAINER_bloomfilter_test (bloom,
1031 dist = get_distance (key,
1033 if (dist < smallest_distance)
1036 smallest_distance = dist;
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Excluded peer `%s' due to BF match in greedy routing for %s\n",
1043 GNUNET_i2s (pos->id),
1045 GNUNET_STATISTICS_update (GDS_stats,
1046 gettext_noop ("# Peers excluded from routing due to Bloomfilter"),
1049 dist = get_distance (key,
1051 if (dist < smallest_distance)
1054 smallest_distance = dist;
1062 GNUNET_STATISTICS_update (GDS_stats,
1063 gettext_noop ("# Peer selection failed"), 1,
1068 /* select "random" peer */
1069 /* count number of peers that are available and not filtered */
1071 for (bc = 0; bc <= closest_bucket; bc++)
1073 pos = k_buckets[bc].head;
1074 while ((pos != NULL) && (count < bucket_size))
1076 if ((bloom != NULL) &&
1078 GNUNET_CONTAINER_bloomfilter_test (bloom,
1081 GNUNET_STATISTICS_update (GDS_stats,
1083 ("# Peers excluded from routing due to Bloomfilter"),
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "Excluded peer `%s' due to BF match in random routing for %s\n",
1087 GNUNET_i2s (pos->id),
1090 continue; /* Ignore bloomfiltered peers */
1096 if (0 == count) /* No peers to select from! */
1098 GNUNET_STATISTICS_update (GDS_stats,
1099 gettext_noop ("# Peer selection failed"), 1,
1103 /* Now actually choose a peer */
1104 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1107 for (bc = 0; bc <= closest_bucket; bc++)
1109 for (pos = k_buckets[bc].head; ((pos != NULL) && (count < bucket_size)); pos = pos->next)
1111 if ((bloom != NULL) &&
1113 GNUNET_CONTAINER_bloomfilter_test (bloom,
1116 continue; /* Ignore bloomfiltered peers */
1118 if (0 == selected--)
1128 * Compute the set of peers that the given request should be
1131 * @param key routing key
1132 * @param bloom bloom filter excluding peers as targets, all selected
1133 * peers will be added to the bloom filter
1134 * @param hop_count number of hops the request has traversed so far
1135 * @param target_replication desired number of replicas
1136 * @param targets where to store an array of target peers (to be
1137 * free'd by the caller)
1138 * @return number of peers returned in 'targets'.
1141 get_target_peers (const struct GNUNET_HashCode *key,
1142 struct GNUNET_CONTAINER_BloomFilter *bloom,
1144 uint32_t target_replication,
1145 struct PeerInfo ***targets)
1149 struct PeerInfo **rtargets;
1150 struct PeerInfo *nxt;
1152 GNUNET_assert (NULL != bloom);
1153 ret = get_forward_count (hop_count,
1154 target_replication);
1160 rtargets = GNUNET_new_array (ret,
1162 for (off = 0; off < ret; off++)
1164 nxt = select_peer (key, bloom, hop_count);
1167 rtargets[off] = nxt;
1168 GNUNET_break (GNUNET_NO ==
1169 GNUNET_CONTAINER_bloomfilter_test (bloom,
1171 GNUNET_CONTAINER_bloomfilter_add (bloom,
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "Selected %u/%u peers at hop %u for %s (target was %u)\n",
1177 GNUNET_CONTAINER_multipeermap_size (all_connected_peers),
1178 (unsigned int) hop_count,
1183 GNUNET_free (rtargets);
1187 *targets = rtargets;
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Forwarding query `%s' to %u peers (goal was %u peers)\n",
1198 * Perform a PUT operation. Forwards the given request to other
1199 * peers. Does not store the data locally. Does not give the
1200 * data to local clients. May do nothing if this is the only
1201 * peer in the network (or if we are the closest peer in the
1204 * @param type type of the block
1205 * @param options routing options
1206 * @param desired_replication_level desired replication count
1207 * @param expiration_time when does the content expire
1208 * @param hop_count how many hops has this message traversed so far
1209 * @param bf Bloom filter of peers this PUT has already traversed
1210 * @param key key for the content
1211 * @param put_path_length number of entries in @a put_path
1212 * @param put_path peers this request has traversed so far (if tracked)
1213 * @param data payload to store
1214 * @param data_size number of bytes in @a data
1215 * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not
1218 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1219 enum GNUNET_DHT_RouteOption options,
1220 uint32_t desired_replication_level,
1221 struct GNUNET_TIME_Absolute expiration_time,
1223 struct GNUNET_CONTAINER_BloomFilter *bf,
1224 const struct GNUNET_HashCode *key,
1225 unsigned int put_path_length,
1226 struct GNUNET_PeerIdentity *put_path,
1230 unsigned int target_count;
1232 struct PeerInfo **targets;
1233 struct PeerInfo *target;
1235 struct GNUNET_MQ_Envelope *env;
1236 struct PeerPutMessage *ppm;
1237 struct GNUNET_PeerIdentity *pp;
1238 unsigned int skip_count;
1240 GNUNET_assert (NULL != bf);
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "Adding myself (%s) to PUT bloomfilter for %s\n",
1243 GNUNET_i2s (&my_identity),
1245 GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity_hash);
1246 GNUNET_STATISTICS_update (GDS_stats,
1247 gettext_noop ("# PUT requests routed"),
1251 = get_target_peers (key,
1254 desired_replication_level,
1256 if (0 == target_count)
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 "Routing PUT for %s terminates after %u hops at %s\n",
1261 (unsigned int) hop_count,
1262 GNUNET_i2s (&my_identity));
1265 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size;
1266 if (msize + sizeof (struct PeerPutMessage)
1267 >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1269 put_path_length = 0;
1272 if (msize + sizeof (struct PeerPutMessage)
1273 >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1276 GNUNET_free (targets);
1279 GNUNET_STATISTICS_update (GDS_stats,
1280 gettext_noop ("# PUT messages queued for transmission"),
1284 for (i = 0; i < target_count; i++)
1286 target = targets[i];
1287 if (GNUNET_MQ_get_length (target->mq) >= MAXIMUM_PENDING_PER_PEER)
1290 GNUNET_STATISTICS_update (GDS_stats,
1291 gettext_noop ("# P2P messages dropped due to full queue"),
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "Routing PUT for %s after %u hops to %s\n",
1300 (unsigned int) hop_count,
1301 GNUNET_i2s (target->id));
1302 env = GNUNET_MQ_msg_extra (ppm,
1304 GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1305 ppm->options = htonl (options);
1306 ppm->type = htonl (type);
1307 ppm->hop_count = htonl (hop_count + 1);
1308 ppm->desired_replication_level = htonl (desired_replication_level);
1309 ppm->put_path_length = htonl (put_path_length);
1310 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1311 GNUNET_break (GNUNET_YES ==
1312 GNUNET_CONTAINER_bloomfilter_test (bf,
1314 GNUNET_assert (GNUNET_OK ==
1315 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
1319 pp = (struct GNUNET_PeerIdentity *) &ppm[1];
1322 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1323 GNUNET_memcpy (&pp[put_path_length],
1326 GNUNET_MQ_send (target->mq,
1329 GNUNET_free (targets);
1330 return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO;
1335 * Perform a GET operation. Forwards the given request to other
1336 * peers. Does not lookup the key locally. May do nothing if this is
1337 * the only peer in the network (or if we are the closest peer in the
1340 * @param type type of the block
1341 * @param options routing options
1342 * @param desired_replication_level desired replication count
1343 * @param hop_count how many hops did this request traverse so far?
1344 * @param key key for the content
1345 * @param xquery extended query
1346 * @param xquery_size number of bytes in @a xquery
1347 * @param bg group to use for filtering replies
1348 * @param peer_bf filter for peers not to select (again)
1349 * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not
1352 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1353 enum GNUNET_DHT_RouteOption options,
1354 uint32_t desired_replication_level,
1356 const struct GNUNET_HashCode *key,
1359 struct GNUNET_BLOCK_Group *bg,
1360 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1362 unsigned int target_count;
1363 struct PeerInfo **targets;
1364 struct PeerInfo *target;
1365 struct GNUNET_MQ_Envelope *env;
1367 struct PeerGetMessage *pgm;
1369 size_t reply_bf_size;
1371 unsigned int skip_count;
1374 GNUNET_assert (NULL != peer_bf);
1375 GNUNET_STATISTICS_update (GDS_stats,
1376 gettext_noop ("# GET requests routed"),
1379 target_count = get_target_peers (key,
1382 desired_replication_level,
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "Adding myself (%s) to GET bloomfilter for %s\n",
1386 GNUNET_i2s (&my_identity),
1388 GNUNET_CONTAINER_bloomfilter_add (peer_bf,
1390 if (0 == target_count)
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Routing GET for %s terminates after %u hops at %s\n",
1395 (unsigned int) hop_count,
1396 GNUNET_i2s (&my_identity));
1400 GNUNET_BLOCK_group_serialize (bg,
1407 bf_nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1410 msize = xquery_size + reply_bf_size;
1411 if (msize + sizeof (struct PeerGetMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1414 GNUNET_free_non_null (reply_bf);
1415 GNUNET_free (targets);
1418 GNUNET_STATISTICS_update (GDS_stats,
1419 gettext_noop ("# GET messages queued for transmission"),
1422 /* forward request */
1424 for (unsigned int i = 0; i < target_count; i++)
1426 target = targets[i];
1427 if (GNUNET_MQ_get_length (target->mq) >= MAXIMUM_PENDING_PER_PEER)
1430 GNUNET_STATISTICS_update (GDS_stats,
1431 gettext_noop ("# P2P messages dropped due to full queue"),
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1437 "Routing GET for %s after %u hops to %s\n",
1439 (unsigned int) hop_count,
1440 GNUNET_i2s (target->id));
1441 env = GNUNET_MQ_msg_extra (pgm,
1443 GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1444 pgm->options = htonl (options);
1445 pgm->type = htonl (type);
1446 pgm->hop_count = htonl (hop_count + 1);
1447 pgm->desired_replication_level = htonl (desired_replication_level);
1448 pgm->xquery_size = htonl (xquery_size);
1449 pgm->bf_mutator = bf_nonce;
1450 GNUNET_break (GNUNET_YES ==
1451 GNUNET_CONTAINER_bloomfilter_test (peer_bf,
1453 GNUNET_assert (GNUNET_OK ==
1454 GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
1458 xq = (char *) &pgm[1];
1462 GNUNET_memcpy (&xq[xquery_size],
1465 GNUNET_MQ_send (target->mq,
1468 GNUNET_free (targets);
1469 GNUNET_free_non_null (reply_bf);
1470 return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO;
1475 * Handle a reply (route to origin). Only forwards the reply back to
1476 * the given peer. Does not do local caching or forwarding to local
1479 * @param target neighbour that should receive the block (if still connected)
1480 * @param type type of the block
1481 * @param expiration_time when does the content expire
1482 * @param key key for the content
1483 * @param put_path_length number of entries in @a put_path
1484 * @param put_path peers the original PUT traversed (if tracked)
1485 * @param get_path_length number of entries in @a get_path
1486 * @param get_path peers this reply has traversed so far (if tracked)
1487 * @param data payload of the reply
1488 * @param data_size number of bytes in @a data
1491 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1492 enum GNUNET_BLOCK_Type type,
1493 struct GNUNET_TIME_Absolute expiration_time,
1494 const struct GNUNET_HashCode *key,
1495 unsigned int put_path_length,
1496 const struct GNUNET_PeerIdentity *put_path,
1497 unsigned int get_path_length,
1498 const struct GNUNET_PeerIdentity *get_path,
1502 struct PeerInfo *pi;
1503 struct GNUNET_MQ_Envelope *env;
1505 struct PeerResultMessage *prm;
1506 struct GNUNET_PeerIdentity *paths;
1508 msize = data_size + (get_path_length + put_path_length) *
1509 sizeof (struct GNUNET_PeerIdentity);
1510 if ((msize + sizeof (struct PeerResultMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1512 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1514 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1515 (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE))
1520 pi = GNUNET_CONTAINER_multipeermap_get (all_connected_peers,
1524 /* peer disconnected in the meantime, drop reply */
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "No matching peer for reply for key %s\n",
1530 if (GNUNET_MQ_get_length (pi->mq) >= MAXIMUM_PENDING_PER_PEER)
1533 GNUNET_STATISTICS_update (GDS_stats,
1534 gettext_noop ("# P2P messages dropped due to full queue"),
1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1538 "Peer queue full, ignoring reply for key %s\n",
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 "Forwarding reply for key %s to peer %s\n",
1546 GNUNET_i2s (target));
1547 GNUNET_STATISTICS_update (GDS_stats,
1549 ("# RESULT messages queued for transmission"), 1,
1551 env = GNUNET_MQ_msg_extra (prm,
1553 GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1554 prm->type = htonl (type);
1555 prm->put_path_length = htonl (put_path_length);
1556 prm->get_path_length = htonl (get_path_length);
1557 prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1559 paths = (struct GNUNET_PeerIdentity *) &prm[1];
1560 GNUNET_memcpy (paths,
1562 put_path_length * sizeof (struct GNUNET_PeerIdentity));
1563 GNUNET_memcpy (&paths[put_path_length],
1565 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1566 GNUNET_memcpy (&paths[put_path_length + get_path_length],
1569 GNUNET_MQ_send (pi->mq,
1575 * To be called on core init/fail.
1577 * @param cls service closure
1578 * @param identity the public identity of this peer
1581 core_init (void *cls,
1582 const struct GNUNET_PeerIdentity *identity)
1584 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1585 "CORE called, I am %s\n",
1586 GNUNET_i2s (identity));
1587 my_identity = *identity;
1588 GNUNET_CRYPTO_hash (identity,
1589 sizeof (struct GNUNET_PeerIdentity),
1591 GNUNET_SERVICE_resume (GDS_service);
1596 * Check validity of a p2p put request.
1598 * @param cls closure with the `struct PeerInfo` of the sender
1599 * @param message message
1600 * @return #GNUNET_OK if the message is valid
1603 check_dht_p2p_put (void *cls,
1604 const struct PeerPutMessage *put)
1609 msize = ntohs (put->header.size);
1610 putlen = ntohl (put->put_path_length);
1612 sizeof (struct PeerPutMessage) +
1613 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
1615 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
1617 GNUNET_break_op (0);
1618 return GNUNET_SYSERR;
1625 * Core handler for p2p put requests.
1627 * @param cls closure with the `struct PeerInfo` of the sender
1628 * @param message message
1631 handle_dht_p2p_put (void *cls,
1632 const struct PeerPutMessage *put)
1634 struct PeerInfo *peer = cls;
1635 const struct GNUNET_PeerIdentity *put_path;
1636 const void *payload;
1639 size_t payload_size;
1640 enum GNUNET_DHT_RouteOption options;
1641 struct GNUNET_CONTAINER_BloomFilter *bf;
1642 struct GNUNET_HashCode test_key;
1645 msize = ntohs (put->header.size);
1646 putlen = ntohl (put->put_path_length);
1647 GNUNET_STATISTICS_update (GDS_stats,
1648 gettext_noop ("# P2P PUT requests received"),
1651 GNUNET_STATISTICS_update (GDS_stats,
1652 gettext_noop ("# P2P PUT bytes received"),
1655 put_path = (const struct GNUNET_PeerIdentity *) &put[1];
1656 payload = &put_path[putlen];
1657 options = ntohl (put->options);
1658 payload_size = msize - (sizeof (struct PeerPutMessage) +
1659 putlen * sizeof (struct GNUNET_PeerIdentity));
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1662 "PUT for `%s' from %s\n",
1663 GNUNET_h2s (&put->key),
1664 GNUNET_i2s (peer->id));
1665 if (GNUNET_YES == log_route_details_stderr)
1669 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
1670 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
1671 "R5N PUT %s: %s->%s (%u, %u=>%u)\n",
1672 GNUNET_h2s (&put->key),
1673 GNUNET_i2s (peer->id),
1675 ntohl(put->hop_count),
1676 GNUNET_CRYPTO_hash_matching_bits (&peer->phash,
1678 GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
1683 switch (GNUNET_BLOCK_get_key
1691 if (0 != memcmp (&test_key,
1693 sizeof (struct GNUNET_HashCode)))
1695 char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
1697 GNUNET_break_op (0);
1698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1699 "PUT with key `%s' for block with key %s\n",
1701 GNUNET_h2s_full (&test_key));
1702 GNUNET_free (put_s);
1707 GNUNET_break_op (0);
1710 /* cannot verify, good luck */
1713 if (ntohl (put->type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
1715 switch (GNUNET_BLOCK_evaluate (GDS_block_context,
1717 NULL, /* query group */
1718 GNUNET_BLOCK_EO_NONE,
1720 NULL, 0, /* xquery */
1724 case GNUNET_BLOCK_EVALUATION_OK_MORE:
1725 case GNUNET_BLOCK_EVALUATION_OK_LAST:
1728 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
1729 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
1730 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
1731 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
1732 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
1733 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
1735 GNUNET_break_op (0);
1740 bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
1742 GNUNET_CONSTANTS_BLOOMFILTER_K);
1743 GNUNET_break_op (GNUNET_YES ==
1744 GNUNET_CONTAINER_bloomfilter_test (bf,
1747 struct GNUNET_PeerIdentity pp[putlen + 1];
1749 /* extend 'put path' by sender */
1750 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
1753 for (unsigned int i=0;i<=putlen;i++)
1755 for (unsigned int j=0;j<i;j++)
1757 GNUNET_break (0 != memcmp (&pp[i],
1759 sizeof (struct GNUNET_PeerIdentity)));
1761 GNUNET_break (0 != memcmp (&pp[i],
1763 sizeof (struct GNUNET_PeerIdentity)));
1768 putlen * sizeof (struct GNUNET_PeerIdentity));
1769 pp[putlen] = *peer->id;
1775 /* give to local clients */
1776 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1786 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1787 (am_closest_peer (&put->key, bf)))
1788 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1795 /* route to other peers */
1796 forwarded = GDS_NEIGHBOURS_handle_put (ntohl (put->type),
1798 ntohl (put->desired_replication_level),
1799 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1800 ntohl (put->hop_count),
1807 /* notify monitoring clients */
1808 GDS_CLIENTS_process_put (options
1809 | ( (GNUNET_OK == forwarded)
1810 ? GNUNET_DHT_RO_LAST_HOP
1813 ntohl (put->hop_count),
1814 ntohl (put->desired_replication_level),
1816 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1821 GNUNET_CONTAINER_bloomfilter_free (bf);
1826 * We have received a FIND PEER request. Send matching
1829 * @param sender sender of the FIND PEER request
1830 * @param key peers close to this key are desired
1831 * @param bg group for filtering peers
1834 handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1835 const struct GNUNET_HashCode *key,
1836 struct GNUNET_BLOCK_Group *bg)
1839 struct PeerBucket *bucket;
1840 struct PeerInfo *peer;
1841 unsigned int choice;
1842 const struct GNUNET_HELLO_Message *hello;
1845 /* first, check about our own HELLO */
1846 if (NULL != GDS_my_hello)
1848 hello_size = GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) GDS_my_hello);
1849 GNUNET_break (hello_size >= sizeof (struct GNUNET_MessageHeader));
1850 if (GNUNET_BLOCK_EVALUATION_OK_MORE ==
1851 GNUNET_BLOCK_evaluate (GDS_block_context,
1852 GNUNET_BLOCK_TYPE_DHT_HELLO,
1854 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
1860 GDS_NEIGHBOURS_handle_reply (sender,
1861 GNUNET_BLOCK_TYPE_DHT_HELLO,
1862 GNUNET_TIME_relative_to_absolute (hello_expiration),
1873 GNUNET_STATISTICS_update (GDS_stats,
1874 gettext_noop ("# FIND PEER requests ignored due to Bloomfilter"),
1881 GNUNET_STATISTICS_update (GDS_stats,
1882 gettext_noop ("# FIND PEER requests ignored due to lack of HELLO"),
1887 /* then, also consider sending a random HELLO from the closest bucket */
1888 if (0 == memcmp (&my_identity_hash,
1890 sizeof (struct GNUNET_HashCode)))
1891 bucket_idx = closest_bucket;
1893 bucket_idx = GNUNET_MIN (closest_bucket,
1895 if (bucket_idx == GNUNET_SYSERR)
1897 bucket = &k_buckets[bucket_idx];
1898 if (bucket->peers_size == 0)
1900 choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1901 bucket->peers_size);
1902 peer = bucket->head;
1905 GNUNET_assert (NULL != peer);
1909 choice = bucket->peers_size;
1914 return; /* no non-masked peer available */
1916 peer = bucket->head;
1917 hello = GDS_HELLO_get (peer->id);
1918 hello_size = GNUNET_HELLO_size (hello);
1919 } while ( (NULL == hello) ||
1920 (GNUNET_BLOCK_EVALUATION_OK_MORE !=
1921 GNUNET_BLOCK_evaluate (GDS_block_context,
1922 GNUNET_BLOCK_TYPE_DHT_HELLO,
1924 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
1929 GDS_NEIGHBOURS_handle_reply (sender,
1930 GNUNET_BLOCK_TYPE_DHT_HELLO,
1931 GNUNET_TIME_relative_to_absolute
1932 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1944 * Handle a result from local datacache for a GET operation.
1946 * @param cls the `struct ClientHandle` of the client doing the query
1947 * @param type type of the block
1948 * @param expiration_time when does the content expire
1949 * @param key key for the content
1950 * @param put_path_length number of entries in @a put_path
1951 * @param put_path peers the original PUT traversed (if tracked)
1952 * @param get_path_length number of entries in @a get_path
1953 * @param get_path peers this reply has traversed so far (if tracked)
1954 * @param data payload of the reply
1955 * @param data_size number of bytes in @a data
1958 handle_local_result (void *cls,
1959 enum GNUNET_BLOCK_Type type,
1960 struct GNUNET_TIME_Absolute expiration_time,
1961 const struct GNUNET_HashCode *key,
1962 unsigned int put_path_length,
1963 const struct GNUNET_PeerIdentity *put_path,
1964 unsigned int get_path_length,
1965 const struct GNUNET_PeerIdentity *get_path,
1969 // FIXME: we can probably do better here by
1970 // passing the peer that did the query in the closure...
1971 GDS_ROUTING_process (NULL,
1975 put_path_length, put_path,
1982 * Check validity of p2p get request.
1984 * @param cls closure with the `struct PeerInfo` of the sender
1985 * @param get the message
1986 * @return #GNUNET_OK if the message is well-formed
1989 check_dht_p2p_get (void *cls,
1990 const struct PeerGetMessage *get)
1992 uint32_t xquery_size;
1995 msize = ntohs (get->header.size);
1996 xquery_size = ntohl (get->xquery_size);
1997 if (msize < sizeof (struct PeerGetMessage) + xquery_size)
1999 GNUNET_break_op (0);
2000 return GNUNET_SYSERR;
2007 * Core handler for p2p get requests.
2009 * @param cls closure with the `struct PeerInfo` of the sender
2010 * @param get the message
2013 handle_dht_p2p_get (void *cls,
2014 const struct PeerGetMessage *get)
2016 struct PeerInfo *peer = cls;
2017 uint32_t xquery_size;
2018 size_t reply_bf_size;
2020 enum GNUNET_BLOCK_Type type;
2021 enum GNUNET_DHT_RouteOption options;
2022 enum GNUNET_BLOCK_EvaluationResult eval;
2023 struct GNUNET_BLOCK_Group *bg;
2024 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
2033 /* parse and validate message */
2034 msize = ntohs (get->header.size);
2035 xquery_size = ntohl (get->xquery_size);
2036 reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
2037 type = ntohl (get->type);
2038 options = ntohl (get->options);
2039 xquery = (const char *) &get[1];
2040 GNUNET_STATISTICS_update (GDS_stats,
2041 gettext_noop ("# P2P GET requests received"),
2044 GNUNET_STATISTICS_update (GDS_stats,
2045 gettext_noop ("# P2P GET bytes received"),
2048 if (GNUNET_YES == log_route_details_stderr)
2052 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
2053 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
2054 "R5N GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n",
2055 GNUNET_h2s (&get->key),
2056 GNUNET_i2s (peer->id),
2058 ntohl(get->hop_count),
2059 GNUNET_CRYPTO_hash_matching_bits (&peer->phash,
2061 GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
2063 ntohl(get->xquery_size),
2068 = GNUNET_BLOCK_evaluate (GDS_block_context,
2071 GNUNET_BLOCK_EO_NONE,
2077 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
2079 /* request invalid or block type not supported */
2080 GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
2083 peer_bf = GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
2085 GNUNET_CONSTANTS_BLOOMFILTER_K);
2086 GNUNET_break_op (GNUNET_YES ==
2087 GNUNET_CONTAINER_bloomfilter_test (peer_bf,
2089 bg = GNUNET_BLOCK_group_create (GDS_block_context,
2092 &xquery[xquery_size],
2097 /* remember request for routing replies */
2098 GDS_ROUTING_add (peer->id,
2100 bg, /* bg now owned by routing, but valid at least until end of this function! */
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2106 "GET for %s at %s after %u hops\n",
2107 GNUNET_h2s (&get->key),
2108 GNUNET_i2s (&my_identity),
2109 (unsigned int) ntohl (get->hop_count));
2110 /* local lookup (this may update the reply_bf) */
2111 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
2112 (am_closest_peer (&get->key,
2115 if ((0 != (options & GNUNET_DHT_RO_FIND_PEER)))
2117 GNUNET_STATISTICS_update (GDS_stats,
2118 gettext_noop ("# P2P FIND PEER requests processed"),
2121 handle_find_peer (peer->id,
2127 eval = GDS_DATACACHE_handle_get (&get->key,
2132 &handle_local_result,
2138 GNUNET_STATISTICS_update (GDS_stats,
2139 gettext_noop ("# P2P GET requests ONLY routed"),
2144 /* P2P forwarding */
2145 forwarded = GNUNET_NO;
2146 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
2147 forwarded = GDS_NEIGHBOURS_handle_get (type,
2149 ntohl (get->desired_replication_level),
2150 ntohl (get->hop_count),
2156 GDS_CLIENTS_process_get (options
2157 | (GNUNET_OK == forwarded)
2158 ? GNUNET_DHT_RO_LAST_HOP : 0,
2160 ntohl (get->hop_count),
2161 ntohl (get->desired_replication_level),
2166 /* clean up; note that 'bg' is owned by routing now! */
2167 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
2172 * Check validity of p2p result message.
2174 * @param cls closure
2175 * @param message message
2176 * @return #GNUNET_YES if the message is well-formed
2179 check_dht_p2p_result (void *cls,
2180 const struct PeerResultMessage *prm)
2182 uint32_t get_path_length;
2183 uint32_t put_path_length;
2186 msize = ntohs (prm->header.size);
2187 put_path_length = ntohl (prm->put_path_length);
2188 get_path_length = ntohl (prm->get_path_length);
2190 sizeof (struct PeerResultMessage) + (get_path_length +
2192 sizeof (struct GNUNET_PeerIdentity)) ||
2194 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
2196 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
2198 GNUNET_break_op (0);
2199 return GNUNET_SYSERR;
2206 * Core handler for p2p result messages.
2208 * @param cls closure
2209 * @param message message
2212 handle_dht_p2p_result (void *cls,
2213 const struct PeerResultMessage *prm)
2215 struct PeerInfo *peer = cls;
2216 const struct GNUNET_PeerIdentity *put_path;
2217 const struct GNUNET_PeerIdentity *get_path;
2219 uint32_t get_path_length;
2220 uint32_t put_path_length;
2223 enum GNUNET_BLOCK_Type type;
2225 /* parse and validate message */
2226 msize = ntohs (prm->header.size);
2227 put_path_length = ntohl (prm->put_path_length);
2228 get_path_length = ntohl (prm->get_path_length);
2229 put_path = (const struct GNUNET_PeerIdentity *) &prm[1];
2230 get_path = &put_path[put_path_length];
2231 type = ntohl (prm->type);
2232 data = (const void *) &get_path[get_path_length];
2233 data_size = msize - (sizeof (struct PeerResultMessage) +
2235 put_path_length) * sizeof (struct GNUNET_PeerIdentity));
2236 GNUNET_STATISTICS_update (GDS_stats,
2237 gettext_noop ("# P2P RESULTS received"),
2240 GNUNET_STATISTICS_update (GDS_stats,
2241 gettext_noop ("# P2P RESULT bytes received"),
2244 if (GNUNET_YES == log_route_details_stderr)
2248 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
2249 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
2250 "R5N RESULT %s: %s->%s (%u)\n",
2251 GNUNET_h2s (&prm->key),
2252 GNUNET_i2s (peer->id),
2254 get_path_length + 1);
2257 /* if we got a HELLO, consider it for our own routing table */
2258 if (GNUNET_BLOCK_TYPE_DHT_HELLO == type)
2260 const struct GNUNET_MessageHeader *h;
2261 struct GNUNET_PeerIdentity pid;
2263 /* Should be a HELLO, validate and consider using it! */
2264 if (data_size < sizeof (struct GNUNET_HELLO_Message))
2266 GNUNET_break_op (0);
2270 if (data_size != ntohs (h->size))
2272 GNUNET_break_op (0);
2276 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h,
2279 GNUNET_break_op (0);
2282 if ( (GNUNET_YES != disable_try_connect) &&
2283 (0 != memcmp (&my_identity,
2285 sizeof (struct GNUNET_PeerIdentity))) )
2290 /* append 'peer' to 'get_path' */
2292 struct GNUNET_PeerIdentity xget_path[get_path_length + 1];
2295 for (unsigned int i=0;i<=get_path_length;i++)
2297 for (unsigned int j=0;j<i;j++)
2299 GNUNET_break (0 != memcmp (&get_path[i],
2301 sizeof (struct GNUNET_PeerIdentity)));
2303 GNUNET_break (0 != memcmp (&get_path[i],
2305 sizeof (struct GNUNET_PeerIdentity)));
2308 GNUNET_memcpy (xget_path,
2310 get_path_length * sizeof (struct GNUNET_PeerIdentity));
2311 xget_path[get_path_length] = *peer->id;
2314 /* forward to local clients */
2315 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
2324 GDS_CLIENTS_process_get_resp (type,
2327 put_path, put_path_length,
2328 GNUNET_TIME_absolute_ntoh (prm->expiration_time),
2332 if (GNUNET_YES == cache_results)
2334 struct GNUNET_PeerIdentity xput_path[get_path_length + 1 + put_path_length];
2336 GNUNET_memcpy (xput_path,
2338 put_path_length * sizeof (struct GNUNET_PeerIdentity));
2339 GNUNET_memcpy (&xput_path[put_path_length],
2341 get_path_length * sizeof (struct GNUNET_PeerIdentity));
2343 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
2345 get_path_length + put_path_length,
2351 /* forward to other peers */
2352 GDS_ROUTING_process (NULL,
2354 GNUNET_TIME_absolute_ntoh (prm->expiration_time),
2367 * Initialize neighbours subsystem.
2369 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2372 GDS_NEIGHBOURS_init ()
2374 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2375 GNUNET_MQ_hd_var_size (dht_p2p_get,
2376 GNUNET_MESSAGE_TYPE_DHT_P2P_GET,
2377 struct PeerGetMessage,
2379 GNUNET_MQ_hd_var_size (dht_p2p_put,
2380 GNUNET_MESSAGE_TYPE_DHT_P2P_PUT,
2381 struct PeerPutMessage,
2383 GNUNET_MQ_hd_var_size (dht_p2p_result,
2384 GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT,
2385 struct PeerResultMessage,
2387 GNUNET_MQ_handler_end ()
2389 unsigned long long temp_config_num;
2392 = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg,
2394 "DISABLE_TRY_CONNECT");
2396 GNUNET_CONFIGURATION_get_value_number (GDS_cfg,
2400 bucket_size = (unsigned int) temp_config_num;
2402 = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg,
2406 log_route_details_stderr =
2407 (NULL != getenv("GNUNET_DHT_ROUTE_DEBUG")) ? GNUNET_YES : GNUNET_NO;
2408 ats_ch = GNUNET_ATS_connectivity_init (GDS_cfg);
2409 core_api = GNUNET_CORE_connect (GDS_cfg,
2412 &handle_core_connect,
2413 &handle_core_disconnect,
2415 if (NULL == core_api)
2416 return GNUNET_SYSERR;
2417 all_connected_peers = GNUNET_CONTAINER_multipeermap_create (256,
2419 all_desired_peers = GNUNET_CONTAINER_multipeermap_create (256,
2426 * Shutdown neighbours subsystem.
2429 GDS_NEIGHBOURS_done ()
2431 if (NULL == core_api)
2433 GNUNET_CORE_disconnect (core_api);
2436 GNUNET_CONTAINER_multipeermap_size (all_connected_peers));
2437 GNUNET_CONTAINER_multipeermap_destroy (all_connected_peers);
2438 all_connected_peers = NULL;
2439 GNUNET_CONTAINER_multipeermap_iterate (all_desired_peers,
2442 GNUNET_CONTAINER_multipeermap_destroy (all_desired_peers);
2443 all_desired_peers = NULL;
2444 GNUNET_ATS_connectivity_done (ats_ch);
2446 GNUNET_assert (NULL == find_peer_task);
2451 * Get the ID of the local node.
2453 * @return identity of the local node
2455 struct GNUNET_PeerIdentity *
2456 GDS_NEIGHBOURS_get_id ()
2458 return &my_identity;
2462 /* end of gnunet-service-dht_neighbours.c */