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_block_group_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_nse_service.h"
35 #include "gnunet_ats_service.h"
36 #include "gnunet_core_service.h"
37 #include "gnunet_datacache_lib.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_hello_lib.h"
40 #include "gnunet_dht_service.h"
41 #include "gnunet_statistics_service.h"
42 #include "gnunet-service-dht.h"
43 #include "gnunet-service-dht_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"
50 #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
53 * Enable slow sanity checks to debug issues.
55 #define SANITY_CHECKS 1
58 * How many buckets will we allow total.
60 #define MAX_BUCKETS sizeof (struct GNUNET_HashCode) * 8
63 * What is the maximum number of peers in a given bucket.
65 #define DEFAULT_BUCKET_SIZE 8
68 * Desired replication level for FIND PEER requests
70 #define FIND_PEER_REPLICATION_LEVEL 4
73 * Maximum allowed replication level for all requests.
75 #define MAXIMUM_REPLICATION_LEVEL 16
78 * Maximum allowed number of pending messages per peer.
80 #define MAXIMUM_PENDING_PER_PEER 64
83 * How long at least to wait before sending another find peer request.
85 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
88 * How long at most to wait before sending another find peer request.
90 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10)
93 * How long at most to wait for transmission of a GET request to another peer?
95 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
98 * Hello address expiration
100 extern struct GNUNET_TIME_Relative hello_expiration;
103 GNUNET_NETWORK_STRUCT_BEGIN
108 struct PeerPutMessage
111 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
113 struct GNUNET_MessageHeader header;
118 uint32_t options GNUNET_PACKED;
123 uint32_t type GNUNET_PACKED;
128 uint32_t hop_count GNUNET_PACKED;
131 * Replication level for this message
133 uint32_t desired_replication_level GNUNET_PACKED;
136 * Length of the PUT path that follows (if tracked).
138 uint32_t put_path_length GNUNET_PACKED;
141 * When does the content expire?
143 struct GNUNET_TIME_AbsoluteNBO expiration_time;
146 * Bloomfilter (for peer identities) to stop circular routes
148 char bloomfilter[DHT_BLOOM_SIZE];
151 * The key we are storing under.
153 struct GNUNET_HashCode key;
155 /* put path (if tracked) */
165 struct PeerResultMessage
168 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
170 struct GNUNET_MessageHeader header;
175 uint32_t type GNUNET_PACKED;
178 * Length of the PUT path that follows (if tracked).
180 uint32_t put_path_length GNUNET_PACKED;
183 * Length of the GET path that follows (if tracked).
185 uint32_t get_path_length GNUNET_PACKED;
188 * When does the content expire?
190 struct GNUNET_TIME_AbsoluteNBO expiration_time;
193 * The key of the corresponding GET request.
195 struct GNUNET_HashCode key;
197 /* put path (if tracked) */
199 /* get path (if tracked) */
209 struct PeerGetMessage
212 * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET
214 struct GNUNET_MessageHeader header;
219 uint32_t options GNUNET_PACKED;
222 * Desired content type.
224 uint32_t type GNUNET_PACKED;
229 uint32_t hop_count GNUNET_PACKED;
232 * Desired replication level for this request.
234 uint32_t desired_replication_level GNUNET_PACKED;
237 * Size of the extended query.
239 uint32_t xquery_size;
242 * Bloomfilter mutator.
247 * Bloomfilter (for peer identities) to stop circular routes
249 char bloomfilter[DHT_BLOOM_SIZE];
252 * The key we are looking for.
254 struct GNUNET_HashCode key;
258 /* result bloomfilter */
261 GNUNET_NETWORK_STRUCT_END
265 * Entry for a peer in a bucket.
270 * Next peer entry (DLL)
272 struct PeerInfo *next;
275 * Prev peer entry (DLL)
277 struct PeerInfo *prev;
280 * Handle for sending messages to this peer.
282 struct GNUNET_MQ_Handle *mq;
285 * What is the identity of the peer?
287 const struct GNUNET_PeerIdentity *id;
292 struct GNUNET_HashCode phash;
295 * Which bucket is this peer in?
303 * Peers are grouped into buckets.
310 struct PeerInfo *head;
315 struct PeerInfo *tail;
318 * Number of peers in the bucket.
320 unsigned int peers_size;
325 * Information about a peer that we would like to connect to.
331 * Handle to active HELLO offer operation, or NULL.
333 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
336 * Handle to active connectivity suggestion operation, or NULL.
338 struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
341 * How much would we like to connect to this peer?
348 * Do we cache all results that we are routing in the local datacache?
350 static int cache_results;
353 * Should routing details be logged to stderr (for debugging)?
355 static int log_route_details_stderr;
358 * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
360 static unsigned int closest_bucket;
363 * How many peers have we added since we sent out our last
366 static unsigned int newly_found_peers;
369 * Option for testing that disables the 'connect' function of the DHT.
371 static int disable_try_connect;
374 * The buckets. Array of size #MAX_BUCKETS. Offset 0 means 0 bits matching.
376 static struct PeerBucket k_buckets[MAX_BUCKETS];
379 * Hash map of all CORE-connected peers, for easy removal from
380 * #k_buckets on disconnect. Values are of type `struct PeerInfo`.
382 static struct GNUNET_CONTAINER_MultiPeerMap *all_connected_peers;
385 * Hash map of all peers we would like to be connected to.
386 * Values are of type `struct ConnectInfo`.
388 static struct GNUNET_CONTAINER_MultiPeerMap *all_desired_peers;
391 * Maximum size for each bucket.
393 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
396 * Task that sends FIND PEER requests.
398 static struct GNUNET_SCHEDULER_Task *find_peer_task;
401 * Identity of this peer.
403 static struct GNUNET_PeerIdentity my_identity;
406 * Hash of the identity of this peer.
408 static struct GNUNET_HashCode my_identity_hash;
413 static struct GNUNET_CORE_Handle *core_api;
416 * Handle to ATS connectivity.
418 static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
422 * Find the optimal bucket for this key.
424 * @param hc the hashcode to compare our identity to
425 * @return the proper bucket index, or GNUNET_SYSERR
426 * on error (same hashcode)
429 find_bucket (const struct GNUNET_HashCode *hc)
433 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, hc);
434 if (bits == MAX_BUCKETS)
436 /* How can all bits match? Got my own ID? */
438 return GNUNET_SYSERR;
440 return MAX_BUCKETS - bits - 1;
445 * Function called when #GNUNET_TRANSPORT_offer_hello() is done.
446 * Clean up the "oh" field in the @a cls
448 * @param cls a `struct ConnectInfo`
451 offer_hello_done (void *cls)
453 struct ConnectInfo *ci = cls;
460 * Function called for all entries in #all_desired_peers to clean up.
463 * @param peer peer the entry is for
464 * @param value the value to remove
465 * @return #GNUNET_YES
468 free_connect_info (void *cls,
469 const struct GNUNET_PeerIdentity *peer,
472 struct ConnectInfo *ci = value;
474 GNUNET_assert (GNUNET_YES ==
475 GNUNET_CONTAINER_multipeermap_remove (all_desired_peers,
480 GNUNET_ATS_connectivity_suggest_cancel (ci->sh);
485 GNUNET_TRANSPORT_offer_hello_cancel (ci->oh);
494 * Consider if we want to connect to a given peer, and if so
495 * let ATS know. If applicable, the HELLO is offered to the
498 * @param pid peer to consider connectivity requirements for
499 * @param h a HELLO message, or NULL
502 try_connect (const struct GNUNET_PeerIdentity *pid,
503 const struct GNUNET_MessageHeader *h)
506 struct GNUNET_HashCode pid_hash;
507 struct ConnectInfo *ci;
510 GNUNET_CRYPTO_hash (pid,
511 sizeof (struct GNUNET_PeerIdentity),
513 bucket = find_bucket (&pid_hash);
516 ci = GNUNET_CONTAINER_multipeermap_get (all_desired_peers,
519 if (k_buckets[bucket].peers_size < bucket_size)
520 strength = (bucket_size - k_buckets[bucket].peers_size) * bucket;
522 strength = bucket; /* minimum value of connectivity */
524 GNUNET_CONTAINER_multipeermap_contains (all_connected_peers,
526 strength *= 2; /* double for connected peers */
527 else if (k_buckets[bucket].peers_size > bucket_size)
528 strength = 0; /* bucket full, we really do not care about more */
530 if ( (0 == strength) &&
533 /* release request */
534 GNUNET_assert (GNUNET_YES ==
535 free_connect_info (NULL,
542 ci = GNUNET_new (struct ConnectInfo);
543 GNUNET_assert (GNUNET_OK ==
544 GNUNET_CONTAINER_multipeermap_put (all_desired_peers,
547 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
549 if ( (NULL != ci->oh) &&
551 GNUNET_TRANSPORT_offer_hello_cancel (ci->oh);
553 ci->oh = GNUNET_TRANSPORT_offer_hello (GDS_cfg,
557 if ( (NULL != ci->sh) &&
558 (ci->strength != strength) )
559 GNUNET_ATS_connectivity_suggest_cancel (ci->sh);
560 if (ci->strength != strength)
561 ci->sh = GNUNET_ATS_connectivity_suggest (ats_ch,
564 ci->strength = strength;
569 * Function called for each peer in #all_desired_peers during
570 * #update_connect_preferences() if we have reason to adjust
571 * the strength of our desire to keep connections to certain
572 * peers. Calls #try_connect() to update the calculations for
576 * @param pid peer to update
577 * @param value unused
578 * @return #GNUNET_YES (continue to iterate)
581 update_desire_strength (void *cls,
582 const struct GNUNET_PeerIdentity *pid,
585 try_connect (pid, NULL);
591 * Update our preferences for connectivity as given to ATS.
593 * @param cls the `struct PeerInfo` of the peer
594 * @param tc scheduler context.
597 update_connect_preferences ()
599 GNUNET_CONTAINER_multipeermap_iterate (all_desired_peers,
600 &update_desire_strength,
606 * Add each of the peers we already know to the bloom filter of
607 * the request so that we don't get duplicate HELLOs.
609 * @param cls the `struct GNUNET_BLOCK_Group`
610 * @param key peer identity to add to the bloom filter
611 * @param value value the peer information (unused)
612 * @return #GNUNET_YES (we should continue to iterate)
615 add_known_to_bloom (void *cls,
616 const struct GNUNET_PeerIdentity *key,
619 struct GNUNET_BLOCK_Group *bg = cls;
620 struct GNUNET_HashCode key_hash;
622 GNUNET_CRYPTO_hash (key,
623 sizeof (struct GNUNET_PeerIdentity),
625 GNUNET_BLOCK_GROUP_bf_test_and_set (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_bf_create (NULL,
661 GNUNET_CONSTANTS_BLOOMFILTER_K,
662 GNUNET_BLOCK_TYPE_DHT_HELLO,
663 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
667 GNUNET_CONTAINER_multipeermap_iterate (all_connected_peers,
670 GNUNET_STATISTICS_update (GDS_stats,
671 gettext_noop ("# FIND PEER messages initiated"),
675 = GNUNET_CONTAINER_bloomfilter_init (NULL,
677 GNUNET_CONSTANTS_BLOOMFILTER_K);
678 // FIXME: pass priority!?
679 GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
680 GNUNET_DHT_RO_FIND_PEER,
681 FIND_PEER_REPLICATION_LEVEL,
688 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
689 GNUNET_BLOCK_group_destroy (bg);
690 /* schedule next round */
691 next_send_time.rel_value_us =
692 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value_us +
693 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
694 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value_us /
695 (newly_found_peers + 1));
696 newly_found_peers = 0;
697 GNUNET_assert (NULL == find_peer_task);
699 GNUNET_SCHEDULER_add_delayed (next_send_time,
700 &send_find_peer_message,
706 * Method called whenever a peer connects.
709 * @param peer peer identity this notification is about
710 * @param mq message queue for sending messages to @a peer
711 * @return our `struct PeerInfo` for @a peer
714 handle_core_connect (void *cls,
715 const struct GNUNET_PeerIdentity *peer,
716 struct GNUNET_MQ_Handle *mq)
720 /* Check for connect to self message */
721 if (0 == memcmp (&my_identity,
723 sizeof (struct GNUNET_PeerIdentity)))
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 GNUNET_assert (GNUNET_NO ==
729 GNUNET_CONTAINER_multipeermap_get (all_connected_peers,
731 GNUNET_STATISTICS_update (GDS_stats,
732 gettext_noop ("# peers connected"),
735 pi = GNUNET_new (struct PeerInfo);
738 GNUNET_CRYPTO_hash (peer,
739 sizeof (struct GNUNET_PeerIdentity),
741 pi->peer_bucket = find_bucket (&pi->phash);
742 GNUNET_assert ( (pi->peer_bucket >= 0) &&
743 (pi->peer_bucket < MAX_BUCKETS) );
744 GNUNET_CONTAINER_DLL_insert_tail (k_buckets[pi->peer_bucket].head,
745 k_buckets[pi->peer_bucket].tail,
747 k_buckets[pi->peer_bucket].peers_size++;
748 closest_bucket = GNUNET_MAX (closest_bucket,
750 GNUNET_assert (GNUNET_OK ==
751 GNUNET_CONTAINER_multipeermap_put (all_connected_peers,
754 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
755 if ( (pi->peer_bucket > 0) &&
756 (k_buckets[pi->peer_bucket].peers_size <= bucket_size))
758 update_connect_preferences ();
761 if ( (1 == GNUNET_CONTAINER_multipeermap_size (all_connected_peers)) &&
762 (GNUNET_YES != disable_try_connect))
764 /* got a first connection, good time to start with FIND PEER requests... */
765 GNUNET_assert (NULL == find_peer_task);
766 find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message,
774 * Method called whenever a peer disconnects.
777 * @param peer peer identity this notification is about
778 * @param internal_cls our `struct PeerInfo` for @a peer
781 handle_core_disconnect (void *cls,
782 const struct GNUNET_PeerIdentity *peer,
785 struct PeerInfo *to_remove = internal_cls;
787 /* Check for disconnect from self message */
788 if (NULL == to_remove)
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 GNUNET_STATISTICS_update (GDS_stats,
794 gettext_noop ("# peers connected"),
797 GNUNET_assert (GNUNET_YES ==
798 GNUNET_CONTAINER_multipeermap_remove (all_connected_peers,
801 if ( (0 == GNUNET_CONTAINER_multipeermap_size (all_connected_peers)) &&
802 (GNUNET_YES != disable_try_connect) )
804 GNUNET_SCHEDULER_cancel (find_peer_task);
805 find_peer_task = NULL;
807 GNUNET_assert (to_remove->peer_bucket >= 0);
808 GNUNET_CONTAINER_DLL_remove (k_buckets[to_remove->peer_bucket].head,
809 k_buckets[to_remove->peer_bucket].tail,
811 GNUNET_assert (k_buckets[to_remove->peer_bucket].peers_size > 0);
812 k_buckets[to_remove->peer_bucket].peers_size--;
813 while ( (closest_bucket > 0) &&
814 (0 == k_buckets[to_remove->peer_bucket].peers_size) )
816 if (k_buckets[to_remove->peer_bucket].peers_size < bucket_size)
817 update_connect_preferences ();
818 GNUNET_free (to_remove);
823 * To how many peers should we (on average) forward the request to
824 * obtain the desired target_replication count (on average).
826 * @param hop_count number of hops the message has traversed
827 * @param target_replication the number of total paths desired
828 * @return Some number of peers to forward the message to
831 get_forward_count (uint32_t hop_count,
832 uint32_t target_replication)
834 uint32_t random_value;
835 uint32_t forward_count;
838 if (hop_count > GDS_NSE_get () * 4.0)
840 /* forcefully terminate */
841 GNUNET_STATISTICS_update (GDS_stats,
842 gettext_noop ("# requests TTL-dropped"),
846 if (hop_count > GDS_NSE_get () * 2.0)
848 /* Once we have reached our ideal number of hops, only forward to 1 peer */
851 /* bound by system-wide maximum */
853 GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL, target_replication);
855 1 + (target_replication - 1.0) / (GDS_NSE_get () +
856 ((float) (target_replication - 1.0) *
858 /* Set forward count to floor of target_value */
859 forward_count = (uint32_t) target_value;
860 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
861 target_value = target_value - forward_count;
863 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
864 if (random_value < (target_value * UINT32_MAX))
866 return forward_count;
871 * Compute the distance between have and target as a 32-bit value.
872 * Differences in the lower bits must count stronger than differences
873 * in the higher bits.
877 * @return 0 if have==target, otherwise a number
878 * that is larger as the distance between
879 * the two hash codes increases
882 get_distance (const struct GNUNET_HashCode *target,
883 const struct GNUNET_HashCode *have)
890 /* We have to represent the distance between two 2^9 (=512)-bit
891 * numbers as a 2^5 (=32)-bit number with "0" being used for the
892 * two numbers being identical; furthermore, we need to
893 * guarantee that a difference in the number of matching
894 * bits is always represented in the result.
896 * We use 2^32/2^9 numerical values to distinguish between
897 * hash codes that have the same LSB bit distance and
898 * use the highest 2^9 bits of the result to signify the
899 * number of (mis)matching LSB bits; if we have 0 matching
900 * and hence 512 mismatching LSB bits we return -1 (since
901 * 512 itself cannot be represented with 9 bits) */
903 /* first, calculate the most significant 9 bits of our
904 * result, aka the number of LSBs */
905 bucket = GNUNET_CRYPTO_hash_matching_bits (target,
907 /* bucket is now a value between 0 and 512 */
909 return 0; /* perfect match */
911 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
912 * below, we'd end up with max+1 (overflow)) */
914 /* calculate the most significant bits of the final result */
915 msb = (512 - bucket) << (32 - 9);
916 /* calculate the 32-9 least significant bits of the final result by
917 * looking at the differences in the 32-9 bits following the
918 * mismatching bit at 'bucket' */
921 (i < sizeof (struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
923 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
924 GNUNET_CRYPTO_hash_get_bit (have, i))
925 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
926 * last bit set will be 31 -- if
927 * i does not reach 512 first... */
934 * Check whether my identity is closer than any known peers. If a
935 * non-null bloomfilter is given, check if this is the closest peer
936 * that hasn't already been routed to.
938 * @param key hash code to check closeness to
939 * @param bloom bloomfilter, exclude these entries from the decision
940 * @return #GNUNET_YES if node location is closest,
941 * #GNUNET_NO otherwise.
944 am_closest_peer (const struct GNUNET_HashCode *key,
945 const struct GNUNET_CONTAINER_BloomFilter *bloom)
951 struct PeerInfo *pos;
953 if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode)))
955 bucket_num = find_bucket (key);
956 GNUNET_assert (bucket_num >= 0);
957 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
959 pos = k_buckets[bucket_num].head;
961 while ((NULL != pos) && (count < bucket_size))
963 if ((NULL != bloom) &&
965 GNUNET_CONTAINER_bloomfilter_test (bloom,
969 continue; /* Skip already checked entries */
971 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->phash,
973 if (other_bits > bits)
975 if (other_bits == bits) /* We match the same number of bits */
979 /* No peers closer, we are the closest! */
985 * Select a peer from the routing table that would be a good routing
986 * destination for sending a message for "key". The resulting peer
987 * must not be in the set of blocked peers.<p>
989 * Note that we should not ALWAYS select the closest peer to the
990 * target, peers further away from the target should be chosen with
991 * exponentially declining probability.
993 * FIXME: double-check that this is fine
996 * @param key the key we are selecting a peer to route to
997 * @param bloom a bloomfilter containing entries this request has seen already
998 * @param hops how many hops has this message traversed thus far
999 * @return Peer to route to, or NULL on error
1001 static struct PeerInfo *
1002 select_peer (const struct GNUNET_HashCode *key,
1003 const struct GNUNET_CONTAINER_BloomFilter *bloom,
1008 unsigned int selected;
1009 struct PeerInfo *pos;
1011 unsigned int smallest_distance;
1012 struct PeerInfo *chosen;
1014 if (hops >= GDS_NSE_get ())
1016 /* greedy selection (closest peer that is not in bloomfilter) */
1017 smallest_distance = UINT_MAX;
1019 for (bc = 0; bc <= closest_bucket; bc++)
1021 pos = k_buckets[bc].head;
1023 while ((pos != NULL) && (count < bucket_size))
1025 if ((bloom == NULL) ||
1027 GNUNET_CONTAINER_bloomfilter_test (bloom,
1030 dist = get_distance (key,
1032 if (dist < smallest_distance)
1035 smallest_distance = dist;
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Excluded peer `%s' due to BF match in greedy routing for %s\n",
1042 GNUNET_i2s (pos->id),
1044 GNUNET_STATISTICS_update (GDS_stats,
1045 gettext_noop ("# Peers excluded from routing due to Bloomfilter"),
1048 dist = get_distance (key,
1050 if (dist < smallest_distance)
1053 smallest_distance = dist;
1061 GNUNET_STATISTICS_update (GDS_stats,
1062 gettext_noop ("# Peer selection failed"), 1,
1067 /* select "random" peer */
1068 /* count number of peers that are available and not filtered */
1070 for (bc = 0; bc <= closest_bucket; bc++)
1072 pos = k_buckets[bc].head;
1073 while ((pos != NULL) && (count < bucket_size))
1075 if ((bloom != NULL) &&
1077 GNUNET_CONTAINER_bloomfilter_test (bloom,
1080 GNUNET_STATISTICS_update (GDS_stats,
1082 ("# Peers excluded from routing due to Bloomfilter"),
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "Excluded peer `%s' due to BF match in random routing for %s\n",
1086 GNUNET_i2s (pos->id),
1089 continue; /* Ignore bloomfiltered peers */
1095 if (0 == count) /* No peers to select from! */
1097 GNUNET_STATISTICS_update (GDS_stats,
1098 gettext_noop ("# Peer selection failed"), 1,
1102 /* Now actually choose a peer */
1103 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1106 for (bc = 0; bc <= closest_bucket; bc++)
1108 for (pos = k_buckets[bc].head; ((pos != NULL) && (count < bucket_size)); pos = pos->next)
1110 if ((bloom != NULL) &&
1112 GNUNET_CONTAINER_bloomfilter_test (bloom,
1115 continue; /* Ignore bloomfiltered peers */
1117 if (0 == selected--)
1127 * Compute the set of peers that the given request should be
1130 * @param key routing key
1131 * @param bloom bloom filter excluding peers as targets, all selected
1132 * peers will be added to the bloom filter
1133 * @param hop_count number of hops the request has traversed so far
1134 * @param target_replication desired number of replicas
1135 * @param targets where to store an array of target peers (to be
1136 * free'd by the caller)
1137 * @return number of peers returned in 'targets'.
1140 get_target_peers (const struct GNUNET_HashCode *key,
1141 struct GNUNET_CONTAINER_BloomFilter *bloom,
1143 uint32_t target_replication,
1144 struct PeerInfo ***targets)
1148 struct PeerInfo **rtargets;
1149 struct PeerInfo *nxt;
1151 GNUNET_assert (NULL != bloom);
1152 ret = get_forward_count (hop_count,
1153 target_replication);
1159 rtargets = GNUNET_new_array (ret,
1161 for (off = 0; off < ret; off++)
1163 nxt = select_peer (key, bloom, hop_count);
1166 rtargets[off] = nxt;
1167 GNUNET_break (GNUNET_NO ==
1168 GNUNET_CONTAINER_bloomfilter_test (bloom,
1170 GNUNET_CONTAINER_bloomfilter_add (bloom,
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Selected %u/%u peers at hop %u for %s (target was %u)\n",
1176 GNUNET_CONTAINER_multipeermap_size (all_connected_peers),
1177 (unsigned int) hop_count,
1182 GNUNET_free (rtargets);
1186 *targets = rtargets;
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "Forwarding query `%s' to %u peers (goal was %u peers)\n",
1197 * Perform a PUT operation. Forwards the given request to other
1198 * peers. Does not store the data locally. Does not give the
1199 * data to local clients. May do nothing if this is the only
1200 * peer in the network (or if we are the closest peer in the
1203 * @param type type of the block
1204 * @param options routing options
1205 * @param desired_replication_level desired replication count
1206 * @param expiration_time when does the content expire
1207 * @param hop_count how many hops has this message traversed so far
1208 * @param bf Bloom filter of peers this PUT has already traversed
1209 * @param key key for the content
1210 * @param put_path_length number of entries in @a put_path
1211 * @param put_path peers this request has traversed so far (if tracked)
1212 * @param data payload to store
1213 * @param data_size number of bytes in @a data
1214 * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not
1217 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1218 enum GNUNET_DHT_RouteOption options,
1219 uint32_t desired_replication_level,
1220 struct GNUNET_TIME_Absolute expiration_time,
1222 struct GNUNET_CONTAINER_BloomFilter *bf,
1223 const struct GNUNET_HashCode *key,
1224 unsigned int put_path_length,
1225 struct GNUNET_PeerIdentity *put_path,
1229 unsigned int target_count;
1231 struct PeerInfo **targets;
1232 struct PeerInfo *target;
1234 struct GNUNET_MQ_Envelope *env;
1235 struct PeerPutMessage *ppm;
1236 struct GNUNET_PeerIdentity *pp;
1237 unsigned int skip_count;
1239 GNUNET_assert (NULL != bf);
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 "Adding myself (%s) to PUT bloomfilter for %s\n",
1242 GNUNET_i2s (&my_identity),
1244 GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity_hash);
1245 GNUNET_STATISTICS_update (GDS_stats,
1246 gettext_noop ("# PUT requests routed"),
1250 = get_target_peers (key,
1253 desired_replication_level,
1255 if (0 == target_count)
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "Routing PUT for %s terminates after %u hops at %s\n",
1260 (unsigned int) hop_count,
1261 GNUNET_i2s (&my_identity));
1264 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size;
1265 if (msize + sizeof (struct PeerPutMessage)
1266 >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1268 put_path_length = 0;
1271 if (msize + sizeof (struct PeerPutMessage)
1272 >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1275 GNUNET_free (targets);
1278 GNUNET_STATISTICS_update (GDS_stats,
1279 gettext_noop ("# PUT messages queued for transmission"),
1283 for (i = 0; i < target_count; i++)
1285 target = targets[i];
1286 if (GNUNET_MQ_get_length (target->mq) >= MAXIMUM_PENDING_PER_PEER)
1289 GNUNET_STATISTICS_update (GDS_stats,
1290 gettext_noop ("# P2P messages dropped due to full queue"),
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "Routing PUT for %s after %u hops to %s\n",
1299 (unsigned int) hop_count,
1300 GNUNET_i2s (target->id));
1301 env = GNUNET_MQ_msg_extra (ppm,
1303 GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1304 ppm->options = htonl (options);
1305 ppm->type = htonl (type);
1306 ppm->hop_count = htonl (hop_count + 1);
1307 ppm->desired_replication_level = htonl (desired_replication_level);
1308 ppm->put_path_length = htonl (put_path_length);
1309 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1310 GNUNET_break (GNUNET_YES ==
1311 GNUNET_CONTAINER_bloomfilter_test (bf,
1313 GNUNET_assert (GNUNET_OK ==
1314 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
1318 pp = (struct GNUNET_PeerIdentity *) &ppm[1];
1321 sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1322 GNUNET_memcpy (&pp[put_path_length],
1325 GNUNET_MQ_send (target->mq,
1328 GNUNET_free (targets);
1329 return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO;
1334 * Perform a GET operation. Forwards the given request to other
1335 * peers. Does not lookup the key locally. May do nothing if this is
1336 * the only peer in the network (or if we are the closest peer in the
1339 * @param type type of the block
1340 * @param options routing options
1341 * @param desired_replication_level desired replication count
1342 * @param hop_count how many hops did this request traverse so far?
1343 * @param key key for the content
1344 * @param xquery extended query
1345 * @param xquery_size number of bytes in @a xquery
1346 * @param bg group to use for filtering replies
1347 * @param peer_bf filter for peers not to select (again)
1348 * @return #GNUNET_OK if the request was forwarded, #GNUNET_NO if not
1351 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1352 enum GNUNET_DHT_RouteOption options,
1353 uint32_t desired_replication_level,
1355 const struct GNUNET_HashCode *key,
1358 struct GNUNET_BLOCK_Group *bg,
1359 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1361 unsigned int target_count;
1362 struct PeerInfo **targets;
1363 struct PeerInfo *target;
1364 struct GNUNET_MQ_Envelope *env;
1366 struct PeerGetMessage *pgm;
1368 size_t reply_bf_size;
1370 unsigned int skip_count;
1373 GNUNET_assert (NULL != peer_bf);
1374 GNUNET_STATISTICS_update (GDS_stats,
1375 gettext_noop ("# GET requests routed"),
1378 target_count = get_target_peers (key,
1381 desired_replication_level,
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "Adding myself (%s) to GET bloomfilter for %s\n",
1385 GNUNET_i2s (&my_identity),
1387 GNUNET_CONTAINER_bloomfilter_add (peer_bf,
1389 if (0 == target_count)
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Routing GET for %s terminates after %u hops at %s\n",
1394 (unsigned int) hop_count,
1395 GNUNET_i2s (&my_identity));
1399 GNUNET_BLOCK_group_serialize (bg,
1406 bf_nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1409 msize = xquery_size + reply_bf_size;
1410 if (msize + sizeof (struct PeerGetMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1413 GNUNET_free_non_null (reply_bf);
1414 GNUNET_free (targets);
1417 GNUNET_STATISTICS_update (GDS_stats,
1418 gettext_noop ("# GET messages queued for transmission"),
1421 /* forward request */
1423 for (unsigned int i = 0; i < target_count; i++)
1425 target = targets[i];
1426 if (GNUNET_MQ_get_length (target->mq) >= MAXIMUM_PENDING_PER_PEER)
1429 GNUNET_STATISTICS_update (GDS_stats,
1430 gettext_noop ("# P2P messages dropped due to full queue"),
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436 "Routing GET for %s after %u hops to %s\n",
1438 (unsigned int) hop_count,
1439 GNUNET_i2s (target->id));
1440 env = GNUNET_MQ_msg_extra (pgm,
1442 GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1443 pgm->options = htonl (options);
1444 pgm->type = htonl (type);
1445 pgm->hop_count = htonl (hop_count + 1);
1446 pgm->desired_replication_level = htonl (desired_replication_level);
1447 pgm->xquery_size = htonl (xquery_size);
1448 pgm->bf_mutator = bf_nonce;
1449 GNUNET_break (GNUNET_YES ==
1450 GNUNET_CONTAINER_bloomfilter_test (peer_bf,
1452 GNUNET_assert (GNUNET_OK ==
1453 GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
1457 xq = (char *) &pgm[1];
1461 GNUNET_memcpy (&xq[xquery_size],
1464 GNUNET_MQ_send (target->mq,
1467 GNUNET_free (targets);
1468 GNUNET_free_non_null (reply_bf);
1469 return (skip_count < target_count) ? GNUNET_OK : GNUNET_NO;
1474 * Handle a reply (route to origin). Only forwards the reply back to
1475 * the given peer. Does not do local caching or forwarding to local
1478 * @param target neighbour that should receive the block (if still connected)
1479 * @param type type of the block
1480 * @param expiration_time when does the content expire
1481 * @param key key for the content
1482 * @param put_path_length number of entries in @a put_path
1483 * @param put_path peers the original PUT traversed (if tracked)
1484 * @param get_path_length number of entries in @a get_path
1485 * @param get_path peers this reply has traversed so far (if tracked)
1486 * @param data payload of the reply
1487 * @param data_size number of bytes in @a data
1490 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1491 enum GNUNET_BLOCK_Type type,
1492 struct GNUNET_TIME_Absolute expiration_time,
1493 const struct GNUNET_HashCode *key,
1494 unsigned int put_path_length,
1495 const struct GNUNET_PeerIdentity *put_path,
1496 unsigned int get_path_length,
1497 const struct GNUNET_PeerIdentity *get_path,
1501 struct PeerInfo *pi;
1502 struct GNUNET_MQ_Envelope *env;
1504 struct PeerResultMessage *prm;
1505 struct GNUNET_PeerIdentity *paths;
1507 msize = data_size + (get_path_length + put_path_length) *
1508 sizeof (struct GNUNET_PeerIdentity);
1509 if ((msize + sizeof (struct PeerResultMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1511 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1513 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1514 (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE))
1519 pi = GNUNET_CONTAINER_multipeermap_get (all_connected_peers,
1523 /* peer disconnected in the meantime, drop reply */
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "No matching peer for reply for key %s\n",
1529 if (GNUNET_MQ_get_length (pi->mq) >= MAXIMUM_PENDING_PER_PEER)
1532 GNUNET_STATISTICS_update (GDS_stats,
1533 gettext_noop ("# P2P messages dropped due to full queue"),
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 "Peer queue full, ignoring reply for key %s\n",
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Forwarding reply for key %s to peer %s\n",
1545 GNUNET_i2s (target));
1546 GNUNET_STATISTICS_update (GDS_stats,
1548 ("# RESULT messages queued for transmission"), 1,
1550 env = GNUNET_MQ_msg_extra (prm,
1552 GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1553 prm->type = htonl (type);
1554 prm->put_path_length = htonl (put_path_length);
1555 prm->get_path_length = htonl (get_path_length);
1556 prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1558 paths = (struct GNUNET_PeerIdentity *) &prm[1];
1559 GNUNET_memcpy (paths,
1561 put_path_length * sizeof (struct GNUNET_PeerIdentity));
1562 GNUNET_memcpy (&paths[put_path_length],
1564 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1565 GNUNET_memcpy (&paths[put_path_length + get_path_length],
1568 GNUNET_MQ_send (pi->mq,
1574 * To be called on core init/fail.
1576 * @param cls service closure
1577 * @param identity the public identity of this peer
1580 core_init (void *cls,
1581 const struct GNUNET_PeerIdentity *identity)
1583 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1584 "CORE called, I am %s\n",
1585 GNUNET_i2s (identity));
1586 my_identity = *identity;
1587 GNUNET_CRYPTO_hash (identity,
1588 sizeof (struct GNUNET_PeerIdentity),
1590 GNUNET_SERVICE_resume (GDS_service);
1595 * Check validity of a p2p put request.
1597 * @param cls closure with the `struct PeerInfo` of the sender
1598 * @param message message
1599 * @return #GNUNET_OK if the message is valid
1602 check_dht_p2p_put (void *cls,
1603 const struct PeerPutMessage *put)
1608 msize = ntohs (put->header.size);
1609 putlen = ntohl (put->put_path_length);
1611 sizeof (struct PeerPutMessage) +
1612 putlen * sizeof (struct GNUNET_PeerIdentity)) ||
1614 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
1616 GNUNET_break_op (0);
1617 return GNUNET_SYSERR;
1624 * Core handler for p2p put requests.
1626 * @param cls closure with the `struct PeerInfo` of the sender
1627 * @param message message
1630 handle_dht_p2p_put (void *cls,
1631 const struct PeerPutMessage *put)
1633 struct PeerInfo *peer = cls;
1634 const struct GNUNET_PeerIdentity *put_path;
1635 const void *payload;
1638 size_t payload_size;
1639 enum GNUNET_DHT_RouteOption options;
1640 struct GNUNET_CONTAINER_BloomFilter *bf;
1641 struct GNUNET_HashCode test_key;
1644 msize = ntohs (put->header.size);
1645 putlen = ntohl (put->put_path_length);
1646 GNUNET_STATISTICS_update (GDS_stats,
1647 gettext_noop ("# P2P PUT requests received"),
1650 GNUNET_STATISTICS_update (GDS_stats,
1651 gettext_noop ("# P2P PUT bytes received"),
1654 put_path = (const struct GNUNET_PeerIdentity *) &put[1];
1655 payload = &put_path[putlen];
1656 options = ntohl (put->options);
1657 payload_size = msize - (sizeof (struct PeerPutMessage) +
1658 putlen * sizeof (struct GNUNET_PeerIdentity));
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "PUT for `%s' from %s\n",
1662 GNUNET_h2s (&put->key),
1663 GNUNET_i2s (peer->id));
1664 if (GNUNET_YES == log_route_details_stderr)
1668 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
1669 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
1670 "R5N PUT %s: %s->%s (%u, %u=>%u)\n",
1671 GNUNET_h2s (&put->key),
1672 GNUNET_i2s (peer->id),
1674 ntohl(put->hop_count),
1675 GNUNET_CRYPTO_hash_matching_bits (&peer->phash,
1677 GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
1682 switch (GNUNET_BLOCK_get_key
1690 if (0 != memcmp (&test_key,
1692 sizeof (struct GNUNET_HashCode)))
1694 char *put_s = GNUNET_strdup (GNUNET_h2s_full (&put->key));
1696 GNUNET_break_op (0);
1697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1698 "PUT with key `%s' for block with key %s\n",
1700 GNUNET_h2s_full (&test_key));
1701 GNUNET_free (put_s);
1706 GNUNET_break_op (0);
1709 /* cannot verify, good luck */
1712 if (ntohl (put->type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */
1714 switch (GNUNET_BLOCK_evaluate (GDS_block_context,
1716 NULL, /* query group */
1717 GNUNET_BLOCK_EO_NONE,
1719 NULL, 0, /* xquery */
1723 case GNUNET_BLOCK_EVALUATION_OK_MORE:
1724 case GNUNET_BLOCK_EVALUATION_OK_LAST:
1727 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
1728 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
1729 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
1730 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
1731 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
1732 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
1734 GNUNET_break_op (0);
1739 bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
1741 GNUNET_CONSTANTS_BLOOMFILTER_K);
1742 GNUNET_break_op (GNUNET_YES ==
1743 GNUNET_CONTAINER_bloomfilter_test (bf,
1746 struct GNUNET_PeerIdentity pp[putlen + 1];
1748 /* extend 'put path' by sender */
1749 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
1752 for (unsigned int i=0;i<=putlen;i++)
1754 for (unsigned int j=0;j<i;j++)
1756 GNUNET_break (0 != memcmp (&pp[i],
1758 sizeof (struct GNUNET_PeerIdentity)));
1760 GNUNET_break (0 != memcmp (&pp[i],
1762 sizeof (struct GNUNET_PeerIdentity)));
1767 putlen * sizeof (struct GNUNET_PeerIdentity));
1768 pp[putlen] = *peer->id;
1774 /* give to local clients */
1775 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1785 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1786 (am_closest_peer (&put->key, bf)))
1787 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1794 /* route to other peers */
1795 forwarded = GDS_NEIGHBOURS_handle_put (ntohl (put->type),
1797 ntohl (put->desired_replication_level),
1798 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1799 ntohl (put->hop_count),
1806 /* notify monitoring clients */
1807 GDS_CLIENTS_process_put (options
1808 | ( (GNUNET_OK == forwarded)
1809 ? GNUNET_DHT_RO_LAST_HOP
1812 ntohl (put->hop_count),
1813 ntohl (put->desired_replication_level),
1815 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1820 GNUNET_CONTAINER_bloomfilter_free (bf);
1825 * We have received a FIND PEER request. Send matching
1828 * @param sender sender of the FIND PEER request
1829 * @param key peers close to this key are desired
1830 * @param bg group for filtering peers
1833 handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1834 const struct GNUNET_HashCode *key,
1835 struct GNUNET_BLOCK_Group *bg)
1838 struct PeerBucket *bucket;
1839 struct PeerInfo *peer;
1840 unsigned int choice;
1841 const struct GNUNET_HELLO_Message *hello;
1843 /* first, check about our own HELLO */
1844 if (NULL != GDS_my_hello)
1847 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
1852 hello_size = GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) GDS_my_hello);
1853 GNUNET_break (hello_size >= sizeof (struct GNUNET_MessageHeader));
1854 GDS_NEIGHBOURS_handle_reply (sender,
1855 GNUNET_BLOCK_TYPE_DHT_HELLO,
1856 GNUNET_TIME_relative_to_absolute
1868 GNUNET_STATISTICS_update (GDS_stats,
1869 gettext_noop ("# FIND PEER requests ignored due to Bloomfilter"),
1876 GNUNET_STATISTICS_update (GDS_stats,
1877 gettext_noop ("# FIND PEER requests ignored due to lack of HELLO"),
1882 /* then, also consider sending a random HELLO from the closest bucket */
1883 if (0 == memcmp (&my_identity_hash,
1885 sizeof (struct GNUNET_HashCode)))
1886 bucket_idx = closest_bucket;
1888 bucket_idx = GNUNET_MIN (closest_bucket,
1890 if (bucket_idx == GNUNET_SYSERR)
1892 bucket = &k_buckets[bucket_idx];
1893 if (bucket->peers_size == 0)
1895 choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1896 bucket->peers_size);
1897 peer = bucket->head;
1900 GNUNET_assert (NULL != peer);
1904 choice = bucket->peers_size;
1909 return; /* no non-masked peer available */
1911 peer = bucket->head;
1912 hello = GDS_HELLO_get (peer->id);
1913 } while ( (NULL == hello) ||
1915 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
1917 GDS_NEIGHBOURS_handle_reply (sender,
1918 GNUNET_BLOCK_TYPE_DHT_HELLO,
1919 GNUNET_TIME_relative_to_absolute
1920 (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1927 GNUNET_HELLO_size (hello));
1932 * Handle a result from local datacache for a GET operation.
1934 * @param cls the `struct ClientHandle` of the client doing the query
1935 * @param type type of the block
1936 * @param expiration_time when does the content expire
1937 * @param key key for the content
1938 * @param put_path_length number of entries in @a put_path
1939 * @param put_path peers the original PUT traversed (if tracked)
1940 * @param get_path_length number of entries in @a get_path
1941 * @param get_path peers this reply has traversed so far (if tracked)
1942 * @param data payload of the reply
1943 * @param data_size number of bytes in @a data
1946 handle_local_result (void *cls,
1947 enum GNUNET_BLOCK_Type type,
1948 struct GNUNET_TIME_Absolute expiration_time,
1949 const struct GNUNET_HashCode *key,
1950 unsigned int put_path_length,
1951 const struct GNUNET_PeerIdentity *put_path,
1952 unsigned int get_path_length,
1953 const struct GNUNET_PeerIdentity *get_path,
1957 // FIXME: we can probably do better here by
1958 // passing the peer that did the query in the closure...
1959 GDS_ROUTING_process (NULL,
1963 put_path_length, put_path,
1970 * Check validity of p2p get request.
1972 * @param cls closure with the `struct PeerInfo` of the sender
1973 * @param get the message
1974 * @return #GNUNET_OK if the message is well-formed
1977 check_dht_p2p_get (void *cls,
1978 const struct PeerGetMessage *get)
1980 uint32_t xquery_size;
1983 msize = ntohs (get->header.size);
1984 xquery_size = ntohl (get->xquery_size);
1985 if (msize < sizeof (struct PeerGetMessage) + xquery_size)
1987 GNUNET_break_op (0);
1988 return GNUNET_SYSERR;
1995 * Core handler for p2p get requests.
1997 * @param cls closure with the `struct PeerInfo` of the sender
1998 * @param get the message
2001 handle_dht_p2p_get (void *cls,
2002 const struct PeerGetMessage *get)
2004 struct PeerInfo *peer = cls;
2005 uint32_t xquery_size;
2006 size_t reply_bf_size;
2008 enum GNUNET_BLOCK_Type type;
2009 enum GNUNET_DHT_RouteOption options;
2010 enum GNUNET_BLOCK_EvaluationResult eval;
2011 struct GNUNET_BLOCK_Group *bg;
2012 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
2021 /* parse and validate message */
2022 msize = ntohs (get->header.size);
2023 xquery_size = ntohl (get->xquery_size);
2024 reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
2025 type = ntohl (get->type);
2026 options = ntohl (get->options);
2027 xquery = (const char *) &get[1];
2028 GNUNET_STATISTICS_update (GDS_stats,
2029 gettext_noop ("# P2P GET requests received"),
2032 GNUNET_STATISTICS_update (GDS_stats,
2033 gettext_noop ("# P2P GET bytes received"),
2036 if (GNUNET_YES == log_route_details_stderr)
2040 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
2041 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
2042 "R5N GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n",
2043 GNUNET_h2s (&get->key),
2044 GNUNET_i2s (peer->id),
2046 ntohl(get->hop_count),
2047 GNUNET_CRYPTO_hash_matching_bits (&peer->phash,
2049 GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
2051 ntohl(get->xquery_size),
2055 bg = GNUNET_BLOCK_group_create (GDS_block_context,
2058 &xquery[xquery_size],
2061 = GNUNET_BLOCK_evaluate (GDS_block_context,
2064 GNUNET_BLOCK_EO_NONE,
2070 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
2072 /* request invalid or block type not supported */
2073 GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
2074 GNUNET_BLOCK_group_destroy (bg);
2077 peer_bf = GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
2079 GNUNET_CONSTANTS_BLOOMFILTER_K);
2080 GNUNET_break_op (GNUNET_YES ==
2081 GNUNET_CONTAINER_bloomfilter_test (peer_bf,
2083 /* remember request for routing replies */
2084 GDS_ROUTING_add (peer->id,
2086 bg, /* bg now owned by routing, but valid at least until end of this function! */
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2092 "GET for %s at %s after %u hops\n",
2093 GNUNET_h2s (&get->key),
2094 GNUNET_i2s (&my_identity),
2095 (unsigned int) ntohl (get->hop_count));
2096 /* local lookup (this may update the reply_bf) */
2097 if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
2098 (am_closest_peer (&get->key,
2101 if ((0 != (options & GNUNET_DHT_RO_FIND_PEER)))
2103 GNUNET_STATISTICS_update (GDS_stats,
2104 gettext_noop ("# P2P FIND PEER requests processed"),
2107 handle_find_peer (peer->id,
2113 eval = GDS_DATACACHE_handle_get (&get->key,
2118 &handle_local_result,
2124 GNUNET_STATISTICS_update (GDS_stats,
2125 gettext_noop ("# P2P GET requests ONLY routed"),
2130 /* P2P forwarding */
2131 forwarded = GNUNET_NO;
2132 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
2133 forwarded = GDS_NEIGHBOURS_handle_get (type,
2135 ntohl (get->desired_replication_level),
2136 ntohl (get->hop_count),
2142 GDS_CLIENTS_process_get (options
2143 | (GNUNET_OK == forwarded)
2144 ? GNUNET_DHT_RO_LAST_HOP : 0,
2146 ntohl (get->hop_count),
2147 ntohl (get->desired_replication_level),
2152 /* clean up; note that 'bg' is owned by routing now! */
2153 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
2158 * Check validity of p2p result message.
2160 * @param cls closure
2161 * @param message message
2162 * @return #GNUNET_YES if the message is well-formed
2165 check_dht_p2p_result (void *cls,
2166 const struct PeerResultMessage *prm)
2168 uint32_t get_path_length;
2169 uint32_t put_path_length;
2172 msize = ntohs (prm->header.size);
2173 put_path_length = ntohl (prm->put_path_length);
2174 get_path_length = ntohl (prm->get_path_length);
2176 sizeof (struct PeerResultMessage) + (get_path_length +
2178 sizeof (struct GNUNET_PeerIdentity)) ||
2180 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
2182 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
2184 GNUNET_break_op (0);
2185 return GNUNET_SYSERR;
2192 * Core handler for p2p result messages.
2194 * @param cls closure
2195 * @param message message
2198 handle_dht_p2p_result (void *cls,
2199 const struct PeerResultMessage *prm)
2201 struct PeerInfo *peer = cls;
2202 const struct GNUNET_PeerIdentity *put_path;
2203 const struct GNUNET_PeerIdentity *get_path;
2205 uint32_t get_path_length;
2206 uint32_t put_path_length;
2209 enum GNUNET_BLOCK_Type type;
2211 /* parse and validate message */
2212 msize = ntohs (prm->header.size);
2213 put_path_length = ntohl (prm->put_path_length);
2214 get_path_length = ntohl (prm->get_path_length);
2215 put_path = (const struct GNUNET_PeerIdentity *) &prm[1];
2216 get_path = &put_path[put_path_length];
2217 type = ntohl (prm->type);
2218 data = (const void *) &get_path[get_path_length];
2219 data_size = msize - (sizeof (struct PeerResultMessage) +
2221 put_path_length) * sizeof (struct GNUNET_PeerIdentity));
2222 GNUNET_STATISTICS_update (GDS_stats,
2223 gettext_noop ("# P2P RESULTS received"),
2226 GNUNET_STATISTICS_update (GDS_stats,
2227 gettext_noop ("# P2P RESULT bytes received"),
2230 if (GNUNET_YES == log_route_details_stderr)
2234 tmp = GNUNET_strdup (GNUNET_i2s (&my_identity));
2235 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
2236 "R5N RESULT %s: %s->%s (%u)\n",
2237 GNUNET_h2s (&prm->key),
2238 GNUNET_i2s (peer->id),
2240 get_path_length + 1);
2243 /* if we got a HELLO, consider it for our own routing table */
2244 if (GNUNET_BLOCK_TYPE_DHT_HELLO == type)
2246 const struct GNUNET_MessageHeader *h;
2247 struct GNUNET_PeerIdentity pid;
2249 /* Should be a HELLO, validate and consider using it! */
2250 if (data_size < sizeof (struct GNUNET_HELLO_Message))
2252 GNUNET_break_op (0);
2256 if (data_size != ntohs (h->size))
2258 GNUNET_break_op (0);
2262 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h,
2265 GNUNET_break_op (0);
2268 if ( (GNUNET_YES != disable_try_connect) &&
2269 (0 != memcmp (&my_identity,
2271 sizeof (struct GNUNET_PeerIdentity))) )
2276 /* append 'peer' to 'get_path' */
2278 struct GNUNET_PeerIdentity xget_path[get_path_length + 1];
2281 for (unsigned int i=0;i<=get_path_length;i++)
2283 for (unsigned int j=0;j<i;j++)
2285 GNUNET_break (0 != memcmp (&get_path[i],
2287 sizeof (struct GNUNET_PeerIdentity)));
2289 GNUNET_break (0 != memcmp (&get_path[i],
2291 sizeof (struct GNUNET_PeerIdentity)));
2294 GNUNET_memcpy (xget_path,
2296 get_path_length * sizeof (struct GNUNET_PeerIdentity));
2297 xget_path[get_path_length] = *peer->id;
2300 /* forward to local clients */
2301 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
2310 GDS_CLIENTS_process_get_resp (type,
2313 put_path, put_path_length,
2314 GNUNET_TIME_absolute_ntoh (prm->expiration_time),
2318 if (GNUNET_YES == cache_results)
2320 struct GNUNET_PeerIdentity xput_path[get_path_length + 1 + put_path_length];
2322 GNUNET_memcpy (xput_path,
2324 put_path_length * sizeof (struct GNUNET_PeerIdentity));
2325 GNUNET_memcpy (&xput_path[put_path_length],
2327 get_path_length * sizeof (struct GNUNET_PeerIdentity));
2329 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
2331 get_path_length + put_path_length,
2337 /* forward to other peers */
2338 GDS_ROUTING_process (NULL,
2340 GNUNET_TIME_absolute_ntoh (prm->expiration_time),
2353 * Initialize neighbours subsystem.
2355 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
2358 GDS_NEIGHBOURS_init ()
2360 struct GNUNET_MQ_MessageHandler core_handlers[] = {
2361 GNUNET_MQ_hd_var_size (dht_p2p_get,
2362 GNUNET_MESSAGE_TYPE_DHT_P2P_GET,
2363 struct PeerGetMessage,
2365 GNUNET_MQ_hd_var_size (dht_p2p_put,
2366 GNUNET_MESSAGE_TYPE_DHT_P2P_PUT,
2367 struct PeerPutMessage,
2369 GNUNET_MQ_hd_var_size (dht_p2p_result,
2370 GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT,
2371 struct PeerResultMessage,
2373 GNUNET_MQ_handler_end ()
2375 unsigned long long temp_config_num;
2378 = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg,
2380 "DISABLE_TRY_CONNECT");
2382 GNUNET_CONFIGURATION_get_value_number (GDS_cfg,
2386 bucket_size = (unsigned int) temp_config_num;
2388 = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg,
2392 log_route_details_stderr =
2393 (NULL != getenv("GNUNET_DHT_ROUTE_DEBUG")) ? GNUNET_YES : GNUNET_NO;
2394 ats_ch = GNUNET_ATS_connectivity_init (GDS_cfg);
2395 core_api = GNUNET_CORE_connect (GDS_cfg,
2398 &handle_core_connect,
2399 &handle_core_disconnect,
2401 if (NULL == core_api)
2402 return GNUNET_SYSERR;
2403 all_connected_peers = GNUNET_CONTAINER_multipeermap_create (256,
2405 all_desired_peers = GNUNET_CONTAINER_multipeermap_create (256,
2412 * Shutdown neighbours subsystem.
2415 GDS_NEIGHBOURS_done ()
2417 if (NULL == core_api)
2419 GNUNET_CORE_disconnect (core_api);
2422 GNUNET_CONTAINER_multipeermap_size (all_connected_peers));
2423 GNUNET_CONTAINER_multipeermap_destroy (all_connected_peers);
2424 all_connected_peers = NULL;
2425 GNUNET_CONTAINER_multipeermap_iterate (all_desired_peers,
2428 GNUNET_CONTAINER_multipeermap_destroy (all_desired_peers);
2429 all_desired_peers = NULL;
2430 GNUNET_ATS_connectivity_done (ats_ch);
2432 GNUNET_assert (NULL == find_peer_task);
2437 * Get the ID of the local node.
2439 * @return identity of the local node
2441 struct GNUNET_PeerIdentity *
2442 GDS_NEIGHBOURS_get_id ()
2444 return &my_identity;
2448 /* end of gnunet-service-dht_neighbours.c */