2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dht/gnunet-service-dht_neighbours.c
23 * @brief GNUnet DHT service's bucket and neighbour management code
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_block_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_nse_service.h"
35 #include "gnunet_core_service.h"
36 #include "gnunet_datacache_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_dht_service.h"
40 #include "gnunet_statistics_service.h"
42 #include "gnunet-service-dht.h"
43 #include "gnunet-service-dht_clients.h"
44 #include "gnunet-service-dht_datacache.h"
45 #include "gnunet-service-dht_hello.h"
46 #include "gnunet-service-dht_neighbours.h"
47 #include "gnunet-service-dht_nse.h"
48 #include "gnunet-service-dht_routing.h"
52 * How many buckets will we allow total.
54 #define MAX_BUCKETS sizeof (GNUNET_HashCode) * 8
57 * What is the maximum number of peers in a given bucket.
59 #define DEFAULT_BUCKET_SIZE 4
62 * Size of the bloom filter the DHT uses to filter peers.
64 #define DHT_BLOOM_SIZE 128
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 * How often to update our preference levels for peers in our routing tables.
79 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
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_MINUTES, 2)
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, 8)
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)
100 struct PeerPutMessage
103 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
105 struct GNUNET_MessageHeader header;
110 uint32_t options GNUNET_PACKED;
115 uint32_t type GNUNET_PACKED;
120 uint32_t hop_count GNUNET_PACKED;
123 * Replication level for this message
125 uint32_t desired_replication_level GNUNET_PACKED;
128 * Length of the PUT path that follows (if tracked).
130 uint32_t put_path_length GNUNET_PACKED;
133 * When does the content expire?
135 struct GNUNET_TIME_AbsoluteNBO expiration_time;
138 * Bloomfilter (for peer identities) to stop circular routes
140 char bloomfilter[DHT_BLOOM_SIZE];
143 * The key we are storing under.
147 /* put path (if tracked) */
157 struct PeerResultMessage
160 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
162 struct GNUNET_MessageHeader header;
167 uint32_t type GNUNET_PACKED;
170 * Length of the PUT path that follows (if tracked).
172 uint32_t put_path_length GNUNET_PACKED;
175 * Length of the GET path that follows (if tracked).
177 uint32_t get_path_length GNUNET_PACKED;
180 * When does the content expire?
182 struct GNUNET_TIME_AbsoluteNBO expiration_time;
185 * The key of the corresponding GET request.
189 /* put path (if tracked) */
191 /* get path (if tracked) */
201 struct PeerGetMessage
204 * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_GET
206 struct GNUNET_MessageHeader header;
211 uint32_t options GNUNET_PACKED;
214 * Desired content type.
216 uint32_t type GNUNET_PACKED;
221 uint32_t hop_count GNUNET_PACKED;
224 * Desired replication level for this request.
226 uint32_t desired_replication_level GNUNET_PACKED;
229 * Size of the extended query.
231 uint32_t xquery_size;
234 * Bloomfilter mutator.
239 * Bloomfilter (for peer identities) to stop circular routes
241 char bloomfilter[DHT_BLOOM_SIZE];
244 * The key we are looking for.
250 /* result bloomfilter */
256 * Linked list of messages to send to a particular other peer.
258 struct P2PPendingMessage
261 * Pointer to next item in the list
263 struct P2PPendingMessage *next;
266 * Pointer to previous item in the list
268 struct P2PPendingMessage *prev;
271 * Message importance level. FIXME: used? useful?
273 unsigned int importance;
276 * When does this message time out?
278 struct GNUNET_TIME_Absolute timeout;
281 * Actual message to be sent, allocated at the end of the struct:
282 * // msg = (cast) &pm[1];
283 * // memcpy (&pm[1], data, len);
285 const struct GNUNET_MessageHeader *msg;
291 * Entry for a peer in a bucket.
296 * Next peer entry (DLL)
298 struct PeerInfo *next;
301 * Prev peer entry (DLL)
303 struct PeerInfo *prev;
306 * Count of outstanding messages for peer. FIXME: NEEDED?
307 * FIXME: bound queue size!?
309 unsigned int pending_count;
312 * Head of pending messages to be sent to this peer.
314 struct P2PPendingMessage *head;
317 * Tail of pending messages to be sent to this peer.
319 struct P2PPendingMessage *tail;
322 * Core handle for sending messages to this peer.
324 struct GNUNET_CORE_TransmitHandle *th;
327 * Preference update context
329 struct GNUNET_CORE_InformationRequestContext *info_ctx;
332 * Task for scheduling preference updates
334 GNUNET_SCHEDULER_TaskIdentifier preference_task;
337 * What is the identity of the peer?
339 struct GNUNET_PeerIdentity id;
343 * What is the average latency for replies received?
345 struct GNUNET_TIME_Relative latency;
348 * Transport level distance to peer.
350 unsigned int distance;
357 * Peers are grouped into buckets.
364 struct PeerInfo *head;
369 struct PeerInfo *tail;
372 * Number of peers in the bucket.
374 unsigned int peers_size;
379 * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
381 static unsigned int closest_bucket;
384 * How many peers have we added since we sent out our last
387 static unsigned int newly_found_peers;
390 * The buckets. Array of size MAX_BUCKET_SIZE. Offset 0 means 0 bits matching.
392 static struct PeerBucket k_buckets[MAX_BUCKETS];
395 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
397 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
400 * Maximum size for each bucket.
402 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
405 * Task that sends FIND PEER requests.
407 static GNUNET_SCHEDULER_TaskIdentifier find_peer_task;
410 * Identity of this peer.
412 static struct GNUNET_PeerIdentity my_identity;
415 * Handle to GNUnet core.
417 static struct GNUNET_CORE_Handle *coreAPI;
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 GNUNET_HashCode * hc)
432 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, 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 * Let GNUnet core know that we like the given peer.
446 * @param cls the 'struct PeerInfo' of the peer
447 * @param tc scheduler context.
450 update_core_preference (void *cls,
451 const struct GNUNET_SCHEDULER_TaskContext *tc);
455 * Function called with statistics about the given peer.
458 * @param peer identifies the peer
459 * @param bpm_out set to the current bandwidth limit (sending) for this peer
460 * @param amount set to the amount that was actually reserved or unreserved;
461 * either the full requested amount or zero (no partial reservations)
462 * @param res_delay if the reservation could not be satisfied (amount was 0), how
463 * long should the client wait until re-trying?
464 * @param preference current traffic preference for the given peer
467 update_core_preference_finish (void *cls,
468 const struct GNUNET_PeerIdentity *peer,
469 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
471 struct GNUNET_TIME_Relative res_delay,
474 struct PeerInfo *peer_info = cls;
476 peer_info->info_ctx = NULL;
477 peer_info->preference_task
478 = GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
479 &update_core_preference, peer_info);
484 * Let GNUnet core know that we like the given peer.
486 * @param cls the 'struct PeerInfo' of the peer
487 * @param tc scheduler context.
490 update_core_preference (void *cls,
491 const struct GNUNET_SCHEDULER_TaskContext *tc)
493 struct PeerInfo *peer = cls;
495 unsigned int matching;
498 peer->preference_task = GNUNET_SCHEDULER_NO_TASK;
499 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
502 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
503 &peer->id.hashPubKey);
506 bucket = find_bucket(&peer->id.hashPubKey);
507 if (bucket == GNUNET_SYSERR)
510 preference = (1LL << matching) / k_buckets[bucket].peers_size;
513 peer->preference_task
514 = GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
515 &update_core_preference, peer);
518 GNUNET_STATISTICS_update (GDS_stats,
519 gettext_noop ("# Preference updates given to core"), 1,
522 GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
523 GNUNET_TIME_UNIT_FOREVER_REL,
524 GNUNET_BANDWIDTH_VALUE_MAX, 0,
526 &update_core_preference_finish, peer);
531 * Closure for 'add_known_to_bloom'.
533 struct BloomConstructorContext
536 * Bloom filter under construction.
538 struct GNUNET_CONTAINER_BloomFilter *bloom;
548 * Add each of the peers we already know to the bloom filter of
549 * the request so that we don't get duplicate HELLOs.
551 * @param cls the 'struct BloomConstructorContext'.
552 * @param key peer identity to add to the bloom filter
553 * @param value value the peer information (unused)
554 * @return GNUNET_YES (we should continue to iterate)
557 add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
559 struct BloomConstructorContext *ctx = cls;
562 GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
563 GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
569 * Task to send a find peer message for our own peer identifier
570 * so that we can find the closest peers in the network to ourselves
571 * and attempt to connect to them.
573 * @param cls closure for this task
574 * @param tc the context under which the task is running
577 send_find_peer_message (void *cls,
578 const struct GNUNET_SCHEDULER_TaskContext *tc)
580 struct GNUNET_TIME_Relative next_send_time;
581 struct BloomConstructorContext bcc;
583 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
584 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
586 if (newly_found_peers > bucket_size)
588 /* If we are finding many peers already, no need to send out our request right now! */
589 find_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
590 &send_find_peer_message, NULL);
591 newly_found_peers = 0;
594 bcc.bf_mutator = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
596 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
597 GNUNET_CONTAINER_multihashmap_iterate (all_known_peers,
600 GNUNET_STATISTICS_update (GDS_stats,
601 gettext_noop ("# FIND PEER messages initiated"), 1,
603 // FIXME: pass priority!?
604 GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
605 GNUNET_DHT_RO_FIND_PEER,
606 FIND_PEER_REPLICATION_LEVEL,
608 &my_identity.hashPubKey,
610 bcc.bloom, bcc.bf_mutator, NULL);
611 GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
612 /* schedule next round */
613 newly_found_peers = 0;
614 next_send_time.rel_value =
615 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
616 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
617 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
618 find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time,
619 &send_find_peer_message,
625 * Method called whenever a peer connects.
628 * @param peer peer identity this notification is about
629 * @param atsi performance data
632 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
633 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
635 struct PeerInfo *ret;
638 /* Check for connect to self message */
639 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
642 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
648 GNUNET_STATISTICS_update (GDS_stats,
649 gettext_noop ("# Peers connected"), 1,
651 peer_bucket = find_bucket (&peer->hashPubKey);
652 GNUNET_assert ( (peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS) );
653 ret = GNUNET_malloc (sizeof (struct PeerInfo));
655 ret->latency = latency;
656 ret->distance = distance;
659 GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head,
660 k_buckets[peer_bucket].tail, ret);
661 k_buckets[peer_bucket].peers_size++;
662 closest_bucket = GNUNET_MAX (closest_bucket,
664 if ( (peer_bucket > 0) &&
665 (k_buckets[peer_bucket].peers_size <= bucket_size) )
666 ret->preference_task = GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
668 GNUNET_assert (GNUNET_OK ==
669 GNUNET_CONTAINER_multihashmap_put (all_known_peers,
670 &peer->hashPubKey, ret,
671 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
672 if (1 == GNUNET_CONTAINER_multihashmap_size (all_known_peers))
674 /* got a first connection, good time to start with FIND PEER requests... */
675 find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message,
682 * Method called whenever a peer disconnects.
685 * @param peer peer identity this notification is about
688 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
690 struct PeerInfo *to_remove;
692 struct P2PPendingMessage *pos;
694 /* Check for disconnect from self message */
695 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
698 GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
699 if (NULL == to_remove)
704 GNUNET_STATISTICS_update (GDS_stats,
705 gettext_noop ("# Peers connected"), -1,
707 GNUNET_assert (GNUNET_YES ==
708 GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
711 if (NULL != to_remove->info_ctx)
713 GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
714 to_remove->info_ctx = NULL;
716 current_bucket = find_bucket (&to_remove->id.hashPubKey);
717 GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
718 k_buckets[current_bucket].tail,
720 GNUNET_assert (k_buckets[current_bucket].peers_size > 0);
721 k_buckets[current_bucket].peers_size--;
722 while ( (closest_bucket > 0) &&
723 (k_buckets[closest_bucket].peers_size == 0) )
726 if (to_remove->th != NULL)
728 GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
729 to_remove->th = NULL;
731 while (NULL != (pos = to_remove->head))
733 GNUNET_CONTAINER_DLL_remove (to_remove->head,
742 * Called when core is ready to send a message we asked for
743 * out to the destination.
745 * @param cls the 'struct PeerInfo' of the target peer
746 * @param size number of bytes available in buf
747 * @param buf where the callee should write the message
748 * @return number of bytes written to buf
751 core_transmit_notify (void *cls, size_t size, void *buf)
753 struct PeerInfo *peer = cls;
755 struct P2PPendingMessage *pending;
762 /* client disconnected */
765 if (peer->head == NULL)
767 /* no messages pending */
771 while ( (NULL != (pending = peer->head)) &&
772 (size - off >= (msize = ntohs (pending->msg->size))) )
774 GNUNET_STATISTICS_update (GDS_stats,
775 gettext_noop ("# Bytes transmitted to other peers"), msize,
777 memcpy (&cbuf[off], pending->msg, msize);
779 peer->pending_count--;
780 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
781 GNUNET_free (pending);
783 if (peer->head != NULL)
785 = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
787 GNUNET_TIME_absolute_get_remaining (pending->timeout),
789 &core_transmit_notify, peer);
795 * Transmit all messages in the peer's message queue.
797 * @param peer message queue to process
800 process_peer_queue (struct PeerInfo *peer)
802 struct P2PPendingMessage *pending;
804 if (NULL != (pending = peer->head))
806 if (NULL != peer->th)
809 = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
811 GNUNET_TIME_absolute_get_remaining (pending->timeout),
813 ntohs (pending->msg->size),
814 &core_transmit_notify, peer);
819 * To how many peers should we (on average) forward the request to
820 * obtain the desired target_replication count (on average).
822 * @param hop_count number of hops the message has traversed
823 * @param target_replication the number of total paths desired
824 * @return Some number of peers to forward the message to
827 get_forward_count (uint32_t hop_count,
828 uint32_t target_replication)
830 uint32_t random_value;
831 uint32_t forward_count;
834 if (hop_count > GDS_NSE_get () * 4.0)
836 /* forcefully terminate */
839 if (hop_count > GDS_NSE_get () * 2.0)
841 /* Once we have reached our ideal number of hops, only forward to 1 peer */
844 /* bound by system-wide maximum */
845 target_replication = GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL,
848 1 + (target_replication - 1.0) / (GDS_NSE_get () +
849 ((float) (target_replication - 1.0) *
851 /* Set forward count to floor of target_value */
852 forward_count = (uint32_t) target_value;
853 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
854 target_value = target_value - forward_count;
856 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
857 if (random_value < (target_value * UINT32_MAX))
859 return forward_count;
864 * Compute the distance between have and target as a 32-bit value.
865 * Differences in the lower bits must count stronger than differences
866 * in the higher bits.
868 * @return 0 if have==target, otherwise a number
869 * that is larger as the distance between
870 * the two hash codes increases
873 get_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
880 /* We have to represent the distance between two 2^9 (=512)-bit
881 * numbers as a 2^5 (=32)-bit number with "0" being used for the
882 * two numbers being identical; furthermore, we need to
883 * guarantee that a difference in the number of matching
884 * bits is always represented in the result.
886 * We use 2^32/2^9 numerical values to distinguish between
887 * hash codes that have the same LSB bit distance and
888 * use the highest 2^9 bits of the result to signify the
889 * number of (mis)matching LSB bits; if we have 0 matching
890 * and hence 512 mismatching LSB bits we return -1 (since
891 * 512 itself cannot be represented with 9 bits) */
893 /* first, calculate the most significant 9 bits of our
894 * result, aka the number of LSBs */
895 bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
896 /* bucket is now a value between 0 and 512 */
898 return 0; /* perfect match */
900 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
901 * below, we'd end up with max+1 (overflow)) */
903 /* calculate the most significant bits of the final result */
904 msb = (512 - bucket) << (32 - 9);
905 /* calculate the 32-9 least significant bits of the final result by
906 * looking at the differences in the 32-9 bits following the
907 * mismatching bit at 'bucket' */
910 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
912 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
913 GNUNET_CRYPTO_hash_get_bit (have, i))
914 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
915 * last bit set will be 31 -- if
916 * i does not reach 512 first... */
923 * Check whether my identity is closer than any known peers. If a
924 * non-null bloomfilter is given, check if this is the closest peer
925 * that hasn't already been routed to.
927 * @param key hash code to check closeness to
928 * @param bloom bloomfilter, exclude these entries from the decision
929 * @return GNUNET_YES if node location is closest,
930 * GNUNET_NO otherwise.
933 am_closest_peer (const GNUNET_HashCode *key,
934 const struct GNUNET_CONTAINER_BloomFilter *bloom)
940 struct PeerInfo *pos;
942 if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
944 bucket_num = find_bucket (key);
945 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, key);
946 pos = k_buckets[bucket_num].head;
948 while ((pos != NULL) && (count < bucket_size))
950 if ((bloom != NULL) &&
952 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
955 continue; /* Skip already checked entries */
957 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, key);
958 if (other_bits > bits)
960 if (other_bits == bits) /* We match the same number of bits */
964 /* No peers closer, we are the closest! */
970 * Select a peer from the routing table that would be a good routing
971 * destination for sending a message for "key". The resulting peer
972 * must not be in the set of blocked peers.<p>
974 * Note that we should not ALWAYS select the closest peer to the
975 * target, peers further away from the target should be chosen with
976 * exponentially declining probability.
978 * FIXME: double-check that this is fine
981 * @param key the key we are selecting a peer to route to
982 * @param bloom a bloomfilter containing entries this request has seen already
983 * @param hops how many hops has this message traversed thus far
984 * @return Peer to route to, or NULL on error
986 static struct PeerInfo *
987 select_peer (const GNUNET_HashCode *key,
988 const struct GNUNET_CONTAINER_BloomFilter *bloom,
993 unsigned int selected;
994 struct PeerInfo *pos;
996 unsigned int smallest_distance;
997 struct PeerInfo *chosen;
999 if (hops >= GDS_NSE_get ())
1001 /* greedy selection (closest peer that is not in bloomfilter) */
1002 smallest_distance = UINT_MAX;
1004 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1006 pos = k_buckets[bc].head;
1008 while ((pos != NULL) && (count < bucket_size))
1011 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
1013 dist = get_distance (key, &pos->id.hashPubKey);
1014 if (dist < smallest_distance)
1017 smallest_distance = dist;
1027 /* select "random" peer */
1028 /* count number of peers that are available and not filtered */
1030 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1032 pos = k_buckets[bc].head;
1033 while ((pos != NULL) && (count < bucket_size))
1036 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
1039 continue; /* Ignore bloomfiltered peers */
1045 if (count == 0) /* No peers to select from! */
1049 /* Now actually choose a peer */
1050 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1052 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1054 pos = k_buckets[bc].head;
1055 while ((pos != NULL) && (count < bucket_size))
1058 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
1061 continue; /* Ignore bloomfiltered peers */
1063 if (0 == selected--)
1074 * Compute the set of peers that the given request should be
1077 * @param key routing key
1078 * @param bloom bloom filter excluding peers as targets, all selected
1079 * peers will be added to the bloom filter
1080 * @param hop_count number of hops the request has traversed so far
1081 * @param target_replication desired number of replicas
1082 * @param targets where to store an array of target peers (to be
1083 * free'd by the caller)
1084 * @return number of peers returned in 'targets'.
1087 get_target_peers (const GNUNET_HashCode *key,
1088 struct GNUNET_CONTAINER_BloomFilter *bloom,
1090 uint32_t target_replication,
1091 struct PeerInfo ***targets)
1095 struct PeerInfo **rtargets;
1096 struct PeerInfo *nxt;
1098 ret = get_forward_count (hop_count, target_replication);
1104 rtargets = GNUNET_malloc (sizeof (struct PeerInfo*) * ret);
1108 nxt = select_peer (key, bloom, hop_count);
1111 rtargets[off++] = nxt;
1112 GNUNET_CONTAINER_bloomfilter_add (bloom, &nxt->id.hashPubKey);
1116 GNUNET_free (rtargets);
1120 *targets = rtargets;
1126 * Perform a PUT operation. Forwards the given request to other
1127 * peers. Does not store the data locally. Does not give the
1128 * data to local clients. May do nothing if this is the only
1129 * peer in the network (or if we are the closest peer in the
1132 * @param type type of the block
1133 * @param options routing options
1134 * @param desired_replication_level desired replication count
1135 * @param expiration_time when does the content expire
1136 * @param hop_count how many hops has this message traversed so far
1137 * @param bf Bloom filter of peers this PUT has already traversed
1138 * @param key key for the content
1139 * @param put_path_length number of entries in put_path
1140 * @param put_path peers this request has traversed so far (if tracked)
1141 * @param data payload to store
1142 * @param data_size number of bytes in data
1145 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1146 enum GNUNET_DHT_RouteOption options,
1147 uint32_t desired_replication_level,
1148 struct GNUNET_TIME_Absolute expiration_time,
1150 struct GNUNET_CONTAINER_BloomFilter *bf,
1151 const GNUNET_HashCode *key,
1152 unsigned int put_path_length,
1153 struct GNUNET_PeerIdentity *put_path,
1157 unsigned int target_count;
1159 struct PeerInfo **targets;
1160 struct PeerInfo *target;
1161 struct P2PPendingMessage *pending;
1163 struct PeerPutMessage *ppm;
1164 struct GNUNET_PeerIdentity *pp;
1166 GNUNET_STATISTICS_update (GDS_stats,
1167 gettext_noop ("# PUT requests routed"), 1,
1169 target_count = get_target_peers (key, bf, hop_count,
1170 desired_replication_level,
1172 if (0 == target_count)
1174 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + sizeof (struct PeerPutMessage);
1175 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1177 put_path_length = 0;
1178 msize = data_size + sizeof (struct PeerPutMessage);
1180 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1185 GNUNET_STATISTICS_update (GDS_stats,
1186 gettext_noop ("# Peers selected as targets for PUT requests"), target_count,
1188 for (i=0;i<target_count;i++)
1190 target = targets[i];
1191 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1192 pending->importance = 0; /* FIXME */
1193 pending->timeout = expiration_time;
1194 ppm = (struct PeerPutMessage*) &pending[1];
1195 pending->msg = &ppm->header;
1196 ppm->header.size = htons (msize);
1197 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1198 ppm->options = htonl (options);
1199 ppm->type = htonl (type);
1200 ppm->hop_count = htonl (hop_count + 1);
1201 ppm->desired_replication_level = htonl (desired_replication_level);
1202 ppm->put_path_length = htonl (put_path_length);
1203 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1204 GNUNET_assert (GNUNET_OK ==
1205 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
1209 pp = (struct GNUNET_PeerIdentity*) &ppm[1];
1210 memcpy (pp, put_path, sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1211 memcpy (&pp[put_path_length], data, data_size);
1212 GNUNET_CONTAINER_DLL_insert_tail (target->head,
1215 target->pending_count++;
1216 process_peer_queue (target);
1218 GNUNET_free (targets);
1223 * Perform a GET operation. Forwards the given request to other
1224 * peers. Does not lookup the key locally. May do nothing if this is
1225 * the only peer in the network (or if we are the closest peer in the
1228 * @param type type of the block
1229 * @param options routing options
1230 * @param desired_replication_level desired replication count
1231 * @param hop_count how many hops did this request traverse so far?
1232 * @param key key for the content
1233 * @param xquery extended query
1234 * @param xquery_size number of bytes in xquery
1235 * @param reply_bf bloomfilter to filter duplicates
1236 * @param reply_bf_mutator mutator for reply_bf
1237 * @param peer_bf filter for peers not to select (again)
1240 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1241 enum GNUNET_DHT_RouteOption options,
1242 uint32_t desired_replication_level,
1244 const GNUNET_HashCode *key,
1247 const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
1248 uint32_t reply_bf_mutator,
1249 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1251 unsigned int target_count;
1253 struct PeerInfo **targets;
1254 struct PeerInfo *target;
1255 struct P2PPendingMessage *pending;
1257 struct PeerGetMessage *pgm;
1259 size_t reply_bf_size;
1261 GNUNET_STATISTICS_update (GDS_stats,
1262 gettext_noop ("# GET requests routed"), 1,
1264 target_count = get_target_peers (key, peer_bf, hop_count,
1265 desired_replication_level,
1267 if (0 == target_count)
1269 reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
1270 msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
1271 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1276 GNUNET_STATISTICS_update (GDS_stats,
1277 gettext_noop ("# Peers selected as targets for GET requests"), target_count,
1279 /* forward request */
1280 for (i=0;i<target_count;i++)
1282 target = targets[i];
1283 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1284 pending->importance = 0; /* FIXME */
1285 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1286 pgm = (struct PeerGetMessage*) &pending[1];
1287 pending->msg = &pgm->header;
1288 pgm->header.size = htons (msize);
1289 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1290 pgm->options = htonl (options);
1291 pgm->type = htonl (type);
1292 pgm->hop_count = htonl (hop_count + 1);
1293 pgm->desired_replication_level = htonl (desired_replication_level);
1294 pgm->xquery_size = htonl (xquery_size);
1295 pgm->bf_mutator = reply_bf_mutator;
1296 GNUNET_assert (GNUNET_OK ==
1297 GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
1301 xq = (char *) &pgm[1];
1302 memcpy (xq, xquery, xquery_size);
1303 GNUNET_assert (GNUNET_OK ==
1304 GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
1307 GNUNET_CONTAINER_DLL_insert_tail (target->head,
1310 target->pending_count++;
1311 process_peer_queue (target);
1313 GNUNET_free (targets);
1318 * Handle a reply (route to origin). Only forwards the reply back to
1319 * the given peer. Does not do local caching or forwarding to local
1322 * @param target neighbour that should receive the block (if still connected)
1323 * @param type type of the block
1324 * @param expiration_time when does the content expire
1325 * @param key key for the content
1326 * @param put_path_length number of entries in put_path
1327 * @param put_path peers the original PUT traversed (if tracked)
1328 * @param get_path_length number of entries in put_path
1329 * @param get_path peers this reply has traversed so far (if tracked)
1330 * @param data payload of the reply
1331 * @param data_size number of bytes in data
1334 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1335 enum GNUNET_BLOCK_Type type,
1336 struct GNUNET_TIME_Absolute expiration_time,
1337 const GNUNET_HashCode *key,
1338 unsigned int put_path_length,
1339 const struct GNUNET_PeerIdentity *put_path,
1340 unsigned int get_path_length,
1341 const struct GNUNET_PeerIdentity *get_path,
1345 struct PeerInfo *pi;
1346 struct P2PPendingMessage *pending;
1348 struct PeerResultMessage *prm;
1349 struct GNUNET_PeerIdentity *paths;
1351 msize = data_size + sizeof (struct PeerResultMessage) +
1352 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
1353 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1354 (get_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1355 (put_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1356 (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE) )
1361 pi = GNUNET_CONTAINER_multihashmap_get (all_known_peers,
1362 &target->hashPubKey);
1365 /* peer disconnected in the meantime, drop reply */
1368 GNUNET_STATISTICS_update (GDS_stats,
1369 gettext_noop ("# REPLIES routed"), 1,
1371 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1372 pending->importance = 0; /* FIXME */
1373 pending->timeout = expiration_time;
1374 prm = (struct PeerResultMessage*) &pending[1];
1375 pending->msg = &prm->header;
1376 prm->header.size = htons (msize);
1377 prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1378 prm->type = htonl (type);
1379 prm->put_path_length = htonl (put_path_length);
1380 prm->get_path_length = htonl (get_path_length);
1381 prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1383 paths = (struct GNUNET_PeerIdentity*) &prm[1];
1384 memcpy (paths, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity));
1385 memcpy (&paths[put_path_length],
1386 get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
1387 memcpy (&paths[put_path_length + get_path_length],
1389 GNUNET_CONTAINER_DLL_insert (pi->head,
1392 pi->pending_count++;
1393 process_peer_queue (pi);
1398 * To be called on core init/fail.
1400 * @param cls service closure
1401 * @param server handle to the server for this service
1402 * @param identity the public identity of this peer
1403 * @param publicKey the public key of this peer
1406 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1407 const struct GNUNET_PeerIdentity *identity,
1408 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1410 GNUNET_assert (server != NULL);
1411 my_identity = *identity;
1416 * Core handler for p2p put requests.
1418 * @param cls closure
1419 * @param peer sender of the request
1420 * @param message message
1421 * @param peer peer identity this notification is about
1422 * @param atsi performance data
1423 * @return GNUNET_OK to keep the connection open,
1424 * GNUNET_SYSERR to close it (signal serious error)
1427 handle_dht_p2p_put (void *cls,
1428 const struct GNUNET_PeerIdentity *peer,
1429 const struct GNUNET_MessageHeader *message,
1430 const struct GNUNET_TRANSPORT_ATS_Information
1433 const struct PeerPutMessage *put;
1434 const struct GNUNET_PeerIdentity *put_path;
1435 const void *payload;
1438 size_t payload_size;
1439 enum GNUNET_DHT_RouteOption options;
1440 struct GNUNET_CONTAINER_BloomFilter *bf;
1441 GNUNET_HashCode test_key;
1443 msize = ntohs (message->size);
1444 if (msize < sizeof (struct PeerPutMessage))
1446 GNUNET_break_op (0);
1449 put = (const struct PeerPutMessage*) message;
1450 putlen = ntohl (put->put_path_length);
1451 if ( (msize < sizeof (struct PeerPutMessage) + putlen * sizeof (struct GNUNET_PeerIdentity)) ||
1452 (putlen > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) )
1454 GNUNET_break_op (0);
1457 GNUNET_STATISTICS_update (GDS_stats,
1458 gettext_noop ("# P2P PUT requests received"), 1,
1460 put_path = (const struct GNUNET_PeerIdentity*) &put[1];
1461 payload = &put_path[putlen];
1462 options = ntohl (put->options);
1463 payload_size = msize - (sizeof (struct PeerPutMessage) +
1464 putlen * sizeof (struct GNUNET_PeerIdentity));
1465 switch (GNUNET_BLOCK_get_key (GDS_block_context,
1467 payload, payload_size,
1471 if (0 != memcmp (&test_key, &put->key, sizeof (GNUNET_HashCode)))
1473 GNUNET_break_op (0);
1478 GNUNET_break_op (0);
1481 /* cannot verify, good luck */
1484 bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
1488 struct GNUNET_PeerIdentity pp[putlen+1];
1490 /* extend 'put path' by sender */
1491 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
1493 memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
1500 /* give to local clients */
1501 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1510 if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1511 (am_closest_peer (&put->key,
1513 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1519 /* route to other peers */
1520 GDS_NEIGHBOURS_handle_put (ntohl (put->type),
1522 ntohl (put->desired_replication_level),
1523 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1524 ntohl (put->hop_count) + 1 /* who adds +1? */,
1531 GNUNET_CONTAINER_bloomfilter_free (bf);
1537 * We have received a FIND PEER request. Send matching
1540 * @param sender sender of the FIND PEER request
1541 * @param key peers close to this key are desired
1542 * @param bf peers matching this bf are excluded
1543 * @param bf_mutator mutator for bf
1546 handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1547 const GNUNET_HashCode *key,
1548 struct GNUNET_CONTAINER_BloomFilter *bf,
1549 uint32_t bf_mutator)
1552 struct PeerBucket *bucket;
1553 struct PeerInfo *peer;
1554 unsigned int choice;
1555 GNUNET_HashCode mhash;
1556 const struct GNUNET_HELLO_Message *hello;
1558 /* first, check about our own HELLO */
1559 if (NULL != GDS_my_hello)
1561 GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
1562 if (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))
1564 GDS_NEIGHBOURS_handle_reply (sender,
1565 GNUNET_BLOCK_TYPE_DHT_HELLO,
1566 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1571 GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*) GDS_my_hello));
1574 /* then, also consider sending a random HELLO from the closest bucket */
1575 bucket_idx = find_bucket (key);
1576 if (bucket_idx == GNUNET_SYSERR)
1578 bucket = &k_buckets[bucket_idx];
1579 if (bucket->peers_size == 0)
1581 choice = GNUNET_CRYPTO_random_u32 (bucket->peers_size,
1582 GNUNET_CRYPTO_QUALITY_WEAK);
1583 peer = bucket->head;
1586 GNUNET_assert (peer != NULL);
1589 choice = bucket->peers_size;
1594 return; /* no non-masked peer available */
1596 peer = bucket->head;
1597 GNUNET_BLOCK_mingle_hash (&peer->id.hashPubKey, bf_mutator, &mhash);
1598 hello = GDS_HELLO_get (&peer->id);
1600 while ( (hello == NULL) ||
1601 (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) );
1602 GDS_NEIGHBOURS_handle_reply (sender,
1603 GNUNET_BLOCK_TYPE_DHT_HELLO,
1604 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1609 GNUNET_HELLO_size (hello));
1614 * Core handler for p2p get requests.
1616 * @param cls closure
1617 * @param peer sender of the request
1618 * @param message message
1619 * @param peer peer identity this notification is about
1620 * @param atsi performance data
1621 * @return GNUNET_OK to keep the connection open,
1622 * GNUNET_SYSERR to close it (signal serious error)
1625 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
1626 const struct GNUNET_MessageHeader *message,
1627 const struct GNUNET_TRANSPORT_ATS_Information
1630 struct PeerGetMessage *get;
1631 uint32_t xquery_size;
1632 size_t reply_bf_size;
1634 enum GNUNET_BLOCK_Type type;
1635 enum GNUNET_DHT_RouteOption options;
1636 enum GNUNET_BLOCK_EvaluationResult eval;
1637 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
1638 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
1641 /* parse and validate message */
1642 msize = ntohs (message->size);
1643 if (msize < sizeof (struct PeerGetMessage))
1645 GNUNET_break_op (0);
1648 get = (struct PeerGetMessage *) message;
1649 xquery_size = ntohl (get->xquery_size);
1650 if (msize < sizeof (struct PeerGetMessage) + xquery_size)
1652 GNUNET_break_op (0);
1655 GNUNET_STATISTICS_update (GDS_stats,
1656 gettext_noop ("# P2P GET requests received"), 1,
1658 reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
1659 type = ntohl (get->type);
1660 options = ntohl (get->options);
1661 xquery = (const char*) &get[1];
1663 if (reply_bf_size > 0)
1664 reply_bf = GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size],
1666 GNUNET_DHT_GET_BLOOMFILTER_K);
1667 eval = GNUNET_BLOCK_evaluate (GDS_block_context,
1672 xquery, xquery_size,
1674 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
1676 /* request invalid or block type not supported */
1677 GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
1678 if (NULL != reply_bf)
1679 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1683 GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
1687 /* remember request for routing replies */
1688 GDS_ROUTING_add (peer,
1692 xquery, xquery_size,
1693 reply_bf, get->bf_mutator);
1695 /* local lookup (this may update the reply_bf) */
1696 if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1697 (am_closest_peer (&get->key,
1700 if ( (0 != (options & GNUNET_DHT_RO_FIND_PEER)))
1702 GNUNET_STATISTICS_update (GDS_stats,
1703 gettext_noop ("# P2P FIND PEER requests processed"), 1,
1705 handle_find_peer (peer,
1712 eval = GDS_DATACACHE_handle_get (&get->key,
1714 xquery, xquery_size,
1720 /* P2P forwarding */
1721 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1722 GDS_NEIGHBOURS_handle_get (type,
1724 ntohl (get->desired_replication_level),
1725 ntohl (get->hop_count) + 1, /* CHECK: where (else) do we do +1? */
1727 xquery, xquery_size,
1732 if (NULL != reply_bf)
1733 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1734 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
1740 * Core handler for p2p result messages.
1742 * @param cls closure
1743 * @param message message
1744 * @param peer peer identity this notification is about
1745 * @param atsi performance data
1746 * @return GNUNET_YES (do not cut p2p connection)
1749 handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
1750 const struct GNUNET_MessageHeader *message,
1751 const struct GNUNET_TRANSPORT_ATS_Information
1754 const struct PeerResultMessage *prm;
1755 const struct GNUNET_PeerIdentity *put_path;
1756 const struct GNUNET_PeerIdentity *get_path;
1758 uint32_t get_path_length;
1759 uint32_t put_path_length;
1762 enum GNUNET_BLOCK_Type type;
1764 /* parse and validate message */
1765 msize = ntohs (message->size);
1766 if (msize < sizeof (struct PeerResultMessage))
1768 GNUNET_break_op (0);
1771 prm = (struct PeerResultMessage *) message;
1772 put_path_length = ntohl (prm->put_path_length);
1773 get_path_length = ntohl (prm->get_path_length);
1774 if ( (msize < sizeof (struct PeerResultMessage) +
1775 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity)) ||
1776 (get_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1777 (put_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) )
1779 GNUNET_break_op (0);
1782 GNUNET_STATISTICS_update (GDS_stats,
1783 gettext_noop ("# P2P RESULTS received"), 1,
1785 put_path = (const struct GNUNET_PeerIdentity*) &prm[1];
1786 get_path = &put_path[put_path_length];
1787 type = ntohl (prm->type);
1788 data = (const void*) &get_path[get_path_length];
1789 data_size = msize - (sizeof (struct PeerResultMessage) +
1790 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity));
1792 /* if we got a HELLO, consider it for our own routing table */
1793 if (type == GNUNET_BLOCK_TYPE_DHT_HELLO)
1795 const struct GNUNET_MessageHeader *h;
1796 struct GNUNET_PeerIdentity pid;
1799 /* Should be a HELLO, validate and consider using it! */
1800 if (data_size < sizeof (struct GNUNET_MessageHeader))
1802 GNUNET_break_op (0);
1806 if (data_size != ntohs (h->size))
1808 GNUNET_break_op (0);
1812 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) h,
1815 GNUNET_break_op (0);
1818 bucket = find_bucket (&pid.hashPubKey);
1819 if ( (bucket >= 0) &&
1820 (k_buckets[bucket].peers_size < bucket_size) )
1822 if (NULL != GDS_transport_handle)
1823 GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
1825 (void) GNUNET_CORE_peer_request_connect (coreAPI,
1831 /* append 'peer' to 'get_path' */
1833 struct GNUNET_PeerIdentity xget_path[get_path_length+1];
1835 memcpy (xget_path, get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
1836 xget_path[get_path_length] = *peer;
1839 /* forward to local clients */
1840 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1850 /* forward to other peers */
1851 GDS_ROUTING_process (type,
1852 GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1866 * Initialize neighbours subsystem.
1868 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1871 GDS_NEIGHBOURS_init ()
1873 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1874 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
1875 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
1876 {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0},
1879 unsigned long long temp_config_num;
1882 GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size",
1884 bucket_size = (unsigned int) temp_config_num;
1885 coreAPI = GNUNET_CORE_connect (GDS_cfg,
1889 &handle_core_connect,
1890 &handle_core_disconnect,
1891 NULL, /* Do we care about "status" updates? */
1895 if (coreAPI == NULL)
1896 return GNUNET_SYSERR;
1897 all_known_peers = GNUNET_CONTAINER_multihashmap_create (256);
1903 * Shutdown neighbours subsystem.
1906 GDS_NEIGHBOURS_done ()
1908 if (coreAPI == NULL)
1910 GNUNET_CORE_disconnect (coreAPI);
1912 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (all_known_peers));
1913 GNUNET_CONTAINER_multihashmap_destroy (all_known_peers);
1914 all_known_peers = NULL;
1915 if (GNUNET_SCHEDULER_NO_TASK != find_peer_task)
1917 GNUNET_SCHEDULER_cancel (find_peer_task);
1918 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
1923 /* end of gnunet-service-dht_neighbours.c */