2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dht/gnunet-service-dht_neighbours.c
23 * @brief GNUnet DHT service's bucket and neighbour management code
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_block_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_nse_service.h"
35 #include "gnunet_core_service.h"
36 #include "gnunet_datacache_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_dht_service.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet_peerinfo_service.h"
43 #include "gnunet-service-dht.h"
44 #include "gnunet-service-dht_clients.h"
45 #include "gnunet-service-dht_datacache.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 * HELLO message for the peer, NULL if not known. FIXME: track
333 * separately? FIXME: free!?
335 struct GNUNET_HELLO_Message *hello;
338 * Task for scheduling preference updates
340 GNUNET_SCHEDULER_TaskIdentifier preference_task;
343 * What is the identity of the peer?
345 struct GNUNET_PeerIdentity id;
349 * What is the average latency for replies received?
351 struct GNUNET_TIME_Relative latency;
354 * Transport level distance to peer.
356 unsigned int distance;
363 * Peers are grouped into buckets.
370 struct PeerInfo *head;
375 struct PeerInfo *tail;
378 * Number of peers in the bucket.
380 unsigned int peers_size;
385 * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
387 static unsigned int closest_bucket;
390 * How many peers have we added since we sent out our last
393 static unsigned int newly_found_peers;
396 * The buckets. Array of size MAX_BUCKET_SIZE. Offset 0 means 0 bits matching.
398 static struct PeerBucket k_buckets[MAX_BUCKETS];
401 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
403 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
406 * Maximum size for each bucket.
408 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
411 * Task that sends FIND PEER requests.
413 static GNUNET_SCHEDULER_TaskIdentifier find_peer_task;
416 * Identity of this peer.
418 static struct GNUNET_PeerIdentity my_identity;
421 * Handle to GNUnet core.
423 static struct GNUNET_CORE_Handle *coreAPI;
426 * Handle for peerinfo notifications.
428 static struct GNUNET_PEERINFO_NotifyContext *pnc;
432 * Find the optimal bucket for this key.
434 * @param hc the hashcode to compare our identity to
435 * @return the proper bucket index, or GNUNET_SYSERR
436 * on error (same hashcode)
439 find_bucket (const GNUNET_HashCode * hc)
443 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
444 if (bits == MAX_BUCKETS)
446 /* How can all bits match? Got my own ID? */
448 return GNUNET_SYSERR;
450 return MAX_BUCKETS - bits - 1;
455 * Let GNUnet core know that we like the given peer.
457 * @param cls the 'struct PeerInfo' of the peer
458 * @param tc scheduler context.
461 update_core_preference (void *cls,
462 const struct GNUNET_SCHEDULER_TaskContext *tc);
466 * Function called with statistics about the given peer.
469 * @param peer identifies the peer
470 * @param bpm_out set to the current bandwidth limit (sending) for this peer
471 * @param amount set to the amount that was actually reserved or unreserved;
472 * either the full requested amount or zero (no partial reservations)
473 * @param res_delay if the reservation could not be satisfied (amount was 0), how
474 * long should the client wait until re-trying?
475 * @param preference current traffic preference for the given peer
478 update_core_preference_finish (void *cls,
479 const struct GNUNET_PeerIdentity *peer,
480 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
482 struct GNUNET_TIME_Relative res_delay,
485 struct PeerInfo *peer_info = cls;
487 peer_info->info_ctx = NULL;
488 peer_info->preference_task
489 = GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
490 &update_core_preference, peer_info);
495 * Let GNUnet core know that we like the given peer.
497 * @param cls the 'struct PeerInfo' of the peer
498 * @param tc scheduler context.
501 update_core_preference (void *cls,
502 const struct GNUNET_SCHEDULER_TaskContext *tc)
504 struct PeerInfo *peer = cls;
506 unsigned int matching;
509 peer->preference_task = GNUNET_SCHEDULER_NO_TASK;
510 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
513 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
514 &peer->id.hashPubKey);
517 bucket = find_bucket(&peer->id.hashPubKey);
518 if (bucket == GNUNET_SYSERR)
521 preference = (1LL << matching) / k_buckets[bucket].peers_size;
524 peer->preference_task
525 = GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
526 &update_core_preference, peer);
530 GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
531 GNUNET_TIME_UNIT_FOREVER_REL,
532 GNUNET_BANDWIDTH_VALUE_MAX, 0,
534 &update_core_preference_finish, peer);
539 * Closure for 'add_known_to_bloom'.
541 struct BloomConstructorContext
544 * Bloom filter under construction.
546 struct GNUNET_CONTAINER_BloomFilter *bloom;
556 * Add each of the peers we already know to the bloom filter of
557 * the request so that we don't get duplicate HELLOs.
559 * @param cls the 'struct BloomConstructorContext'.
560 * @param key peer identity to add to the bloom filter
561 * @param value value the peer information (unused)
562 * @return GNUNET_YES (we should continue to iterate)
565 add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
567 struct BloomConstructorContext *ctx = cls;
570 GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
571 GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
577 * Task to send a find peer message for our own peer identifier
578 * so that we can find the closest peers in the network to ourselves
579 * and attempt to connect to them.
581 * @param cls closure for this task
582 * @param tc the context under which the task is running
585 send_find_peer_message (void *cls,
586 const struct GNUNET_SCHEDULER_TaskContext *tc)
588 struct GNUNET_TIME_Relative next_send_time;
589 struct BloomConstructorContext bcc;
591 find_peer_task = GNUNET_SCHEDULER_NO_TASK;
592 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
594 if (newly_found_peers > bucket_size)
596 /* If we are finding many peers already, no need to send out our request right now! */
597 find_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
598 &send_find_peer_message, NULL);
599 newly_found_peers = 0;
602 bcc.bf_mutator = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
604 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
605 GNUNET_CONTAINER_multihashmap_iterate (all_known_peers,
608 // FIXME: pass priority!?
609 GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
610 GNUNET_DHT_RO_FIND_PEER,
611 FIND_PEER_REPLICATION_LEVEL,
613 &my_identity.hashPubKey,
615 bcc.bloom, bcc.bf_mutator, NULL);
616 GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
617 /* schedule next round */
618 newly_found_peers = 0;
619 next_send_time.rel_value =
620 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
621 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
622 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
623 find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time,
624 &send_find_peer_message,
630 * Method called whenever a peer connects.
633 * @param peer peer identity this notification is about
634 * @param atsi performance data
637 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
638 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
640 struct PeerInfo *ret;
643 /* Check for connect to self message */
644 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
647 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
653 peer_bucket = find_bucket (&peer->hashPubKey);
654 GNUNET_assert ( (peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS) );
655 ret = GNUNET_malloc (sizeof (struct PeerInfo));
657 ret->latency = latency;
658 ret->distance = distance;
661 GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head,
662 k_buckets[peer_bucket].tail, ret);
663 k_buckets[peer_bucket].peers_size++;
664 closest_bucket = GNUNET_MAX (closest_bucket,
666 if ( (peer_bucket > 0) &&
667 (k_buckets[peer_bucket].peers_size <= bucket_size) )
668 ret->preference_task = GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
670 GNUNET_assert (GNUNET_OK ==
671 GNUNET_CONTAINER_multihashmap_put (all_known_peers,
672 &peer->hashPubKey, ret,
673 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
674 if (1 == GNUNET_CONTAINER_multihashmap_size (all_known_peers))
676 /* got a first connection, good time to start with FIND PEER requests... */
677 find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message,
684 * Method called whenever a peer disconnects.
687 * @param peer peer identity this notification is about
690 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
692 struct PeerInfo *to_remove;
694 struct P2PPendingMessage *pos;
696 /* Check for disconnect from self message */
697 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
700 GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
701 if (NULL == to_remove)
706 GNUNET_assert (GNUNET_YES ==
707 GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
710 if (NULL != to_remove->info_ctx)
712 GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
713 to_remove->info_ctx = NULL;
715 current_bucket = find_bucket (&to_remove->id.hashPubKey);
716 GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
717 k_buckets[current_bucket].tail,
719 GNUNET_assert (k_buckets[current_bucket].peers_size > 0);
720 k_buckets[current_bucket].peers_size--;
721 while ( (closest_bucket > 0) &&
722 (k_buckets[closest_bucket].peers_size == 0) )
725 if (to_remove->th != NULL)
727 GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
728 to_remove->th = NULL;
730 while (NULL != (pos = to_remove->head))
732 GNUNET_CONTAINER_DLL_remove (to_remove->head,
741 * Called when core is ready to send a message we asked for
742 * out to the destination.
744 * @param cls the 'struct PeerInfo' of the target peer
745 * @param size number of bytes available in buf
746 * @param buf where the callee should write the message
747 * @return number of bytes written to buf
750 core_transmit_notify (void *cls, size_t size, void *buf)
752 struct PeerInfo *peer = cls;
754 struct P2PPendingMessage *pending;
761 /* client disconnected */
764 if (peer->head == NULL)
766 /* no messages pending */
770 while ( (NULL != (pending = peer->head)) &&
771 (size - off >= (msize = ntohs (pending->msg->size))) )
773 memcpy (&cbuf[off], pending->msg, msize);
775 peer->pending_count--;
776 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
777 GNUNET_free (pending);
779 if (peer->head != NULL)
781 = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
783 GNUNET_TIME_absolute_get_remaining (pending->timeout),
785 &core_transmit_notify, peer);
791 * Transmit all messages in the peer's message queue.
793 * @param peer message queue to process
796 process_peer_queue (struct PeerInfo *peer)
798 struct P2PPendingMessage *pending;
800 if (NULL != (pending = peer->head))
802 if (NULL != peer->th)
805 = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
807 GNUNET_TIME_absolute_get_remaining (pending->timeout),
809 ntohs (pending->msg->size),
810 &core_transmit_notify, peer);
815 * To how many peers should we (on average) forward the request to
816 * obtain the desired target_replication count (on average).
818 * @param hop_count number of hops the message has traversed
819 * @param target_replication the number of total paths desired
820 * @return Some number of peers to forward the message to
823 get_forward_count (uint32_t hop_count,
824 uint32_t target_replication)
826 uint32_t random_value;
827 uint32_t forward_count;
830 if (hop_count > GDS_NSE_get () * 4.0)
832 /* forcefully terminate */
835 if (hop_count > GDS_NSE_get () * 2.0)
837 /* Once we have reached our ideal number of hops, only forward to 1 peer */
840 /* bound by system-wide maximum */
841 target_replication = GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL,
844 1 + (target_replication - 1.0) / (GDS_NSE_get () +
845 ((float) (target_replication - 1.0) *
847 /* Set forward count to floor of target_value */
848 forward_count = (uint32_t) target_value;
849 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
850 target_value = target_value - forward_count;
852 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
853 if (random_value < (target_value * UINT32_MAX))
855 return forward_count;
860 * Compute the distance between have and target as a 32-bit value.
861 * Differences in the lower bits must count stronger than differences
862 * in the higher bits.
864 * @return 0 if have==target, otherwise a number
865 * that is larger as the distance between
866 * the two hash codes increases
869 get_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
876 /* We have to represent the distance between two 2^9 (=512)-bit
877 * numbers as a 2^5 (=32)-bit number with "0" being used for the
878 * two numbers being identical; furthermore, we need to
879 * guarantee that a difference in the number of matching
880 * bits is always represented in the result.
882 * We use 2^32/2^9 numerical values to distinguish between
883 * hash codes that have the same LSB bit distance and
884 * use the highest 2^9 bits of the result to signify the
885 * number of (mis)matching LSB bits; if we have 0 matching
886 * and hence 512 mismatching LSB bits we return -1 (since
887 * 512 itself cannot be represented with 9 bits) */
889 /* first, calculate the most significant 9 bits of our
890 * result, aka the number of LSBs */
891 bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
892 /* bucket is now a value between 0 and 512 */
894 return 0; /* perfect match */
896 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
897 * below, we'd end up with max+1 (overflow)) */
899 /* calculate the most significant bits of the final result */
900 msb = (512 - bucket) << (32 - 9);
901 /* calculate the 32-9 least significant bits of the final result by
902 * looking at the differences in the 32-9 bits following the
903 * mismatching bit at 'bucket' */
906 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
908 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
909 GNUNET_CRYPTO_hash_get_bit (have, i))
910 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
911 * last bit set will be 31 -- if
912 * i does not reach 512 first... */
919 * Check whether my identity is closer than any known peers. If a
920 * non-null bloomfilter is given, check if this is the closest peer
921 * that hasn't already been routed to.
923 * @param key hash code to check closeness to
924 * @param bloom bloomfilter, exclude these entries from the decision
925 * @return GNUNET_YES if node location is closest,
926 * GNUNET_NO otherwise.
929 am_closest_peer (const GNUNET_HashCode *key,
930 const struct GNUNET_CONTAINER_BloomFilter *bloom)
936 struct PeerInfo *pos;
937 unsigned int my_distance;
939 if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
941 bucket_num = find_bucket (key);
942 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, key);
943 my_distance = get_distance (&my_identity.hashPubKey, key);
944 pos = k_buckets[bucket_num].head;
946 while ((pos != NULL) && (count < bucket_size))
948 if ((bloom != NULL) &&
950 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
953 continue; /* Skip already checked entries */
955 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, key);
956 if (other_bits > bits)
958 if (other_bits == bits) /* We match the same number of bits */
962 /* No peers closer, we are the closest! */
968 * Select a peer from the routing table that would be a good routing
969 * destination for sending a message for "key". The resulting peer
970 * must not be in the set of blocked peers.<p>
972 * Note that we should not ALWAYS select the closest peer to the
973 * target, peers further away from the target should be chosen with
974 * exponentially declining probability.
976 * FIXME: double-check that this is fine
979 * @param key the key we are selecting a peer to route to
980 * @param bloom a bloomfilter containing entries this request has seen already
981 * @param hops how many hops has this message traversed thus far
982 * @return Peer to route to, or NULL on error
984 static struct PeerInfo *
985 select_peer (const GNUNET_HashCode *key,
986 const struct GNUNET_CONTAINER_BloomFilter *bloom,
991 unsigned int selected;
992 struct PeerInfo *pos;
994 unsigned int smallest_distance;
995 struct PeerInfo *chosen;
997 if (hops >= GDS_NSE_get ())
999 /* greedy selection (closest peer that is not in bloomfilter) */
1000 smallest_distance = UINT_MAX;
1002 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1004 pos = k_buckets[bc].head;
1006 while ((pos != NULL) && (count < bucket_size))
1009 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
1011 dist = get_distance (key, &pos->id.hashPubKey);
1012 if (dist < smallest_distance)
1015 smallest_distance = dist;
1025 /* select "random" peer */
1026 /* count number of peers that are available and not filtered */
1028 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1030 pos = k_buckets[bc].head;
1031 while ((pos != NULL) && (count < bucket_size))
1034 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
1037 continue; /* Ignore bloomfiltered peers */
1043 if (count == 0) /* No peers to select from! */
1047 /* Now actually choose a peer */
1048 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1050 for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1052 pos = k_buckets[bc].head;
1053 while ((pos != NULL) && (count < bucket_size))
1056 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
1059 continue; /* Ignore bloomfiltered peers */
1061 if (0 == selected--)
1072 * Compute the set of peers that the given request should be
1075 * @param key routing key
1076 * @param bloom bloom filter excluding peers as targets, all selected
1077 * peers will be added to the bloom filter
1078 * @param hop_count number of hops the request has traversed so far
1079 * @param target_replication desired number of replicas
1080 * @param targets where to store an array of target peers (to be
1081 * free'd by the caller)
1082 * @return number of peers returned in 'targets'.
1085 get_target_peers (const GNUNET_HashCode *key,
1086 struct GNUNET_CONTAINER_BloomFilter *bloom,
1088 uint32_t target_replication,
1089 struct PeerInfo ***targets)
1093 struct PeerInfo **rtargets;
1094 struct PeerInfo *nxt;
1096 ret = get_forward_count (hop_count, target_replication);
1102 rtargets = GNUNET_malloc (sizeof (struct PeerInfo*) * ret);
1106 nxt = select_peer (key, bloom, hop_count);
1109 rtargets[off++] = nxt;
1110 GNUNET_CONTAINER_bloomfilter_add (bloom, &nxt->id.hashPubKey);
1114 GNUNET_free (rtargets);
1118 *targets = rtargets;
1124 * Perform a PUT operation. Forwards the given request to other
1125 * peers. Does not store the data locally. Does not give the
1126 * data to local clients. May do nothing if this is the only
1127 * peer in the network (or if we are the closest peer in the
1130 * @param type type of the block
1131 * @param options routing options
1132 * @param desired_replication_level desired replication count
1133 * @param expiration_time when does the content expire
1134 * @param hop_count how many hops has this message traversed so far
1135 * @param bf Bloom filter of peers this PUT has already traversed
1136 * @param key key for the content
1137 * @param put_path_length number of entries in put_path
1138 * @param put_path peers this request has traversed so far (if tracked)
1139 * @param data payload to store
1140 * @param data_size number of bytes in data
1143 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1144 enum GNUNET_DHT_RouteOption options,
1145 uint32_t desired_replication_level,
1146 struct GNUNET_TIME_Absolute expiration_time,
1148 struct GNUNET_CONTAINER_BloomFilter *bf,
1149 const GNUNET_HashCode *key,
1150 unsigned int put_path_length,
1151 struct GNUNET_PeerIdentity *put_path,
1155 unsigned int target_count;
1157 struct PeerInfo **targets;
1158 struct PeerInfo *target;
1159 struct P2PPendingMessage *pending;
1161 struct PeerPutMessage *ppm;
1162 struct GNUNET_PeerIdentity *pp;
1164 target_count = get_target_peers (key, bf, hop_count,
1165 desired_replication_level,
1167 if (0 == target_count)
1169 msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + sizeof (struct PeerPutMessage);
1170 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1172 put_path_length = 0;
1173 msize = data_size + sizeof (struct PeerPutMessage);
1175 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1180 for (i=0;i<target_count;i++)
1182 target = targets[i];
1183 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1184 pending->importance = 0; /* FIXME */
1185 pending->timeout = expiration_time;
1186 ppm = (struct PeerPutMessage*) &pending[1];
1187 pending->msg = &ppm->header;
1188 ppm->header.size = htons (msize);
1189 ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1190 ppm->options = htonl (options);
1191 ppm->type = htonl (type);
1192 ppm->hop_count = htonl (hop_count + 1);
1193 ppm->desired_replication_level = htonl (desired_replication_level);
1194 ppm->put_path_length = htonl (put_path_length);
1195 ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1196 GNUNET_assert (GNUNET_OK ==
1197 GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
1201 pp = (struct GNUNET_PeerIdentity*) &ppm[1];
1202 memcpy (pp, put_path, sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1203 memcpy (&pp[put_path_length], data, data_size);
1204 GNUNET_CONTAINER_DLL_insert_tail (target->head,
1207 target->pending_count++;
1208 process_peer_queue (target);
1210 GNUNET_free (targets);
1215 * Perform a GET operation. Forwards the given request to other
1216 * peers. Does not lookup the key locally. May do nothing if this is
1217 * the only peer in the network (or if we are the closest peer in the
1220 * @param type type of the block
1221 * @param options routing options
1222 * @param desired_replication_level desired replication count
1223 * @param hop_count how many hops did this request traverse so far?
1224 * @param key key for the content
1225 * @param xquery extended query
1226 * @param xquery_size number of bytes in xquery
1227 * @param reply_bf bloomfilter to filter duplicates
1228 * @param reply_bf_mutator mutator for reply_bf
1229 * @param peer_bf filter for peers not to select (again)
1232 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1233 enum GNUNET_DHT_RouteOption options,
1234 uint32_t desired_replication_level,
1236 const GNUNET_HashCode *key,
1239 const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
1240 uint32_t reply_bf_mutator,
1241 struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1243 unsigned int target_count;
1245 struct PeerInfo **targets;
1246 struct PeerInfo *target;
1247 struct P2PPendingMessage *pending;
1249 struct PeerGetMessage *pgm;
1251 size_t reply_bf_size;
1253 target_count = get_target_peers (key, peer_bf, hop_count,
1254 desired_replication_level,
1256 if (0 == target_count)
1258 reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
1259 msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
1260 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1265 /* forward request */
1266 for (i=0;i<target_count;i++)
1268 target = targets[i];
1269 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1270 pending->importance = 0; /* FIXME */
1271 pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1272 pgm = (struct PeerGetMessage*) &pending[1];
1273 pending->msg = &pgm->header;
1274 pgm->header.size = htons (msize);
1275 pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1276 pgm->options = htonl (options);
1277 pgm->type = htonl (type);
1278 pgm->hop_count = htonl (hop_count + 1);
1279 pgm->desired_replication_level = htonl (desired_replication_level);
1280 pgm->xquery_size = htonl (xquery_size);
1281 pgm->bf_mutator = reply_bf_mutator;
1282 GNUNET_assert (GNUNET_OK ==
1283 GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
1287 xq = (char *) &pgm[1];
1288 memcpy (xq, xquery, xquery_size);
1289 GNUNET_assert (GNUNET_OK ==
1290 GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
1293 GNUNET_CONTAINER_DLL_insert_tail (target->head,
1296 target->pending_count++;
1297 process_peer_queue (target);
1299 GNUNET_free (targets);
1304 * Handle a reply (route to origin). Only forwards the reply back to
1305 * the given peer. Does not do local caching or forwarding to local
1308 * @param target neighbour that should receive the block (if still connected)
1309 * @param type type of the block
1310 * @param expiration_time when does the content expire
1311 * @param key key for the content
1312 * @param put_path_length number of entries in put_path
1313 * @param put_path peers the original PUT traversed (if tracked)
1314 * @param get_path_length number of entries in put_path
1315 * @param get_path peers this reply has traversed so far (if tracked)
1316 * @param data payload of the reply
1317 * @param data_size number of bytes in data
1320 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1321 enum GNUNET_BLOCK_Type type,
1322 struct GNUNET_TIME_Absolute expiration_time,
1323 const GNUNET_HashCode *key,
1324 unsigned int put_path_length,
1325 const struct GNUNET_PeerIdentity *put_path,
1326 unsigned int get_path_length,
1327 const struct GNUNET_PeerIdentity *get_path,
1331 struct PeerInfo *pi;
1332 struct P2PPendingMessage *pending;
1334 struct PeerResultMessage *prm;
1335 struct GNUNET_PeerIdentity *paths;
1337 msize = data_size + sizeof (struct PeerResultMessage) +
1338 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
1339 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1340 (get_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1341 (put_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1342 (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE) )
1347 pi = GNUNET_CONTAINER_multihashmap_get (all_known_peers,
1348 &target->hashPubKey);
1351 /* peer disconnected in the meantime, drop reply */
1354 pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1355 pending->importance = 0; /* FIXME */
1356 pending->timeout = expiration_time;
1357 prm = (struct PeerResultMessage*) &pending[1];
1358 pending->msg = &prm->header;
1359 prm->header.size = htons (msize);
1360 prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1361 prm->type = htonl (type);
1362 prm->put_path_length = htonl (put_path_length);
1363 prm->get_path_length = htonl (get_path_length);
1364 prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1366 paths = (struct GNUNET_PeerIdentity*) &prm[1];
1367 memcpy (paths, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity));
1368 memcpy (&paths[put_path_length],
1369 get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
1370 memcpy (&paths[put_path_length + get_path_length],
1372 GNUNET_CONTAINER_DLL_insert (pi->head,
1375 pi->pending_count++;
1376 process_peer_queue (pi);
1381 * To be called on core init/fail.
1383 * @param cls service closure
1384 * @param server handle to the server for this service
1385 * @param identity the public identity of this peer
1386 * @param publicKey the public key of this peer
1389 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1390 const struct GNUNET_PeerIdentity *identity,
1391 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1393 GNUNET_assert (server != NULL);
1394 my_identity = *identity;
1399 * Core handler for p2p put requests.
1401 * @param cls closure
1402 * @param peer sender of the request
1403 * @param message message
1404 * @param peer peer identity this notification is about
1405 * @param atsi performance data
1406 * @return GNUNET_OK to keep the connection open,
1407 * GNUNET_SYSERR to close it (signal serious error)
1410 handle_dht_p2p_put (void *cls,
1411 const struct GNUNET_PeerIdentity *peer,
1412 const struct GNUNET_MessageHeader *message,
1413 const struct GNUNET_TRANSPORT_ATS_Information
1416 const struct PeerPutMessage *put;
1417 const struct GNUNET_PeerIdentity *put_path;
1418 const void *payload;
1421 size_t payload_size;
1422 enum GNUNET_DHT_RouteOption options;
1423 struct GNUNET_CONTAINER_BloomFilter *bf;
1424 GNUNET_HashCode test_key;
1426 msize = ntohs (message->size);
1427 if (msize < sizeof (struct PeerPutMessage))
1429 GNUNET_break_op (0);
1432 put = (const struct PeerPutMessage*) message;
1433 putlen = ntohl (put->put_path_length);
1434 if ( (msize < sizeof (struct PeerPutMessage) + putlen * sizeof (struct GNUNET_PeerIdentity)) ||
1435 (putlen > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) )
1437 GNUNET_break_op (0);
1440 put_path = (const struct GNUNET_PeerIdentity*) &put[1];
1441 payload = &put_path[putlen];
1442 options = ntohl (put->options);
1443 payload_size = msize - (sizeof (struct PeerPutMessage) +
1444 putlen * sizeof (struct GNUNET_PeerIdentity));
1445 switch (GNUNET_BLOCK_get_key (GDS_block_context,
1447 payload, payload_size,
1451 if (0 != memcmp (&test_key, &put->key, sizeof (GNUNET_HashCode)))
1453 GNUNET_break_op (0);
1458 GNUNET_break_op (0);
1461 /* cannot verify, good luck */
1464 bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
1468 struct GNUNET_PeerIdentity pp[putlen+1];
1470 /* extend 'put path' by sender */
1471 if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
1473 memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
1480 /* give to local clients */
1481 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1490 if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1491 (am_closest_peer (&put->key,
1493 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1499 /* route to other peers */
1500 GDS_NEIGHBOURS_handle_put (ntohl (put->type),
1502 ntohl (put->desired_replication_level),
1503 GNUNET_TIME_absolute_ntoh (put->expiration_time),
1504 ntohl (put->hop_count) + 1 /* who adds +1? */,
1511 GNUNET_CONTAINER_bloomfilter_free (bf);
1517 * We have received a FIND PEER request. Send matching
1520 * @param sender sender of the FIND PEER request
1521 * @param key peers close to this key are desired
1522 * @param bf peers matching this bf are excluded
1523 * @param bf_mutator mutator for bf
1526 handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1527 const GNUNET_HashCode *key,
1528 struct GNUNET_CONTAINER_BloomFilter *bf,
1529 uint32_t bf_mutator)
1532 struct PeerBucket *bucket;
1533 struct PeerInfo *peer;
1534 unsigned int choice;
1535 GNUNET_HashCode mhash;
1537 /* first, check about our own HELLO */
1538 if (NULL != GDS_my_hello)
1540 GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
1541 if (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))
1543 GDS_NEIGHBOURS_handle_reply (sender,
1544 GNUNET_BLOCK_TYPE_DHT_HELLO,
1545 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1550 GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*) GDS_my_hello));
1553 /* then, also consider sending a random HELLO from the closest bucket */
1554 bucket_idx = find_bucket (key);
1555 if (bucket_idx == GNUNET_SYSERR)
1557 bucket = &k_buckets[bucket_idx];
1558 if (bucket->peers_size == 0)
1560 choice = GNUNET_CRYPTO_random_u32 (bucket->peers_size,
1561 GNUNET_CRYPTO_QUALITY_WEAK);
1562 peer = bucket->head;
1565 GNUNET_assert (peer != NULL);
1568 choice = bucket->peers_size;
1573 return; /* no non-masked peer available */
1575 peer = bucket->head;
1576 GNUNET_BLOCK_mingle_hash (&peer->id.hashPubKey, bf_mutator, &mhash);
1578 while ( (peer->hello == NULL) ||
1579 (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) );
1580 GDS_NEIGHBOURS_handle_reply (sender,
1581 GNUNET_BLOCK_TYPE_DHT_HELLO,
1582 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1587 GNUNET_HELLO_size (peer->hello));
1592 * Core handler for p2p get requests.
1594 * @param cls closure
1595 * @param peer sender of the request
1596 * @param message message
1597 * @param peer peer identity this notification is about
1598 * @param atsi performance data
1599 * @return GNUNET_OK to keep the connection open,
1600 * GNUNET_SYSERR to close it (signal serious error)
1603 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
1604 const struct GNUNET_MessageHeader *message,
1605 const struct GNUNET_TRANSPORT_ATS_Information
1608 struct PeerGetMessage *get;
1609 uint32_t xquery_size;
1610 size_t reply_bf_size;
1612 enum GNUNET_BLOCK_Type type;
1613 enum GNUNET_DHT_RouteOption options;
1614 enum GNUNET_BLOCK_EvaluationResult eval;
1615 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
1616 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
1619 /* parse and validate message */
1620 msize = ntohs (message->size);
1621 if (msize < sizeof (struct PeerGetMessage))
1623 GNUNET_break_op (0);
1626 get = (struct PeerGetMessage *) message;
1627 xquery_size = ntohl (get->xquery_size);
1628 if (msize < sizeof (struct PeerGetMessage) + xquery_size)
1630 GNUNET_break_op (0);
1633 reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
1634 type = ntohl (get->type);
1635 options = ntohl (get->options);
1636 xquery = (const char*) &get[1];
1638 if (reply_bf_size > 0)
1639 reply_bf = GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size],
1641 GNUNET_DHT_GET_BLOOMFILTER_K);
1642 eval = GNUNET_BLOCK_evaluate (GDS_block_context,
1647 xquery, xquery_size,
1649 if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
1651 /* request invalid or block type not supported */
1652 GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
1653 if (NULL != reply_bf)
1654 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1658 GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter,
1662 /* remember request for routing replies */
1663 GDS_ROUTING_add (peer,
1667 xquery, xquery_size,
1668 reply_bf, get->bf_mutator);
1670 /* local lookup (this may update the reply_bf) */
1671 if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1672 (am_closest_peer (&get->key,
1675 if ( (0 != (options & GNUNET_DHT_RO_FIND_PEER)))
1677 handle_find_peer (peer,
1684 eval = GDS_DATACACHE_handle_get (&get->key,
1686 xquery, xquery_size,
1692 /* P2P forwarding */
1693 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1694 GDS_NEIGHBOURS_handle_get (type,
1696 ntohl (get->desired_replication_level),
1697 ntohl (get->hop_count) + 1, /* CHECK: where (else) do we do +1? */
1699 xquery, xquery_size,
1704 if (NULL != reply_bf)
1705 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1706 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
1712 * Core handler for p2p result messages.
1714 * @param cls closure
1715 * @param message message
1716 * @param peer peer identity this notification is about
1717 * @param atsi performance data
1718 * @return GNUNET_YES (do not cut p2p connection)
1721 handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
1722 const struct GNUNET_MessageHeader *message,
1723 const struct GNUNET_TRANSPORT_ATS_Information
1726 const struct PeerResultMessage *prm;
1727 const struct GNUNET_PeerIdentity *put_path;
1728 const struct GNUNET_PeerIdentity *get_path;
1730 uint32_t get_path_length;
1731 uint32_t put_path_length;
1734 enum GNUNET_BLOCK_Type type;
1736 /* parse and validate message */
1737 msize = ntohs (message->size);
1738 if (msize < sizeof (struct PeerResultMessage))
1740 GNUNET_break_op (0);
1743 prm = (struct PeerResultMessage *) message;
1744 put_path_length = ntohl (prm->put_path_length);
1745 get_path_length = ntohl (prm->get_path_length);
1746 if ( (msize < sizeof (struct PeerResultMessage) +
1747 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity)) ||
1748 (get_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1749 (put_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) )
1751 GNUNET_break_op (0);
1754 put_path = (const struct GNUNET_PeerIdentity*) &prm[1];
1755 get_path = &put_path[put_path_length];
1756 type = ntohl (prm->type);
1757 data = (const void*) &get_path[get_path_length];
1758 data_size = msize - (sizeof (struct PeerResultMessage) +
1759 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity));
1761 /* if we got a HELLO, consider it for our own routing table */
1762 if (type == GNUNET_BLOCK_TYPE_DHT_HELLO)
1764 const struct GNUNET_MessageHeader *h;
1765 struct GNUNET_PeerIdentity pid;
1768 /* Should be a HELLO, validate and consider using it! */
1769 if (data_size < sizeof (struct GNUNET_MessageHeader))
1771 GNUNET_break_op (0);
1775 if (data_size != ntohs (h->size))
1777 GNUNET_break_op (0);
1781 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) h,
1784 GNUNET_break_op (0);
1787 bucket = find_bucket (&pid.hashPubKey);
1788 if ( (bucket >= 0) &&
1789 (k_buckets[bucket].peers_size < bucket_size) )
1791 if (NULL != GDS_transport_handle)
1792 GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
1794 (void) GNUNET_CORE_peer_request_connect (coreAPI,
1800 /* append 'peer' to 'get_path' */
1802 struct GNUNET_PeerIdentity xget_path[get_path_length+1];
1804 memcpy (xget_path, get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
1805 xget_path[get_path_length] = *peer;
1808 /* forward to local clients */
1809 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1819 /* forward to other peers */
1820 GDS_ROUTING_process (type,
1821 GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1835 * Function called for each HELLO known to PEERINFO.
1837 * @param cls closure
1838 * @param peer id of the peer, NULL for last call
1839 * @param hello hello message for the peer (can be NULL)
1840 * @param error message
1843 process_hello (void *cls,
1844 const struct GNUNET_PeerIdentity *
1846 const struct GNUNET_HELLO_Message *
1847 hello, const char *err_msg)
1849 // FIXME: consider moving HELLO processing to another file!
1850 // FIXME: first, filter HELLOs without addresses (!)
1851 // FIXME: track HELLOs (for responding to FIND PEER requests)
1852 // FIXME: add code to possibly ask core to establish connections
1853 // (using our own peerinfo is better than using FIND PEER!)
1858 * Initialize neighbours subsystem.
1860 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1863 GDS_NEIGHBOURS_init ()
1865 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1866 {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
1867 {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
1868 {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0},
1871 unsigned long long temp_config_num;
1874 GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size",
1876 bucket_size = (unsigned int) temp_config_num;
1877 coreAPI = GNUNET_CORE_connect (GDS_cfg,
1881 &handle_core_connect,
1882 &handle_core_disconnect,
1883 NULL, /* Do we care about "status" updates? */
1887 if (coreAPI == NULL)
1888 return GNUNET_SYSERR;
1889 all_known_peers = GNUNET_CONTAINER_multihashmap_create (256);
1890 pnc = GNUNET_PEERINFO_notify (GDS_cfg,
1898 * Shutdown neighbours subsystem.
1901 GDS_NEIGHBOURS_done ()
1903 if (coreAPI == NULL)
1907 GNUNET_PEERINFO_notify_cancel (pnc);
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 */