2 This file is part of GNUnet.
3 (C) 2009, 2010 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.c
23 * @brief main DHT service shell, building block for DHT implementations
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_block_lib.h"
30 #include "gnunet_client_lib.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_core_service.h"
36 #include "gnunet_signal_lib.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_datacache_lib.h"
39 #include "gnunet_transport_service.h"
40 #include "gnunet_hello_lib.h"
41 #include "gnunet_dht_service.h"
42 #include "gnunet_statistics_service.h"
47 #define PRINT_TABLES GNUNET_NO
49 #define REAL_DISTANCE GNUNET_NO
51 #define EXTRA_CHECKS GNUNET_NO
54 * How many buckets will we allow total.
56 #define MAX_BUCKETS sizeof (GNUNET_HashCode) * 8
59 * Should the DHT issue FIND_PEER requests to get better routing tables?
61 #define DEFAULT_DO_FIND_PEER GNUNET_YES
64 * Defines whether find peer requests send their HELLO's outgoing,
65 * or expect replies to contain hellos.
67 #define FIND_PEER_WITH_HELLO GNUNET_YES
70 * What is the maximum number of peers in a given bucket.
72 #define DEFAULT_BUCKET_SIZE 4
75 * Minimum number of peers we need for "good" routing,
76 * any less than this and we will allow messages to
77 * travel much further through the network!
79 #define MINIMUM_PEER_THRESHOLD 20
81 #define DHT_MAX_RECENT 1000
83 #define FIND_PEER_CALC_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
86 * Default time to wait to send messages on behalf of other peers.
88 #define DHT_DEFAULT_P2P_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
91 * Default importance for handling messages on behalf of other peers.
93 #define DHT_DEFAULT_P2P_IMPORTANCE 0
96 * How long to keep recent requests around by default.
98 #define DEFAULT_RECENT_REMOVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
101 * Default time to wait to send find peer messages sent by the dht service.
103 #define DHT_DEFAULT_FIND_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
106 * Default importance for find peer messages sent by the dht service.
108 #define DHT_DEFAULT_FIND_PEER_IMPORTANCE 8
111 * Default replication parameter for find peer messages sent by the dht service.
113 #define DHT_DEFAULT_PUT_REPLICATION 4
116 * Default replication parameter for find peer messages sent by the dht service.
118 #define DHT_DEFAULT_FIND_PEER_REPLICATION 4
121 * Default options for find peer requests sent by the dht service.
123 #define DHT_DEFAULT_FIND_PEER_OPTIONS GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
124 /*#define DHT_DEFAULT_FIND_PEER_OPTIONS GNUNET_DHT_RO_NONE*/
127 * How long at least to wait before sending another find peer request.
129 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
132 * How long at most to wait before sending another find peer request.
134 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
137 * How often to update our preference levels for peers in our routing tables.
139 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
142 * How long at most on average will we allow a reply forward to take
143 * (before we quit sending out new requests)
145 #define MAX_REQUEST_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
148 * How many initial requests to send out (in true Kademlia fashion)
150 #define DHT_KADEMLIA_REPLICATION 3
153 * Default frequency for sending malicious get messages
155 #define DEFAULT_MALICIOUS_GET_FREQUENCY 1000 /* Number of milliseconds */
158 * Default frequency for sending malicious put messages
160 #define DEFAULT_MALICIOUS_PUT_FREQUENCY 1000 /* Default is in milliseconds */
163 * Type for a malicious request, so we can ignore it during testing
165 #define DHT_MALICIOUS_MESSAGE_TYPE 42
167 #define DHT_DEFAULT_PING_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1)
170 * Real maximum number of hops, at which point we refuse
171 * to forward the message.
173 #define DEFAULT_MAX_HOPS 10
176 * How many time differences between requesting a core send and
177 * the actual callback to remember.
179 #define MAX_REPLY_TIMES 8
181 enum ConvergenceOptions
184 * Use the linear method for convergence.
189 * Converge using a fast converging square
195 * Converge using a slower exponential
198 DHT_CONVERGE_EXPONENTIAL,
201 * Don't do any special convergence, allow
202 * the algorithm to hopefully route to closer
209 * Linked list of messages to send to clients.
211 struct P2PPendingMessage
214 * Pointer to next item in the list
216 struct P2PPendingMessage *next;
219 * Pointer to previous item in the list
221 struct P2PPendingMessage *prev;
224 * Message importance level.
226 unsigned int importance;
229 * Time when this request was scheduled to be sent.
231 struct GNUNET_TIME_Absolute scheduled;
234 * How long to wait before sending message.
236 struct GNUNET_TIME_Relative timeout;
239 * Actual message to be sent; // avoid allocation
241 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
246 * Per-peer information.
251 * Next peer entry (DLL)
253 struct PeerInfo *next;
256 * Prev peer entry (DLL)
258 struct PeerInfo *prev;
261 * Head of pending messages to be sent to this peer.
263 struct P2PPendingMessage *head;
266 * Tail of pending messages to be sent to this peer.
268 struct P2PPendingMessage *tail;
271 * Core handle for sending messages to this peer.
273 struct GNUNET_CORE_TransmitHandle *th;
276 * Task for scheduling message sends.
278 GNUNET_SCHEDULER_TaskIdentifier send_task;
281 * Task for scheduling preference updates
283 GNUNET_SCHEDULER_TaskIdentifier preference_task;
286 * Preference update context
288 struct GNUNET_CORE_InformationRequestContext *info_ctx;
291 * What is the average latency for replies received?
293 struct GNUNET_TIME_Relative latency;
296 * What is the identity of the peer?
298 struct GNUNET_PeerIdentity id;
301 * Transport level distance to peer.
303 unsigned int distance;
306 * Holds matching bits from peer to current target,
307 * used for distance comparisons between peers. May
308 * be considered a really bad idea.
309 * FIXME: remove this value (create struct which holds
310 * a single peerinfo and the matching bits, use
311 * that to pass to comparitor)
313 unsigned int matching_bits;
316 * Task for scheduling periodic ping messages for this peer.
318 GNUNET_SCHEDULER_TaskIdentifier ping_task;
322 * Peers are grouped into buckets.
329 struct PeerInfo *head;
334 struct PeerInfo *tail;
337 * Number of peers in the bucket.
339 unsigned int peers_size;
343 * Linked list of messages to send to clients.
345 struct PendingMessage
348 * Pointer to next item in the list
350 struct PendingMessage *next;
353 * Pointer to previous item in the list
355 struct PendingMessage *prev;
358 * Actual message to be sent; // avoid allocation
360 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
365 * Struct containing information about a client,
366 * handle to connect to it, and any pending messages
367 * that need to be sent to it.
372 * Linked list of active clients
374 struct ClientList *next;
377 * The handle to this client
379 struct GNUNET_SERVER_Client *client_handle;
382 * Handle to the current transmission request, NULL
385 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
388 * Linked list of pending messages for this client
390 struct PendingMessage *pending_head;
393 * Tail of linked list of pending messages for this client
395 struct PendingMessage *pending_tail;
400 * Context containing information about a DHT message received.
402 struct DHT_MessageContext
405 * The client this request was received from.
406 * (NULL if received from another peer)
408 struct ClientList *client;
411 * The peer this request was received from.
412 * (NULL if received from local client)
414 const struct GNUNET_PeerIdentity *peer;
417 * The key this request was about
422 * The unique identifier of this request
427 * Desired replication level
429 uint32_t replication;
432 * Network size estimate, either ours or the sum of
433 * those routed to thus far. =~ Log of number of peers
434 * chosen from for this request.
436 uint32_t network_size;
439 * Any message options for this request
441 uint32_t msg_options;
444 * How many hops has the message already traversed?
449 * How important is this message?
451 unsigned int importance;
454 * How long should we wait to transmit this request?
456 struct GNUNET_TIME_Relative timeout;
459 * Bloomfilter for this routing request.
461 struct GNUNET_CONTAINER_BloomFilter *bloom;
464 * Did we forward this message? (may need to remember it!)
469 * Are we the closest known peer to this key (out of our neighbors?)
475 * Record used for remembering what peers are waiting for what
476 * responses (based on search key).
478 struct DHTRouteSource
483 struct DHTRouteSource *next;
488 struct DHTRouteSource *prev;
491 * Source of the request. Replies should be forwarded to
494 struct GNUNET_PeerIdentity source;
497 * If this was a local request, remember the client; otherwise NULL.
499 struct ClientList *client;
502 * Pointer to this nodes heap location (for removal)
504 struct GNUNET_CONTAINER_HeapNode *hnode;
507 * Back pointer to the record storing this information.
509 struct DHTQueryRecord *record;
512 * Task to remove this entry on timeout.
514 GNUNET_SCHEDULER_TaskIdentifier delete_task;
517 * Bloomfilter of peers we have already sent back as
518 * replies to the initial request. Allows us to not
519 * forward the same peer multiple times for a find peer
522 struct GNUNET_CONTAINER_BloomFilter *find_peers_responded;
527 * Entry in the DHT routing table.
529 struct DHTQueryRecord
532 * Head of DLL for result forwarding.
534 struct DHTRouteSource *head;
537 * Tail of DLL for result forwarding.
539 struct DHTRouteSource *tail;
542 * Key that the record concerns.
547 * GET message of this record (what we already forwarded?).
549 //DV_DHT_MESSAGE get; Try to get away with not saving this.
552 * Bloomfilter of the peers we've replied to so far
554 //struct GNUNET_BloomFilter *bloom_results; Don't think we need this, just remove from DLL on response.
559 * Context used to calculate the number of find peer messages
560 * per X time units since our last scheduled find peer message
561 * was sent. If we have seen too many messages, delay or don't
564 struct FindPeerMessageContext
568 struct GNUNET_TIME_Absolute start;
570 struct GNUNET_TIME_Absolute end;
574 * DHT Routing results structure
579 * Min heap for removal upon reaching limit
581 struct GNUNET_CONTAINER_Heap *minHeap;
584 * Hashmap for fast key based lookup
586 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
591 * DHT structure for recent requests.
593 struct RecentRequests
596 * Min heap for removal upon reaching limit
598 struct GNUNET_CONTAINER_Heap *minHeap;
601 * Hashmap for key based lookup
603 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
609 * Position of this node in the min heap.
611 struct GNUNET_CONTAINER_HeapNode *heap_node;
614 * Bloomfilter containing entries for peers
615 * we forwarded this request to.
617 struct GNUNET_CONTAINER_BloomFilter *bloom;
620 * Timestamp of this request, for ordering
623 struct GNUNET_TIME_Absolute timestamp;
626 * Key of this request.
631 * Unique identifier for this request.
636 * Task to remove this entry on timeout.
638 GNUNET_SCHEDULER_TaskIdentifier remove_task;
641 struct RepublishContext
656 * Which kind of convergence will we be using?
658 static enum ConvergenceOptions converge_option;
661 * Modifier for the convergence function
663 static float converge_modifier;
666 * Recent requests by hash/uid and by time inserted.
668 static struct RecentRequests recent;
671 * Context to use to calculate find peer rates.
673 static struct FindPeerMessageContext find_peer_context;
676 * Don't use our routing algorithm, always route
677 * to closest peer; initially send requests to 3
680 static unsigned int strict_kademlia;
683 * Routing option to end routing when closest peer found.
685 static unsigned int stop_on_closest;
688 * Routing option to end routing when data is found.
690 static unsigned int stop_on_found;
693 * Whether DHT needs to manage find peer requests, or
694 * an external force will do it on behalf of the DHT.
696 static unsigned int do_find_peer;
699 * Once we have stored an item in the DHT, refresh it
700 * according to our republish interval.
702 static unsigned int do_republish;
705 * Use the "real" distance metric when selecting the
706 * next routing hop. Can be less accurate.
708 static unsigned int use_real_distance;
711 * How many peers have we added since we sent out our last
714 static unsigned int newly_found_peers;
717 * Container of active queries we should remember
719 static struct DHTResults forward_list;
722 * Handle to the datacache service (for inserting/retrieving data)
724 static struct GNUNET_DATACACHE_Handle *datacache;
727 * Handle for the statistics service.
729 struct GNUNET_STATISTICS_Handle *stats;
732 * The main scheduler to use for the DHT service
734 static struct GNUNET_SCHEDULER_Handle *sched;
737 * The configuration the DHT service is running with
739 static const struct GNUNET_CONFIGURATION_Handle *cfg;
742 * Handle to the core service
744 static struct GNUNET_CORE_Handle *coreAPI;
747 * Handle to the transport service, for getting our hello
749 static struct GNUNET_TRANSPORT_Handle *transport_handle;
752 * The identity of our peer.
754 static struct GNUNET_PeerIdentity my_identity;
757 * Short id of the peer, for printing
759 static char *my_short_id;
764 static struct GNUNET_MessageHeader *my_hello;
767 * Task to run when we shut down, cleaning up all our trash
769 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
772 * The lowest currently used bucket.
774 static unsigned int lowest_bucket; /* Initially equal to MAX_BUCKETS - 1 */
777 * The maximum number of hops before we stop routing messages.
779 static unsigned long long max_hops;
782 * How often to republish content we have previously stored.
784 static struct GNUNET_TIME_Relative dht_republish_frequency;
787 * GNUNET_YES to stop at max_hops, GNUNET_NO to heuristically decide when to stop forwarding.
789 static int use_max_hops;
792 * The buckets (Kademlia routing table, complete with growth).
793 * Array of size MAX_BUCKET_SIZE.
795 static struct PeerBucket k_buckets[MAX_BUCKETS]; /* From 0 to MAX_BUCKETS - 1 */
798 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
800 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
803 * Recently seen find peer requests.
805 static struct GNUNET_CONTAINER_MultiHashMap *recent_find_peer_requests;
808 * Maximum size for each bucket.
810 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE; /* Initially equal to DEFAULT_BUCKET_SIZE */
813 * List of active clients.
815 static struct ClientList *client_list;
818 * Handle to the DHT logger.
820 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
823 * Whether or not to send routing debugging information
824 * to the dht logging server
826 static unsigned int debug_routes;
829 * Whether or not to send FULL route information to
832 static unsigned int debug_routes_extended;
835 * GNUNET_YES or GNUNET_NO, whether or not to act as
836 * a malicious node which drops all messages
838 static unsigned int malicious_dropper;
841 * GNUNET_YES or GNUNET_NO, whether or not to act as
842 * a malicious node which sends out lots of GETS
844 static unsigned int malicious_getter;
847 * GNUNET_YES or GNUNET_NO, whether or not to act as
848 * a malicious node which sends out lots of PUTS
850 static unsigned int malicious_putter;
853 * Frequency for malicious get requests.
855 static unsigned long long malicious_get_frequency;
858 * Frequency for malicious put requests.
860 static unsigned long long malicious_put_frequency;
863 * Reply times for requests, if we are busy, don't send any
866 static struct GNUNET_TIME_Relative reply_times[MAX_REPLY_TIMES];
869 * Current counter for replies.
871 static unsigned int reply_counter;
874 * Our handle to the BLOCK library.
876 static struct GNUNET_BLOCK_Context *block_context;
880 * Forward declaration.
883 send_generic_reply (void *cls, size_t size, void *buf);
886 /** Declare here so retry_core_send is aware of it */
888 core_transmit_notify (void *cls,
889 size_t size, void *buf);
892 * Convert unique ID to hash code.
894 * @param uid unique ID to convert
895 * @param hash set to uid (extended with zeros)
898 hash_from_uid (uint64_t uid,
899 GNUNET_HashCode *hash)
901 memset (hash, 0, sizeof(GNUNET_HashCode));
902 *((uint64_t*)hash) = uid;
907 * Calculate the average send time between messages so that we can
908 * ignore certain requests if we get too busy.
910 * @return the average time between asking core to send a message
911 * and when the buffer for copying it is passed
913 static struct GNUNET_TIME_Relative get_average_send_delay()
916 unsigned int divisor;
917 struct GNUNET_TIME_Relative average_time;
918 average_time = GNUNET_TIME_relative_get_zero();
920 for (i = 0; i < MAX_REPLY_TIMES; i++)
922 average_time = GNUNET_TIME_relative_add(average_time, reply_times[i]);
923 if (reply_times[i].value == (uint64_t)0)
933 average_time = GNUNET_TIME_relative_divide(average_time, divisor);
934 fprintf(stderr, "Avg send delay: %u sends is %llu\n", divisor, (long long unsigned int)average_time.value);
940 * Given the largest send delay, artificially decrease it
941 * so the next time around we may have a chance at sending
944 static void decrease_max_send_delay(struct GNUNET_TIME_Relative max_time)
947 for (i = 0; i < MAX_REPLY_TIMES; i++)
949 if (reply_times[i].value == max_time.value)
951 reply_times[i].value = reply_times[i].value / 2;
958 * Find the maximum send time of the recently sent values.
960 * @return the average time between asking core to send a message
961 * and when the buffer for copying it is passed
963 static struct GNUNET_TIME_Relative get_max_send_delay()
966 struct GNUNET_TIME_Relative max_time;
967 max_time = GNUNET_TIME_relative_get_zero();
969 for (i = 0; i < MAX_REPLY_TIMES; i++)
971 if (reply_times[i].value > max_time.value)
972 max_time.value = reply_times[i].value;
975 if (max_time.value > MAX_REQUEST_TIME.value)
976 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n", (long long unsigned int)max_time.value);
981 increment_stats(const char *value)
985 GNUNET_STATISTICS_update (stats, value, 1, GNUNET_NO);
990 * Try to send another message from our core send list
993 try_core_send (void *cls,
994 const struct GNUNET_SCHEDULER_TaskContext *tc)
996 struct PeerInfo *peer = cls;
997 struct P2PPendingMessage *pending;
1000 peer->send_task = GNUNET_SCHEDULER_NO_TASK;
1002 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1005 if (peer->th != NULL)
1006 return; /* Message send already in progress */
1008 pending = peer->head;
1009 if (pending != NULL)
1011 ssize = ntohs(pending->msg->size);
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n", my_short_id,
1015 "DHT", ssize, GNUNET_i2s(&peer->id));
1017 pending->scheduled = GNUNET_TIME_absolute_get();
1019 if (reply_counter >= MAX_REPLY_TIMES)
1021 peer->th = GNUNET_CORE_notify_transmit_ready(coreAPI, pending->importance,
1022 pending->timeout, &peer->id,
1023 ssize, &core_transmit_notify, peer);
1028 * Function called to send a request out to another peer.
1029 * Called both for locally initiated requests and those
1030 * received from other peers.
1032 * @param cls DHT service closure argument
1033 * @param msg the encapsulated message
1034 * @param peer the peer to forward the message to
1035 * @param msg_ctx the context of the message (hop count, bloom, etc.)
1038 forward_result_message (void *cls,
1039 const struct GNUNET_MessageHeader *msg,
1040 struct PeerInfo *peer,
1041 struct DHT_MessageContext *msg_ctx)
1043 struct GNUNET_DHT_P2PRouteResultMessage *result_message;
1044 struct P2PPendingMessage *pending;
1048 increment_stats(STAT_RESULT_FORWARDS);
1049 msize = sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs(msg->size);
1050 GNUNET_assert(msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1051 psize = sizeof(struct P2PPendingMessage) + msize;
1052 pending = GNUNET_malloc(psize);
1053 pending->msg = (struct GNUNET_MessageHeader *)&pending[1];
1054 pending->importance = DHT_SEND_PRIORITY;
1055 pending->timeout = GNUNET_TIME_relative_get_forever();
1056 result_message = (struct GNUNET_DHT_P2PRouteResultMessage *)pending->msg;
1057 result_message->header.size = htons(msize);
1058 result_message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT);
1059 result_message->put_path_length = htons(0); /* FIXME: implement */
1060 result_message->get_path_length = htons(0); /* FIXME: implement */
1061 result_message->options = htonl(msg_ctx->msg_options);
1062 result_message->hop_count = htonl(msg_ctx->hop_count + 1);
1063 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(msg_ctx->bloom, result_message->bloomfilter, DHT_BLOOM_SIZE));
1064 result_message->unique_id = GNUNET_htonll(msg_ctx->unique_id);
1065 memcpy(&result_message->key, &msg_ctx->key, sizeof(GNUNET_HashCode));
1066 memcpy(&result_message[1], msg, ntohs(msg->size));
1068 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Adding pending message size %d for peer %s\n", my_short_id, "DHT", msize, GNUNET_i2s(&peer->id));
1070 GNUNET_CONTAINER_DLL_insert_after(peer->head, peer->tail, peer->tail, pending);
1071 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1072 peer->send_task = GNUNET_SCHEDULER_add_now(sched, &try_core_send, peer);
1077 * Called when core is ready to send a message we asked for
1078 * out to the destination.
1080 * @param cls closure (NULL)
1081 * @param size number of bytes available in buf
1082 * @param buf where the callee should write the message
1083 * @return number of bytes written to buf
1086 core_transmit_notify (void *cls,
1087 size_t size, void *buf)
1089 struct PeerInfo *peer = cls;
1091 struct P2PPendingMessage *pending;
1098 /* client disconnected */
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n", my_short_id, "DHT");
1105 if (peer->head == NULL)
1110 pending = peer->head;
1111 reply_times[reply_counter] = GNUNET_TIME_absolute_get_difference(pending->scheduled, GNUNET_TIME_absolute_get());
1112 msize = ntohs(pending->msg->size);
1116 memcpy (cbuf, pending->msg, msize);
1117 GNUNET_CONTAINER_DLL_remove (peer->head,
1121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Removing pending message size %d for peer %s\n", my_short_id, "DHT", msize, GNUNET_i2s(&peer->id));
1123 GNUNET_free (pending);
1126 while (NULL != pending &&
1127 (size - off >= (msize = ntohs (pending->msg->size))))
1129 #if DEBUG_DHT_ROUTING
1130 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s:%s' : transmit_notify (core) called with size %d, available %d\n", my_short_id, "dht service", msize, size);
1132 memcpy (&cbuf[off], pending->msg, msize);
1134 GNUNET_CONTAINER_DLL_remove (peer->head,
1137 GNUNET_free (pending);
1138 pending = peer->head;
1141 if ((peer->head != NULL) && (peer->send_task == GNUNET_SCHEDULER_NO_TASK))
1142 peer->send_task = GNUNET_SCHEDULER_add_now(sched, &try_core_send, peer);
1144 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s:%s' : transmit_notify (core) called with size %d, available %d, returning %d\n", my_short_id, "dht service", msize, size, off);
1151 * Compute the distance between have and target as a 32-bit value.
1152 * Differences in the lower bits must count stronger than differences
1153 * in the higher bits.
1155 * @return 0 if have==target, otherwise a number
1156 * that is larger as the distance between
1157 * the two hash codes increases
1160 distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
1162 unsigned int bucket;
1167 /* We have to represent the distance between two 2^9 (=512)-bit
1168 numbers as a 2^5 (=32)-bit number with "0" being used for the
1169 two numbers being identical; furthermore, we need to
1170 guarantee that a difference in the number of matching
1171 bits is always represented in the result.
1173 We use 2^32/2^9 numerical values to distinguish between
1174 hash codes that have the same LSB bit distance and
1175 use the highest 2^9 bits of the result to signify the
1176 number of (mis)matching LSB bits; if we have 0 matching
1177 and hence 512 mismatching LSB bits we return -1 (since
1178 512 itself cannot be represented with 9 bits) */
1180 /* first, calculate the most significant 9 bits of our
1181 result, aka the number of LSBs */
1182 bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
1183 /* bucket is now a value between 0 and 512 */
1185 return 0; /* perfect match */
1187 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
1188 below, we'd end up with max+1 (overflow)) */
1190 /* calculate the most significant bits of the final result */
1191 msb = (512 - bucket) << (32 - 9);
1192 /* calculate the 32-9 least significant bits of the final result by
1193 looking at the differences in the 32-9 bits following the
1194 mismatching bit at 'bucket' */
1196 for (i = bucket + 1;
1197 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
1199 if (GNUNET_CRYPTO_hash_get_bit (target, i) != GNUNET_CRYPTO_hash_get_bit (have, i))
1200 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
1201 last bit set will be 31 -- if
1202 i does not reach 512 first... */
1208 * Return a number that is larger the closer the
1209 * "have" GNUNET_hash code is to the "target".
1211 * @return inverse distance metric, non-zero.
1212 * Must fudge the value if NO bits match.
1215 inverse_distance (const GNUNET_HashCode * target,
1216 const GNUNET_HashCode * have)
1218 if (GNUNET_CRYPTO_hash_matching_bits(target, have) == 0)
1219 return 1; /* Never return 0! */
1220 return ((unsigned int) -1) - distance (target, have);
1224 * Find the optimal bucket for this key, regardless
1225 * of the current number of buckets in use.
1227 * @param hc the hashcode to compare our identity to
1229 * @return the proper bucket index, or GNUNET_SYSERR
1230 * on error (same hashcode)
1232 static int find_bucket(const GNUNET_HashCode *hc)
1236 bits = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, hc);
1237 if (bits == MAX_BUCKETS)
1238 return GNUNET_SYSERR;
1239 return MAX_BUCKETS - bits - 1;
1243 * Find which k-bucket this peer should go into,
1244 * taking into account the size of the k-bucket
1245 * array. This means that if more bits match than
1246 * there are currently buckets, lowest_bucket will
1249 * @param hc GNUNET_HashCode we are finding the bucket for.
1251 * @return the proper bucket index for this key,
1252 * or GNUNET_SYSERR on error (same hashcode)
1254 static int find_current_bucket(const GNUNET_HashCode *hc)
1257 actual_bucket = find_bucket(hc);
1259 if (actual_bucket == GNUNET_SYSERR) /* hc and our peer identity match! */
1260 return lowest_bucket;
1261 else if (actual_bucket < lowest_bucket) /* actual_bucket not yet used */
1262 return lowest_bucket;
1264 return actual_bucket;
1269 * Find a routing table entry from a peer identity
1271 * @param peer the peer to look up
1273 * @return the bucket number holding the peer, GNUNET_SYSERR if not found
1276 find_bucket_by_peer(const struct PeerInfo *peer)
1279 struct PeerInfo *pos;
1281 for (bucket = lowest_bucket; bucket < MAX_BUCKETS - 1; bucket++)
1283 pos = k_buckets[bucket].head;
1292 return GNUNET_SYSERR; /* No such peer. */
1298 * Print the complete routing table for this peer.
1301 print_routing_table ()
1304 struct PeerInfo *pos;
1305 char char_buf[30000];
1307 memset(char_buf, 0, sizeof(char_buf));
1309 char_pos += sprintf(&char_buf[char_pos], "Printing routing table for peer %s\n", my_short_id);
1310 //fprintf(stderr, "Printing routing table for peer %s\n", my_short_id);
1311 for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
1313 pos = k_buckets[bucket].head;
1314 char_pos += sprintf(&char_buf[char_pos], "Bucket %d:\n", bucket);
1315 //fprintf(stderr, "Bucket %d:\n", bucket);
1318 //fprintf(stderr, "\tPeer %s, best bucket %d, %d bits match\n", GNUNET_i2s(&pos->id), find_bucket(&pos->id.hashPubKey), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey));
1319 char_pos += sprintf(&char_buf[char_pos], "\tPeer %s, best bucket %d, %d bits match\n", GNUNET_i2s(&pos->id), find_bucket(&pos->id.hashPubKey), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey));
1323 fprintf(stderr, "%s", char_buf);
1329 * Find a routing table entry from a peer identity
1331 * @param peer the peer identity to look up
1333 * @return the routing table entry, or NULL if not found
1335 static struct PeerInfo *
1336 find_peer_by_id(const struct GNUNET_PeerIdentity *peer)
1339 struct PeerInfo *pos;
1340 bucket = find_current_bucket(&peer->hashPubKey);
1342 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
1345 pos = k_buckets[bucket].head;
1348 if (0 == memcmp(&pos->id, peer, sizeof(struct GNUNET_PeerIdentity)))
1352 return NULL; /* No such peer. */
1355 /* Forward declaration */
1357 update_core_preference (void *cls,
1358 const struct GNUNET_SCHEDULER_TaskContext *tc);
1360 * Function called with statistics about the given peer.
1362 * @param cls closure
1363 * @param peer identifies the peer
1364 * @param bpm_in set to the current bandwidth limit (receiving) for this peer
1365 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1366 * @param amount set to the amount that was actually reserved or unreserved;
1367 * either the full requested amount or zero (no partial reservations)
1368 * @param preference current traffic preference for the given peer
1371 update_core_preference_finish (void *cls,
1372 const struct GNUNET_PeerIdentity * peer,
1373 struct GNUNET_BANDWIDTH_Value32NBO bpm_in,
1374 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1375 int amount, uint64_t preference)
1377 struct PeerInfo *peer_info = cls;
1378 peer_info->info_ctx = NULL;
1379 GNUNET_SCHEDULER_add_delayed(sched, DHT_DEFAULT_PREFERENCE_INTERVAL, &update_core_preference, peer_info);
1383 update_core_preference (void *cls,
1384 const struct GNUNET_SCHEDULER_TaskContext *tc)
1386 struct PeerInfo *peer = cls;
1387 uint64_t preference;
1388 unsigned int matching;
1389 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1393 matching = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &peer->id.hashPubKey);
1396 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer identifier matches by %u bits, only shifting as much as we can!\n", matching);
1399 preference = 1LL << matching;
1400 peer->info_ctx = GNUNET_CORE_peer_change_preference (sched, cfg,
1402 GNUNET_TIME_relative_get_forever(),
1403 GNUNET_BANDWIDTH_value_init (UINT32_MAX),
1406 &update_core_preference_finish,
1411 * Really add a peer to a bucket (only do assertions
1414 * @param peer GNUNET_PeerIdentity of the peer to add
1415 * @param bucket the already figured out bucket to add
1417 * @param latency the core reported latency of this peer
1418 * @param distance the transport level distance to this peer
1420 * @return the newly added PeerInfo
1422 static struct PeerInfo *
1423 add_peer(const struct GNUNET_PeerIdentity *peer,
1424 unsigned int bucket,
1425 struct GNUNET_TIME_Relative latency,
1426 unsigned int distance)
1428 struct PeerInfo *new_peer;
1429 GNUNET_assert(bucket < MAX_BUCKETS);
1430 GNUNET_assert(peer != NULL);
1431 new_peer = GNUNET_malloc(sizeof(struct PeerInfo));
1432 new_peer->latency = latency;
1433 new_peer->distance = distance;
1435 memcpy(&new_peer->id, peer, sizeof(struct GNUNET_PeerIdentity));
1437 GNUNET_CONTAINER_DLL_insert_after(k_buckets[bucket].head,
1438 k_buckets[bucket].tail,
1439 k_buckets[bucket].tail,
1441 k_buckets[bucket].peers_size++;
1443 if ((GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &peer->hashPubKey) > 0) && (k_buckets[bucket].peers_size <= bucket_size))
1445 #if DO_UPDATE_PREFERENCE
1446 new_peer->preference_task = GNUNET_SCHEDULER_add_now(sched, &update_core_preference, new_peer);
1454 * Given a peer and its corresponding bucket,
1455 * remove it from that bucket. Does not free
1456 * the PeerInfo struct, nor cancel messages
1457 * or free messages waiting to be sent to this
1460 * @param peer the peer to remove
1461 * @param bucket the bucket the peer belongs to
1463 static void remove_peer (struct PeerInfo *peer,
1464 unsigned int bucket)
1466 GNUNET_assert(k_buckets[bucket].peers_size > 0);
1467 GNUNET_CONTAINER_DLL_remove(k_buckets[bucket].head,
1468 k_buckets[bucket].tail,
1470 k_buckets[bucket].peers_size--;
1472 if ((bucket == lowest_bucket) && (k_buckets[lowest_bucket].peers_size == 0) && (lowest_bucket < MAX_BUCKETS - 1))
1478 * Removes peer from a bucket, then frees associated
1479 * resources and frees peer.
1481 * @param peer peer to be removed and freed
1482 * @param bucket which bucket this peer belongs to
1484 static void delete_peer (struct PeerInfo *peer,
1485 unsigned int bucket)
1487 struct P2PPendingMessage *pos;
1488 struct P2PPendingMessage *next;
1490 struct PeerInfo *peer_pos;
1492 peer_pos = k_buckets[bucket].head;
1493 while ((peer_pos != NULL) && (peer_pos != peer))
1494 peer_pos = peer_pos->next;
1495 if (peer_pos == NULL)
1497 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s: Expected peer `%s' in bucket %d\n", my_short_id, "DHT", GNUNET_i2s(&peer->id), bucket);
1498 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s: Lowest bucket: %d, find_current_bucket: %d, peer resides in bucket: %d\n", my_short_id, "DHT", lowest_bucket, find_current_bucket(&peer->id.hashPubKey), find_bucket_by_peer(peer));
1500 GNUNET_assert(peer_pos != NULL);
1502 remove_peer(peer, bucket); /* First remove the peer from its bucket */
1504 if (peer->send_task != GNUNET_SCHEDULER_NO_TASK)
1505 GNUNET_SCHEDULER_cancel(sched, peer->send_task);
1506 if (peer->th != NULL)
1507 GNUNET_CORE_notify_transmit_ready_cancel(peer->th);
1510 while (pos != NULL) /* Remove any pending messages for this peer */
1517 GNUNET_assert(GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->id.hashPubKey));
1518 GNUNET_CONTAINER_multihashmap_remove (all_known_peers, &peer->id.hashPubKey, peer);
1524 * Iterator over hash map entries.
1526 * @param cls closure
1527 * @param key current key code
1528 * @param value PeerInfo of the peer to move to new lowest bucket
1529 * @return GNUNET_YES if we should continue to
1533 static int move_lowest_bucket (void *cls,
1534 const GNUNET_HashCode * key,
1537 struct PeerInfo *peer = value;
1540 GNUNET_assert(lowest_bucket > 0);
1541 new_bucket = lowest_bucket - 1;
1542 remove_peer(peer, lowest_bucket);
1543 GNUNET_CONTAINER_DLL_insert_after(k_buckets[new_bucket].head,
1544 k_buckets[new_bucket].tail,
1545 k_buckets[new_bucket].tail,
1547 k_buckets[new_bucket].peers_size++;
1553 * The current lowest bucket is full, so change the lowest
1554 * bucket to the next lower down, and move any appropriate
1555 * entries in the current lowest bucket to the new bucket.
1557 static void enable_next_bucket()
1559 struct GNUNET_CONTAINER_MultiHashMap *to_remove;
1560 struct PeerInfo *pos;
1561 GNUNET_assert(lowest_bucket > 0);
1562 to_remove = GNUNET_CONTAINER_multihashmap_create(bucket_size);
1563 pos = k_buckets[lowest_bucket].head;
1566 fprintf(stderr, "Printing RT before new bucket\n");
1567 print_routing_table();
1569 /* Populate the array of peers which should be in the next lowest bucket */
1572 if (find_bucket(&pos->id.hashPubKey) < lowest_bucket)
1573 GNUNET_CONTAINER_multihashmap_put(to_remove, &pos->id.hashPubKey, pos, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1577 /* Remove peers from lowest bucket, insert into next lowest bucket */
1578 GNUNET_CONTAINER_multihashmap_iterate(to_remove, &move_lowest_bucket, NULL);
1579 GNUNET_CONTAINER_multihashmap_destroy(to_remove);
1580 lowest_bucket = lowest_bucket - 1;
1582 fprintf(stderr, "Printing RT after new bucket\n");
1583 print_routing_table();
1588 * Find the closest peer in our routing table to the
1591 * @return The closest peer in our routing table to the
1592 * key, or NULL on error.
1594 static struct PeerInfo *
1595 find_closest_peer (const GNUNET_HashCode *hc)
1597 struct PeerInfo *pos;
1598 struct PeerInfo *current_closest;
1599 unsigned int lowest_distance;
1600 unsigned int temp_distance;
1604 lowest_distance = -1;
1606 if (k_buckets[lowest_bucket].peers_size == 0)
1609 current_closest = NULL;
1610 for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
1612 pos = k_buckets[bucket].head;
1614 while ((pos != NULL) && (count < bucket_size))
1616 temp_distance = distance(&pos->id.hashPubKey, hc);
1617 if (temp_distance <= lowest_distance)
1619 lowest_distance = temp_distance;
1620 current_closest = pos;
1626 GNUNET_assert(current_closest != NULL);
1627 return current_closest;
1632 * Function called to send a request out to another peer.
1633 * Called both for locally initiated requests and those
1634 * received from other peers.
1636 * @param cls DHT service closure argument (unused)
1637 * @param msg the encapsulated message
1638 * @param peer the peer to forward the message to
1639 * @param msg_ctx the context of the message (hop count, bloom, etc.)
1641 static void forward_message (void *cls,
1642 const struct GNUNET_MessageHeader *msg,
1643 struct PeerInfo *peer,
1644 struct DHT_MessageContext *msg_ctx)
1646 struct GNUNET_DHT_P2PRouteMessage *route_message;
1647 struct P2PPendingMessage *pending;
1651 increment_stats(STAT_ROUTE_FORWARDS);
1653 if ((msg_ctx->closest != GNUNET_YES) && (peer == find_closest_peer(&msg_ctx->key)))
1654 increment_stats(STAT_ROUTE_FORWARDS_CLOSEST);
1656 msize = sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs(msg->size);
1657 GNUNET_assert(msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1658 psize = sizeof(struct P2PPendingMessage) + msize;
1659 pending = GNUNET_malloc(psize);
1660 pending->msg = (struct GNUNET_MessageHeader *)&pending[1];
1661 pending->importance = msg_ctx->importance;
1662 pending->timeout = msg_ctx->timeout;
1663 route_message = (struct GNUNET_DHT_P2PRouteMessage *)pending->msg;
1664 route_message->header.size = htons(msize);
1665 route_message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE);
1666 route_message->options = htonl(msg_ctx->msg_options);
1667 route_message->hop_count = htonl(msg_ctx->hop_count + 1);
1668 route_message->network_size = htonl(msg_ctx->network_size);
1669 route_message->desired_replication_level = htonl(msg_ctx->replication);
1670 route_message->unique_id = GNUNET_htonll(msg_ctx->unique_id);
1671 if (msg_ctx->bloom != NULL)
1672 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(msg_ctx->bloom, route_message->bloomfilter, DHT_BLOOM_SIZE));
1673 memcpy(&route_message->key, &msg_ctx->key, sizeof(GNUNET_HashCode));
1674 memcpy(&route_message[1], msg, ntohs(msg->size));
1676 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Adding pending message size %d for peer %s\n", my_short_id, "DHT", msize, GNUNET_i2s(&peer->id));
1678 GNUNET_CONTAINER_DLL_insert_after(peer->head, peer->tail, peer->tail, pending);
1679 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1680 peer->send_task = GNUNET_SCHEDULER_add_now(sched, &try_core_send, peer);
1685 * Task used to send ping messages to peers so that
1686 * they don't get disconnected.
1688 * @param cls the peer to send a ping message to
1689 * @param tc context, reason, etc.
1692 periodic_ping_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1694 struct PeerInfo *peer = cls;
1695 struct GNUNET_MessageHeader ping_message;
1696 struct DHT_MessageContext message_context;
1698 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1701 ping_message.size = htons(sizeof(struct GNUNET_MessageHeader));
1702 ping_message.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_PING);
1704 memset(&message_context, 0, sizeof(struct DHT_MessageContext));
1706 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Sending periodic ping to %s\n", my_short_id, "DHT", GNUNET_i2s(&peer->id));
1708 forward_message(NULL, &ping_message, peer, &message_context);
1709 peer->ping_task = GNUNET_SCHEDULER_add_delayed(sched, DHT_DEFAULT_PING_DELAY, &periodic_ping_task, peer);
1713 * Schedule PING messages for the top X peers in each
1714 * bucket of the routing table (so core won't disconnect them!)
1716 void schedule_ping_messages()
1718 unsigned int bucket;
1720 struct PeerInfo *pos;
1721 for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
1723 pos = k_buckets[bucket].head;
1727 if ((count < bucket_size) && (pos->ping_task == GNUNET_SCHEDULER_NO_TASK))
1728 GNUNET_SCHEDULER_add_now(sched, &periodic_ping_task, pos);
1729 else if ((count >= bucket_size) && (pos->ping_task != GNUNET_SCHEDULER_NO_TASK))
1731 GNUNET_SCHEDULER_cancel(sched, pos->ping_task);
1732 pos->ping_task = GNUNET_SCHEDULER_NO_TASK;
1742 * Attempt to add a peer to our k-buckets.
1744 * @param peer the peer identity of the peer being added
1745 * @param bucket the bucket that we want this peer to go in
1746 * @param latency transport latency of this peer
1747 * @param distance transport distance to this peer
1749 * @return NULL if the peer was not added,
1750 * pointer to PeerInfo for new peer otherwise
1752 static struct PeerInfo *
1753 try_add_peer(const struct GNUNET_PeerIdentity *peer,
1754 unsigned int bucket,
1755 struct GNUNET_TIME_Relative latency,
1756 unsigned int distance)
1759 struct PeerInfo *new_peer;
1761 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))
1764 peer_bucket = find_current_bucket(&peer->hashPubKey);
1766 GNUNET_assert(peer_bucket >= lowest_bucket);
1767 new_peer = add_peer(peer, peer_bucket, latency, distance);
1769 if ((k_buckets[lowest_bucket].peers_size) >= bucket_size)
1770 enable_next_bucket();
1772 schedule_ping_messages();
1779 * Task run to check for messages that need to be sent to a client.
1781 * @param client a ClientList, containing the client and any messages to be sent to it
1784 process_pending_messages (struct ClientList *client)
1786 if (client->pending_head == NULL)
1788 if (client->transmit_handle != NULL)
1791 client->transmit_handle =
1792 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
1793 ntohs (client->pending_head->msg->
1795 GNUNET_TIME_UNIT_FOREVER_REL,
1796 &send_generic_reply, client);
1800 * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
1801 * request. A ClientList is passed as closure, take the head of the list
1802 * and copy it into buf, which has the result of sending the message to the
1805 * @param cls closure to this call
1806 * @param size maximum number of bytes available to send
1807 * @param buf where to copy the actual message to
1809 * @return the number of bytes actually copied, 0 indicates failure
1812 send_generic_reply (void *cls, size_t size, void *buf)
1814 struct ClientList *client = cls;
1816 struct PendingMessage *reply;
1820 client->transmit_handle = NULL;
1823 /* client disconnected */
1827 while ( (NULL != (reply = client->pending_head)) &&
1828 (size >= off + (msize = ntohs (reply->msg->size))))
1830 GNUNET_CONTAINER_DLL_remove (client->pending_head,
1831 client->pending_tail,
1833 memcpy (&cbuf[off], reply->msg, msize);
1834 GNUNET_free (reply);
1837 process_pending_messages (client);
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1840 "Transmitted %u bytes of replies to client\n",
1841 (unsigned int) off);
1848 * Add a PendingMessage to the clients list of messages to be sent
1850 * @param client the active client to send the message to
1851 * @param pending_message the actual message to send
1854 add_pending_message (struct ClientList *client,
1855 struct PendingMessage *pending_message)
1857 GNUNET_CONTAINER_DLL_insert_after (client->pending_head,
1858 client->pending_tail,
1859 client->pending_tail,
1861 process_pending_messages (client);
1868 * Called when a reply needs to be sent to a client, as
1869 * a result it found to a GET or FIND PEER request.
1871 * @param client the client to send the reply to
1872 * @param message the encapsulated message to send
1873 * @param uid the unique identifier of this request
1876 send_reply_to_client (struct ClientList *client,
1877 const struct GNUNET_MessageHeader *message,
1878 unsigned long long uid,
1879 const GNUNET_HashCode *key)
1881 struct GNUNET_DHT_RouteResultMessage *reply;
1882 struct PendingMessage *pending_message;
1886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1887 "`%s:%s': Sending reply to client.\n", my_short_id, "DHT");
1889 msize = ntohs (message->size);
1890 tsize = sizeof (struct GNUNET_DHT_RouteResultMessage) + msize;
1891 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1893 GNUNET_break_op (0);
1896 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize);
1897 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
1898 reply = (struct GNUNET_DHT_RouteResultMessage *)&pending_message[1];
1899 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT);
1900 reply->header.size = htons (tsize);
1901 reply->put_path_length = htons(0); /* FIXME: implement */
1902 reply->get_path_length = htons(0); /* FIXME: implement */
1903 reply->unique_id = GNUNET_htonll (uid);
1905 memcpy (&reply[1], message, msize);
1906 add_pending_message (client, pending_message);
1910 * Consider whether or not we would like to have this peer added to
1911 * our routing table. Check whether bucket for this peer is full,
1912 * if so return negative; if not return positive. Since peers are
1913 * only added on CORE level connect, this doesn't actually add the
1914 * peer to the routing table.
1916 * @param peer the peer we are considering adding
1918 * @return GNUNET_YES if we want this peer, GNUNET_NO if not (bucket
1921 static int consider_peer (struct GNUNET_PeerIdentity *peer)
1925 if ((GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey)) || (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity))))
1926 return GNUNET_NO; /* We already know this peer (are connected even!) */
1927 bucket = find_current_bucket(&peer->hashPubKey);
1929 if ((k_buckets[bucket].peers_size < bucket_size) || ((bucket == lowest_bucket) && (lowest_bucket > 0)))
1936 * Main function that handles whether or not to route a result
1937 * message to other peers, or to send to our local client.
1939 * @param cls closure (unused, always should be NULL)
1940 * @param msg the result message to be routed
1941 * @param message_context context of the message we are routing
1943 * @return the number of peers the message was routed to,
1944 * GNUNET_SYSERR on failure
1946 static int route_result_message(void *cls,
1947 struct GNUNET_MessageHeader *msg,
1948 struct DHT_MessageContext *message_context)
1950 struct GNUNET_PeerIdentity new_peer;
1951 struct DHTQueryRecord *record;
1952 struct DHTRouteSource *pos;
1953 struct PeerInfo *peer_info;
1954 const struct GNUNET_MessageHeader *hello_msg;
1956 increment_stats(STAT_RESULTS);
1958 * If a find peer result message is received and contains a valid
1959 * HELLO for another peer, offer it to the transport service.
1961 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
1963 if (ntohs(msg->size) <= sizeof(struct GNUNET_MessageHeader))
1966 hello_msg = &msg[1];
1967 if ((ntohs(hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO) || (GNUNET_SYSERR == GNUNET_HELLO_get_id((const struct GNUNET_HELLO_Message *)hello_msg, &new_peer)))
1969 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Received non-HELLO message type in find peer result message!\n", my_short_id, "DHT");
1973 else /* We have a valid hello, and peer id stored in new_peer */
1975 find_peer_context.count++;
1976 increment_stats(STAT_FIND_PEER_REPLY);
1977 if (GNUNET_YES == consider_peer(&new_peer))
1979 increment_stats(STAT_HELLOS_PROVIDED);
1980 GNUNET_TRANSPORT_offer_hello(transport_handle, hello_msg);
1981 GNUNET_CORE_peer_request_connect(sched, cfg, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), &new_peer, NULL, NULL);
1986 if (malicious_dropper == GNUNET_YES)
1989 record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, &message_context->key);
1991 if (record == NULL) /* No record of this message! */
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1995 "`%s:%s': Have no record of response key %s uid %llu\n", my_short_id,
1996 "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id);
1998 #if DEBUG_DHT_ROUTING
1999 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2001 dhtlog_handle->insert_route (NULL,
2002 message_context->unique_id,
2004 message_context->hop_count,
2007 &message_context->key,
2008 message_context->peer, NULL);
2011 if (message_context->bloom != NULL)
2013 GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
2014 message_context->bloom = NULL;
2022 #if STRICT_FORWARDING
2023 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) /* If we have already forwarded this peer id, don't do it again! */
2025 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pos->find_peers_responded, &new_peer.hashPubKey))
2027 increment_stats("# find peer responses NOT forwarded (bloom match)");
2032 GNUNET_CONTAINER_bloomfilter_add(pos->find_peers_responded, &new_peer.hashPubKey);
2036 if (0 == memcmp(&pos->source, &my_identity, sizeof(struct GNUNET_PeerIdentity))) /* Local client (or DHT) initiated request! */
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2040 "`%s:%s': Sending response key %s uid %llu to client\n", my_short_id,
2041 "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id);
2043 #if DEBUG_DHT_ROUTING
2044 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2046 dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_RESULT,
2047 message_context->hop_count,
2048 GNUNET_YES, &my_identity, &message_context->key,
2049 message_context->peer, NULL);
2052 increment_stats(STAT_RESULTS_TO_CLIENT);
2053 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
2054 increment_stats(STAT_GET_REPLY);
2056 send_reply_to_client(pos->client, msg,
2057 message_context->unique_id,
2058 &message_context->key);
2060 else /* Send to peer */
2062 peer_info = find_peer_by_id(&pos->source);
2063 if (peer_info == NULL) /* Didn't find the peer in our routing table, perhaps peer disconnected! */
2069 if (message_context->bloom == NULL)
2070 message_context->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2071 GNUNET_CONTAINER_bloomfilter_add (message_context->bloom, &my_identity.hashPubKey);
2072 if ((GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (message_context->bloom, &peer_info->id.hashPubKey)))
2075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2076 "`%s:%s': Forwarding response key %s uid %llu to peer %s\n", my_short_id,
2077 "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id, GNUNET_i2s(&peer_info->id));
2079 #if DEBUG_DHT_ROUTING
2080 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2082 dhtlog_handle->insert_route (NULL, message_context->unique_id,
2084 message_context->hop_count,
2085 GNUNET_NO, &my_identity, &message_context->key,
2086 message_context->peer, &pos->source);
2089 forward_result_message(cls, msg, peer_info, message_context);
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2095 "`%s:%s': NOT Forwarding response (bloom match) key %s uid %llu to peer %s\n", my_short_id,
2096 "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id, GNUNET_i2s(&peer_info->id));
2102 if (message_context->bloom != NULL)
2103 GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
2108 * Iterator for local get request results,
2110 * @param cls closure for iterator, a DatacacheGetContext
2111 * @param exp when does this value expire?
2112 * @param key the key this data is stored under
2113 * @param size the size of the data identified by key
2114 * @param data the actual data
2115 * @param type the type of the data
2117 * @return GNUNET_OK to continue iteration, anything else
2118 * to stop iteration.
2121 datacache_get_iterator (void *cls,
2122 struct GNUNET_TIME_Absolute exp,
2123 const GNUNET_HashCode * key,
2124 uint32_t size, const char *data, uint32_t type)
2126 struct DHT_MessageContext *msg_ctx = cls;
2127 struct DHT_MessageContext *new_msg_ctx;
2128 struct GNUNET_DHT_GetResultMessage *get_result;
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 "`%s:%s': Received `%s' response from datacache\n", my_short_id, "DHT", "GET");
2133 new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext));
2134 memcpy(new_msg_ctx, msg_ctx, sizeof(struct DHT_MessageContext));
2136 GNUNET_malloc (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
2137 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
2138 get_result->header.size =
2139 htons (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
2140 get_result->expiration = GNUNET_TIME_absolute_hton(exp);
2141 get_result->type = htons (type);
2142 memcpy (&get_result[1], data, size);
2143 new_msg_ctx->peer = &my_identity;
2144 new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2145 new_msg_ctx->hop_count = 0;
2146 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE * 2; /* Make result routing a higher priority */
2147 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
2148 increment_stats(STAT_GET_RESPONSE_START);
2149 route_result_message(cls, &get_result->header, new_msg_ctx);
2150 GNUNET_free(new_msg_ctx);
2151 //send_reply_to_client (datacache_get_ctx->client, &get_result->header,
2152 // datacache_get_ctx->unique_id);
2153 GNUNET_free (get_result);
2159 * Server handler for all dht get requests, look for data,
2160 * if found, send response either to clients or other peers.
2162 * @param cls closure for service
2163 * @param msg the actual get message
2164 * @param message_context struct containing pertinent information about the get request
2166 * @return number of items found for GET request
2169 handle_dht_get (void *cls,
2170 const struct GNUNET_MessageHeader *msg,
2171 struct DHT_MessageContext *message_context)
2173 const struct GNUNET_DHT_GetMessage *get_msg;
2177 uint16_t xquery_size;
2178 unsigned int results;
2179 struct GNUNET_CONTAINER_BloomFilter *bf;
2183 msize = ntohs (msg->size);
2184 if (msize < sizeof (struct GNUNET_DHT_GetMessage))
2189 get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
2190 bf_size = ntohs (get_msg->bf_size);
2191 xquery_size = ntohs (get_msg->xquery_size);
2192 if (msize != sizeof (struct GNUNET_DHT_GetMessage) + bf_size + xquery_size)
2197 end = (const char*) &get_msg[1];
2198 if (xquery_size == 0)
2204 xquery = (const void*) end;
2213 bf = GNUNET_CONTAINER_bloomfilter_init (end,
2215 GNUNET_DHT_GET_BLOOMFILTER_K);
2218 get_type = ntohs (get_msg->type);
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n",
2225 GNUNET_h2s (&message_context->key),
2226 message_context->unique_id);
2228 increment_stats(STAT_GETS);
2231 if (get_type == DHT_MALICIOUS_MESSAGE_TYPE)
2233 GNUNET_CONTAINER_bloomfilter_free (bf);
2237 /* FIXME: put xquery / bf into message_context and use
2238 them for processing! */
2239 if (datacache != NULL)
2241 = GNUNET_DATACACHE_get (datacache,
2242 &message_context->key, get_type,
2243 &datacache_get_iterator,
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 "`%s:%s': Found %d results for `%s' request uid %llu\n", my_short_id, "DHT",
2251 results, "GET", message_context->unique_id);
2253 #if DEBUG_DHT_ROUTING
2254 if ((debug_routes) && (dhtlog_handle != NULL))
2256 dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_GET,
2257 message_context->hop_count, GNUNET_YES, &my_identity,
2258 &message_context->key);
2261 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2263 dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
2264 message_context->hop_count, GNUNET_YES,
2265 &my_identity, &message_context->key, message_context->peer,
2271 if (message_context->hop_count == 0) /* Locally initiated request */
2273 #if DEBUG_DHT_ROUTING
2274 if ((debug_routes) && (dhtlog_handle != NULL))
2276 dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_GET,
2277 message_context->hop_count, GNUNET_NO, &my_identity,
2278 &message_context->key);
2282 GNUNET_CONTAINER_bloomfilter_free (bf);
2287 remove_recent_find_peer(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2289 GNUNET_HashCode *key = cls;
2290 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(recent_find_peer_requests, key, key))
2297 * Server handler for initiating local dht find peer requests
2299 * @param cls closure for service
2300 * @param find_msg the actual find peer message
2301 * @param message_context struct containing pertinent information about the request
2305 handle_dht_find_peer (void *cls,
2306 const struct GNUNET_MessageHeader *find_msg,
2307 struct DHT_MessageContext *message_context)
2309 struct GNUNET_MessageHeader *find_peer_result;
2310 struct GNUNET_DHT_FindPeerMessage *find_peer_message;
2311 struct DHT_MessageContext *new_msg_ctx;
2312 struct GNUNET_CONTAINER_BloomFilter *incoming_bloom;
2315 GNUNET_HashCode *recent_hash;
2316 struct GNUNET_MessageHeader *other_hello;
2317 size_t other_hello_size;
2318 struct GNUNET_PeerIdentity peer_id;
2320 find_peer_message = (struct GNUNET_DHT_FindPeerMessage *)find_msg;
2321 GNUNET_break_op(ntohs(find_msg->size) >= (sizeof(struct GNUNET_DHT_FindPeerMessage)));
2322 if (ntohs(find_msg->size) < sizeof(struct GNUNET_DHT_FindPeerMessage))
2325 other_hello_size = 0;
2326 if (ntohs(find_msg->size) > sizeof(struct GNUNET_DHT_FindPeerMessage))
2328 other_hello_size = ntohs(find_msg->size) - sizeof(struct GNUNET_DHT_FindPeerMessage);
2329 other_hello = GNUNET_malloc(other_hello_size);
2330 memcpy(other_hello, &find_peer_message[1], other_hello_size);
2331 if ((GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)other_hello) == 0) || (GNUNET_OK != GNUNET_HELLO_get_id((struct GNUNET_HELLO_Message *)other_hello, &peer_id)))
2333 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Received invalid HELLO message in find peer request!\n");
2334 GNUNET_free(other_hello);
2337 #if FIND_PEER_WITH_HELLO
2338 if (GNUNET_YES == consider_peer(&peer_id))
2340 increment_stats(STAT_HELLOS_PROVIDED);
2341 GNUNET_TRANSPORT_offer_hello(transport_handle, other_hello);
2342 GNUNET_CORE_peer_request_connect(sched, cfg, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), &peer_id, NULL, NULL);
2345 else /* We don't want this peer! */
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 "`%s:%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
2353 my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&message_context->key),
2354 ntohs (find_msg->size),
2355 sizeof (struct GNUNET_MessageHeader));
2357 if (my_hello == NULL)
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "`%s': Our HELLO is null, can't return.\n",
2364 GNUNET_free_non_null(other_hello);
2368 incoming_bloom = GNUNET_CONTAINER_bloomfilter_init(find_peer_message->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2369 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(incoming_bloom, &my_identity.hashPubKey))
2371 increment_stats(STAT_BLOOM_FIND_PEER);
2372 GNUNET_CONTAINER_bloomfilter_free(incoming_bloom);
2373 GNUNET_free_non_null(other_hello);
2374 return; /* We match the bloomfilter, do not send a response to this peer (they likely already know us!)*/
2376 GNUNET_CONTAINER_bloomfilter_free(incoming_bloom);
2378 #if RESTRICT_FIND_PEER
2381 * Ignore any find peer requests from a peer we have seen very recently.
2383 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(recent_find_peer_requests, &message_context->key)) /* We have recently responded to a find peer request for this peer! */
2385 increment_stats("# dht find peer requests ignored (recently seen!)");
2386 GNUNET_free_non_null(other_hello);
2391 * Use this check to only allow the peer to respond to find peer requests if
2392 * it would be beneficial to have the requesting peer in this peers routing
2393 * table. Can be used to thwart peers flooding the network with find peer
2394 * requests that we don't care about. However, if a new peer is joining
2395 * the network and has no other peers this is a problem (assume all buckets
2396 * full, no one will respond!).
2398 memcpy(&peer_id.hashPubKey, &message_context->key, sizeof(GNUNET_HashCode));
2399 if (GNUNET_NO == consider_peer(&peer_id))
2401 increment_stats("# dht find peer requests ignored (do not need!)");
2402 GNUNET_free_non_null(other_hello);
2407 recent_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
2408 memcpy(recent_hash, &message_context->key, sizeof(GNUNET_HashCode));
2409 GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests, &message_context->key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2410 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30), &remove_recent_find_peer, &recent_hash);
2412 /* Simplistic find_peer functionality, always return our hello */
2413 hello_size = ntohs(my_hello->size);
2414 tsize = hello_size + sizeof (struct GNUNET_MessageHeader);
2416 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2418 GNUNET_break_op (0);
2419 GNUNET_free_non_null(other_hello);
2423 find_peer_result = GNUNET_malloc (tsize);
2424 find_peer_result->type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT);
2425 find_peer_result->size = htons (tsize);
2426 memcpy (&find_peer_result[1], my_hello, hello_size);
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2429 "`%s': Sending hello size %d to requesting peer.\n",
2432 new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext));
2433 memcpy(new_msg_ctx, message_context, sizeof(struct DHT_MessageContext));
2434 new_msg_ctx->peer = &my_identity;
2435 new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2436 new_msg_ctx->hop_count = 0;
2437 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE * 2; /* Make find peer requests a higher priority */
2438 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
2439 increment_stats(STAT_FIND_PEER_ANSWER);
2440 route_result_message(cls, find_peer_result, new_msg_ctx);
2441 GNUNET_free(new_msg_ctx);
2442 #if DEBUG_DHT_ROUTING
2443 if ((debug_routes) && (dhtlog_handle != NULL))
2445 dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_FIND_PEER,
2446 message_context->hop_count, GNUNET_YES, &my_identity,
2447 &message_context->key);
2450 GNUNET_free_non_null(other_hello);
2451 GNUNET_free(find_peer_result);
2455 * Task used to republish data.
2456 * Forward declaration; function call loop.
2458 * @param cls closure (a struct RepublishContext)
2459 * @param tc runtime context for this task
2462 republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2465 * Server handler for initiating local dht put requests
2467 * @param cls closure for service
2468 * @param msg the actual put message
2469 * @param message_context struct containing pertinent information about the request
2472 handle_dht_put (void *cls,
2473 const struct GNUNET_MessageHeader *msg,
2474 struct DHT_MessageContext *message_context)
2476 struct GNUNET_DHT_PutMessage *put_msg;
2480 struct RepublishContext *put_context;
2482 GNUNET_assert (ntohs (msg->size) >=
2483 sizeof (struct GNUNET_DHT_PutMessage));
2486 put_msg = (struct GNUNET_DHT_PutMessage *)msg;
2487 put_type = ntohs (put_msg->type);
2489 if (put_type == DHT_MALICIOUS_MESSAGE_TYPE)
2492 data_size = ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2496 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&message_context->key), message_context->unique_id);
2498 #if DEBUG_DHT_ROUTING
2499 if (message_context->hop_count == 0) /* Locally initiated request */
2501 if ((debug_routes) && (dhtlog_handle != NULL))
2503 dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_PUT,
2504 message_context->hop_count, GNUNET_NO, &my_identity,
2505 &message_context->key);
2510 if (message_context->closest != GNUNET_YES)
2513 #if DEBUG_DHT_ROUTING
2514 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2516 dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
2517 message_context->hop_count, GNUNET_YES,
2518 &my_identity, &message_context->key, message_context->peer,
2522 if ((debug_routes) && (dhtlog_handle != NULL))
2524 dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_PUT,
2525 message_context->hop_count, GNUNET_YES, &my_identity,
2526 &message_context->key);
2530 increment_stats(STAT_PUTS_INSERTED);
2531 if (datacache != NULL)
2533 ret = GNUNET_DATACACHE_put (datacache, &message_context->key, data_size,
2534 (char *) &put_msg[1], put_type,
2535 GNUNET_TIME_absolute_ntoh(put_msg->expiration));
2537 if ((ret == GNUNET_YES) && (do_republish == GNUNET_YES))
2539 put_context = GNUNET_malloc(sizeof(struct RepublishContext));
2540 memcpy(&put_context->key, &message_context->key, sizeof(GNUNET_HashCode));
2541 put_context->type = put_type;
2542 GNUNET_SCHEDULER_add_delayed (sched, dht_republish_frequency, &republish_content, put_context);
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2547 "`%s:%s': %s request received, but have no datacache!\n",
2548 my_short_id, "DHT", "PUT");
2552 * Estimate the diameter of the network based
2553 * on how many buckets are currently in use.
2554 * Concept here is that the diameter of the network
2555 * is roughly the distance a message must travel in
2556 * order to reach its intended destination. Since
2557 * at each hop we expect to get one bit closer, and
2558 * we have one bit per bucket, the number of buckets
2559 * in use should be the largest number of hops for
2560 * a sucessful message. (of course, this assumes we
2561 * know all peers in the network!)
2563 * @return ballpark diameter figure
2565 static unsigned int estimate_diameter()
2567 return MAX_BUCKETS - lowest_bucket;
2571 * To how many peers should we (on average)
2572 * forward the request to obtain the desired
2573 * target_replication count (on average).
2575 * Always 0, 1 or 2 (don't send, send once, split)
2578 get_forward_count (unsigned int hop_count, size_t target_replication)
2581 double target_count;
2582 double random_probability;
2584 uint32_t random_value;
2586 unsigned int target_value;
2587 unsigned int diameter;
2590 * If we are behaving in strict kademlia mode, send multiple initial requests,
2591 * but then only send to 1 or 0 peers based strictly on the number of hops.
2593 if (strict_kademlia == GNUNET_YES)
2596 return DHT_KADEMLIA_REPLICATION;
2597 else if (hop_count < max_hops)
2603 /* FIXME: the smaller we think the network is the more lenient we should be for
2604 * routing right? The estimation below only works if we think we have reasonably
2605 * full routing tables, which for our RR topologies may not be the case!
2607 diameter = estimate_diameter ();
2608 if ((hop_count > (diameter + 1) * 2) && (MINIMUM_PEER_THRESHOLD < estimate_diameter() * bucket_size))
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2612 "`%s:%s': Hop count too high (est %d, lowest %d), NOT Forwarding request\n", my_short_id,
2613 "DHT", estimate_diameter(), lowest_bucket);
2617 else if (hop_count > max_hops)
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2621 "`%s:%s': Hop count too high (greater than max)\n", my_short_id,
2628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Replication %d, hop_count %u, diameter %u\n", target_replication, hop_count, diameter);
2629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Numerator %f, denominator %f\n", (double)target_replication, ((double)target_replication * (hop_count + 1) + diameter));
2630 target_count = /* target_count is ALWAYS < 1 unless replication is < 1 */
2631 (double)target_replication / ((double)target_replication * (hop_count + 1) + diameter);
2632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Target count is %f\n", target_count);
2633 random_probability = ((double)GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2634 RAND_MAX)) / RAND_MAX;
2635 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Random is %f\n", random_probability);
2638 //while (target_value < target_count)
2639 if (target_value < target_count)
2640 target_value++; /* target_value is ALWAYS 1 after this "loop", right? Because target_count is always > 0, right? Or does it become 0.00000... at some point because the hop count is so high? */
2643 //if ((target_count + 1 - (double)target_value) > random_probability)
2644 if ((target_count) > random_probability)
2648 random_value = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, target_replication * (hop_count + 1) + diameter) + 1;
2649 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "replication %u, at hop %d, will split with probability %f\n", target_replication, hop_count, target_replication / (double)((target_replication * (hop_count + 1) + diameter) + 1));
2651 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "random %u, target %u, max %u\n", random_value, target_replication, target_replication * (hop_count + 1) + diameter);
2652 if (random_value < target_replication)
2655 return target_value;
2659 * Check whether my identity is closer than any known peers.
2660 * If a non-null bloomfilter is given, check if this is the closest
2661 * peer that hasn't already been routed to.
2663 * @param target hash code to check closeness to
2664 * @param bloom bloomfilter, exclude these entries from the decision
2666 * Return GNUNET_YES if node location is closest, GNUNET_NO
2670 am_closest_peer (const GNUNET_HashCode * target, struct GNUNET_CONTAINER_BloomFilter *bloom)
2676 struct PeerInfo *pos;
2677 unsigned int my_distance;
2679 if (0 == memcmp(&my_identity.hashPubKey, target, sizeof(GNUNET_HashCode)))
2682 bucket_num = find_current_bucket(target);
2684 bits = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, target);
2685 my_distance = distance(&my_identity.hashPubKey, target);
2686 pos = k_buckets[bucket_num].head;
2688 while ((pos != NULL) && (count < bucket_size))
2690 if ((bloom != NULL) && (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(bloom, &pos->id.hashPubKey)))
2693 continue; /* Skip already checked entries */
2696 other_bits = GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target);
2697 if (other_bits > bits)
2699 else if (other_bits == bits) /* We match the same number of bits, do distance comparison */
2701 if (strict_kademlia != GNUNET_YES) /* Return that we at as close as any other peer */
2703 else if (distance(&pos->id.hashPubKey, target) < my_distance) /* Check all known peers, only return if we are the true closest */
2710 GNUNET_GE_LOG (coreAPI->ectx,
2711 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
2712 GNUNET_GE_BULK, "closest peer\n");
2713 printPeerBits (&closest);
2714 GNUNET_GE_LOG (coreAPI->ectx,
2715 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
2716 GNUNET_GE_BULK, "me\n");
2717 printPeerBits (coreAPI->my_identity);
2718 GNUNET_GE_LOG (coreAPI->ectx,
2719 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
2720 GNUNET_GE_BULK, "key\n");
2721 printKeyBits (target);
2722 GNUNET_GE_LOG (coreAPI->ectx,
2723 GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
2725 "closest peer inverse distance is %u, mine is %u\n",
2726 inverse_distance (target, &closest.hashPubKey),
2727 inverse_distance (target,
2728 &coreAPI->my_identity->hashPubKey));
2731 /* No peers closer, we are the closest! */
2737 * Return this peers adjusted value based on the convergence
2738 * function chosen. This is the key function for randomized
2739 * routing decisions.
2741 * @param target the key of the request
2742 * @param peer the peer we would like the value of
2743 * @param hops number of hops this message has already traveled
2745 * @return bit distance from target to peer raised to an exponent
2746 * adjusted based on the current routing convergence algorithm
2750 converge_distance (const GNUNET_HashCode *target,
2751 struct PeerInfo *peer,
2754 unsigned long long ret;
2755 unsigned int other_matching_bits;
2756 double converge_modifier = 0.0;
2757 double base_converge_modifier = .1;
2763 curr_max_hops = max_hops;
2765 curr_max_hops = (estimate_diameter() + 1) * 2;
2767 if (converge_modifier > 0)
2768 converge_modifier = converge_modifier * base_converge_modifier;
2771 converge_modifier = base_converge_modifier;
2772 base_converge_modifier = 0.0;
2775 GNUNET_assert(converge_modifier > 0);
2777 other_matching_bits = GNUNET_CRYPTO_hash_matching_bits(target, &peer->id.hashPubKey);
2779 switch (converge_option)
2781 case DHT_CONVERGE_RANDOM:
2782 return 1; /* Always return 1, choose equally among all peers */
2783 case DHT_CONVERGE_LINEAR:
2784 calc_value = hops * curr_max_hops * converge_modifier;
2786 case DHT_CONVERGE_SQUARE:
2788 * Simple square based curve.
2790 calc_value = (sqrt(hops) / sqrt(curr_max_hops)) * (curr_max_hops / (curr_max_hops * converge_modifier));
2792 case DHT_CONVERGE_EXPONENTIAL:
2794 * Simple exponential curve.
2796 if (base_converge_modifier > 0)
2797 calc_value = (converge_modifier * hops * hops) / curr_max_hops;
2799 calc_value = (hops * hops) / curr_max_hops;
2805 /* Take the log (base e) of the number of bits matching the other peer */
2806 exponent = log(other_matching_bits);
2808 /* Check if we would overflow; our largest possible value is 2^64 = e^44.361419555836498 */
2809 if (exponent * calc_value >= 44.361419555836498)
2812 /* Clear errno and all math exceptions */
2814 feclearexcept(FE_ALL_EXCEPT);
2815 ret = (unsigned long long)pow(other_matching_bits, calc_value);
2816 if ((errno != 0) || fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
2819 if (0 != fetestexcept(FE_OVERFLOW))
2820 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_OVERFLOW\n");
2821 if (0 != fetestexcept(FE_INVALID))
2822 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_INVALID\n");
2823 if (0 != fetestexcept(FE_UNDERFLOW))
2824 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_UNDERFLOW\n");
2832 * Comparison function for two struct PeerInfo's
2833 * which have already had their matching bits to
2834 * some target calculated.
2836 * @param p1 a pointer pointer to a struct PeerInfo
2837 * @param p2 a pointer pointer to a struct PeerInfo
2839 * @return 0 if equidistant to target,
2840 * -1 if p1 is closer,
2844 compare_peers (const void *p1, const void *p2)
2846 struct PeerInfo **first = (struct PeerInfo **)p1;
2847 struct PeerInfo **second = (struct PeerInfo **)p2;
2849 if ((*first)->matching_bits > (*second)->matching_bits)
2851 if ((*first)->matching_bits < (*second)->matching_bits)
2859 * Select a peer from the routing table that would be a good routing
2860 * destination for sending a message for "target". The resulting peer
2861 * must not be in the set of blocked peers.<p>
2863 * Note that we should not ALWAYS select the closest peer to the
2864 * target, peers further away from the target should be chosen with
2865 * exponentially declining probability.
2867 * @param target the key we are selecting a peer to route to
2868 * @param bloom a bloomfilter containing entries this request has seen already
2870 * @return Peer to route to, or NULL on error
2872 static struct PeerInfo *
2873 select_peer (const GNUNET_HashCode * target,
2874 struct GNUNET_CONTAINER_BloomFilter *bloom, unsigned int hops)
2879 unsigned int offset;
2880 unsigned int my_matching_bits;
2882 struct PeerInfo *pos;
2883 struct PeerInfo *sorted_closest[bucket_size];
2884 unsigned long long temp_converge_distance;
2885 unsigned long long total_distance;
2886 unsigned long long selected;
2888 unsigned long long stats_total_distance;
2892 unsigned int distance;
2893 unsigned int largest_distance;
2894 struct PeerInfo *chosen;
2896 my_matching_bits = GNUNET_CRYPTO_hash_matching_bits(target, &my_identity.hashPubKey);
2899 if (strict_kademlia == GNUNET_YES)
2901 largest_distance = 0;
2903 for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
2905 pos = k_buckets[bc].head;
2907 while ((pos != NULL) && (count < bucket_size))
2909 /* If we are doing strict Kademlia routing, then checking the bloomfilter is basically cheating! */
2910 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
2912 distance = inverse_distance (target, &pos->id.hashPubKey);
2913 if (distance > largest_distance)
2916 largest_distance = distance;
2924 if ((largest_distance > 0) && (chosen != NULL))
2926 GNUNET_CONTAINER_bloomfilter_add(bloom, &chosen->id.hashPubKey);
2937 /* Three steps: order peers in closest bucket (most matching bits).
2938 * Then go over all LOWER buckets (matching same bits we do)
2939 * Then go over all HIGHER buckets (matching less then we do)
2942 closest_bucket = find_current_bucket(target);
2943 GNUNET_assert(closest_bucket >= lowest_bucket);
2944 pos = k_buckets[closest_bucket].head;
2946 offset = 0; /* Need offset as well as count in case peers are bloomfiltered */
2947 memset(sorted_closest, 0, sizeof(sorted_closest));
2948 /* Put any peers in the closest bucket in the sorting array */
2949 while ((pos != NULL) && (count < bucket_size))
2951 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
2955 continue; /* Ignore bloomfiltered peers */
2957 pos->matching_bits = GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target);
2958 sorted_closest[offset] = pos;
2964 /* Sort the peers in descending order */
2965 qsort(&sorted_closest[0], offset, sizeof(struct PeerInfo *), &compare_peers);
2967 /* Put the sorted closest peers into the possible bins first, in case of overflow. */
2968 for (i = 0; i < offset; i++)
2970 temp_converge_distance = converge_distance(target, sorted_closest[i], hops);
2971 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey))
2972 break; /* Ignore bloomfiltered peers */
2973 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
2974 total_distance += temp_converge_distance;
2976 break; /* overflow case */
2979 /* Now handle peers in lower buckets (matches same # of bits as target) */
2980 for (bc = lowest_bucket; bc < closest_bucket; bc++)
2982 pos = k_buckets[bc].head;
2984 while ((pos != NULL) && (count < bucket_size))
2986 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
2990 continue; /* Ignore bloomfiltered peers */
2992 temp_converge_distance = converge_distance(target, pos, hops);
2993 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
2994 total_distance += temp_converge_distance;
2996 break; /* overflow case */
3002 /* Now handle all the further away peers */
3003 for (bc = closest_bucket + 1; bc < MAX_BUCKETS; bc++)
3005 pos = k_buckets[bc].head;
3007 while ((pos != NULL) && (count < bucket_size))
3009 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3013 continue; /* Ignore bloomfiltered peers */
3015 temp_converge_distance = converge_distance(target, pos, hops);
3016 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
3017 total_distance += temp_converge_distance;
3019 break; /* overflow case */
3025 if (total_distance == 0) /* No peers to select from! */
3027 increment_stats("# select_peer, total_distance == 0");
3031 #if DEBUG_DHT_ROUTING > 1
3034 /* Put the sorted closest peers into the possible bins first, in case of overflow. */
3035 stats_total_distance = 0;
3036 for (i = 0; i < offset; i++)
3038 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey))
3039 break; /* Ignore bloomfiltered peers */
3040 temp_converge_distance = converge_distance(target, sorted_closest[i], hops);
3041 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
3042 stats_total_distance += temp_converge_distance;
3044 break; /* overflow case */
3045 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&sorted_closest[i]->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&sorted_closest[i]->id.hashPubKey, &my_identity.hashPubKey), (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance);
3048 /* Now handle peers in lower buckets (matches same # of bits as target) */
3049 for (bc = lowest_bucket; bc < closest_bucket; bc++)
3051 pos = k_buckets[bc].head;
3053 while ((pos != NULL) && (count < bucket_size))
3055 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3059 continue; /* Ignore bloomfiltered peers */
3061 temp_converge_distance = converge_distance(target, pos, hops);
3062 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
3063 stats_total_distance += temp_converge_distance;
3065 break; /* overflow case */
3066 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey), (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance);
3072 /* Now handle all the further away peers */
3073 for (bc = closest_bucket + 1; bc < MAX_BUCKETS; bc++)
3075 pos = k_buckets[bc].head;
3077 while ((pos != NULL) && (count < bucket_size))
3079 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3083 continue; /* Ignore bloomfiltered peers */
3085 temp_converge_distance = converge_distance(target, pos, hops);
3086 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
3087 stats_total_distance += temp_converge_distance;
3089 break; /* overflow case */
3090 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey), (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance);
3095 /* END PRINT STATS */
3098 /* Now actually choose a peer */
3099 selected = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, total_distance);
3101 /* Put the sorted closest peers into the possible bins first, in case of overflow. */
3102 for (i = 0; i < offset; i++)
3104 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey))
3105 break; /* Ignore bloomfiltered peers */
3106 temp_converge_distance = converge_distance(target, sorted_closest[i], hops);
3107 if (temp_converge_distance >= selected)
3108 return sorted_closest[i];
3110 selected -= temp_converge_distance;
3113 /* Now handle peers in lower buckets (matches same # of bits as target) */
3114 for (bc = lowest_bucket; bc < closest_bucket; bc++)
3116 pos = k_buckets[bc].head;
3118 while ((pos != NULL) && (count < bucket_size))
3120 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3124 continue; /* Ignore bloomfiltered peers */
3126 temp_converge_distance = converge_distance(target, pos, hops);
3127 if (temp_converge_distance >= selected)
3130 selected -= temp_converge_distance;
3136 /* Now handle all the further away peers */
3137 for (bc = closest_bucket + 1; bc < MAX_BUCKETS; bc++)
3139 pos = k_buckets[bc].head;
3141 while ((pos != NULL) && (count < bucket_size))
3143 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3147 continue; /* Ignore bloomfiltered peers */
3149 temp_converge_distance = converge_distance(target, pos, hops);
3150 if (temp_converge_distance >= selected)
3153 selected -= temp_converge_distance;
3159 increment_stats("# failed to select peer");
3165 * Task used to remove recent entries, either
3166 * after timeout, when full, or on shutdown.
3168 * @param cls the entry to remove
3169 * @param tc context, reason, etc.
3172 remove_recent (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3174 struct RecentRequest *req = cls;
3175 static GNUNET_HashCode hash;
3177 GNUNET_assert(req != NULL);
3178 hash_from_uid(req->uid, &hash);
3179 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(recent.hashmap, &hash, req));
3180 GNUNET_CONTAINER_heap_remove_node(recent.minHeap, req->heap_node);
3181 GNUNET_CONTAINER_bloomfilter_free(req->bloom);
3184 if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) && (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)) && (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap)))
3186 GNUNET_CONTAINER_multihashmap_destroy(recent.hashmap);
3187 GNUNET_CONTAINER_heap_destroy(recent.minHeap);
3193 * Task used to remove forwarding entries, either
3194 * after timeout, when full, or on shutdown.
3196 * @param cls the entry to remove
3197 * @param tc context, reason, etc.
3200 remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3202 struct DHTRouteSource *source_info = cls;
3203 struct DHTQueryRecord *record;
3204 source_info = GNUNET_CONTAINER_heap_remove_node(forward_list.minHeap, source_info->hnode);
3205 record = source_info->record;
3206 GNUNET_CONTAINER_DLL_remove(record->head, record->tail, source_info);
3208 if (record->head == NULL) /* No more entries in DLL */
3210 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(forward_list.hashmap, &record->key, record));
3211 GNUNET_free(record);
3213 if (source_info->find_peers_responded != NULL)
3214 GNUNET_CONTAINER_bloomfilter_free(source_info->find_peers_responded);
3215 GNUNET_free(source_info);
3219 * Remember this routing request so that if a reply is
3220 * received we can either forward it to the correct peer
3221 * or return the result locally.
3223 * @param cls DHT service closure
3224 * @param msg_ctx Context of the route request
3226 * @return GNUNET_YES if this response was cached, GNUNET_NO if not
3228 static int cache_response(void *cls, struct DHT_MessageContext *msg_ctx)
3230 struct DHTQueryRecord *record;
3231 struct DHTRouteSource *source_info;
3232 struct DHTRouteSource *pos;
3233 struct GNUNET_TIME_Absolute now;
3234 unsigned int current_size;
3236 current_size = GNUNET_CONTAINER_multihashmap_size(forward_list.hashmap);
3237 while (current_size >= MAX_OUTSTANDING_FORWARDS)
3239 source_info = GNUNET_CONTAINER_heap_remove_root(forward_list.minHeap);
3240 GNUNET_assert(source_info != NULL);
3241 record = source_info->record;
3242 GNUNET_CONTAINER_DLL_remove(record->head, record->tail, source_info);
3243 if (record->head == NULL) /* No more entries in DLL */
3245 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(forward_list.hashmap, &record->key, record));
3246 GNUNET_free(record);
3248 GNUNET_SCHEDULER_cancel(sched, source_info->delete_task);
3249 if (source_info->find_peers_responded != NULL)
3250 GNUNET_CONTAINER_bloomfilter_free(source_info->find_peers_responded);
3251 GNUNET_free(source_info);
3252 current_size = GNUNET_CONTAINER_multihashmap_size(forward_list.hashmap);
3254 now = GNUNET_TIME_absolute_get();
3255 record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, &msg_ctx->key);
3256 if (record != NULL) /* Already know this request! */
3261 if (0 == memcmp(msg_ctx->peer, &pos->source, sizeof(struct GNUNET_PeerIdentity)))
3262 break; /* Already have this peer in reply list! */
3265 if ((pos != NULL) && (pos->client == msg_ctx->client)) /* Seen this already */
3267 GNUNET_CONTAINER_heap_update_cost(forward_list.minHeap, pos->hnode, now.value);
3273 record = GNUNET_malloc(sizeof (struct DHTQueryRecord));
3274 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(forward_list.hashmap, &msg_ctx->key, record, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3275 memcpy(&record->key, &msg_ctx->key, sizeof(GNUNET_HashCode));
3278 source_info = GNUNET_malloc(sizeof(struct DHTRouteSource));
3279 source_info->record = record;
3280 source_info->delete_task = GNUNET_SCHEDULER_add_delayed(sched, DHT_FORWARD_TIMEOUT, &remove_forward_entry, source_info);
3281 source_info->find_peers_responded = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3282 memcpy(&source_info->source, msg_ctx->peer, sizeof(struct GNUNET_PeerIdentity));
3283 GNUNET_CONTAINER_DLL_insert_after(record->head, record->tail, record->tail, source_info);
3284 if (msg_ctx->client != NULL) /* For local request, set timeout so high it effectively never gets pushed out */
3286 source_info->client = msg_ctx->client;
3287 now = GNUNET_TIME_absolute_get_forever();
3289 source_info->hnode = GNUNET_CONTAINER_heap_insert(forward_list.minHeap, source_info, now.value);
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3292 "`%s:%s': Created new forward source info for %s uid %llu\n", my_short_id,
3293 "DHT", GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id);
3300 * Main function that handles whether or not to route a message to other
3303 * @param cls closure for dht service (NULL)
3304 * @param msg the message to be routed
3305 * @param message_context the context containing all pertinent information about the message
3307 * @return the number of peers the message was routed to,
3308 * GNUNET_SYSERR on failure
3310 static int route_message(void *cls,
3311 const struct GNUNET_MessageHeader *msg,
3312 struct DHT_MessageContext *message_context)
3316 struct PeerInfo *selected;
3317 #if DEBUG_DHT_ROUTING > 1
3318 struct PeerInfo *nearest;
3320 unsigned int forward_count;
3321 struct RecentRequest *recent_req;
3322 GNUNET_HashCode unique_hash;
3323 char *stat_forward_count;
3324 char *temp_stat_str;
3325 #if DEBUG_DHT_ROUTING
3329 if (malicious_dropper == GNUNET_YES)
3331 #if DEBUG_DHT_ROUTING
3332 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3334 dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
3335 message_context->hop_count, GNUNET_SYSERR,
3336 &my_identity, &message_context->key, message_context->peer,
3340 if (message_context->bloom != NULL)
3341 GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
3345 increment_stats(STAT_ROUTES);
3346 /* Semantics of this call means we find whether we are the closest peer out of those already
3347 * routed to on this messages path.
3349 global_closest = am_closest_peer(&message_context->key, NULL);
3350 message_context->closest = am_closest_peer(&message_context->key, message_context->bloom);
3351 forward_count = get_forward_count(message_context->hop_count, message_context->replication);
3352 GNUNET_asprintf(&stat_forward_count, "# forward counts of %d", forward_count);
3353 increment_stats(stat_forward_count);
3354 GNUNET_free(stat_forward_count);
3355 if (message_context->bloom == NULL)
3356 message_context->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3358 if ((stop_on_closest == GNUNET_YES) && (global_closest == GNUNET_YES) && (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
3362 * NOTICE: In Kademlia, a find peer request goes no further if the peer doesn't return
3363 * any closer peers (which is being checked for below). Since we are doing recursive
3364 * routing we have no choice but to stop forwarding in this case. This means that at
3365 * any given step the request may NOT be forwarded to alpha peers (because routes will
3366 * stop and the parallel route will not be aware of it). Of course, assuming that we
3367 * have fulfilled the Kademlia requirements for routing table fullness this will never
3368 * ever ever be a problem.
3370 * However, is this fair?
3372 * Since we use these requests to build our routing tables (and we build them in the
3373 * testing driver) we will ignore this restriction for FIND_PEER messages so that
3374 * routing tables still get constructed.
3376 if ((GNUNET_YES == strict_kademlia) && (global_closest == GNUNET_YES) && (message_context->hop_count > 0) && (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER))
3379 #if DEBUG_DHT_ROUTING
3380 if (forward_count == 0)
3381 ret = GNUNET_SYSERR;
3385 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3387 dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
3388 message_context->hop_count, ret,
3389 &my_identity, &message_context->key, message_context->peer,
3394 switch (ntohs(msg->type))
3396 case GNUNET_MESSAGE_TYPE_DHT_GET: /* Add to hashmap of requests seen, search for data (always) */
3397 cache_response (cls, message_context);
3398 if ((handle_dht_get (cls, msg, message_context) > 0) && (stop_on_found == GNUNET_YES))
3401 case GNUNET_MESSAGE_TYPE_DHT_PUT: /* Check if closest, if so insert data. FIXME: thresholding to reduce complexity?*/
3402 increment_stats(STAT_PUTS);
3403 message_context->closest = global_closest;
3404 handle_dht_put (cls, msg, message_context);
3406 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: /* Check if closest and not started by us, check options, add to requests seen */
3407 increment_stats(STAT_FIND_PEER);
3408 if (((message_context->hop_count > 0) && (0 != memcmp(message_context->peer, &my_identity, sizeof(struct GNUNET_PeerIdentity)))) || (message_context->client != NULL))
3410 cache_response (cls, message_context);
3411 if ((message_context->closest == GNUNET_YES) || (message_context->msg_options == GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE))
3412 handle_dht_find_peer (cls, msg, message_context);
3414 #if DEBUG_DHT_ROUTING
3415 if (message_context->hop_count == 0) /* Locally initiated request */
3417 if ((debug_routes) && (dhtlog_handle != NULL))
3419 dhtlog_handle->insert_dhtkey(NULL, &message_context->key);
3420 dhtlog_handle->insert_query (NULL, message_context->unique_id, DHTLOG_FIND_PEER,
3421 message_context->hop_count, GNUNET_NO, &my_identity,
3422 &message_context->key);
3428 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3429 "`%s': Message type (%d) not handled\n", "DHT", ntohs(msg->type));
3432 GNUNET_CONTAINER_bloomfilter_add (message_context->bloom, &my_identity.hashPubKey);
3433 hash_from_uid (message_context->unique_id, &unique_hash);
3434 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (recent.hashmap, &unique_hash))
3436 recent_req = GNUNET_CONTAINER_multihashmap_get(recent.hashmap, &unique_hash);
3437 GNUNET_assert(recent_req != NULL);
3438 if (0 != memcmp(&recent_req->key, &message_context->key, sizeof(GNUNET_HashCode)))
3439 increment_stats(STAT_DUPLICATE_UID);
3442 increment_stats(STAT_RECENT_SEEN);
3443 GNUNET_CONTAINER_bloomfilter_or2(message_context->bloom, recent_req->bloom, DHT_BLOOM_SIZE);
3448 recent_req = GNUNET_malloc(sizeof(struct RecentRequest));
3449 recent_req->uid = message_context->unique_id;
3450 memcpy(&recent_req->key, &message_context->key, sizeof(GNUNET_HashCode));
3451 recent_req->remove_task = GNUNET_SCHEDULER_add_delayed(sched, DEFAULT_RECENT_REMOVAL, &remove_recent, recent_req);
3452 recent_req->heap_node = GNUNET_CONTAINER_heap_insert(recent.minHeap, recent_req, GNUNET_TIME_absolute_get().value);
3453 recent_req->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3454 GNUNET_CONTAINER_multihashmap_put(recent.hashmap, &unique_hash, recent_req, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3457 if (GNUNET_CONTAINER_multihashmap_size(recent.hashmap) > DHT_MAX_RECENT)
3459 recent_req = GNUNET_CONTAINER_heap_peek(recent.minHeap);
3460 GNUNET_assert(recent_req != NULL);
3461 GNUNET_SCHEDULER_cancel(sched, recent_req->remove_task);
3462 GNUNET_SCHEDULER_add_now(sched, &remove_recent, recent_req);
3465 for (i = 0; i < forward_count; i++)
3467 selected = select_peer(&message_context->key, message_context->bloom, message_context->hop_count);
3469 if (selected != NULL)
3471 if (GNUNET_CRYPTO_hash_matching_bits(&selected->id.hashPubKey, &message_context->key) >= GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &message_context->key))
3472 GNUNET_asprintf(&temp_stat_str, "# requests routed to close(r) peer hop %u", message_context->hop_count);
3474 GNUNET_asprintf(&temp_stat_str, "# requests routed to less close peer hop %u", message_context->hop_count);
3475 if (temp_stat_str != NULL)
3477 increment_stats(temp_stat_str);
3478 GNUNET_free(temp_stat_str);
3480 GNUNET_CONTAINER_bloomfilter_add(message_context->bloom, &selected->id.hashPubKey);
3481 #if DEBUG_DHT_ROUTING > 1
3482 nearest = find_closest_peer(&message_context->key);
3483 nearest_buf = GNUNET_strdup(GNUNET_i2s(&nearest->id));
3484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3485 "`%s:%s': Forwarding request key %s uid %llu to peer %s (closest %s, bits %d, distance %u)\n", my_short_id,
3486 "DHT", GNUNET_h2s (&message_context->key), message_context->unique_id, GNUNET_i2s(&selected->id), nearest_buf, GNUNET_CRYPTO_hash_matching_bits(&nearest->id.hashPubKey, message_context->key), distance(&nearest->id.hashPubKey, message_context->key));
3487 GNUNET_free(nearest_buf);
3489 #if DEBUG_DHT_ROUTING
3490 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3492 dhtlog_handle->insert_route (NULL, message_context->unique_id, DHTLOG_ROUTE,
3493 message_context->hop_count, GNUNET_NO,
3494 &my_identity, &message_context->key, message_context->peer,
3498 forward_message(cls, msg, selected, message_context);
3502 if (message_context->bloom != NULL)
3504 GNUNET_CONTAINER_bloomfilter_or2(recent_req->bloom, message_context->bloom, DHT_BLOOM_SIZE);
3505 GNUNET_CONTAINER_bloomfilter_free(message_context->bloom);
3508 return forward_count;
3512 * Iterator for local get request results,
3514 * @param cls closure for iterator, NULL
3515 * @param exp when does this value expire?
3516 * @param key the key this data is stored under
3517 * @param size the size of the data identified by key
3518 * @param data the actual data
3519 * @param type the type of the data
3521 * @return GNUNET_OK to continue iteration, anything else
3522 * to stop iteration.
3525 republish_content_iterator (void *cls,
3526 struct GNUNET_TIME_Absolute exp,
3527 const GNUNET_HashCode * key,
3528 uint32_t size, const char *data, uint32_t type)
3531 struct DHT_MessageContext *new_msg_ctx;
3532 struct GNUNET_DHT_PutMessage *put_msg;
3534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3535 "`%s:%s': Received `%s' response from datacache\n", my_short_id, "DHT", "GET");
3537 new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext));
3540 GNUNET_malloc (sizeof (struct GNUNET_DHT_PutMessage) + size);
3541 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
3542 put_msg->header.size = htons (sizeof (struct GNUNET_DHT_PutMessage) + size);
3543 put_msg->expiration = GNUNET_TIME_absolute_hton(exp);
3544 put_msg->type = htons (type);
3545 memcpy (&put_msg[1], data, size);
3546 new_msg_ctx->unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1));
3547 new_msg_ctx->replication = ntohl (DHT_DEFAULT_PUT_REPLICATION);
3548 new_msg_ctx->msg_options = ntohl (0);
3549 new_msg_ctx->network_size = estimate_diameter();
3550 new_msg_ctx->peer = &my_identity;
3551 new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3552 new_msg_ctx->hop_count = 0;
3553 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE;
3554 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
3555 increment_stats(STAT_PUT_START);
3556 route_message(cls, &put_msg->header, new_msg_ctx);
3558 GNUNET_free(new_msg_ctx);
3559 GNUNET_free (put_msg);
3564 * Task used to republish data.
3566 * @param cls closure (a struct RepublishContext)
3567 * @param tc runtime context for this task
3570 republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3572 struct RepublishContext *put_context = cls;
3574 unsigned int results;
3576 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3578 GNUNET_free(put_context);
3582 GNUNET_assert (datacache != NULL); /* If we have no datacache we never should have scheduled this! */
3583 results = GNUNET_DATACACHE_get(datacache, &put_context->key, put_context->type, &republish_content_iterator, NULL);
3584 if (results == 0) /* Data must have expired */
3585 GNUNET_free(put_context);
3586 else /* Reschedule task for next time period */
3587 GNUNET_SCHEDULER_add_delayed(sched, dht_republish_frequency, &republish_content, put_context);
3593 * Iterator over hash map entries.
3595 * @param cls client to search for in source routes
3596 * @param key current key code (ignored)
3597 * @param value value in the hash map, a DHTQueryRecord
3598 * @return GNUNET_YES if we should continue to
3602 static int find_client_records (void *cls,
3603 const GNUNET_HashCode * key, void *value)
3605 struct ClientList *client = cls;
3606 struct DHTQueryRecord *record = value;
3607 struct DHTRouteSource *pos;
3611 if (pos->client == client)
3617 GNUNET_CONTAINER_DLL_remove(record->head, record->tail, pos);
3618 GNUNET_CONTAINER_heap_remove_node(forward_list.minHeap, pos->hnode);
3619 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
3620 GNUNET_SCHEDULER_cancel(sched, pos->delete_task);
3622 if (pos->find_peers_responded != NULL)
3623 GNUNET_CONTAINER_bloomfilter_free(pos->find_peers_responded);
3626 if (record->head == NULL) /* No more entries in DLL */
3628 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(forward_list.hashmap, &record->key, record));
3629 GNUNET_free(record);
3635 * Functions with this signature are called whenever a client
3636 * is disconnected on the network level.
3638 * @param cls closure (NULL for dht)
3639 * @param client identification of the client; NULL
3640 * for the last call when the server is destroyed
3642 static void handle_client_disconnect (void *cls,
3643 struct GNUNET_SERVER_Client* client)
3645 struct ClientList *pos = client_list;
3646 struct ClientList *prev;
3647 struct ClientList *found;
3648 struct PendingMessage *reply;
3654 if (pos->client_handle == client)
3657 prev->next = pos->next;
3659 client_list = pos->next;
3669 while(NULL != (reply = found->pending_head))
3671 GNUNET_CONTAINER_DLL_remove(found->pending_head, found->pending_tail, reply);
3674 GNUNET_CONTAINER_multihashmap_iterate(forward_list.hashmap, &find_client_records, found);
3680 * Find a client if it exists, add it otherwise.
3682 * @param client the server handle to the client
3684 * @return the client if found, a new client otherwise
3686 static struct ClientList *
3687 find_active_client (struct GNUNET_SERVER_Client *client)
3689 struct ClientList *pos = client_list;
3690 struct ClientList *ret;
3694 if (pos->client_handle == client)
3699 ret = GNUNET_malloc (sizeof (struct ClientList));
3700 ret->client_handle = client;
3701 ret->next = client_list;
3708 * Task to send a malicious put message across the network.
3710 * @param cls closure for this task
3711 * @param tc the context under which the task is running
3714 malicious_put_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3716 static struct GNUNET_DHT_PutMessage put_message;
3717 static struct DHT_MessageContext message_context;
3718 static GNUNET_HashCode key;
3719 uint32_t random_key;
3721 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3724 put_message.header.size = htons(sizeof(struct GNUNET_DHT_PutMessage));
3725 put_message.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_PUT);
3726 put_message.type = htons(DHT_MALICIOUS_MESSAGE_TYPE);
3727 put_message.expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_forever());
3728 memset(&message_context, 0, sizeof(struct DHT_MessageContext));
3729 message_context.client = NULL;
3730 random_key = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t)-1);
3731 GNUNET_CRYPTO_hash(&random_key, sizeof(uint32_t), &key);
3732 memcpy(&message_context.key, &key, sizeof(GNUNET_HashCode));
3733 message_context.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1));
3734 message_context.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3735 message_context.msg_options = ntohl (0);
3736 message_context.network_size = estimate_diameter();
3737 message_context.peer = &my_identity;
3738 message_context.importance = DHT_DEFAULT_P2P_IMPORTANCE; /* Make result routing a higher priority */
3739 message_context.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3740 #if DEBUG_DHT_ROUTING
3741 if (dhtlog_handle != NULL)
3742 dhtlog_handle->insert_dhtkey(NULL, &key);
3744 increment_stats(STAT_PUT_START);
3745 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Sending malicious PUT message with hash %s", my_short_id, "DHT", GNUNET_h2s(&key));
3746 route_message(NULL, &put_message.header, &message_context);
3747 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, malicious_put_frequency), &malicious_put_task, NULL);
3752 * Task to send a malicious put message across the network.
3754 * @param cls closure for this task
3755 * @param tc the context under which the task is running
3758 malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3760 static struct GNUNET_DHT_GetMessage get_message;
3761 struct DHT_MessageContext message_context;
3762 static GNUNET_HashCode key;
3763 uint32_t random_key;
3765 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3768 get_message.header.size = htons(sizeof(struct GNUNET_DHT_GetMessage));
3769 get_message.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_GET);
3770 get_message.type = htons(DHT_MALICIOUS_MESSAGE_TYPE);
3771 memset(&message_context, 0, sizeof(struct DHT_MessageContext));
3772 message_context.client = NULL;
3773 random_key = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t)-1);
3774 GNUNET_CRYPTO_hash(&random_key, sizeof(uint32_t), &key);
3775 memcpy(&message_context.key, &key, sizeof(GNUNET_HashCode));
3776 message_context.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1));
3777 message_context.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3778 message_context.msg_options = ntohl (0);
3779 message_context.network_size = estimate_diameter();
3780 message_context.peer = &my_identity;
3781 message_context.importance = DHT_DEFAULT_P2P_IMPORTANCE; /* Make result routing a higher priority */
3782 message_context.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3783 #if DEBUG_DHT_ROUTING
3784 if (dhtlog_handle != NULL)
3785 dhtlog_handle->insert_dhtkey(NULL, &key);
3787 increment_stats(STAT_GET_START);
3788 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Sending malicious GET message with hash %s", my_short_id, "DHT", GNUNET_h2s(&key));
3789 route_message (NULL, &get_message.header, &message_context);
3790 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, malicious_get_frequency), &malicious_get_task, NULL);
3794 * Iterator over hash map entries.
3796 * @param cls closure
3797 * @param key current key code
3798 * @param value value in the hash map
3799 * @return GNUNET_YES if we should continue to
3804 add_known_to_bloom (void *cls,
3805 const GNUNET_HashCode * key,
3808 struct GNUNET_CONTAINER_BloomFilter *bloom = cls;
3809 GNUNET_CONTAINER_bloomfilter_add (bloom, key);
3814 * Task to send a find peer message for our own peer identifier
3815 * so that we can find the closest peers in the network to ourselves
3816 * and attempt to connect to them.
3818 * @param cls closure for this task
3819 * @param tc the context under which the task is running
3822 send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3824 struct GNUNET_DHT_FindPeerMessage *find_peer_msg;
3825 struct DHT_MessageContext message_context;
3827 struct GNUNET_TIME_Relative next_send_time;
3828 struct GNUNET_CONTAINER_BloomFilter *temp_bloom;
3830 struct GNUNET_TIME_Relative time_diff;
3831 struct GNUNET_TIME_Absolute end;
3833 double count_per_interval;
3835 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3838 if ((newly_found_peers > bucket_size) && (GNUNET_YES == do_find_peer)) /* If we are finding peers already, no need to send out our request right now! */
3840 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Have %d newly found peers since last find peer message sent!\n", newly_found_peers);
3841 GNUNET_SCHEDULER_add_delayed (sched,
3842 GNUNET_TIME_UNIT_MINUTES,
3843 &send_find_peer_message, NULL);
3844 newly_found_peers = 0;
3848 increment_stats(STAT_FIND_PEER_START);
3850 end = GNUNET_TIME_absolute_get();
3851 time_diff = GNUNET_TIME_absolute_get_difference(find_peer_context.start, end);
3853 if (time_diff.value > FIND_PEER_CALC_INTERVAL.value)
3855 multiplier = time_diff.value / FIND_PEER_CALC_INTERVAL.value;
3856 count_per_interval = find_peer_context.count / multiplier;
3860 multiplier = FIND_PEER_CALC_INTERVAL.value / time_diff.value;
3861 count_per_interval = find_peer_context.count * multiplier;
3865 #if FIND_PEER_WITH_HELLO
3866 find_peer_msg = GNUNET_malloc(sizeof(struct GNUNET_DHT_FindPeerMessage) + GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello));
3867 find_peer_msg->header.size = htons(sizeof(struct GNUNET_DHT_FindPeerMessage) + GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello));
3868 memcpy(&find_peer_msg[1], my_hello, GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello));
3870 find_peer_msg = GNUNET_malloc(sizeof(struct GNUNET_DHT_FindPeerMessage));
3871 find_peer_msg->header.size = htons(sizeof(struct GNUNET_DHT_FindPeerMessage));
3873 find_peer_msg->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
3874 temp_bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3875 GNUNET_CONTAINER_multihashmap_iterate(all_known_peers, &add_known_to_bloom, temp_bloom);
3876 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(temp_bloom, find_peer_msg->bloomfilter, DHT_BLOOM_SIZE));
3877 memset(&message_context, 0, sizeof(struct DHT_MessageContext));
3878 memcpy(&message_context.key, &my_identity.hashPubKey, sizeof(GNUNET_HashCode));
3879 message_context.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, (uint64_t)-1));
3880 message_context.replication = DHT_DEFAULT_FIND_PEER_REPLICATION;
3881 message_context.msg_options = DHT_DEFAULT_FIND_PEER_OPTIONS;
3882 message_context.network_size = estimate_diameter();
3883 message_context.peer = &my_identity;
3884 message_context.importance = DHT_DEFAULT_FIND_PEER_IMPORTANCE;
3885 message_context.timeout = DHT_DEFAULT_FIND_PEER_TIMEOUT;
3887 ret = route_message(NULL, &find_peer_msg->header, &message_context);
3888 GNUNET_free(find_peer_msg);
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3890 "`%s:%s': Sent `%s' request to %d peers\n", my_short_id, "DHT",
3892 if (newly_found_peers < bucket_size)
3894 next_send_time.value = (DHT_MAXIMUM_FIND_PEER_INTERVAL.value / 2) +
3895 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
3896 DHT_MAXIMUM_FIND_PEER_INTERVAL.value / 2);
3900 next_send_time.value = DHT_MINIMUM_FIND_PEER_INTERVAL.value +
3901 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
3902 DHT_MAXIMUM_FIND_PEER_INTERVAL.value - DHT_MINIMUM_FIND_PEER_INTERVAL.value);
3905 GNUNET_assert (next_send_time.value != 0);
3906 find_peer_context.count = 0;
3907 newly_found_peers = 0;
3908 find_peer_context.start = GNUNET_TIME_absolute_get();
3909 if (GNUNET_YES == do_find_peer)
3911 GNUNET_SCHEDULER_add_delayed (sched,
3913 &send_find_peer_message, NULL);
3918 * Handler for any generic DHT messages, calls the appropriate handler
3919 * depending on message type, sends confirmation if responses aren't otherwise
3922 * @param cls closure for the service
3923 * @param client the client we received this message from
3924 * @param message the actual message received
3927 handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client,
3928 const struct GNUNET_MessageHeader *message)
3930 const struct GNUNET_DHT_RouteMessage *dht_msg = (const struct GNUNET_DHT_RouteMessage *) message;
3931 const struct GNUNET_MessageHeader *enc_msg;
3932 struct DHT_MessageContext message_context;
3934 enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3937 "`%s:%s': Received `%s' request from client, message type %d, key %s, uid %llu\n",
3941 ntohs (message->type),
3942 GNUNET_h2s (&dht_msg->key),
3943 GNUNET_ntohll (dht_msg->unique_id));
3945 #if DEBUG_DHT_ROUTING
3946 if (dhtlog_handle != NULL)
3947 dhtlog_handle->insert_dhtkey (NULL, &dht_msg->key);
3949 memset(&message_context, 0, sizeof(struct DHT_MessageContext));
3950 message_context.client = find_active_client (client);
3951 memcpy(&message_context.key, &dht_msg->key, sizeof(GNUNET_HashCode));
3952 message_context.unique_id = GNUNET_ntohll (dht_msg->unique_id);
3953 message_context.replication = ntohl (dht_msg->desired_replication_level);
3954 message_context.msg_options = ntohl (dht_msg->options);
3955 message_context.network_size = estimate_diameter();
3956 message_context.peer = &my_identity;
3957 message_context.importance = DHT_DEFAULT_P2P_IMPORTANCE * 4; /* Make local routing a higher priority */
3958 message_context.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3959 if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
3960 increment_stats(STAT_GET_START);
3961 else if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
3962 increment_stats(STAT_PUT_START);
3963 else if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER)
3964 increment_stats(STAT_FIND_PEER_START);
3966 route_message(cls, enc_msg, &message_context);
3968 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3973 * Handler for any locally received DHT control messages,
3974 * sets malicious flags mostly for now.
3976 * @param cls closure for the service
3977 * @param client the client we received this message from
3978 * @param message the actual message received
3982 handle_dht_control_message (void *cls, struct GNUNET_SERVER_Client *client,
3983 const struct GNUNET_MessageHeader *message)
3985 const struct GNUNET_DHT_ControlMessage *dht_control_msg =
3986 (const struct GNUNET_DHT_ControlMessage *) message;
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3989 "`%s:%s': Received `%s' request from client, command %d\n", my_short_id, "DHT",
3990 "CONTROL", ntohs(dht_control_msg->command));
3993 switch (ntohs(dht_control_msg->command))
3995 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
3996 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending self seeking find peer request!\n");
3997 GNUNET_SCHEDULER_add_now(sched, &send_find_peer_message, NULL);
3999 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
4000 if (ntohs(dht_control_msg->variable) > 0)
4001 malicious_get_frequency = ntohs(dht_control_msg->variable);
4002 if (malicious_get_frequency == 0)
4003 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
4004 if (malicious_getter != GNUNET_YES)
4005 GNUNET_SCHEDULER_add_now(sched, &malicious_get_task, NULL);
4006 malicious_getter = GNUNET_YES;
4007 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4008 "%s:%s Initiating malicious GET behavior, frequency %d\n", my_short_id, "DHT", malicious_get_frequency);
4010 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
4011 if (ntohs(dht_control_msg->variable) > 0)
4012 malicious_put_frequency = ntohs(dht_control_msg->variable);
4013 if (malicious_put_frequency == 0)
4014 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
4015 if (malicious_putter != GNUNET_YES)
4016 GNUNET_SCHEDULER_add_now(sched, &malicious_put_task, NULL);
4017 malicious_putter = GNUNET_YES;
4018 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4019 "%s:%s Initiating malicious PUT behavior, frequency %d\n", my_short_id, "DHT", malicious_put_frequency);
4021 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
4022 #if DEBUG_DHT_ROUTING
4023 if ((malicious_dropper != GNUNET_YES) && (dhtlog_handle != NULL))
4024 dhtlog_handle->set_malicious(&my_identity);
4026 malicious_dropper = GNUNET_YES;
4027 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
4028 "%s:%s Initiating malicious DROP behavior\n", my_short_id, "DHT");
4031 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4032 "%s:%s Unknown control command type `%d'!\n",
4034 ntohs(dht_control_msg->command));
4038 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4042 * Handler for any generic DHT stop messages, calls the appropriate handler
4043 * depending on message type (if processed locally)
4045 * @param cls closure for the service
4046 * @param client the client we received this message from
4047 * @param message the actual message received
4051 handle_dht_local_route_stop(void *cls, struct GNUNET_SERVER_Client *client,
4052 const struct GNUNET_MessageHeader *message)
4055 const struct GNUNET_DHT_StopMessage *dht_stop_msg =
4056 (const struct GNUNET_DHT_StopMessage *) message;
4057 struct DHTQueryRecord *record;
4058 struct DHTRouteSource *pos;
4060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4061 "`%s:%s': Received `%s' request from client, uid %llu\n", my_short_id, "DHT",
4062 "GENERIC STOP", GNUNET_ntohll (dht_stop_msg->unique_id));
4064 record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, &dht_stop_msg->key);
4071 if ((pos->client != NULL) && (pos->client->client_handle == client))
4073 GNUNET_SCHEDULER_cancel(sched, pos->delete_task);
4074 GNUNET_SCHEDULER_add_now(sched, &remove_forward_entry, pos);
4080 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4085 * Core handler for p2p route requests.
4088 handle_dht_p2p_route_request (void *cls,
4089 const struct GNUNET_PeerIdentity *peer,
4090 const struct GNUNET_MessageHeader *message,
4091 struct GNUNET_TIME_Relative latency, uint32_t distance)
4094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4095 "`%s:%s': Received P2P request from peer %s\n", my_short_id, "DHT", GNUNET_i2s(peer));
4097 struct GNUNET_DHT_P2PRouteMessage *incoming = (struct GNUNET_DHT_P2PRouteMessage *)message;
4098 struct GNUNET_MessageHeader *enc_msg = (struct GNUNET_MessageHeader *)&incoming[1];
4099 struct DHT_MessageContext *message_context;
4101 if (get_max_send_delay().value > MAX_REQUEST_TIME.value)
4103 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sending of previous replies took too long, backing off!\n");
4104 increment_stats("# route requests dropped due to high load");
4105 decrease_max_send_delay(get_max_send_delay());
4109 if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_P2P_PING) /* Throw these away. FIXME: Don't throw these away? (reply)*/
4112 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Received P2P Ping message.\n", my_short_id, "DHT");
4117 if (ntohs(enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4122 message_context = GNUNET_malloc(sizeof (struct DHT_MessageContext));
4123 message_context->bloom = GNUNET_CONTAINER_bloomfilter_init(incoming->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K);
4124 GNUNET_assert(message_context->bloom != NULL);
4125 message_context->hop_count = ntohl(incoming->hop_count);
4126 memcpy(&message_context->key, &incoming->key, sizeof(GNUNET_HashCode));
4127 message_context->replication = ntohl(incoming->desired_replication_level);
4128 message_context->unique_id = GNUNET_ntohll(incoming->unique_id);
4129 message_context->msg_options = ntohl(incoming->options);
4130 message_context->network_size = ntohl(incoming->network_size);
4131 message_context->peer = peer;
4132 message_context->importance = DHT_DEFAULT_P2P_IMPORTANCE;
4133 message_context->timeout = DHT_DEFAULT_P2P_TIMEOUT;
4134 route_message(cls, enc_msg, message_context);
4135 GNUNET_free(message_context);
4141 * Core handler for p2p route results.
4144 handle_dht_p2p_route_result (void *cls,
4145 const struct GNUNET_PeerIdentity *peer,
4146 const struct GNUNET_MessageHeader *message,
4147 struct GNUNET_TIME_Relative latency, uint32_t distance)
4150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4151 "`%s:%s': Received request from peer %s\n", my_short_id, "DHT", GNUNET_i2s(peer));
4153 struct GNUNET_DHT_P2PRouteResultMessage *incoming = (struct GNUNET_DHT_P2PRouteResultMessage *)message;
4154 struct GNUNET_MessageHeader *enc_msg = (struct GNUNET_MessageHeader *)&incoming[1];
4155 struct DHT_MessageContext message_context;
4157 if (ntohs(enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4163 memset(&message_context, 0, sizeof(struct DHT_MessageContext));
4164 // FIXME: call GNUNET_BLOCK_evaluate (...) -- instead of doing your own bloomfilter!
4165 message_context.bloom = GNUNET_CONTAINER_bloomfilter_init(incoming->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K);
4166 GNUNET_assert(message_context.bloom != NULL);
4167 memcpy(&message_context.key, &incoming->key, sizeof(GNUNET_HashCode));
4168 message_context.unique_id = GNUNET_ntohll(incoming->unique_id);
4169 message_context.msg_options = ntohl(incoming->options);
4170 message_context.hop_count = ntohl(incoming->hop_count);
4171 message_context.peer = peer;
4172 message_context.importance = DHT_DEFAULT_P2P_IMPORTANCE * 2; /* Make result routing a higher priority */
4173 message_context.timeout = DHT_DEFAULT_P2P_TIMEOUT;
4174 route_result_message(cls, enc_msg, &message_context);
4180 * Receive the HELLO from transport service,
4181 * free current and replace if necessary.
4184 * @param message HELLO message of peer
4187 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4191 "Received our `%s' from transport service\n",
4195 GNUNET_assert (message != NULL);
4196 GNUNET_free_non_null(my_hello);
4197 my_hello = GNUNET_malloc(ntohs(message->size));
4198 memcpy(my_hello, message, ntohs(message->size));
4203 * Task run during shutdown.
4209 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4212 struct PeerInfo *pos;
4214 if (transport_handle != NULL)
4216 GNUNET_free_non_null(my_hello);
4217 GNUNET_TRANSPORT_get_hello_cancel(transport_handle, &process_hello, NULL);
4218 GNUNET_TRANSPORT_disconnect(transport_handle);
4220 for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS; bucket_count++)
4222 while (k_buckets[bucket_count].head != NULL)
4224 pos = k_buckets[bucket_count].head;
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4227 "%s:%s Removing peer %s from bucket %d!\n", my_short_id, "DHT", GNUNET_i2s(&pos->id), bucket_count);
4229 delete_peer(pos, bucket_count);
4232 if (coreAPI != NULL)
4235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4236 "%s:%s Disconnecting core!\n", my_short_id, "DHT");
4238 GNUNET_CORE_disconnect (coreAPI);
4241 if (datacache != NULL)
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4245 "%s:%s Destroying datacache!\n", my_short_id, "DHT");
4247 GNUNET_DATACACHE_destroy (datacache);
4252 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
4255 if (dhtlog_handle != NULL)
4257 GNUNET_DHTLOG_disconnect(dhtlog_handle);
4258 dhtlog_handle = NULL;
4260 if (block_context != NULL)
4262 GNUNET_BLOCK_context_destroy (block_context);
4263 block_context = NULL;
4265 GNUNET_free_non_null(my_short_id);
4271 * To be called on core init/fail.
4273 * @param cls service closure
4274 * @param server handle to the server for this service
4275 * @param identity the public identity of this peer
4276 * @param publicKey the public key of this peer
4279 core_init (void *cls,
4280 struct GNUNET_CORE_Handle *server,
4281 const struct GNUNET_PeerIdentity *identity,
4282 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
4288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4289 "%s: Connection to core FAILED!\n", "dht",
4290 GNUNET_i2s (identity));
4292 GNUNET_SCHEDULER_cancel (sched, cleanup_task);
4293 GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL);
4297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4298 "%s: Core connection initialized, I am peer: %s\n", "dht",
4299 GNUNET_i2s (identity));
4302 /* Copy our identity so we can use it */
4303 memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity));
4304 if (my_short_id != NULL)
4305 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s Receive CORE INIT message but have already been initialized! Did CORE fail?\n", "DHT SERVICE");
4306 my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity));
4307 /* Set the server to local variable */
4310 if (dhtlog_handle != NULL)
4311 dhtlog_handle->insert_node (NULL, &my_identity);
4315 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
4316 {&handle_dht_local_route_request, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE, 0},
4317 {&handle_dht_local_route_stop, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP, 0},
4318 {&handle_dht_control_message, NULL, GNUNET_MESSAGE_TYPE_DHT_CONTROL, 0},
4323 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4324 {&handle_dht_p2p_route_request, GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE, 0},
4325 {&handle_dht_p2p_route_result, GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT, 0},
4331 * Method called whenever a peer connects.
4333 * @param cls closure
4334 * @param peer peer identity this notification is about
4335 * @param latency reported latency of the connection with peer
4336 * @param distance reported distance (DV) to peer
4338 void handle_core_connect (void *cls,
4339 const struct GNUNET_PeerIdentity * peer,
4340 struct GNUNET_TIME_Relative latency,
4343 struct PeerInfo *ret;
4346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4347 "%s:%s Receives core connect message for peer %s distance %d!\n", my_short_id, "dht", GNUNET_i2s(peer), distance);
4350 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey))
4352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Received %s message for peer %s, but already have peer in RT!", my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s(peer));
4356 if (datacache != NULL)
4357 GNUNET_DATACACHE_put(datacache, &peer->hashPubKey, sizeof(struct GNUNET_PeerIdentity), (const char *)peer, 0, GNUNET_TIME_absolute_get_forever());
4358 ret = try_add_peer(peer,
4359 find_current_bucket(&peer->hashPubKey),
4364 newly_found_peers++;
4365 GNUNET_CONTAINER_multihashmap_put(all_known_peers, &peer->hashPubKey, ret, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4369 "%s:%s Adding peer to routing list: %s\n", my_short_id, "DHT", ret == NULL ? "NOT ADDED" : "PEER ADDED");
4375 * Method called whenever a peer disconnects.
4377 * @param cls closure
4378 * @param peer peer identity this notification is about
4380 void handle_core_disconnect (void *cls,
4382 GNUNET_PeerIdentity * peer)
4384 struct PeerInfo *to_remove;
4387 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s: Received peer disconnect message for peer `%s' from %s\n", my_short_id, "DHT", GNUNET_i2s(peer), "CORE");
4389 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey))
4391 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s: do not have peer `%s' in RT, can't disconnect!\n", my_short_id, "DHT", GNUNET_i2s(peer));
4394 increment_stats(STAT_DISCONNECTS);
4395 GNUNET_assert(GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey));
4396 to_remove = GNUNET_CONTAINER_multihashmap_get(all_known_peers, &peer->hashPubKey);
4397 GNUNET_assert(0 == memcmp(peer, &to_remove->id, sizeof(struct GNUNET_PeerIdentity)));
4398 current_bucket = find_current_bucket(&to_remove->id.hashPubKey);
4399 delete_peer(to_remove, current_bucket);
4404 * Process dht requests.
4406 * @param cls closure
4407 * @param scheduler scheduler to use
4408 * @param server the initialized server
4409 * @param c configuration to use
4413 struct GNUNET_SCHEDULER_Handle *scheduler,
4414 struct GNUNET_SERVER_Handle *server,
4415 const struct GNUNET_CONFIGURATION_Handle *c)
4417 struct GNUNET_TIME_Relative next_send_time;
4418 unsigned long long temp_config_num;
4419 char *converge_modifier_buf;
4423 datacache = GNUNET_DATACACHE_create (sched, cfg, "dhtcache");
4424 GNUNET_SERVER_add_handlers (server, plugin_handlers);
4425 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
4426 coreAPI = GNUNET_CORE_connect (sched, /* Main scheduler */
4427 cfg, /* Main configuration */
4428 GNUNET_TIME_UNIT_FOREVER_REL,
4429 NULL, /* Closure passed to DHT functions */
4430 &core_init, /* Call core_init once connected */
4431 &handle_core_connect, /* Handle connects */
4432 &handle_core_disconnect, /* remove peers on disconnects */
4433 NULL, /* Do we care about "status" updates? */
4434 NULL, /* Don't want notified about all incoming messages */
4435 GNUNET_NO, /* For header only inbound notification */
4436 NULL, /* Don't want notified about all outbound messages */
4437 GNUNET_NO, /* For header only outbound notification */
4438 core_handlers); /* Register these handlers */
4440 if (coreAPI == NULL)
4442 transport_handle = GNUNET_TRANSPORT_connect(sched, cfg,
4443 NULL, NULL, NULL, NULL, NULL);
4444 if (transport_handle != NULL)
4445 GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
4447 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4448 "Failed to connect to transport service!\n");
4449 block_context = GNUNET_BLOCK_context_create (cfg);
4450 lowest_bucket = MAX_BUCKETS - 1;
4451 forward_list.hashmap = GNUNET_CONTAINER_multihashmap_create(MAX_OUTSTANDING_FORWARDS / 10);
4452 forward_list.minHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
4453 all_known_peers = GNUNET_CONTAINER_multihashmap_create(MAX_BUCKETS / 8);
4454 recent_find_peer_requests = GNUNET_CONTAINER_multihashmap_create(MAX_BUCKETS / 8);
4455 GNUNET_assert(all_known_peers != NULL);
4456 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging"))
4458 debug_routes = GNUNET_YES;
4462 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4465 strict_kademlia = GNUNET_YES;
4469 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4472 stop_on_closest = GNUNET_YES;
4476 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4479 stop_on_found = GNUNET_YES;
4483 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4484 "malicious_getter"))
4486 malicious_getter = GNUNET_YES;
4487 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
4488 "MALICIOUS_GET_FREQUENCY",
4489 &malicious_get_frequency))
4490 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
4493 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
4497 max_hops = DEFAULT_MAX_HOPS;
4500 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT",
4503 use_max_hops = GNUNET_YES;
4507 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4508 "malicious_putter"))
4510 malicious_putter = GNUNET_YES;
4511 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
4512 "MALICIOUS_PUT_FREQUENCY",
4513 &malicious_put_frequency))
4514 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
4517 dht_republish_frequency = DEFAULT_DHT_REPUBLISH_FREQUENCY;
4518 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "DHT", "REPLICATION_FREQUENCY", &temp_config_num))
4520 dht_republish_frequency = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, temp_config_num);
4524 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4525 "malicious_dropper"))
4527 malicious_dropper = GNUNET_YES;
4531 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4533 do_republish = GNUNET_NO;
4536 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4539 do_find_peer = GNUNET_NO;
4542 do_find_peer = GNUNET_YES;
4545 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4546 "use_real_distance"))
4547 use_real_distance = GNUNET_YES;
4550 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing",
4551 "mysql_logging_extended"))
4553 debug_routes = GNUNET_YES;
4554 debug_routes_extended = GNUNET_YES;
4557 #if DEBUG_DHT_ROUTING
4558 if (GNUNET_YES == debug_routes)
4560 dhtlog_handle = GNUNET_DHTLOG_connect(cfg);
4561 if (dhtlog_handle == NULL)
4563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4564 "Could not connect to mysql logging server, logging will not happen!");
4569 converge_option = DHT_CONVERGE_SQUARE;
4571 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4574 converge_option = DHT_CONVERGE_LINEAR;
4576 else if (GNUNET_YES ==
4577 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4578 "converge_exponential"))
4580 converge_option = DHT_CONVERGE_EXPONENTIAL;
4582 else if (GNUNET_YES ==
4583 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
4586 converge_option = DHT_CONVERGE_RANDOM;
4589 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "dht_testing", "converge_modifier", &converge_modifier_buf))
4591 if (1 != sscanf(converge_modifier_buf, "%f", &converge_modifier))
4593 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read decimal value for %s from `%s'\n", "CONVERGE_MODIFIER", converge_modifier_buf);
4594 converge_modifier = 0.0;
4596 GNUNET_free(converge_modifier_buf);
4599 stats = GNUNET_STATISTICS_create(sched, "dht", cfg);
4603 GNUNET_STATISTICS_set(stats, STAT_ROUTES, 0, GNUNET_NO);
4604 GNUNET_STATISTICS_set(stats, STAT_ROUTE_FORWARDS, 0, GNUNET_NO);
4605 GNUNET_STATISTICS_set(stats, STAT_ROUTE_FORWARDS_CLOSEST, 0, GNUNET_NO);
4606 GNUNET_STATISTICS_set(stats, STAT_RESULTS, 0, GNUNET_NO);
4607 GNUNET_STATISTICS_set(stats, STAT_RESULTS_TO_CLIENT, 0, GNUNET_NO);
4608 GNUNET_STATISTICS_set(stats, STAT_RESULT_FORWARDS, 0, GNUNET_NO);
4609 GNUNET_STATISTICS_set(stats, STAT_GETS, 0, GNUNET_NO);
4610 GNUNET_STATISTICS_set(stats, STAT_PUTS, 0, GNUNET_NO);
4611 GNUNET_STATISTICS_set(stats, STAT_PUTS_INSERTED, 0, GNUNET_NO);
4612 GNUNET_STATISTICS_set(stats, STAT_FIND_PEER, 0, GNUNET_NO);
4613 GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_START, 0, GNUNET_NO);
4614 GNUNET_STATISTICS_set(stats, STAT_GET_START, 0, GNUNET_NO);
4615 GNUNET_STATISTICS_set(stats, STAT_PUT_START, 0, GNUNET_NO);
4616 GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_REPLY, 0, GNUNET_NO);
4617 GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_ANSWER, 0, GNUNET_NO);
4618 GNUNET_STATISTICS_set(stats, STAT_BLOOM_FIND_PEER, 0, GNUNET_NO);
4619 GNUNET_STATISTICS_set(stats, STAT_GET_REPLY, 0, GNUNET_NO);
4620 GNUNET_STATISTICS_set(stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO);
4621 GNUNET_STATISTICS_set(stats, STAT_HELLOS_PROVIDED, 0, GNUNET_NO);
4622 GNUNET_STATISTICS_set(stats, STAT_DISCONNECTS, 0, GNUNET_NO);
4624 /* FIXME: if there are no recent requests then these never get freed, but alternative is _annoying_! */
4625 recent.hashmap = GNUNET_CONTAINER_multihashmap_create(DHT_MAX_RECENT / 2);
4626 recent.minHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
4627 if (GNUNET_YES == do_find_peer)
4629 next_send_time.value = DHT_MINIMUM_FIND_PEER_INTERVAL.value +
4630 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
4631 (DHT_MAXIMUM_FIND_PEER_INTERVAL.value / 2) - DHT_MINIMUM_FIND_PEER_INTERVAL.value);
4632 find_peer_context.start = GNUNET_TIME_absolute_get();
4633 GNUNET_SCHEDULER_add_delayed (sched,
4635 &send_find_peer_message, &find_peer_context);
4638 /* Scheduled the task to clean up when shutdown is called */
4639 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
4640 GNUNET_TIME_UNIT_FOREVER_REL,
4641 &shutdown_task, NULL);
4645 * The main function for the dht service.
4647 * @param argc number of arguments from the command line
4648 * @param argv command line arguments
4649 * @return 0 ok, 1 on error
4652 main (int argc, char *const *argv)
4654 return (GNUNET_OK ==
4655 GNUNET_SERVICE_run (argc,
4658 GNUNET_SERVICE_OPTION_NONE,
4659 &run, NULL)) ? 0 : 1;