2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file dht/gnunet-service-dht.c
23 * @brief GNUnet DHT service
24 * @author Christian Grothoff
25 * @author Nathan Evans
28 * - decide which 'benchmark'/test functions to keep (malicious code, kademlia, etc.)
29 * - decide on 'stop_on_closest', 'stop_on_found', 'do_find_peer', 'paper_forwarding'
30 * - use OPTION_MULTIPLE instead of linked list ofr the forward_list.hashmap
31 * - use different 'struct DHT_MessageContext' for the different types of
32 * messages (currently rather confusing, especially with things like
33 * peer bloom filters occuring when processing replies).
34 * - why do we have request UIDs again?
38 #include "gnunet_block_lib.h"
39 #include "gnunet_client_lib.h"
40 #include "gnunet_getopt_lib.h"
41 #include "gnunet_os_lib.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_service_lib.h"
44 #include "gnunet_nse_service.h"
45 #include "gnunet_core_service.h"
46 #include "gnunet_signal_lib.h"
47 #include "gnunet_util_lib.h"
48 #include "gnunet_datacache_lib.h"
49 #include "gnunet_transport_service.h"
50 #include "gnunet_hello_lib.h"
51 #include "gnunet_dht_service.h"
52 #include "gnunet_statistics_service.h"
59 * How many buckets will we allow total.
61 #define MAX_BUCKETS sizeof (GNUNET_HashCode) * 8
64 * Should the DHT issue FIND_PEER requests to get better routing tables?
66 #define DEFAULT_DO_FIND_PEER GNUNET_YES
69 * Defines whether find peer requests send their HELLO's outgoing,
70 * or expect replies to contain hellos.
72 #define FIND_PEER_WITH_HELLO GNUNET_YES
75 * What is the maximum number of peers in a given bucket.
77 #define DEFAULT_BUCKET_SIZE 4
79 #define DEFAULT_CORE_QUEUE_SIZE 32
82 * Minimum number of peers we need for "good" routing,
83 * any less than this and we will allow messages to
84 * travel much further through the network!
86 #define MINIMUM_PEER_THRESHOLD 20
89 * Number of requests we track at most (for routing replies).
91 #define DHT_MAX_RECENT (1024 * 16)
94 * How long do we wait at most when queueing messages with core
95 * that we are sending on behalf of other peers.
97 #define DHT_DEFAULT_P2P_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
100 * Default importance for handling messages on behalf of other peers.
102 #define DHT_DEFAULT_P2P_IMPORTANCE 0
105 * How long to keep recent requests around by default.
107 #define DEFAULT_RECENT_REMOVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
110 * Default time to wait to send find peer messages sent by the dht service.
112 #define DHT_DEFAULT_FIND_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
115 * Default importance for find peer messages sent by the dht service.
117 #define DHT_DEFAULT_FIND_PEER_IMPORTANCE 8
120 * Default replication parameter for find peer messages sent by the dht service.
122 #define DHT_DEFAULT_FIND_PEER_REPLICATION 4
125 * How long at least to wait before sending another find peer request.
127 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
130 * How long at most to wait before sending another find peer request.
132 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
135 * How often to update our preference levels for peers in our routing tables.
137 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
140 * How long at most on average will we allow a reply forward to take
141 * (before we quit sending out new requests)
143 #define MAX_REQUEST_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
146 * How many initial requests to send out (in true Kademlia fashion)
148 #define DEFAULT_KADEMLIA_REPLICATION 3
151 * Default frequency for sending malicious get messages
153 #define DEFAULT_MALICIOUS_GET_FREQUENCY GNUNET_TIME_UNIT_SECONDS
156 * Default frequency for sending malicious put messages
158 #define DEFAULT_MALICIOUS_PUT_FREQUENCY GNUNET_TIME_UNIT_SECONDS
161 * How many time differences between requesting a core send and
162 * the actual callback to remember.
164 #define MAX_REPLY_TIMES 8
168 * Linked list of messages to send to clients.
170 struct P2PPendingMessage
173 * Pointer to next item in the list
175 struct P2PPendingMessage *next;
178 * Pointer to previous item in the list
180 struct P2PPendingMessage *prev;
183 * Message importance level.
185 unsigned int importance;
188 * Time when this request was scheduled to be sent.
190 struct GNUNET_TIME_Absolute scheduled;
193 * How long to wait before sending message.
195 struct GNUNET_TIME_Relative timeout;
198 * Actual message to be sent; // avoid allocation
200 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
206 * Per-peer information.
211 * Next peer entry (DLL)
213 struct PeerInfo *next;
216 * Prev peer entry (DLL)
218 struct PeerInfo *prev;
221 * Count of outstanding messages for peer.
223 unsigned int pending_count;
226 * Head of pending messages to be sent to this peer.
228 struct P2PPendingMessage *head;
231 * Tail of pending messages to be sent to this peer.
233 struct P2PPendingMessage *tail;
236 * Core handle for sending messages to this peer.
238 struct GNUNET_CORE_TransmitHandle *th;
241 * Task for scheduling message sends.
243 GNUNET_SCHEDULER_TaskIdentifier send_task;
246 * Task for scheduling preference updates
248 GNUNET_SCHEDULER_TaskIdentifier preference_task;
251 * Preference update context
253 struct GNUNET_CORE_InformationRequestContext *info_ctx;
256 * What is the identity of the peer?
258 struct GNUNET_PeerIdentity id;
262 * What is the average latency for replies received?
264 struct GNUNET_TIME_Relative latency;
267 * Transport level distance to peer.
269 unsigned int distance;
273 * Task for scheduling periodic ping messages for this peer.
275 GNUNET_SCHEDULER_TaskIdentifier ping_task;
280 * Peers are grouped into buckets.
287 struct PeerInfo *head;
292 struct PeerInfo *tail;
295 * Number of peers in the bucket.
297 unsigned int peers_size;
302 * Linked list of messages to send to clients.
304 struct PendingMessage
307 * Pointer to next item in the list
309 struct PendingMessage *next;
312 * Pointer to previous item in the list
314 struct PendingMessage *prev;
317 * Actual message to be sent; // avoid allocation
319 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
325 * Struct containing information about a client,
326 * handle to connect to it, and any pending messages
327 * that need to be sent to it.
332 * Linked list of active clients
334 struct ClientList *next;
337 * The handle to this client
339 struct GNUNET_SERVER_Client *client_handle;
342 * Handle to the current transmission request, NULL
345 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
348 * Linked list of pending messages for this client
350 struct PendingMessage *pending_head;
353 * Tail of linked list of pending messages for this client
355 struct PendingMessage *pending_tail;
360 * Context containing information about a DHT message received.
362 struct DHT_MessageContext
365 * The client this request was received from.
366 * (NULL if received from another peer)
368 struct ClientList *client;
371 * The peer this request was received from.
373 struct GNUNET_PeerIdentity peer;
376 * Bloomfilter for this routing request.
378 struct GNUNET_CONTAINER_BloomFilter *bloom;
381 * extended query (see gnunet_block_lib.h).
386 * Bloomfilter to filter out duplicate replies.
388 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
391 * The key this request was about
396 * How long should we wait to transmit this request?
398 struct GNUNET_TIME_Relative timeout;
401 * The unique identifier of this request
406 * Number of bytes in xquery.
411 * Mutator value for the reply_bf, see gnunet_block_lib.h
413 uint32_t reply_bf_mutator;
416 * Desired replication level
418 uint32_t replication;
421 * Network size estimate, either ours or the sum of
422 * those routed to thus far. =~ Log of number of peers
423 * chosen from for this request.
425 uint32_t network_size;
428 * Any message options for this request
430 uint32_t msg_options;
433 * How many hops has the message already traversed?
438 * How many peer identities are present in the path history?
440 uint32_t path_history_len;
448 * How important is this message?
450 unsigned int importance;
453 * Should we (still) forward the request on to other peers?
458 * Did we forward this message? (may need to remember it!)
463 * Are we the closest known peer to this key (out of our neighbors?)
470 * Record used for remembering what peers are waiting for what
471 * responses (based on search key).
473 struct DHTRouteSource
478 struct DHTRouteSource *next;
483 struct DHTRouteSource *prev;
486 * UID of the request, 0 if from another peer.
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;
528 * Entry in the DHT routing table.
530 struct DHTQueryRecord
533 * Head of DLL for result forwarding.
535 struct DHTRouteSource *head;
538 * Tail of DLL for result forwarding.
540 struct DHTRouteSource *tail;
543 * Key that the record concerns.
551 * Context used to calculate the number of find peer messages
552 * per X time units since our last scheduled find peer message
553 * was sent. If we have seen too many messages, delay or don't
556 struct FindPeerMessageContext
560 struct GNUNET_TIME_Absolute start;
562 struct GNUNET_TIME_Absolute end;
567 * DHT Routing results structure
572 * Min heap for removal upon reaching limit
574 struct GNUNET_CONTAINER_Heap *minHeap;
577 * Hashmap for fast key based lookup
579 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
585 * DHT structure for recent requests.
587 struct RecentRequests
590 * Min heap for removal upon reaching limit
592 struct GNUNET_CONTAINER_Heap *minHeap;
595 * Hashmap for key based lookup
597 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
604 * Position of this node in the min heap.
606 struct GNUNET_CONTAINER_HeapNode *heap_node;
609 * Bloomfilter containing entries for peers
610 * we forwarded this request to.
612 struct GNUNET_CONTAINER_BloomFilter *bloom;
615 * Timestamp of this request, for ordering
618 struct GNUNET_TIME_Absolute timestamp;
621 * Key of this request.
626 * Unique identifier for this request, 0 if from another peer.
631 * Task to remove this entry on timeout.
633 GNUNET_SCHEDULER_TaskIdentifier remove_task;
638 * log of the current network size estimate, used as the point where
639 * we switch between random and deterministic routing. Default
640 * value of 4.0 is used if NSE module is not available (i.e. not
643 static double log_of_network_size_estimate = 4.0;
646 * Recent requests by hash/uid and by time inserted.
648 static struct RecentRequests recent;
651 * Context to use to calculate find peer rates.
653 static struct FindPeerMessageContext find_peer_context;
656 * Don't use our routing algorithm, always route
657 * to closest peer; initially send requests to 3
660 static int strict_kademlia;
663 * Routing option to end routing when closest peer found.
665 static int stop_on_closest;
668 * Routing option to end routing when data is found.
670 static int stop_on_found;
673 * Whether DHT needs to manage find peer requests, or
674 * an external force will do it on behalf of the DHT.
676 static int do_find_peer;
679 * Use exactly the forwarding formula as described in
680 * the paper if set to GNUNET_YES, otherwise use the
681 * slightly modified version.
683 static int paper_forwarding;
686 * PUT Peer Identities of peers we know about into
689 static int put_peer_identities;
692 * Use the "real" distance metric when selecting the
693 * next routing hop. Can be less accurate.
695 static int use_real_distance;
698 * How many peers have we added since we sent out our last
701 static unsigned int newly_found_peers;
704 * Container of active queries we should remember
706 static struct DHTResults forward_list;
709 * Handle to the datacache service (for inserting/retrieving data)
711 static struct GNUNET_DATACACHE_Handle *datacache;
714 * Handle for the statistics service.
716 struct GNUNET_STATISTICS_Handle *stats;
719 * Handle to get our current HELLO.
721 static struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
724 * The configuration the DHT service is running with
726 static const struct GNUNET_CONFIGURATION_Handle *cfg;
729 * Handle to the core service
731 static struct GNUNET_CORE_Handle *coreAPI;
734 * Handle to the transport service, for getting our hello
736 static struct GNUNET_TRANSPORT_Handle *transport_handle;
739 * The identity of our peer.
741 static struct GNUNET_PeerIdentity my_identity;
744 * Short id of the peer, for printing
746 static char *my_short_id;
751 static struct GNUNET_MessageHeader *my_hello;
754 * Task to run when we shut down, cleaning up all our trash
756 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
759 * The lowest currently used bucket.
761 static unsigned int lowest_bucket; /* Initially equal to MAX_BUCKETS - 1 */
764 * The buckets (Kademlia routing table, complete with growth).
765 * Array of size MAX_BUCKET_SIZE.
767 static struct PeerBucket k_buckets[MAX_BUCKETS];
770 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
772 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
775 * Recently seen find peer requests.
777 static struct GNUNET_CONTAINER_MultiHashMap *recent_find_peer_requests;
780 * Maximum size for each bucket.
782 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
785 * List of active clients.
787 static struct ClientList *client_list;
790 * Handle to the DHT logger.
792 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
795 * Whether or not to send routing debugging information
796 * to the dht logging server
798 static unsigned int debug_routes;
801 * Whether or not to send FULL route information to
804 static unsigned int debug_routes_extended;
807 * GNUNET_YES or GNUNET_NO, whether or not to act as
808 * a malicious node which drops all messages
810 static unsigned int malicious_dropper;
813 * GNUNET_YES or GNUNET_NO, whether or not to act as
814 * a malicious node which sends out lots of GETS
816 static unsigned int malicious_getter;
819 * GNUNET_YES or GNUNET_NO, whether or not to act as
820 * a malicious node which sends out lots of PUTS
822 static unsigned int malicious_putter;
825 * Frequency for malicious get requests.
827 static struct GNUNET_TIME_Relative malicious_get_frequency;
830 * Frequency for malicious put requests.
832 static struct GNUNET_TIME_Relative malicious_put_frequency;
835 * Kademlia replication
837 static unsigned long long kademlia_replication;
840 * Reply times for requests, if we are busy, don't send any
843 static struct GNUNET_TIME_Relative reply_times[MAX_REPLY_TIMES];
846 * Current counter for replies.
848 static unsigned int reply_counter;
851 * Our handle to the BLOCK library.
853 static struct GNUNET_BLOCK_Context *block_context;
856 * Network size estimation handle.
858 static struct GNUNET_NSE_Handle *nse;
862 * Callback that is called when network size estimate is updated.
865 * @param timestamp time when the estimate was received from the server (or created by the server)
866 * @param logestimate the log(Base 2) value of the current network size estimate
867 * @param std_dev standard deviation for the estimate
871 update_network_size_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
872 double logestimate, double std_dev)
874 log_of_network_size_estimate = logestimate;
879 * Forward declaration.
882 send_generic_reply (void *cls, size_t size, void *buf);
885 /** Declare here so retry_core_send is aware of it */
887 core_transmit_notify (void *cls, size_t size, void *buf);
891 * Convert unique ID to hash code.
893 * @param uid unique ID to convert
894 * @param hash set to uid (extended with zeros)
897 hash_from_uid (uint64_t uid, GNUNET_HashCode * hash)
899 memset (hash, 0, sizeof (GNUNET_HashCode));
900 *((uint64_t *) hash) = uid;
905 * Given the largest send delay, artificially decrease it
906 * so the next time around we may have a chance at sending
910 decrease_max_send_delay (struct GNUNET_TIME_Relative max_time)
914 for (i = 0; i < MAX_REPLY_TIMES; i++)
916 if (reply_times[i].rel_value == max_time.rel_value)
918 reply_times[i].rel_value = reply_times[i].rel_value / 2;
926 * Find the maximum send time of the recently sent values.
928 * @return the average time between asking core to send a message
929 * and when the buffer for copying it is passed
931 static struct GNUNET_TIME_Relative
932 get_max_send_delay ()
935 struct GNUNET_TIME_Relative max_time;
937 max_time = GNUNET_TIME_relative_get_zero ();
939 for (i = 0; i < MAX_REPLY_TIMES; i++)
941 if (reply_times[i].rel_value > max_time.rel_value)
942 max_time.rel_value = reply_times[i].rel_value;
945 if (max_time.rel_value > MAX_REQUEST_TIME.rel_value)
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n",
947 (unsigned long long) max_time.rel_value);
954 increment_stats (const char *value)
958 GNUNET_STATISTICS_update (stats, value, 1, GNUNET_NO);
963 decrement_stats (const char *value)
967 GNUNET_STATISTICS_update (stats, value, -1, GNUNET_NO);
972 * Try to send another message from our core send list
975 try_core_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
977 struct PeerInfo *peer = cls;
978 struct P2PPendingMessage *pending;
981 peer->send_task = GNUNET_SCHEDULER_NO_TASK;
983 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
986 if (peer->th != NULL)
987 return; /* Message send already in progress */
989 pending = peer->head;
992 ssize = ntohs (pending->msg->size);
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n",
996 my_short_id, "DHT", ssize, GNUNET_i2s (&peer->id));
998 pending->scheduled = GNUNET_TIME_absolute_get ();
1000 if (reply_counter >= MAX_REPLY_TIMES)
1003 GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
1004 pending->importance,
1005 pending->timeout, &peer->id, ssize,
1006 &core_transmit_notify, peer);
1007 if (peer->th == NULL)
1008 increment_stats ("# notify transmit ready failed");
1014 * Function called to send a request out to another peer.
1015 * Called both for locally initiated requests and those
1016 * received from other peers.
1018 * @param msg the encapsulated message
1019 * @param peer the peer to forward the message to
1020 * @param msg_ctx the context of the message (hop count, bloom, etc.)
1023 forward_result_message (const struct GNUNET_MessageHeader *msg,
1024 struct PeerInfo *peer,
1025 struct DHT_MessageContext *msg_ctx)
1027 struct GNUNET_DHT_P2PRouteResultMessage *result_message;
1028 struct P2PPendingMessage *pending;
1038 increment_stats (STAT_RESULT_FORWARDS);
1040 sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs (msg->size) +
1041 (sizeof (struct GNUNET_PeerIdentity) * msg_ctx->path_history_len);
1042 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1043 psize = sizeof (struct P2PPendingMessage) + msize;
1044 pending = GNUNET_malloc (psize);
1045 pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
1046 pending->importance = DHT_SEND_PRIORITY;
1047 pending->timeout = GNUNET_TIME_relative_get_forever ();
1048 result_message = (struct GNUNET_DHT_P2PRouteResultMessage *) pending->msg;
1049 result_message->header.size = htons (msize);
1050 result_message->header.type =
1051 htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT);
1052 result_message->outgoing_path_length = htonl (msg_ctx->path_history_len);
1053 if (msg_ctx->path_history_len > 0)
1055 /* End of pending is where enc_msg starts */
1056 path_start = (char *) &pending[1];
1057 /* Offset by the size of the enc_msg */
1058 path_start += ntohs (msg->size);
1059 memcpy (path_start, msg_ctx->path_history,
1060 msg_ctx->path_history_len * (sizeof (struct GNUNET_PeerIdentity)));
1062 for (i = 0; i < msg_ctx->path_history_len; i++)
1065 &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "(forward_result) Key %s Found peer %d:%s\n",
1068 GNUNET_h2s (&msg_ctx->key), i,
1069 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1073 result_message->options = htonl (msg_ctx->msg_options);
1074 result_message->hop_count = htonl (msg_ctx->hop_count + 1);
1075 #if HAVE_REPLY_BLOOMFILTER
1076 GNUNET_assert (GNUNET_OK ==
1077 GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
1082 #if HAVE_UID_FOR_TESTING
1083 result_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1085 memcpy (&result_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1086 /* Copy the enc_msg, then the path history as well! */
1087 memcpy (&result_message[1], msg, ntohs (msg->size));
1088 path_offset = (char *) &result_message[1];
1089 path_offset += ntohs (msg->size);
1090 /* If we have path history, copy it to the end of the whole thing */
1091 if (msg_ctx->path_history_len > 0)
1092 memcpy (path_offset, msg_ctx->path_history,
1093 msg_ctx->path_history_len * (sizeof (struct GNUNET_PeerIdentity)));
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096 "%s:%s Adding pending message size %d for peer %s\n", my_short_id,
1097 "DHT", msize, GNUNET_i2s (&peer->id));
1099 peer->pending_count++;
1100 increment_stats ("# pending messages scheduled");
1101 GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
1103 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1104 peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1109 * Called when core is ready to send a message we asked for
1110 * out to the destination.
1112 * @param cls closure (NULL)
1113 * @param size number of bytes available in buf
1114 * @param buf where the callee should write the message
1115 * @return number of bytes written to buf
1118 core_transmit_notify (void *cls, size_t size, void *buf)
1120 struct PeerInfo *peer = cls;
1122 struct P2PPendingMessage *pending;
1130 /* client disconnected */
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n",
1133 my_short_id, "DHT");
1138 if (peer->head == NULL)
1142 pending = peer->head;
1144 reply_times[reply_counter] =
1145 GNUNET_TIME_absolute_get_difference (pending->scheduled,
1146 GNUNET_TIME_absolute_get ());
1147 msize = ntohs (pending->msg->size);
1151 memcpy (cbuf, pending->msg, msize);
1152 peer->pending_count--;
1153 increment_stats ("# pending messages sent");
1154 GNUNET_assert (peer->pending_count >= 0);
1155 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
1156 GNUNET_free (pending);
1159 while (NULL != pending &&
1160 (size - off >= (msize = ntohs (pending->msg->size))))
1162 memcpy (&cbuf[off], pending->msg, msize);
1164 peer->pending_count--;
1165 increment_stats ("# pending messages sent");
1166 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
1167 GNUNET_free (pending);
1168 pending = peer->head;
1171 if ((peer->head != NULL) && (peer->send_task == GNUNET_SCHEDULER_NO_TASK))
1172 peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1179 * Compute the distance between have and target as a 32-bit value.
1180 * Differences in the lower bits must count stronger than differences
1181 * in the higher bits.
1183 * @return 0 if have==target, otherwise a number
1184 * that is larger as the distance between
1185 * the two hash codes increases
1188 distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
1190 unsigned int bucket;
1195 /* We have to represent the distance between two 2^9 (=512)-bit
1196 * numbers as a 2^5 (=32)-bit number with "0" being used for the
1197 * two numbers being identical; furthermore, we need to
1198 * guarantee that a difference in the number of matching
1199 * bits is always represented in the result.
1201 * We use 2^32/2^9 numerical values to distinguish between
1202 * hash codes that have the same LSB bit distance and
1203 * use the highest 2^9 bits of the result to signify the
1204 * number of (mis)matching LSB bits; if we have 0 matching
1205 * and hence 512 mismatching LSB bits we return -1 (since
1206 * 512 itself cannot be represented with 9 bits) */
1208 /* first, calculate the most significant 9 bits of our
1209 * result, aka the number of LSBs */
1210 bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
1211 /* bucket is now a value between 0 and 512 */
1213 return 0; /* perfect match */
1215 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
1216 * below, we'd end up with max+1 (overflow)) */
1218 /* calculate the most significant bits of the final result */
1219 msb = (512 - bucket) << (32 - 9);
1220 /* calculate the 32-9 least significant bits of the final result by
1221 * looking at the differences in the 32-9 bits following the
1222 * mismatching bit at 'bucket' */
1224 for (i = bucket + 1;
1225 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
1227 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
1228 GNUNET_CRYPTO_hash_get_bit (have, i))
1229 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
1230 * last bit set will be 31 -- if
1231 * i does not reach 512 first... */
1238 * Return a number that is larger the closer the
1239 * "have" GNUNET_hash code is to the "target".
1241 * @return inverse distance metric, non-zero.
1242 * Must fudge the value if NO bits match.
1245 inverse_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
1247 if (GNUNET_CRYPTO_hash_matching_bits (target, have) == 0)
1248 return 1; /* Never return 0! */
1249 return ((unsigned int) -1) - distance (target, have);
1254 * Find the optimal bucket for this key, regardless
1255 * of the current number of buckets in use.
1257 * @param hc the hashcode to compare our identity to
1259 * @return the proper bucket index, or GNUNET_SYSERR
1260 * on error (same hashcode)
1263 find_bucket (const GNUNET_HashCode * hc)
1267 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
1268 if (bits == MAX_BUCKETS)
1269 return GNUNET_SYSERR;
1270 return MAX_BUCKETS - bits - 1;
1275 * Find which k-bucket this peer should go into,
1276 * taking into account the size of the k-bucket
1277 * array. This means that if more bits match than
1278 * there are currently buckets, lowest_bucket will
1281 * @param hc GNUNET_HashCode we are finding the bucket for.
1283 * @return the proper bucket index for this key,
1284 * or GNUNET_SYSERR on error (same hashcode)
1287 find_current_bucket (const GNUNET_HashCode * hc)
1291 actual_bucket = find_bucket (hc);
1292 if (actual_bucket == GNUNET_SYSERR) /* hc and our peer identity match! */
1293 return lowest_bucket;
1294 if (actual_bucket < lowest_bucket) /* actual_bucket not yet used */
1295 return lowest_bucket;
1296 return actual_bucket;
1301 * Find a routing table entry from a peer identity
1303 * @param peer the peer identity to look up
1305 * @return the routing table entry, or NULL if not found
1307 static struct PeerInfo *
1308 find_peer_by_id (const struct GNUNET_PeerIdentity *peer)
1311 struct PeerInfo *pos;
1313 bucket = find_current_bucket (&peer->hashPubKey);
1315 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1318 pos = k_buckets[bucket].head;
1321 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1325 return NULL; /* No such peer. */
1328 /* Forward declaration */
1330 update_core_preference (void *cls,
1331 const struct GNUNET_SCHEDULER_TaskContext *tc);
1335 * Function called with statistics about the given peer.
1337 * @param cls closure
1338 * @param peer identifies the peer
1339 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1340 * @param amount set to the amount that was actually reserved or unreserved;
1341 * either the full requested amount or zero (no partial reservations)
1342 * @param res_delay if the reservation could not be satisfied (amount was 0), how
1343 * long should the client wait until re-trying?
1344 * @param preference current traffic preference for the given peer
1347 update_core_preference_finish (void *cls,
1348 const struct GNUNET_PeerIdentity *peer,
1349 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1351 struct GNUNET_TIME_Relative res_delay,
1352 uint64_t preference)
1354 struct PeerInfo *peer_info = cls;
1356 peer_info->info_ctx = NULL;
1357 GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
1358 &update_core_preference, peer_info);
1362 update_core_preference (void *cls,
1363 const struct GNUNET_SCHEDULER_TaskContext *tc)
1365 struct PeerInfo *peer = cls;
1366 uint64_t preference;
1367 unsigned int matching;
1369 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1374 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
1375 &peer->id.hashPubKey);
1379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1380 "Peer identifier matches by %u bits, only shifting as much as we can!\n",
1385 preference = 1LL << matching;
1387 GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
1388 GNUNET_TIME_UNIT_FOREVER_REL,
1389 GNUNET_BANDWIDTH_VALUE_MAX, 0,
1391 &update_core_preference_finish, peer);
1396 * Given a peer and its corresponding bucket,
1397 * remove it from that bucket. Does not free
1398 * the PeerInfo struct, nor cancel messages
1399 * or free messages waiting to be sent to this
1402 * @param peer the peer to remove
1403 * @param bucket the bucket the peer belongs to
1406 remove_peer (struct PeerInfo *peer, unsigned int bucket)
1408 GNUNET_assert (k_buckets[bucket].peers_size > 0);
1409 GNUNET_CONTAINER_DLL_remove (k_buckets[bucket].head, k_buckets[bucket].tail,
1411 k_buckets[bucket].peers_size--;
1413 if ((bucket == lowest_bucket) && (k_buckets[lowest_bucket].peers_size == 0) &&
1414 (lowest_bucket < MAX_BUCKETS - 1))
1420 * Removes peer from a bucket, then frees associated
1421 * resources and frees peer.
1423 * @param peer peer to be removed and freed
1424 * @param bucket which bucket this peer belongs to
1427 delete_peer (struct PeerInfo *peer, unsigned int bucket)
1429 struct P2PPendingMessage *pos;
1430 struct P2PPendingMessage *next;
1432 remove_peer (peer, bucket); /* First remove the peer from its bucket */
1433 if (peer->send_task != GNUNET_SCHEDULER_NO_TASK)
1434 GNUNET_SCHEDULER_cancel (peer->send_task);
1435 if ((peer->th != NULL) && (coreAPI != NULL))
1436 GNUNET_CORE_notify_transmit_ready_cancel (peer->th);
1439 while (pos != NULL) /* Remove any pending messages for this peer */
1442 ("# dht pending messages discarded (due to disconnect/shutdown)");
1448 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
1449 (all_known_peers, &peer->id.hashPubKey));
1450 GNUNET_assert (GNUNET_YES ==
1451 GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
1452 &peer->id.hashPubKey,
1455 decrement_stats (STAT_PEERS_KNOWN);
1460 * Iterator over hash map entries.
1462 * @param cls closure
1463 * @param key current key code
1464 * @param value PeerInfo of the peer to move to new lowest bucket
1465 * @return GNUNET_YES if we should continue to
1470 move_lowest_bucket (void *cls, const GNUNET_HashCode * key, void *value)
1472 struct PeerInfo *peer = value;
1475 GNUNET_assert (lowest_bucket > 0);
1476 new_bucket = lowest_bucket - 1;
1477 remove_peer (peer, lowest_bucket);
1478 GNUNET_CONTAINER_DLL_insert_after (k_buckets[new_bucket].head,
1479 k_buckets[new_bucket].tail,
1480 k_buckets[new_bucket].tail, peer);
1481 k_buckets[new_bucket].peers_size++;
1487 * The current lowest bucket is full, so change the lowest
1488 * bucket to the next lower down, and move any appropriate
1489 * entries in the current lowest bucket to the new bucket.
1492 enable_next_bucket ()
1494 struct GNUNET_CONTAINER_MultiHashMap *to_remove;
1495 struct PeerInfo *pos;
1497 GNUNET_assert (lowest_bucket > 0);
1498 to_remove = GNUNET_CONTAINER_multihashmap_create (bucket_size);
1499 pos = k_buckets[lowest_bucket].head;
1501 /* Populate the array of peers which should be in the next lowest bucket */
1504 if (find_bucket (&pos->id.hashPubKey) < lowest_bucket)
1505 GNUNET_CONTAINER_multihashmap_put (to_remove, &pos->id.hashPubKey, pos,
1506 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1510 /* Remove peers from lowest bucket, insert into next lowest bucket */
1511 GNUNET_CONTAINER_multihashmap_iterate (to_remove, &move_lowest_bucket, NULL);
1512 GNUNET_CONTAINER_multihashmap_destroy (to_remove);
1513 lowest_bucket = lowest_bucket - 1;
1518 * Find the closest peer in our routing table to the
1521 * @return The closest peer in our routing table to the
1522 * key, or NULL on error.
1524 static struct PeerInfo *
1525 find_closest_peer (const GNUNET_HashCode * hc)
1527 struct PeerInfo *pos;
1528 struct PeerInfo *current_closest;
1529 unsigned int lowest_distance;
1530 unsigned int temp_distance;
1534 lowest_distance = -1;
1536 if (k_buckets[lowest_bucket].peers_size == 0)
1539 current_closest = NULL;
1540 for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
1542 pos = k_buckets[bucket].head;
1544 while ((pos != NULL) && (count < bucket_size))
1546 temp_distance = distance (&pos->id.hashPubKey, hc);
1547 if (temp_distance <= lowest_distance)
1549 lowest_distance = temp_distance;
1550 current_closest = pos;
1556 GNUNET_assert (current_closest != NULL);
1557 return current_closest;
1562 * Function called to send a request out to another peer.
1563 * Called both for locally initiated requests and those
1564 * received from other peers.
1566 * @param msg the encapsulated message
1567 * @param peer the peer to forward the message to
1568 * @param msg_ctx the context of the message (hop count, bloom, etc.)
1571 forward_message (const struct GNUNET_MessageHeader *msg, struct PeerInfo *peer,
1572 struct DHT_MessageContext *msg_ctx)
1574 struct GNUNET_DHT_P2PRouteMessage *route_message;
1575 struct P2PPendingMessage *pending;
1580 increment_stats (STAT_ROUTE_FORWARDS);
1581 GNUNET_assert (peer != NULL);
1582 if ((msg_ctx->closest != GNUNET_YES) &&
1583 (peer == find_closest_peer (&msg_ctx->key)))
1584 increment_stats (STAT_ROUTE_FORWARDS_CLOSEST);
1587 sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (msg->size) +
1588 (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1589 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1590 psize = sizeof (struct P2PPendingMessage) + msize;
1591 pending = GNUNET_malloc (psize);
1592 pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
1593 pending->importance = msg_ctx->importance;
1594 pending->timeout = msg_ctx->timeout;
1595 route_message = (struct GNUNET_DHT_P2PRouteMessage *) pending->msg;
1596 route_message->header.size = htons (msize);
1597 route_message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE);
1598 route_message->options = htonl (msg_ctx->msg_options);
1599 route_message->hop_count = htonl (msg_ctx->hop_count + 1);
1600 route_message->network_size = htonl (msg_ctx->network_size);
1601 route_message->desired_replication_level = htonl (msg_ctx->replication);
1602 #if HAVE_UID_FOR_TESTING
1603 route_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1605 if (msg_ctx->bloom != NULL)
1606 GNUNET_assert (GNUNET_OK ==
1607 GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
1611 memcpy (&route_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1612 memcpy (&route_message[1], msg, ntohs (msg->size));
1613 if (GNUNET_DHT_RO_RECORD_ROUTE ==
1614 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
1616 route_message->outgoing_path_length = htonl (msg_ctx->path_history_len);
1617 /* Set pointer to start of enc_msg */
1618 route_path = (char *) &route_message[1];
1619 /* Offset to the end of the enc_msg */
1620 route_path += ntohs (msg->size);
1621 /* Copy the route_path after enc_msg */
1622 memcpy (route_path, msg_ctx->path_history,
1623 msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627 "%s:%s Adding pending message size %d for peer %s\n", my_short_id,
1628 "DHT", msize, GNUNET_i2s (&peer->id));
1630 peer->pending_count++;
1631 increment_stats ("# pending messages scheduled");
1632 GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
1634 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1635 peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1640 * Task run to check for messages that need to be sent to a client.
1642 * @param client a ClientList, containing the client and any messages to be sent to it
1645 process_pending_messages (struct ClientList *client)
1647 if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
1649 client->transmit_handle =
1650 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
1651 ntohs (client->pending_head->
1653 GNUNET_TIME_UNIT_FOREVER_REL,
1654 &send_generic_reply, client);
1658 * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
1659 * request. A ClientList is passed as closure, take the head of the list
1660 * and copy it into buf, which has the result of sending the message to the
1663 * @param cls closure to this call
1664 * @param size maximum number of bytes available to send
1665 * @param buf where to copy the actual message to
1667 * @return the number of bytes actually copied, 0 indicates failure
1670 send_generic_reply (void *cls, size_t size, void *buf)
1672 struct ClientList *client = cls;
1674 struct PendingMessage *reply;
1678 client->transmit_handle = NULL;
1681 /* client disconnected */
1685 while ((NULL != (reply = client->pending_head)) &&
1686 (size >= off + (msize = ntohs (reply->msg->size))))
1688 GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
1690 memcpy (&cbuf[off], reply->msg, msize);
1691 GNUNET_free (reply);
1694 process_pending_messages (client);
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697 "Transmitted %u bytes of replies to client\n",
1698 (unsigned int) off);
1705 * Add a PendingMessage to the clients list of messages to be sent
1707 * @param client the active client to send the message to
1708 * @param pending_message the actual message to send
1711 add_pending_message (struct ClientList *client,
1712 struct PendingMessage *pending_message)
1714 GNUNET_CONTAINER_DLL_insert_after (client->pending_head, client->pending_tail,
1715 client->pending_tail, pending_message);
1716 process_pending_messages (client);
1721 * Called when a reply needs to be sent to a client, as
1722 * a result it found to a GET or FIND PEER request.
1724 * @param client the client to send the reply to
1725 * @param message the encapsulated message to send
1726 * @param msg_ctx the context of the received message
1729 send_reply_to_client (struct ClientList *client,
1730 const struct GNUNET_MessageHeader *message,
1731 struct DHT_MessageContext *msg_ctx)
1733 struct GNUNET_DHT_RouteResultMessage *reply;
1734 struct PendingMessage *pending_message;
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': Sending reply to client.\n",
1745 my_short_id, "DHT");
1747 msize = ntohs (message->size);
1749 sizeof (struct GNUNET_DHT_RouteResultMessage) + msize +
1750 (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1751 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1753 GNUNET_break_op (0);
1756 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize);
1757 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
1758 reply = (struct GNUNET_DHT_RouteResultMessage *) &pending_message[1];
1759 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT);
1760 reply->header.size = htons (tsize);
1761 reply->outgoing_path_length = htonl (msg_ctx->path_history_len);
1762 reply->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1763 memcpy (&reply->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1764 reply_offset = (char *) &reply[1];
1765 memcpy (&reply[1], message, msize);
1766 if (msg_ctx->path_history_len > 0)
1768 reply_offset += msize;
1769 memcpy (reply_offset, msg_ctx->path_history,
1770 msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1774 "Returning message with outgoing path length %d\n",
1775 msg_ctx->path_history_len);
1776 for (i = 0; i < msg_ctx->path_history_len; i++)
1779 &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer %d:%s\n", i,
1781 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1784 add_pending_message (client, pending_message);
1788 * Consider whether or not we would like to have this peer added to
1789 * our routing table. Check whether bucket for this peer is full,
1790 * if so return negative; if not return positive. Since peers are
1791 * only added on CORE level connect, this doesn't actually add the
1792 * peer to the routing table.
1794 * @param peer the peer we are considering adding
1796 * @return GNUNET_YES if we want this peer, GNUNET_NO if not (bucket
1800 consider_peer (struct GNUNET_PeerIdentity *peer)
1805 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
1806 &peer->hashPubKey)) ||
1807 (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))))
1808 return GNUNET_NO; /* We already know this peer (are connected even!) */
1809 bucket = find_current_bucket (&peer->hashPubKey);
1811 if ((k_buckets[bucket].peers_size < bucket_size) ||
1812 ((bucket == lowest_bucket) && (lowest_bucket > 0)))
1820 * Task used to remove forwarding entries, either
1821 * after timeout, when full, or on shutdown.
1823 * @param cls the entry to remove
1824 * @param tc context, reason, etc.
1827 remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1829 struct DHTRouteSource *source_info = cls;
1830 struct DHTQueryRecord *record;
1832 source_info = GNUNET_CONTAINER_heap_remove_node (source_info->hnode);
1833 record = source_info->record;
1834 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
1836 if (record->head == NULL) /* No more entries in DLL */
1838 GNUNET_assert (GNUNET_YES ==
1839 GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
1840 &record->key, record));
1841 GNUNET_free (record);
1843 if (source_info->find_peers_responded != NULL)
1844 GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
1845 GNUNET_free (source_info);
1849 * Main function that handles whether or not to route a result
1850 * message to other peers, or to send to our local client.
1852 * @param msg the result message to be routed
1853 * @param msg_ctx context of the message we are routing
1855 * @return the number of peers the message was routed to,
1856 * GNUNET_SYSERR on failure
1859 route_result_message (struct GNUNET_MessageHeader *msg,
1860 struct DHT_MessageContext *msg_ctx)
1862 struct GNUNET_PeerIdentity new_peer;
1863 struct DHTQueryRecord *record;
1864 struct DHTRouteSource *pos;
1865 struct PeerInfo *peer_info;
1866 const struct GNUNET_MessageHeader *hello_msg;
1872 increment_stats (STAT_RESULTS);
1874 * If a find peer result message is received and contains a valid
1875 * HELLO for another peer, offer it to the transport service.
1877 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
1879 if (ntohs (msg->size) <= sizeof (struct GNUNET_MessageHeader))
1880 GNUNET_break_op (0);
1882 hello_msg = &msg[1];
1883 if ((ntohs (hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
1885 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello_msg,
1888 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1889 "%s:%s Received non-HELLO message type in find peer result message!\n",
1890 my_short_id, "DHT");
1891 GNUNET_break_op (0);
1894 else /* We have a valid hello, and peer id stored in new_peer */
1896 find_peer_context.count++;
1897 increment_stats (STAT_FIND_PEER_REPLY);
1898 if (GNUNET_YES == consider_peer (&new_peer))
1900 increment_stats (STAT_HELLOS_PROVIDED);
1901 GNUNET_TRANSPORT_offer_hello (transport_handle, hello_msg, NULL, NULL);
1902 GNUNET_CORE_peer_request_connect (coreAPI, &new_peer, NULL, NULL);
1907 if (malicious_dropper == GNUNET_YES)
1911 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
1913 if (record == NULL) /* No record of this message! */
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "`%s:%s': Have no record of response key %s uid %llu\n",
1918 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
1919 msg_ctx->unique_id);
1921 #if DEBUG_DHT_ROUTING
1922 if ((debug_routes_extended) && (dhtlog_handle != NULL))
1924 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
1925 msg_ctx->hop_count, GNUNET_SYSERR,
1926 &my_identity, &msg_ctx->key, &msg_ctx->peer,
1930 #if HAVE_REPLY_BLOOMFILTER
1931 if (msg_ctx->bloom != NULL)
1933 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
1934 msg_ctx->bloom = NULL;
1943 #if STRICT_FORWARDING
1944 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) /* If we have already forwarded this peer id, don't do it again! */
1947 GNUNET_CONTAINER_bloomfilter_test (pos->find_peers_responded,
1948 &new_peer.hashPubKey))
1950 increment_stats ("# find peer responses NOT forwarded (bloom match)");
1955 GNUNET_CONTAINER_bloomfilter_add (pos->find_peers_responded,
1956 &new_peer.hashPubKey);
1960 if (0 == memcmp (&pos->source, &my_identity, sizeof (struct GNUNET_PeerIdentity))) /* Local client (or DHT) initiated request! */
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964 "`%s:%s': Sending response key %s uid %llu to client\n",
1965 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
1966 msg_ctx->unique_id);
1968 #if DEBUG_DHT_ROUTING
1969 if ((debug_routes_extended) && (dhtlog_handle != NULL))
1971 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
1972 msg_ctx->hop_count, GNUNET_YES,
1973 &my_identity, &msg_ctx->key, &msg_ctx->peer,
1977 increment_stats (STAT_RESULTS_TO_CLIENT);
1978 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
1979 increment_stats (STAT_GET_REPLY);
1981 for (i = 0; i < msg_ctx->path_history_len; i++)
1986 &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1988 "(before client) Key %s Found peer %d:%s\n",
1989 GNUNET_h2s (&msg_ctx->key), i,
1990 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1993 send_reply_to_client (pos->client, msg, msg_ctx);
1995 else /* Send to peer */
1997 peer_info = find_peer_by_id (&pos->source);
1998 if (peer_info == NULL) /* Didn't find the peer in our routing table, perhaps peer disconnected! */
2003 #if HAVE_REPLY_BLOOMFILTER
2004 if (msg_ctx->bloom == NULL)
2006 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
2008 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
2009 &my_identity.hashPubKey);
2011 GNUNET_CONTAINER_bloomfilter_test (msg_ctx->bloom,
2012 &peer_info->id.hashPubKey)))
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017 "`%s:%s': Forwarding response key %s uid %llu to peer %s\n",
2018 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
2019 msg_ctx->unique_id, GNUNET_i2s (&peer_info->id));
2021 #if DEBUG_DHT_ROUTING
2022 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2024 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
2025 msg_ctx->hop_count, GNUNET_NO,
2026 &my_identity, &msg_ctx->key,
2027 &msg_ctx->peer, &pos->source);
2030 forward_result_message (msg, peer_info, msg_ctx);
2031 /* Try removing forward entries after sending once, only allows ONE response per request */
2032 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
2034 GNUNET_SCHEDULER_cancel (pos->delete_task);
2036 GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);
2038 #if HAVE_REPLY_BLOOMFILTER
2044 #if HAVE_REPLY_BLOOMFILTER
2045 if (msg_ctx->bloom != NULL)
2047 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
2048 msg_ctx->bloom = NULL;
2056 * Iterator for local get request results,
2058 * @param cls closure for iterator, a DatacacheGetContext
2059 * @param exp when does this value expire?
2060 * @param key the key this data is stored under
2061 * @param size the size of the data identified by key
2062 * @param data the actual data
2063 * @param type the type of the data
2065 * @return GNUNET_OK to continue iteration, anything else
2066 * to stop iteration.
2069 datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
2070 const GNUNET_HashCode * key, size_t size,
2071 const char *data, enum GNUNET_BLOCK_Type type)
2073 struct DHT_MessageContext *msg_ctx = cls;
2074 struct DHT_MessageContext new_msg_ctx;
2075 struct GNUNET_DHT_GetResultMessage *get_result;
2076 enum GNUNET_BLOCK_EvaluationResult eval;
2077 const struct DHTPutEntry *put_entry;
2086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2087 "`%s:%s': Received `%s' response from datacache\n", my_short_id,
2091 put_entry = (const struct DHTPutEntry *) data;
2094 sizeof (struct DHTPutEntry) + put_entry->data_size +
2095 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)))
2097 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2098 "Path + data size doesn't add up for data inserted into datacache!\nData size %d, path length %d, expected %d, got %d\n",
2099 put_entry->data_size, put_entry->path_length,
2100 sizeof (struct DHTPutEntry) + put_entry->data_size +
2101 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)),
2103 msg_ctx->do_forward = GNUNET_NO;
2108 GNUNET_BLOCK_evaluate (block_context, type, key, &msg_ctx->reply_bf,
2109 msg_ctx->reply_bf_mutator, msg_ctx->xquery,
2110 msg_ctx->xquery_size, &put_entry[1],
2111 put_entry->data_size);
2115 case GNUNET_BLOCK_EVALUATION_OK_LAST:
2116 msg_ctx->do_forward = GNUNET_NO;
2117 case GNUNET_BLOCK_EVALUATION_OK_MORE:
2118 memcpy (&new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2119 if (GNUNET_DHT_RO_RECORD_ROUTE ==
2120 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2122 new_msg_ctx.msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2124 for (i = 0; i < new_msg_ctx.path_history_len; i++)
2127 &new_msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 "(get_iterator) Key %s Found peer %d:%s\n",
2130 GNUNET_h2s (&msg_ctx->key), i,
2131 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
2137 sizeof (struct GNUNET_DHT_GetResultMessage) + put_entry->data_size +
2138 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity));
2139 get_result = GNUNET_malloc (get_size);
2140 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
2141 get_result->header.size = htons (get_size);
2142 get_result->expiration = GNUNET_TIME_absolute_hton (exp);
2143 get_result->type = htons (type);
2144 get_result->put_path_length = htons (put_entry->path_length);
2145 path_offset = (char *) &put_entry[1];
2146 path_offset += put_entry->data_size;
2148 for (i = 0; i < put_entry->path_length; i++)
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 "(get_iterator PUT path) Key %s Found peer %d:%s\n",
2152 GNUNET_h2s (&msg_ctx->key), i,
2153 GNUNET_i2s ((struct GNUNET_PeerIdentity *)
2156 GNUNET_PeerIdentity)]));
2159 /* Copy the actual data and the path_history to the end of the get result */
2160 memcpy (&get_result[1], &put_entry[1],
2161 put_entry->data_size +
2162 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)));
2163 new_msg_ctx.peer = my_identity;
2164 new_msg_ctx.bloom = NULL;
2165 new_msg_ctx.hop_count = 0;
2166 new_msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */
2167 new_msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
2168 increment_stats (STAT_GET_RESPONSE_START);
2169 route_result_message (&get_result->header, &new_msg_ctx);
2170 GNUNET_free (get_result);
2172 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': Duplicate block error\n",
2175 my_short_id, "DHT");
2178 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
2180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "`%s:%s': Invalid request error\n",
2181 my_short_id, "DHT");
2184 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
2186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2187 "`%s:%s': Valid request, no results.\n", my_short_id, "DHT");
2191 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
2192 GNUNET_break_op (0);
2193 msg_ctx->do_forward = GNUNET_NO;
2195 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
2197 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2198 "`%s:%s': Unsupported block type (%u) in response!\n",
2199 my_short_id, "DHT", type);
2201 /* msg_ctx->do_forward = GNUNET_NO; // not sure... */
2209 * Main function that handles whether or not to route a message to other
2212 * @param msg the message to be routed
2213 * @param msg_ctx the context containing all pertinent information about the message
2216 route_message (const struct GNUNET_MessageHeader *msg,
2217 struct DHT_MessageContext *msg_ctx);
2221 * Server handler for all dht get requests, look for data,
2222 * if found, send response either to clients or other peers.
2224 * @param msg the actual get message
2225 * @param msg_ctx struct containing pertinent information about the get request
2227 * @return number of items found for GET request
2230 handle_dht_get (const struct GNUNET_MessageHeader *msg,
2231 struct DHT_MessageContext *msg_ctx)
2233 const struct GNUNET_DHT_GetMessage *get_msg;
2236 unsigned int results;
2238 enum GNUNET_BLOCK_Type type;
2240 msize = ntohs (msg->size);
2241 if (msize < sizeof (struct GNUNET_DHT_GetMessage))
2246 get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
2247 bf_size = ntohs (get_msg->bf_size);
2248 msg_ctx->xquery_size = ntohs (get_msg->xquery_size);
2249 msg_ctx->reply_bf_mutator = get_msg->bf_mutator;
2251 sizeof (struct GNUNET_DHT_GetMessage) + bf_size + msg_ctx->xquery_size)
2253 GNUNET_break_op (0);
2256 end = (const char *) &get_msg[1];
2257 if (msg_ctx->xquery_size == 0)
2259 msg_ctx->xquery = NULL;
2263 msg_ctx->xquery = (const void *) end;
2264 end += msg_ctx->xquery_size;
2268 msg_ctx->reply_bf = NULL;
2273 GNUNET_CONTAINER_bloomfilter_init (end, bf_size,
2274 GNUNET_DHT_GET_BLOOMFILTER_K);
2276 type = (enum GNUNET_BLOCK_Type) ntohl (get_msg->type);
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279 "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n",
2280 my_short_id, "DHT", "GET", type, GNUNET_h2s (&msg_ctx->key),
2281 msg_ctx->unique_id);
2283 increment_stats (STAT_GETS);
2286 if (type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
2288 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
2292 msg_ctx->do_forward = GNUNET_YES;
2293 if (datacache != NULL)
2295 GNUNET_DATACACHE_get (datacache, &msg_ctx->key, type,
2296 &datacache_get_iterator, msg_ctx);
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2299 "`%s:%s': Found %d results for `%s' request uid %llu\n",
2300 my_short_id, "DHT", results, "GET", msg_ctx->unique_id);
2304 #if DEBUG_DHT_ROUTING
2305 if ((debug_routes) && (dhtlog_handle != NULL))
2307 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
2308 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2312 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2314 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2315 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2316 &msg_ctx->key, &msg_ctx->peer, NULL);
2322 /* check query valid */
2323 if (GNUNET_BLOCK_EVALUATION_REQUEST_INVALID ==
2324 GNUNET_BLOCK_evaluate (block_context, type, &msg_ctx->key,
2325 &msg_ctx->reply_bf, msg_ctx->reply_bf_mutator,
2326 msg_ctx->xquery, msg_ctx->xquery_size, NULL, 0))
2328 GNUNET_break_op (0);
2329 msg_ctx->do_forward = GNUNET_NO;
2333 if (msg_ctx->hop_count == 0) /* Locally initiated request */
2335 #if DEBUG_DHT_ROUTING
2336 if ((debug_routes) && (dhtlog_handle != NULL))
2338 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
2339 msg_ctx->hop_count, GNUNET_NO, &my_identity,
2344 if (msg_ctx->do_forward == GNUNET_YES)
2345 route_message (msg, msg_ctx);
2346 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
2352 remove_recent_find_peer (void *cls,
2353 const struct GNUNET_SCHEDULER_TaskContext *tc)
2355 GNUNET_HashCode *key = cls;
2357 GNUNET_assert (GNUNET_YES ==
2358 GNUNET_CONTAINER_multihashmap_remove
2359 (recent_find_peer_requests, key, NULL));
2365 * Server handler for initiating local dht find peer requests
2367 * @param find_msg the actual find peer message
2368 * @param msg_ctx struct containing pertinent information about the request
2372 handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
2373 struct DHT_MessageContext *msg_ctx)
2375 struct GNUNET_MessageHeader *find_peer_result;
2376 struct GNUNET_DHT_FindPeerMessage *find_peer_message;
2377 struct DHT_MessageContext *new_msg_ctx;
2378 struct GNUNET_CONTAINER_BloomFilter *incoming_bloom;
2381 GNUNET_HashCode *recent_hash;
2382 struct GNUNET_MessageHeader *other_hello;
2383 size_t other_hello_size;
2384 struct GNUNET_PeerIdentity peer_id;
2386 find_peer_message = (struct GNUNET_DHT_FindPeerMessage *) find_msg;
2387 GNUNET_break_op (ntohs (find_msg->size) >=
2388 (sizeof (struct GNUNET_DHT_FindPeerMessage)));
2389 if (ntohs (find_msg->size) < sizeof (struct GNUNET_DHT_FindPeerMessage))
2392 other_hello_size = 0;
2393 if (ntohs (find_msg->size) > sizeof (struct GNUNET_DHT_FindPeerMessage))
2396 ntohs (find_msg->size) - sizeof (struct GNUNET_DHT_FindPeerMessage);
2397 other_hello = GNUNET_malloc (other_hello_size);
2398 memcpy (other_hello, &find_peer_message[1], other_hello_size);
2399 if ((GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) other_hello) == 0)
2401 GNUNET_HELLO_get_id ((struct GNUNET_HELLO_Message *) other_hello,
2404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2405 "Received invalid HELLO message in find peer request!\n");
2406 GNUNET_free (other_hello);
2409 #if FIND_PEER_WITH_HELLO
2410 if (GNUNET_YES == consider_peer (&peer_id))
2412 increment_stats (STAT_HELLOS_PROVIDED);
2413 GNUNET_TRANSPORT_offer_hello (transport_handle, other_hello, NULL, NULL);
2414 GNUNET_CORE_peer_request_connect (coreAPI, &peer_id, NULL, NULL);
2415 route_message (find_msg, msg_ctx);
2416 GNUNET_free (other_hello);
2419 else /* We don't want this peer! */
2421 route_message (find_msg, msg_ctx);
2422 GNUNET_free (other_hello);
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2430 "`%s:%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
2431 my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&msg_ctx->key),
2432 ntohs (find_msg->size), sizeof (struct GNUNET_MessageHeader));
2434 if (my_hello == NULL)
2437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2438 "`%s': Our HELLO is null, can't return.\n", "DHT");
2440 GNUNET_free_non_null (other_hello);
2441 route_message (find_msg, msg_ctx);
2446 GNUNET_CONTAINER_bloomfilter_init (find_peer_message->bloomfilter,
2447 DHT_BLOOM_SIZE, DHT_BLOOM_K);
2449 GNUNET_CONTAINER_bloomfilter_test (incoming_bloom,
2450 &my_identity.hashPubKey))
2452 increment_stats (STAT_BLOOM_FIND_PEER);
2453 GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
2454 GNUNET_free_non_null (other_hello);
2455 route_message (find_msg, msg_ctx);
2456 return; /* We match the bloomfilter, do not send a response to this peer (they likely already know us!) */
2458 GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
2460 #if RESTRICT_FIND_PEER
2463 * Ignore any find peer requests from a peer we have seen very recently.
2465 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (recent_find_peer_requests, &msg_ctx->key)) /* We have recently responded to a find peer request for this peer! */
2467 increment_stats ("# dht find peer requests ignored (recently seen!)");
2468 GNUNET_free_non_null (other_hello);
2473 * Use this check to only allow the peer to respond to find peer requests if
2474 * it would be beneficial to have the requesting peer in this peers routing
2475 * table. Can be used to thwart peers flooding the network with find peer
2476 * requests that we don't care about. However, if a new peer is joining
2477 * the network and has no other peers this is a problem (assume all buckets
2478 * full, no one will respond!).
2480 memcpy (&peer_id.hashPubKey, &msg_ctx->key, sizeof (GNUNET_HashCode));
2481 if (GNUNET_NO == consider_peer (&peer_id))
2483 increment_stats ("# dht find peer requests ignored (do not need!)");
2484 GNUNET_free_non_null (other_hello);
2485 route_message (find_msg, msg_ctx);
2490 recent_hash = GNUNET_malloc (sizeof (GNUNET_HashCode));
2491 memcpy (recent_hash, &msg_ctx->key, sizeof (GNUNET_HashCode));
2492 if (GNUNET_SYSERR !=
2493 GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests,
2494 &msg_ctx->key, NULL,
2495 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "Adding recent remove task for key `%s`!\n",
2500 GNUNET_h2s (&msg_ctx->key));
2502 /* Only add a task if there wasn't one for this key already! */
2503 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2504 (GNUNET_TIME_UNIT_SECONDS, 30),
2505 &remove_recent_find_peer, recent_hash);
2509 GNUNET_free (recent_hash);
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512 "Received duplicate find peer request too soon!\n");
2516 /* Simplistic find_peer functionality, always return our hello */
2517 hello_size = ntohs (my_hello->size);
2518 tsize = hello_size + sizeof (struct GNUNET_MessageHeader);
2520 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2522 GNUNET_break_op (0);
2523 GNUNET_free_non_null (other_hello);
2527 find_peer_result = GNUNET_malloc (tsize);
2528 find_peer_result->type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT);
2529 find_peer_result->size = htons (tsize);
2530 memcpy (&find_peer_result[1], my_hello, hello_size);
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 "`%s': Sending hello size %d to requesting peer.\n", "DHT",
2537 new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
2538 memcpy (new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2539 new_msg_ctx->peer = my_identity;
2540 new_msg_ctx->bloom =
2541 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2542 new_msg_ctx->hop_count = 0;
2543 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make find peer requests a higher priority */
2544 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
2545 increment_stats (STAT_FIND_PEER_ANSWER);
2546 if (GNUNET_DHT_RO_RECORD_ROUTE ==
2547 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2549 new_msg_ctx->msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2550 new_msg_ctx->path_history_len = msg_ctx->path_history_len;
2551 /* Assign to previous msg_ctx path history, caller should free after our return */
2552 new_msg_ctx->path_history = msg_ctx->path_history;
2554 route_result_message (find_peer_result, new_msg_ctx);
2555 GNUNET_free (new_msg_ctx);
2556 #if DEBUG_DHT_ROUTING
2557 if ((debug_routes) && (dhtlog_handle != NULL))
2559 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER,
2560 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2564 GNUNET_free_non_null (other_hello);
2565 GNUNET_free (find_peer_result);
2566 route_message (find_msg, msg_ctx);
2571 * Server handler for initiating local dht put requests
2573 * @param msg the actual put message
2574 * @param msg_ctx struct containing pertinent information about the request
2577 handle_dht_put (const struct GNUNET_MessageHeader *msg,
2578 struct DHT_MessageContext *msg_ctx)
2580 const struct GNUNET_DHT_PutMessage *put_msg;
2581 struct DHTPutEntry *put_entry;
2582 unsigned int put_size;
2584 enum GNUNET_BLOCK_Type put_type;
2587 GNUNET_HashCode key;
2588 struct DHTQueryRecord *record;
2590 GNUNET_assert (ntohs (msg->size) >= sizeof (struct GNUNET_DHT_PutMessage));
2592 put_msg = (const struct GNUNET_DHT_PutMessage *) msg;
2593 put_type = (enum GNUNET_BLOCK_Type) ntohl (put_msg->type);
2595 if (put_type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
2597 #if DEBUG_DHT_ROUTING
2598 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2600 /** Log routes that die due to high load! */
2601 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2602 msg_ctx->hop_count, GNUNET_SYSERR,
2603 &my_identity, &msg_ctx->key, msg_ctx->peer,
2611 ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
2613 GNUNET_BLOCK_get_key (block_context, put_type, &put_msg[1], data_size,
2615 if (GNUNET_NO == ret)
2617 #if DEBUG_DHT_ROUTING
2618 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2620 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2621 msg_ctx->hop_count, GNUNET_SYSERR,
2622 &my_identity, &msg_ctx->key, &msg_ctx->peer,
2627 GNUNET_break_op (0);
2630 if ((GNUNET_YES == ret) &&
2631 (0 != memcmp (&key, &msg_ctx->key, sizeof (GNUNET_HashCode))))
2633 #if DEBUG_DHT_ROUTING
2634 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2636 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2637 msg_ctx->hop_count, GNUNET_SYSERR,
2638 &my_identity, &msg_ctx->key, &msg_ctx->peer,
2642 /* invalid wrapper: key mismatch! */
2643 GNUNET_break_op (0);
2646 /* ret == GNUNET_SYSERR means that there is no known relationship between
2647 * data and the key, so we cannot check it */
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2651 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
2652 msg_ctx->unique_id);
2654 #if DEBUG_DHT_ROUTING
2655 if (msg_ctx->hop_count == 0) /* Locally initiated request */
2657 if ((debug_routes) && (dhtlog_handle != NULL))
2659 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
2660 msg_ctx->hop_count, GNUNET_NO, &my_identity,
2666 record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap,
2670 struct DHTRouteSource *pos;
2671 struct GNUNET_DHT_GetResultMessage *get_result;
2672 struct DHT_MessageContext new_msg_ctx;
2678 /* TODO: do only for local started requests? or also for remote peers? */
2679 /* TODO: include this in statistics? under what? */
2680 /* TODO: reverse order of path_history? */
2681 if (NULL == pos->client)
2687 memcpy (&new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2688 if (GNUNET_DHT_RO_RECORD_ROUTE ==
2689 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2691 new_msg_ctx.msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2693 for (i = 0; i < new_msg_ctx.path_history_len; i++)
2696 &new_msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "(put for active get) Key %s Found peer %d:%s\n",
2699 GNUNET_h2s (&msg_ctx->key), i,
2700 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
2706 sizeof (struct GNUNET_DHT_GetResultMessage) + data_size +
2707 (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2708 get_result = GNUNET_malloc (get_size);
2709 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
2710 get_result->header.size = htons (get_size);
2711 get_result->expiration = put_msg->expiration;
2712 get_result->type = put_msg->type;
2713 get_result->put_path_length = htons (msg_ctx->path_history_len);
2715 /* Copy the actual data and the path_history to the end of the get result */
2716 memcpy (&get_result[1], &put_msg[1], data_size);
2717 path_offset = (char *) &get_result[1];
2718 path_offset += data_size;
2719 memcpy (path_offset, msg_ctx->path_history,
2720 msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2721 new_msg_ctx.peer = my_identity;
2722 new_msg_ctx.bloom = NULL;
2723 new_msg_ctx.hop_count = 0;
2724 /* Make result routing a higher priority */
2725 new_msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;
2726 new_msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
2727 new_msg_ctx.unique_id = pos->uid;
2728 send_reply_to_client(pos->client, &get_result->header, &new_msg_ctx);
2729 GNUNET_free (get_result);
2734 if (msg_ctx->closest != GNUNET_YES)
2736 route_message (msg, msg_ctx);
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2742 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2743 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
2744 msg_ctx->unique_id);
2747 #if DEBUG_DHT_ROUTING
2748 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2750 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2751 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2752 &msg_ctx->key, &msg_ctx->peer, NULL);
2755 if ((debug_routes) && (dhtlog_handle != NULL))
2757 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
2758 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2763 increment_stats (STAT_PUTS_INSERTED);
2764 if (datacache != NULL)
2766 /* Put size is actual data size plus struct overhead plus path length (if any) */
2768 data_size + sizeof (struct DHTPutEntry) +
2769 (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2770 put_entry = GNUNET_malloc (put_size);
2771 put_entry->data_size = data_size;
2772 put_entry->path_length = msg_ctx->path_history_len;
2773 /* Copy data to end of put entry */
2774 memcpy (&put_entry[1], &put_msg[1], data_size);
2775 if (msg_ctx->path_history_len > 0)
2777 /* Copy path after data */
2778 path_offset = (char *) &put_entry[1];
2779 path_offset += data_size;
2780 memcpy (path_offset, msg_ctx->path_history,
2781 msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2785 GNUNET_DATACACHE_put (datacache, &msg_ctx->key, put_size,
2786 (const char *) put_entry, put_type,
2787 GNUNET_TIME_absolute_ntoh (put_msg->expiration));
2788 GNUNET_free (put_entry);
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2792 "`%s:%s': %s request received, but have no datacache!\n",
2793 my_short_id, "DHT", "PUT");
2795 if (stop_on_closest == GNUNET_NO)
2796 route_message (msg, msg_ctx);
2801 * To how many peers should we (on average)
2802 * forward the request to obtain the desired
2803 * target_replication count (on average).
2805 * returns: target_replication / (est. hops) + (target_replication * hop_count)
2806 * where est. hops is typically 2 * the routing table depth
2808 * @param hop_count number of hops the message has traversed
2809 * @param target_replication the number of total paths desired
2811 * @return Some number of peers to forward the message to
2814 get_forward_count (unsigned int hop_count, size_t target_replication)
2816 uint32_t random_value;
2817 unsigned int forward_count;
2821 * If we are behaving in strict kademlia mode, send multiple initial requests,
2822 * but then only send to 1 or 0 peers based strictly on the number of hops.
2824 if (strict_kademlia == GNUNET_YES)
2827 return kademlia_replication;
2828 if (hop_count < log_of_network_size_estimate * 2.0)
2833 if (hop_count > log_of_network_size_estimate * 2.0)
2835 if (GNUNET_YES == paper_forwarding)
2837 /* Once we have reached our ideal number of hops, don't stop forwarding! */
2841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2842 "Hop count too high (est %f, lowest %d), NOT Forwarding request\n",
2843 log_of_network_size_estimate * 2.0, lowest_bucket);
2848 if (GNUNET_YES == paper_forwarding)
2850 /* FIXME: re-run replication trials with this formula */
2852 1 + (target_replication - 1.0) / (log_of_network_size_estimate +
2853 ((float) (target_replication - 1.0) *
2855 /* Set forward count to floor of target_value */
2856 forward_count = (unsigned int) target_value;
2857 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
2858 target_value = target_value - forward_count;
2860 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
2862 if (random_value < (target_value * UINT32_MAX))
2870 target_replication / (log_of_network_size_estimate +
2871 ((float) target_replication * hop_count));
2872 if (target_value > 1)
2874 /* Set forward count to floor of target_value */
2875 forward_count = (unsigned int) target_value;
2876 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
2877 target_value = target_value - forward_count;
2881 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
2883 if (random_value < (target_value * UINT32_MAX))
2887 return forward_count;
2892 * Check whether my identity is closer than any known peers.
2893 * If a non-null bloomfilter is given, check if this is the closest
2894 * peer that hasn't already been routed to.
2896 * @param target hash code to check closeness to
2897 * @param bloom bloomfilter, exclude these entries from the decision
2898 * @return GNUNET_YES if node location is closest,
2899 * GNUNET_NO otherwise.
2902 am_closest_peer (const GNUNET_HashCode * target,
2903 struct GNUNET_CONTAINER_BloomFilter *bloom)
2909 struct PeerInfo *pos;
2910 unsigned int my_distance;
2912 if (0 == memcmp (&my_identity.hashPubKey, target, sizeof (GNUNET_HashCode)))
2915 bucket_num = find_current_bucket (target);
2917 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, target);
2918 my_distance = distance (&my_identity.hashPubKey, target);
2919 pos = k_buckets[bucket_num].head;
2921 while ((pos != NULL) && (count < bucket_size))
2923 if ((bloom != NULL) &&
2925 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
2928 continue; /* Skip already checked entries */
2931 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, target);
2932 if (other_bits > bits)
2934 else if (other_bits == bits) /* We match the same number of bits */
2936 if (strict_kademlia != GNUNET_YES) /* Return that we at as close as any other peer */
2938 if (distance (&pos->id.hashPubKey, target) < my_distance) /* Check all known peers, only return if we are the true closest */
2944 /* No peers closer, we are the closest! */
2950 * Select a peer from the routing table that would be a good routing
2951 * destination for sending a message for "target". The resulting peer
2952 * must not be in the set of blocked peers.<p>
2954 * Note that we should not ALWAYS select the closest peer to the
2955 * target, peers further away from the target should be chosen with
2956 * exponentially declining probability.
2958 * @param target the key we are selecting a peer to route to
2959 * @param bloom a bloomfilter containing entries this request has seen already
2960 * @param hops how many hops has this message traversed thus far
2962 * @return Peer to route to, or NULL on error
2964 static struct PeerInfo *
2965 select_peer (const GNUNET_HashCode * target,
2966 struct GNUNET_CONTAINER_BloomFilter *bloom, unsigned int hops)
2970 unsigned int selected;
2971 struct PeerInfo *pos;
2972 unsigned int distance;
2973 unsigned int largest_distance;
2974 struct PeerInfo *chosen;
2976 /** If we are doing kademlia routing (saves some cycles) */
2977 if ((strict_kademlia == GNUNET_YES) || (hops >= log_of_network_size_estimate))
2979 /* greedy selection (closest peer that is not in bloomfilter) */
2980 largest_distance = 0;
2982 for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
2984 pos = k_buckets[bc].head;
2986 while ((pos != NULL) && (count < bucket_size))
2988 /* If we are doing strict Kademlia routing, then checking the bloomfilter is basically cheating! */
2990 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
2992 distance = inverse_distance (target, &pos->id.hashPubKey);
2993 if (distance > largest_distance)
2996 largest_distance = distance;
3003 if ((largest_distance > 0) && (chosen != NULL))
3005 GNUNET_CONTAINER_bloomfilter_add (bloom, &chosen->id.hashPubKey);
3008 return NULL; /* no peer available or we are the closest */
3012 /* select "random" peer */
3013 /* count number of peers that are available and not filtered */
3015 for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
3017 pos = k_buckets[bc].head;
3018 while ((pos != NULL) && (count < bucket_size))
3021 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3024 continue; /* Ignore bloomfiltered peers */
3030 if (count == 0) /* No peers to select from! */
3032 increment_stats ("# failed to select peer");
3035 /* Now actually choose a peer */
3036 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
3038 for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
3040 pos = k_buckets[bc].head;
3041 while ((pos != NULL) && (count < bucket_size))
3044 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3047 continue; /* Ignore bloomfiltered peers */
3049 if (0 == selected--)
3060 * Task used to remove recent entries, either
3061 * after timeout, when full, or on shutdown.
3063 * @param cls the entry to remove
3064 * @param tc context, reason, etc.
3067 remove_recent (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3069 struct RecentRequest *req = cls;
3070 static GNUNET_HashCode hash;
3072 GNUNET_assert (req != NULL);
3073 hash_from_uid (req->uid, &hash);
3074 GNUNET_assert (GNUNET_YES ==
3075 GNUNET_CONTAINER_multihashmap_remove (recent.hashmap, &hash,
3077 GNUNET_CONTAINER_heap_remove_node (req->heap_node);
3078 GNUNET_CONTAINER_bloomfilter_free (req->bloom);
3082 * if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) && (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)) && (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap)))
3084 * GNUNET_CONTAINER_multihashmap_destroy(recent.hashmap);
3085 * GNUNET_CONTAINER_heap_destroy(recent.minHeap);
3091 * Remember this routing request so that if a reply is
3092 * received we can either forward it to the correct peer
3093 * or return the result locally.
3095 * @param msg_ctx Context of the route request
3097 * @return GNUNET_YES if this response was cached, GNUNET_NO if not
3100 cache_response (struct DHT_MessageContext *msg_ctx)
3102 struct DHTQueryRecord *record;
3103 struct DHTRouteSource *source_info;
3104 struct DHTRouteSource *pos;
3105 struct GNUNET_TIME_Absolute now;
3106 unsigned int current_size;
3108 current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
3110 #if DELETE_WHEN_FULL
3111 while (current_size >= MAX_OUTSTANDING_FORWARDS)
3113 source_info = GNUNET_CONTAINER_heap_remove_root (forward_list.minHeap);
3114 GNUNET_assert (source_info != NULL);
3115 record = source_info->record;
3116 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
3117 if (record->head == NULL) /* No more entries in DLL */
3119 GNUNET_assert (GNUNET_YES ==
3120 GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
3123 GNUNET_free (record);
3125 if (source_info->delete_task != GNUNET_SCHEDULER_NO_TASK)
3127 GNUNET_SCHEDULER_cancel (source_info->delete_task);
3128 source_info->delete_task = GNUNET_SCHEDULER_NO_TASK;
3130 if (source_info->find_peers_responded != NULL)
3131 GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
3132 GNUNET_free (source_info);
3133 current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
3136 /** Non-local request and have too many outstanding forwards, discard! */
3137 if ((current_size >= MAX_OUTSTANDING_FORWARDS) && (msg_ctx->client == NULL))
3140 now = GNUNET_TIME_absolute_get ();
3142 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
3143 if (record != NULL) /* Already know this request! */
3149 memcmp (&msg_ctx->peer, &pos->source,
3150 sizeof (struct GNUNET_PeerIdentity)))
3151 break; /* Already have this peer in reply list! */
3154 if ((pos != NULL) && (pos->client == msg_ctx->client)) /* Seen this already */
3156 GNUNET_CONTAINER_heap_update_cost (forward_list.minHeap, pos->hnode,
3163 record = GNUNET_malloc (sizeof (struct DHTQueryRecord));
3164 GNUNET_assert (GNUNET_OK ==
3165 GNUNET_CONTAINER_multihashmap_put (forward_list.hashmap,
3166 &msg_ctx->key, record,
3167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3168 memcpy (&record->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
3171 source_info = GNUNET_malloc (sizeof (struct DHTRouteSource));
3172 source_info->record = record;
3173 source_info->delete_task =
3174 GNUNET_SCHEDULER_add_delayed (DHT_FORWARD_TIMEOUT, &remove_forward_entry,
3176 source_info->find_peers_responded =
3177 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3178 source_info->source = msg_ctx->peer;
3179 GNUNET_CONTAINER_DLL_insert_after (record->head, record->tail, record->tail,
3181 if (msg_ctx->client != NULL) /* For local request, set timeout so high it effectively never gets pushed out */
3183 source_info->client = msg_ctx->client;
3184 now = GNUNET_TIME_absolute_get_forever ();
3186 source_info->hnode =
3187 GNUNET_CONTAINER_heap_insert (forward_list.minHeap, source_info,
3189 source_info->uid = msg_ctx->unique_id;
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192 "`%s:%s': Created new forward source info for %s uid %llu\n",
3193 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
3194 msg_ctx->unique_id);
3201 * Main function that handles whether or not to route a message to other
3204 * @param msg the message to be routed
3205 * @param msg_ctx the context containing all pertinent information about the message
3208 route_message (const struct GNUNET_MessageHeader *msg,
3209 struct DHT_MessageContext *msg_ctx)
3212 struct PeerInfo *selected;
3214 #if DEBUG_DHT_ROUTING > 1
3215 struct PeerInfo *nearest;
3217 unsigned int target_forward_count;
3218 unsigned int forward_count;
3219 struct RecentRequest *recent_req;
3220 GNUNET_HashCode unique_hash;
3221 char *stat_forward_count;
3222 char *temp_stat_str;
3224 #if DEBUG_DHT_ROUTING
3228 if (malicious_dropper == GNUNET_YES)
3230 #if DEBUG_DHT_ROUTING
3231 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3233 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3234 msg_ctx->hop_count, GNUNET_SYSERR,
3235 &my_identity, &msg_ctx->key, &msg_ctx->peer,
3239 if (msg_ctx->bloom != NULL)
3241 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
3242 msg_ctx->bloom = NULL;
3247 increment_stats (STAT_ROUTES);
3248 target_forward_count =
3249 get_forward_count (msg_ctx->hop_count, msg_ctx->replication);
3250 GNUNET_asprintf (&stat_forward_count, "# forward counts of %d",
3251 target_forward_count);
3252 increment_stats (stat_forward_count);
3253 GNUNET_free (stat_forward_count);
3254 if (msg_ctx->bloom == NULL)
3256 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3258 if ((stop_on_closest == GNUNET_YES) && (msg_ctx->closest == GNUNET_YES) &&
3259 (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
3260 target_forward_count = 0;
3263 * NOTICE: In Kademlia, a find peer request goes no further if the peer doesn't return
3264 * any closer peers (which is being checked for below). Since we are doing recursive
3265 * routing we have no choice but to stop forwarding in this case. This means that at
3266 * any given step the request may NOT be forwarded to alpha peers (because routes will
3267 * stop and the parallel route will not be aware of it). Of course, assuming that we
3268 * have fulfilled the Kademlia requirements for routing table fullness this will never
3269 * ever ever be a problem.
3271 * However, is this fair?
3273 * Since we use these requests to build our routing tables (and we build them in the
3274 * testing driver) we will ignore this restriction for FIND_PEER messages so that
3275 * routing tables still get constructed.
3277 if ((GNUNET_YES == strict_kademlia) && (msg_ctx->closest == GNUNET_YES) &&
3278 (msg_ctx->hop_count > 0) &&
3279 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER))
3280 target_forward_count = 0;
3283 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom, &my_identity.hashPubKey);
3284 hash_from_uid (msg_ctx->unique_id, &unique_hash);
3286 GNUNET_CONTAINER_multihashmap_contains (recent.hashmap, &unique_hash))
3289 GNUNET_CONTAINER_multihashmap_get (recent.hashmap, &unique_hash);
3290 GNUNET_assert (recent_req != NULL);
3291 if (0 != memcmp (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode)))
3292 increment_stats (STAT_DUPLICATE_UID);
3295 increment_stats (STAT_RECENT_SEEN);
3296 GNUNET_CONTAINER_bloomfilter_or2 (msg_ctx->bloom, recent_req->bloom,
3302 recent_req = GNUNET_malloc (sizeof (struct RecentRequest));
3303 recent_req->uid = msg_ctx->unique_id;
3304 memcpy (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
3305 recent_req->remove_task =
3306 GNUNET_SCHEDULER_add_delayed (DEFAULT_RECENT_REMOVAL, &remove_recent,
3308 recent_req->heap_node =
3309 GNUNET_CONTAINER_heap_insert (recent.minHeap, recent_req,
3310 GNUNET_TIME_absolute_get ().abs_value);
3312 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3313 GNUNET_CONTAINER_multihashmap_put (recent.hashmap, &unique_hash, recent_req,
3314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3317 if (GNUNET_CONTAINER_multihashmap_size (recent.hashmap) > DHT_MAX_RECENT)
3319 recent_req = GNUNET_CONTAINER_heap_peek (recent.minHeap);
3320 GNUNET_assert (recent_req != NULL);
3321 GNUNET_SCHEDULER_cancel (recent_req->remove_task);
3322 recent_req->remove_task =
3323 GNUNET_SCHEDULER_add_now (&remove_recent, recent_req);
3327 for (i = 0; i < target_forward_count; i++)
3329 selected = select_peer (&msg_ctx->key, msg_ctx->bloom, msg_ctx->hop_count);
3331 if (selected != NULL)
3334 if (GNUNET_CRYPTO_hash_matching_bits
3335 (&selected->id.hashPubKey,
3337 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
3339 GNUNET_asprintf (&temp_stat_str,
3340 "# requests routed to close(r) peer hop %u",
3341 msg_ctx->hop_count);
3343 GNUNET_asprintf (&temp_stat_str,
3344 "# requests routed to less close peer hop %u",
3345 msg_ctx->hop_count);
3346 if (temp_stat_str != NULL)
3348 increment_stats (temp_stat_str);
3349 GNUNET_free (temp_stat_str);
3351 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
3352 &selected->id.hashPubKey);
3353 #if DEBUG_DHT_ROUTING > 1
3354 nearest = find_closest_peer (&msg_ctx->key);
3355 nearest_buf = GNUNET_strdup (GNUNET_i2s (&nearest->id));
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3357 "`%s:%s': Forwarding request key %s uid %llu to peer %s (closest %s, bits %d, distance %u)\n",
3358 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
3359 msg_ctx->unique_id, GNUNET_i2s (&selected->id), nearest_buf,
3360 GNUNET_CRYPTO_hash_matching_bits (&nearest->id.hashPubKey,
3362 distance (&nearest->id.hashPubKey, msg_ctx->key));
3363 GNUNET_free (nearest_buf);
3365 #if DEBUG_DHT_ROUTING
3366 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3368 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3369 msg_ctx->hop_count, GNUNET_NO,
3370 &my_identity, &msg_ctx->key, &msg_ctx->peer,
3374 forward_message (msg, selected, msg_ctx);
3378 if (msg_ctx->bloom != NULL)
3380 GNUNET_CONTAINER_bloomfilter_or2 (recent_req->bloom, msg_ctx->bloom,
3382 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
3383 msg_ctx->bloom = NULL;
3386 #if DEBUG_DHT_ROUTING
3387 if (forward_count == 0)
3388 ret = GNUNET_SYSERR;
3392 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3394 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3395 msg_ctx->hop_count, ret, &my_identity,
3396 &msg_ctx->key, &msg_ctx->peer, NULL);
3403 * Main function that handles whether or not to route a message to other
3406 * @param msg the message to be routed
3407 * @param msg_ctx the context containing all pertinent information about the message
3410 demultiplex_message (const struct GNUNET_MessageHeader *msg,
3411 struct DHT_MessageContext *msg_ctx)
3413 /* FIXME: Should we use closest excluding those we won't route to (the bloomfilter problem)? */
3414 msg_ctx->closest = am_closest_peer (&msg_ctx->key, msg_ctx->bloom);
3416 switch (ntohs (msg->type))
3418 case GNUNET_MESSAGE_TYPE_DHT_GET: /* Add to hashmap of requests seen, search for data (always) */
3419 cache_response (msg_ctx);
3420 handle_dht_get (msg, msg_ctx);
3422 case GNUNET_MESSAGE_TYPE_DHT_PUT: /* Check if closest, if so insert data. */
3423 increment_stats (STAT_PUTS);
3424 handle_dht_put (msg, msg_ctx);
3426 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: /* Check if closest and not started by us, check options, add to requests seen */
3427 increment_stats (STAT_FIND_PEER);
3428 if (((msg_ctx->hop_count > 0) &&
3430 memcmp (&msg_ctx->peer, &my_identity,
3431 sizeof (struct GNUNET_PeerIdentity)))) ||
3432 (msg_ctx->client != NULL))
3434 cache_response (msg_ctx);
3435 if ((msg_ctx->closest == GNUNET_YES) ||
3436 (msg_ctx->msg_options == GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE))
3437 handle_dht_find_peer (msg, msg_ctx);
3440 route_message (msg, msg_ctx);
3441 #if DEBUG_DHT_ROUTING
3442 if (msg_ctx->hop_count == 0) /* Locally initiated request */
3444 if ((debug_routes) && (dhtlog_handle != NULL))
3446 dhtlog_handle->insert_dhtkey (NULL, &msg_ctx->key);
3447 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER,
3448 msg_ctx->hop_count, GNUNET_NO,
3449 &my_identity, &msg_ctx->key);
3455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3456 "`%s': Message type (%d) not handled, forwarding anyway!\n",
3457 "DHT", ntohs (msg->type));
3458 route_message (msg, msg_ctx);
3464 * Iterator over hash map entries.
3466 * @param cls client to search for in source routes
3467 * @param key current key code (ignored)
3468 * @param value value in the hash map, a DHTQueryRecord
3469 * @return GNUNET_YES if we should continue to
3474 find_client_records (void *cls, const GNUNET_HashCode * key, void *value)
3476 struct ClientList *client = cls;
3477 struct DHTQueryRecord *record = value;
3478 struct DHTRouteSource *pos;
3483 if (pos->client == client)
3489 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, pos);
3490 GNUNET_CONTAINER_heap_remove_node (pos->hnode);
3491 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
3493 GNUNET_SCHEDULER_cancel (pos->delete_task);
3494 pos->delete_task = GNUNET_SCHEDULER_NO_TASK;
3496 if (pos->find_peers_responded != NULL)
3497 GNUNET_CONTAINER_bloomfilter_free (pos->find_peers_responded);
3500 if (record->head == NULL) /* No more entries in DLL */
3502 GNUNET_assert (GNUNET_YES ==
3503 GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
3504 &record->key, record));
3505 GNUNET_free (record);
3511 * Functions with this signature are called whenever a client
3512 * is disconnected on the network level.
3514 * @param cls closure (NULL for dht)
3515 * @param client identification of the client; NULL
3516 * for the last call when the server is destroyed
3519 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3521 struct ClientList *pos = client_list;
3522 struct ClientList *prev;
3523 struct ClientList *found;
3524 struct PendingMessage *reply;
3530 if (pos->client_handle == client)
3533 prev->next = pos->next;
3535 client_list = pos->next;
3545 if (found->transmit_handle != NULL)
3546 GNUNET_CONNECTION_notify_transmit_ready_cancel (found->transmit_handle);
3548 while (NULL != (reply = found->pending_head))
3550 GNUNET_CONTAINER_DLL_remove (found->pending_head, found->pending_tail,
3552 GNUNET_free (reply);
3554 GNUNET_CONTAINER_multihashmap_iterate (forward_list.hashmap,
3555 &find_client_records, found);
3556 GNUNET_free (found);
3561 * Find a client if it exists, add it otherwise.
3563 * @param client the server handle to the client
3565 * @return the client if found, a new client otherwise
3567 static struct ClientList *
3568 find_active_client (struct GNUNET_SERVER_Client *client)
3570 struct ClientList *pos = client_list;
3571 struct ClientList *ret;
3575 if (pos->client_handle == client)
3580 ret = GNUNET_malloc (sizeof (struct ClientList));
3581 ret->client_handle = client;
3582 ret->next = client_list;
3590 * Task to send a malicious put message across the network.
3592 * @param cls closure for this task
3593 * @param tc the context under which the task is running
3596 malicious_put_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3598 static struct GNUNET_DHT_PutMessage put_message;
3599 static struct DHT_MessageContext msg_ctx;
3600 static GNUNET_HashCode key;
3601 uint32_t random_key;
3603 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3605 put_message.header.size = htons (sizeof (struct GNUNET_DHT_PutMessage));
3606 put_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
3607 put_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
3608 put_message.expiration =
3609 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_forever ());
3610 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3612 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
3613 GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
3614 memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
3616 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3617 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
3618 msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3619 msg_ctx.msg_options = ntohl (0);
3620 msg_ctx.network_size = log_of_network_size_estimate;
3621 msg_ctx.peer = &my_identity;
3622 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
3623 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3624 #if DEBUG_DHT_ROUTING
3625 if (dhtlog_handle != NULL)
3626 dhtlog_handle->insert_dhtkey (NULL, &key);
3628 increment_stats (STAT_PUT_START);
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3630 "%s:%s Sending malicious PUT message with hash %s\n", my_short_id,
3631 "DHT", GNUNET_h2s (&key));
3632 demultiplex_message (&put_message.header, &msg_ctx);
3633 GNUNET_SCHEDULER_add_delayed (malicious_put_frequency, &malicious_put_task,
3639 * Task to send a malicious put message across the network.
3641 * @param cls closure for this task
3642 * @param tc the context under which the task is running
3645 malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3647 static struct GNUNET_DHT_GetMessage get_message;
3648 struct DHT_MessageContext msg_ctx;
3649 static GNUNET_HashCode key;
3650 uint32_t random_key;
3652 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3655 get_message.header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
3656 get_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
3657 get_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
3658 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3660 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
3661 GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
3662 memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
3664 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3665 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
3666 msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3667 msg_ctx.msg_options = ntohl (0);
3668 msg_ctx.network_size = log_of_network_size_estimate;
3669 msg_ctx.peer = &my_identity;
3670 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
3671 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3672 #if DEBUG_DHT_ROUTING
3673 if (dhtlog_handle != NULL)
3674 dhtlog_handle->insert_dhtkey (NULL, &key);
3676 increment_stats (STAT_GET_START);
3677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3678 "%s:%s Sending malicious GET message with hash %s\n", my_short_id,
3679 "DHT", GNUNET_h2s (&key));
3680 demultiplex_message (&get_message.header, &msg_ctx);
3681 GNUNET_SCHEDULER_add_delayed (malicious_get_frequency, &malicious_get_task,
3688 * Iterator over hash map entries.
3690 * @param cls closure
3691 * @param key current key code
3692 * @param value value in the hash map
3693 * @return GNUNET_YES if we should continue to
3698 add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
3700 struct GNUNET_CONTAINER_BloomFilter *bloom = cls;
3702 GNUNET_CONTAINER_bloomfilter_add (bloom, key);
3707 * Task to send a find peer message for our own peer identifier
3708 * so that we can find the closest peers in the network to ourselves
3709 * and attempt to connect to them.
3711 * @param cls closure for this task
3712 * @param tc the context under which the task is running
3715 send_find_peer_message (void *cls,
3716 const struct GNUNET_SCHEDULER_TaskContext *tc)
3718 struct GNUNET_DHT_FindPeerMessage *find_peer_msg;
3719 struct DHT_MessageContext msg_ctx;
3720 struct GNUNET_TIME_Relative next_send_time;
3721 struct GNUNET_CONTAINER_BloomFilter *temp_bloom;
3723 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3726 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! */
3728 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3729 "Have %d newly found peers since last find peer message sent!\n",
3731 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
3732 &send_find_peer_message, NULL);
3733 newly_found_peers = 0;
3737 increment_stats (STAT_FIND_PEER_START);
3738 #if FIND_PEER_WITH_HELLO
3740 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage) +
3741 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *)
3743 find_peer_msg->header.size =
3744 htons (sizeof (struct GNUNET_DHT_FindPeerMessage) +
3745 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
3746 memcpy (&find_peer_msg[1], my_hello,
3747 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
3749 find_peer_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage));
3750 find_peer_msg->header.size =
3751 htons (sizeof (struct GNUNET_DHT_FindPeerMessage));
3753 find_peer_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
3755 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3756 GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, &add_known_to_bloom,
3758 GNUNET_assert (GNUNET_OK ==
3759 GNUNET_CONTAINER_bloomfilter_get_raw_data (temp_bloom,
3763 GNUNET_CONTAINER_bloomfilter_free (temp_bloom);
3764 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3765 memcpy (&msg_ctx.key, &my_identity.hashPubKey, sizeof (GNUNET_HashCode));
3767 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3768 (GNUNET_CRYPTO_QUALITY_STRONG, UINT64_MAX));
3769 msg_ctx.replication = DHT_DEFAULT_FIND_PEER_REPLICATION;
3770 msg_ctx.msg_options = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
3771 msg_ctx.network_size = log_of_network_size_estimate;
3772 msg_ctx.peer = my_identity;
3773 msg_ctx.importance = DHT_DEFAULT_FIND_PEER_IMPORTANCE;
3774 msg_ctx.timeout = DHT_DEFAULT_FIND_PEER_TIMEOUT;
3776 demultiplex_message (&find_peer_msg->header, &msg_ctx);
3777 GNUNET_free (find_peer_msg);
3778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3779 "`%s:%s': Sent `%s' request to some (?) peers\n", my_short_id,
3780 "DHT", "FIND PEER");
3781 if (newly_found_peers < bucket_size)
3783 next_send_time.rel_value =
3784 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
3785 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
3786 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
3790 next_send_time.rel_value =
3791 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
3792 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
3793 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value -
3794 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
3797 GNUNET_assert (next_send_time.rel_value != 0);
3798 find_peer_context.count = 0;
3799 newly_found_peers = 0;
3800 find_peer_context.start = GNUNET_TIME_absolute_get ();
3801 if (GNUNET_YES == do_find_peer)
3803 GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
3809 * Handler for any generic DHT messages, calls the appropriate handler
3810 * depending on message type, sends confirmation if responses aren't otherwise
3813 * @param cls closure for the service
3814 * @param client the client we received this message from
3815 * @param message the actual message received
3818 handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client,
3819 const struct GNUNET_MessageHeader *message)
3821 const struct GNUNET_DHT_RouteMessage *dht_msg =
3822 (const struct GNUNET_DHT_RouteMessage *) message;
3823 const struct GNUNET_MessageHeader *enc_msg;
3824 struct DHT_MessageContext msg_ctx;
3826 enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
3828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3829 "`%s:%s': Received `%s' request from client, message type %d, key %s, uid %llu\n",
3830 my_short_id, "DHT", "GENERIC", ntohs (message->type),
3831 GNUNET_h2s (&dht_msg->key), GNUNET_ntohll (dht_msg->unique_id));
3833 #if DEBUG_DHT_ROUTING
3834 if (dhtlog_handle != NULL)
3835 dhtlog_handle->insert_dhtkey (NULL, &dht_msg->key);
3838 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3839 msg_ctx.client = find_active_client (client);
3840 memcpy (&msg_ctx.key, &dht_msg->key, sizeof (GNUNET_HashCode));
3841 msg_ctx.unique_id = GNUNET_ntohll (dht_msg->unique_id);
3842 msg_ctx.replication = ntohl (dht_msg->desired_replication_level);
3843 msg_ctx.msg_options = ntohl (dht_msg->options);
3844 if (GNUNET_DHT_RO_RECORD_ROUTE ==
3845 (msg_ctx.msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
3847 msg_ctx.path_history = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
3848 memcpy (msg_ctx.path_history, &my_identity,
3849 sizeof (struct GNUNET_PeerIdentity));
3850 msg_ctx.path_history_len = 1;
3852 msg_ctx.network_size = log_of_network_size_estimate;
3853 msg_ctx.peer = my_identity;
3854 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 4; /* Make local routing a higher priority */
3855 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3857 if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
3858 increment_stats (STAT_GET_START);
3859 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
3860 increment_stats (STAT_PUT_START);
3861 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER)
3862 increment_stats (STAT_FIND_PEER_START);
3864 if (GNUNET_YES == malicious_dropper)
3866 if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
3868 #if DEBUG_DHT_ROUTING
3869 if ((debug_routes) && (dhtlog_handle != NULL))
3871 dhtlog_handle->insert_query (NULL, msg_ctx.unique_id, DHTLOG_GET,
3872 msg_ctx.hop_count, GNUNET_NO, &my_identity,
3877 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
3879 #if DEBUG_DHT_ROUTING
3880 if ((debug_routes) && (dhtlog_handle != NULL))
3882 dhtlog_handle->insert_query (NULL, msg_ctx.unique_id, DHTLOG_PUT,
3883 msg_ctx.hop_count, GNUNET_NO, &my_identity,
3888 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3889 GNUNET_free_non_null (msg_ctx.path_history);
3893 demultiplex_message (enc_msg, &msg_ctx);
3894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3899 * Handler for any locally received DHT control messages,
3900 * sets malicious flags mostly for now.
3902 * @param cls closure for the service
3903 * @param client the client we received this message from
3904 * @param message the actual message received
3908 handle_dht_control_message (void *cls, struct GNUNET_SERVER_Client *client,
3909 const struct GNUNET_MessageHeader *message)
3911 const struct GNUNET_DHT_ControlMessage *dht_control_msg =
3912 (const struct GNUNET_DHT_ControlMessage *) message;
3915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3916 "`%s:%s': Received `%s' request from client, command %d\n",
3917 my_short_id, "DHT", "CONTROL", ntohs (dht_control_msg->command));
3920 switch (ntohs (dht_control_msg->command))
3922 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3924 "Sending self seeking find peer request!\n");
3925 GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
3928 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
3929 if (ntohs (dht_control_msg->variable) > 0)
3930 malicious_get_frequency.rel_value = ntohs (dht_control_msg->variable);
3931 if (malicious_get_frequency.rel_value == 0)
3932 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
3933 if (malicious_getter != GNUNET_YES)
3934 GNUNET_SCHEDULER_add_now (&malicious_get_task, NULL);
3935 malicious_getter = GNUNET_YES;
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3937 "%s:%s Initiating malicious GET behavior, frequency %llu\n",
3938 my_short_id, "DHT", malicious_get_frequency.rel_value);
3940 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
3941 if (ntohs (dht_control_msg->variable) > 0)
3942 malicious_put_frequency.rel_value = ntohs (dht_control_msg->variable);
3943 if (malicious_put_frequency.rel_value == 0)
3944 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
3945 if (malicious_putter != GNUNET_YES)
3946 GNUNET_SCHEDULER_add_now (&malicious_put_task, NULL);
3947 malicious_putter = GNUNET_YES;
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3949 "%s:%s Initiating malicious PUT behavior, frequency %d\n",
3950 my_short_id, "DHT", malicious_put_frequency);
3952 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
3953 #if DEBUG_DHT_ROUTING
3954 if ((malicious_dropper != GNUNET_YES) && (dhtlog_handle != NULL))
3955 dhtlog_handle->set_malicious (&my_identity);
3957 malicious_dropper = GNUNET_YES;
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3959 "%s:%s Initiating malicious DROP behavior\n", my_short_id,
3964 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3965 "%s:%s Unknown control command type `%d'!\n", my_short_id,
3966 "DHT", ntohs (dht_control_msg->command));
3970 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3974 * Handler for any generic DHT stop messages, calls the appropriate handler
3975 * depending on message type (if processed locally)
3977 * @param cls closure for the service
3978 * @param client the client we received this message from
3979 * @param message the actual message received
3983 handle_dht_local_route_stop (void *cls, struct GNUNET_SERVER_Client *client,
3984 const struct GNUNET_MessageHeader *message)
3987 const struct GNUNET_DHT_StopMessage *dht_stop_msg =
3988 (const struct GNUNET_DHT_StopMessage *) message;
3989 struct DHTQueryRecord *record;
3990 struct DHTRouteSource *pos;
3993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3994 "`%s:%s': Received `%s' request from client, uid %llu\n",
3995 my_short_id, "DHT", "GENERIC STOP",
3996 GNUNET_ntohll (dht_stop_msg->unique_id));
3999 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap,
4000 &dht_stop_msg->key);
4007 /* If the client is non-null (local request) and the client matches the requesting client, remove the entry. */
4008 if ((pos->client != NULL) && (pos->client->client_handle == client))
4010 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
4011 GNUNET_SCHEDULER_cancel (pos->delete_task);
4013 GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);
4019 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4024 * Core handler for p2p route requests.
4026 * @param cls closure
4027 * @param message message
4028 * @param peer peer identity this notification is about
4029 * @param atsi performance data
4030 * @return GNUNET_OK to keep the connection open,
4031 * GNUNET_SYSERR to close it (signal serious error)
4034 handle_dht_p2p_route_request (void *cls, const struct GNUNET_PeerIdentity *peer,
4035 const struct GNUNET_MessageHeader *message,
4036 const struct GNUNET_TRANSPORT_ATS_Information
4040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4041 "`%s:%s': Received P2P request from peer %s\n", my_short_id,
4042 "DHT", GNUNET_i2s (peer));
4044 struct GNUNET_DHT_P2PRouteMessage *incoming =
4045 (struct GNUNET_DHT_P2PRouteMessage *) message;
4046 struct GNUNET_MessageHeader *enc_msg =
4047 (struct GNUNET_MessageHeader *) &incoming[1];
4048 struct DHT_MessageContext *msg_ctx;
4052 if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4054 GNUNET_break_op (0);
4058 if (malicious_dropper == GNUNET_YES)
4060 #if DEBUG_DHT_ROUTING
4061 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4063 /** Log routes that die due to high load! */
4064 dhtlog_handle->insert_route (NULL,
4065 #if HAVE_UID_FOR_TESTING
4066 GNUNET_ntohll (incoming->unique_id),
4070 DHTLOG_ROUTE, ntohl (incoming->hop_count),
4071 GNUNET_SYSERR, &my_identity, &incoming->key,
4078 if (get_max_send_delay ().rel_value > MAX_REQUEST_TIME.rel_value)
4080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4081 "Sending of previous replies took too long, backing off!\n");
4082 increment_stats ("# route requests dropped due to high load");
4083 decrease_max_send_delay (get_max_send_delay ());
4084 #if DEBUG_DHT_ROUTING
4085 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4087 /** Log routes that die due to high load! */
4088 dhtlog_handle->insert_route (NULL,
4089 #if HAVE_UID_FOR_TESTING
4090 GNUNET_ntohll (incoming->unique_id),
4094 DHTLOG_ROUTE, ntohl (incoming->hop_count),
4095 GNUNET_SYSERR, &my_identity, &incoming->key,
4101 msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
4103 GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
4105 GNUNET_assert (msg_ctx->bloom != NULL);
4106 msg_ctx->hop_count = ntohl (incoming->hop_count);
4107 memcpy (&msg_ctx->key, &incoming->key, sizeof (GNUNET_HashCode));
4108 msg_ctx->replication = ntohl (incoming->desired_replication_level);
4109 #if HAVE_UID_FOR_TESTING
4110 msg_ctx->unique_id = GNUNET_ntohll (incoming->unique_id);
4112 msg_ctx->msg_options = ntohl (incoming->options);
4113 if (GNUNET_DHT_RO_RECORD_ROUTE ==
4114 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
4117 ntohl (incoming->outgoing_path_length) *
4118 sizeof (struct GNUNET_PeerIdentity);
4119 if (ntohs (message->size) !=
4120 (sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (enc_msg->size) +
4123 GNUNET_break_op (0);
4124 GNUNET_free (msg_ctx);
4127 route_path = (char *) &incoming[1];
4128 route_path = route_path + ntohs (enc_msg->size);
4129 msg_ctx->path_history =
4130 GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) + path_size);
4131 memcpy (msg_ctx->path_history, route_path, path_size);
4132 memcpy (&msg_ctx->path_history[path_size], &my_identity,
4133 sizeof (struct GNUNET_PeerIdentity));
4134 msg_ctx->path_history_len = ntohl (incoming->outgoing_path_length) + 1;
4136 msg_ctx->network_size = ntohl (incoming->network_size);
4137 msg_ctx->peer = *peer;
4138 msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE;
4139 msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
4140 demultiplex_message (enc_msg, msg_ctx);
4141 if (msg_ctx->bloom != NULL)
4143 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
4144 msg_ctx->bloom = NULL;
4146 GNUNET_free (msg_ctx);
4152 * Core handler for p2p route results.
4154 * @param cls closure
4155 * @param message message
4156 * @param peer peer identity this notification is about
4157 * @param atsi performance data
4161 handle_dht_p2p_route_result (void *cls, const struct GNUNET_PeerIdentity *peer,
4162 const struct GNUNET_MessageHeader *message,
4163 const struct GNUNET_TRANSPORT_ATS_Information
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4168 "`%s:%s': Received request from peer %s\n", my_short_id, "DHT",
4171 const struct GNUNET_DHT_P2PRouteResultMessage *incoming =
4172 (const struct GNUNET_DHT_P2PRouteResultMessage *) message;
4173 struct GNUNET_MessageHeader *enc_msg =
4174 (struct GNUNET_MessageHeader *) &incoming[1];
4175 struct DHT_MessageContext msg_ctx;
4181 if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4183 GNUNET_break_op (0);
4187 if (malicious_dropper == GNUNET_YES)
4189 #if DEBUG_DHT_ROUTING
4190 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4192 /** Log routes that die due to high load! */
4193 dhtlog_handle->insert_route (NULL,
4194 #if HAVE_UID_FOR_TESTING
4195 GNUNET_ntohll (incoming->unique_id),
4199 DHTLOG_ROUTE, ntohl (incoming->hop_count),
4200 GNUNET_SYSERR, &my_identity, &incoming->key,
4207 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
4208 memcpy (&msg_ctx.key, &incoming->key, sizeof (GNUNET_HashCode));
4209 #if HAVE_UID_FOR_TESTING
4210 msg_ctx.unique_id = GNUNET_ntohll (incoming->unique_id);
4212 msg_ctx.msg_options = ntohl (incoming->options);
4213 msg_ctx.hop_count = ntohl (incoming->hop_count);
4214 msg_ctx.peer = *peer;
4215 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */
4216 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
4217 if ((GNUNET_DHT_RO_RECORD_ROUTE ==
4218 (msg_ctx.msg_options & GNUNET_DHT_RO_RECORD_ROUTE)) &&
4219 (ntohl (incoming->outgoing_path_length) > 0))
4221 if (ntohs (message->size) -
4222 sizeof (struct GNUNET_DHT_P2PRouteResultMessage) -
4223 ntohs (enc_msg->size) !=
4224 ntohl (incoming->outgoing_path_length) *
4225 sizeof (struct GNUNET_PeerIdentity))
4228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4229 "Return message indicated a path was included, but sizes are wrong: Total size %d, enc size %d, left %d, expected %d\n",
4230 ntohs (message->size), ntohs (enc_msg->size),
4231 ntohs (message->size) -
4232 sizeof (struct GNUNET_DHT_P2PRouteResultMessage) -
4233 ntohs (enc_msg->size),
4234 ntohl (incoming->outgoing_path_length) *
4235 sizeof (struct GNUNET_PeerIdentity));
4237 GNUNET_break_op (0);
4240 msg_ctx.path_history = (char *) &incoming[1];
4241 msg_ctx.path_history += ntohs (enc_msg->size);
4242 msg_ctx.path_history_len = ntohl (incoming->outgoing_path_length);
4244 for (i = 0; i < msg_ctx.path_history_len; i++)
4247 &msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
4248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4249 "(handle_p2p_route_result) Key %s Found peer %d:%s\n",
4250 GNUNET_h2s (&msg_ctx.key), i,
4251 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
4255 #if HAVE_REPLY_BLOOMFILTER
4257 GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
4259 GNUNET_assert (msg_ctx.bloom != NULL);
4261 route_result_message (enc_msg, &msg_ctx);
4267 * Receive the HELLO from transport service,
4268 * free current and replace if necessary.
4271 * @param message HELLO message of peer
4274 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
4277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4278 "Received our `%s' from transport service\n", "HELLO");
4281 GNUNET_assert (message != NULL);
4282 GNUNET_free_non_null (my_hello);
4283 my_hello = GNUNET_malloc (ntohs (message->size));
4284 memcpy (my_hello, message, ntohs (message->size));
4289 * Task run during shutdown.
4295 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4298 struct PeerInfo *pos;
4302 GNUNET_TRANSPORT_get_hello_cancel (ghh);
4305 if (transport_handle != NULL)
4307 GNUNET_free_non_null (my_hello);
4308 GNUNET_TRANSPORT_disconnect (transport_handle);
4309 transport_handle = NULL;
4313 GNUNET_NSE_disconnect (nse);
4316 if (coreAPI != NULL)
4319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Disconnecting core!\n",
4320 my_short_id, "DHT");
4322 GNUNET_CORE_disconnect (coreAPI);
4325 for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS; bucket_count++)
4327 while (k_buckets[bucket_count].head != NULL)
4329 pos = k_buckets[bucket_count].head;
4331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4332 "%s:%s Removing peer %s from bucket %d!\n", my_short_id,
4333 "DHT", GNUNET_i2s (&pos->id), bucket_count);
4335 delete_peer (pos, bucket_count);
4338 if (datacache != NULL)
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Destroying datacache!\n",
4342 my_short_id, "DHT");
4344 GNUNET_DATACACHE_destroy (datacache);
4349 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
4352 if (dhtlog_handle != NULL)
4354 GNUNET_DHTLOG_disconnect (dhtlog_handle);
4355 dhtlog_handle = NULL;
4357 if (block_context != NULL)
4359 GNUNET_BLOCK_context_destroy (block_context);
4360 block_context = NULL;
4362 GNUNET_free_non_null (my_short_id);
4368 * To be called on core init/fail.
4370 * @param cls service closure
4371 * @param server handle to the server for this service
4372 * @param identity the public identity of this peer
4373 * @param publicKey the public key of this peer
4376 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4377 const struct GNUNET_PeerIdentity *identity,
4378 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
4384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Connection to core FAILED!\n",
4385 "dht", GNUNET_i2s (identity));
4387 GNUNET_SCHEDULER_cancel (cleanup_task);
4388 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
4392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4393 "%s: Core connection initialized, I am peer: %s\n", "dht",
4394 GNUNET_i2s (identity));
4397 /* Copy our identity so we can use it */
4398 memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity));
4399 if (my_short_id != NULL)
4400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4401 "%s Receive CORE INIT message but have already been initialized! Did CORE fail?\n",
4403 my_short_id = GNUNET_strdup (GNUNET_i2s (&my_identity));
4404 if (dhtlog_handle != NULL)
4405 dhtlog_handle->insert_node (NULL, &my_identity);
4409 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
4410 {&handle_dht_local_route_request, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE,
4412 {&handle_dht_local_route_stop, NULL,
4413 GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP, 0},
4414 {&handle_dht_control_message, NULL, GNUNET_MESSAGE_TYPE_DHT_CONTROL, 0},
4419 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4420 {&handle_dht_p2p_route_request, GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE, 0},
4421 {&handle_dht_p2p_route_result, GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT, 0},
4427 * Method called whenever a peer connects.
4429 * @param cls closure
4430 * @param peer peer identity this notification is about
4431 * @param atsi performance data
4434 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4435 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4437 struct PeerInfo *ret;
4438 struct DHTPutEntry *put_entry;
4441 /* Check for connect to self message */
4442 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
4446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4447 "%s:%s Receives core connect message for peer %s distance %d!\n",
4448 my_short_id, "dht", GNUNET_i2s (peer), distance);
4452 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
4456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4457 "%s:%s Received %s message for peer %s, but already have peer in RT!",
4458 my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s (peer));
4464 if ((datacache != NULL) && (GNUNET_YES == put_peer_identities))
4467 GNUNET_malloc (sizeof (struct DHTPutEntry) +
4468 sizeof (struct GNUNET_PeerIdentity));
4469 put_entry->path_length = 0;
4470 put_entry->data_size = sizeof (struct GNUNET_PeerIdentity);
4471 memcpy (&put_entry[1], peer, sizeof (struct GNUNET_PeerIdentity));
4472 GNUNET_DATACACHE_put (datacache, &peer->hashPubKey,
4473 sizeof (struct DHTPutEntry) +
4474 sizeof (struct GNUNET_PeerIdentity),
4475 (char *) put_entry, GNUNET_BLOCK_TYPE_DHT_HELLO,
4476 GNUNET_TIME_absolute_get_forever ());
4477 GNUNET_free (put_entry);
4479 else if (datacache == NULL)
4480 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4481 "DHT has no connection to datacache!\n");
4483 peer_bucket = find_current_bucket (&peer->hashPubKey);
4484 GNUNET_assert (peer_bucket >= lowest_bucket);
4485 GNUNET_assert (peer_bucket < MAX_BUCKETS);
4486 ret = GNUNET_malloc (sizeof (struct PeerInfo));
4488 ret->latency = latency;
4489 ret->distance = distance;
4492 GNUNET_CONTAINER_DLL_insert_after (k_buckets[peer_bucket].head,
4493 k_buckets[peer_bucket].tail,
4494 k_buckets[peer_bucket].tail, ret);
4495 k_buckets[peer_bucket].peers_size++;
4496 #if DO_UPDATE_PREFERENCE
4497 if ((GNUNET_CRYPTO_hash_matching_bits
4498 (&my_identity.hashPubKey, &peer->hashPubKey) > 0) &&
4499 (k_buckets[peer_bucket].peers_size <= bucket_size))
4500 ret->preference_task =
4501 GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
4503 if ((k_buckets[lowest_bucket].peers_size) >= bucket_size)
4504 enable_next_bucket ();
4505 newly_found_peers++;
4506 GNUNET_CONTAINER_multihashmap_put (all_known_peers, &peer->hashPubKey, ret,
4507 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4508 increment_stats (STAT_PEERS_KNOWN);
4511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4512 "%s:%s Adding peer to routing list: %s\n", my_short_id, "DHT",
4513 ret == NULL ? "NOT ADDED" : "PEER ADDED");
4519 * Method called whenever a peer disconnects.
4521 * @param cls closure
4522 * @param peer peer identity this notification is about
4525 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4527 struct PeerInfo *to_remove;
4530 /* Check for disconnect from self message */
4531 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
4534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4535 "%s:%s: Received peer disconnect message for peer `%s' from %s\n",
4536 my_short_id, "DHT", GNUNET_i2s (peer), "CORE");
4540 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
4545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4546 "%s:%s: do not have peer `%s' in RT, can't disconnect!\n",
4547 my_short_id, "DHT", GNUNET_i2s (peer));
4551 increment_stats (STAT_DISCONNECTS);
4552 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
4553 (all_known_peers, &peer->hashPubKey));
4555 GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
4556 GNUNET_assert (to_remove != NULL);
4557 if (NULL != to_remove->info_ctx)
4559 GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
4560 to_remove->info_ctx = NULL;
4563 memcmp (peer, &to_remove->id,
4564 sizeof (struct GNUNET_PeerIdentity)));
4565 current_bucket = find_current_bucket (&to_remove->id.hashPubKey);
4566 delete_peer (to_remove, current_bucket);
4571 * Process dht requests.
4573 * @param cls closure
4574 * @param server the initialized server
4575 * @param c configuration to use
4578 run (void *cls, struct GNUNET_SERVER_Handle *server,
4579 const struct GNUNET_CONFIGURATION_Handle *c)
4581 struct GNUNET_TIME_Relative next_send_time;
4582 unsigned long long temp_config_num;
4585 datacache = GNUNET_DATACACHE_create (cfg, "dhtcache");
4586 GNUNET_SERVER_add_handlers (server, plugin_handlers);
4587 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
4588 nse = GNUNET_NSE_connect (cfg, &update_network_size_estimate, NULL);
4589 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
4590 DEFAULT_CORE_QUEUE_SIZE, /* queue size */
4591 NULL, /* Closure passed to DHT functions */
4592 &core_init, /* Call core_init once connected */
4593 &handle_core_connect, /* Handle connects */
4594 &handle_core_disconnect, /* remove peers on disconnects */
4595 NULL, /* Do we care about "status" updates? */
4596 NULL, /* Don't want notified about all incoming messages */
4597 GNUNET_NO, /* For header only inbound notification */
4598 NULL, /* Don't want notified about all outbound messages */
4599 GNUNET_NO, /* For header only outbound notification */
4600 core_handlers); /* Register these handlers */
4602 if (coreAPI == NULL)
4605 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
4606 if (transport_handle != NULL)
4607 ghh = GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
4609 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4610 "Failed to connect to transport service!\n");
4611 block_context = GNUNET_BLOCK_context_create (cfg);
4612 lowest_bucket = MAX_BUCKETS - 1;
4613 forward_list.hashmap =
4614 GNUNET_CONTAINER_multihashmap_create (MAX_OUTSTANDING_FORWARDS / 10);
4615 forward_list.minHeap =
4616 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4617 all_known_peers = GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
4618 GNUNET_assert (all_known_peers != NULL);
4620 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
4623 debug_routes = GNUNET_YES;
4627 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "strict_kademlia"))
4629 strict_kademlia = GNUNET_YES;
4633 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_on_closest"))
4635 stop_on_closest = GNUNET_YES;
4639 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_found"))
4641 stop_on_found = GNUNET_YES;
4645 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_getter"))
4647 malicious_getter = GNUNET_YES;
4649 GNUNET_CONFIGURATION_get_value_time (cfg, "DHT",
4650 "MALICIOUS_GET_FREQUENCY",
4651 &malicious_get_frequency))
4652 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
4656 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_putter"))
4658 malicious_putter = GNUNET_YES;
4660 GNUNET_CONFIGURATION_get_value_time (cfg, "DHT",
4661 "MALICIOUS_PUT_FREQUENCY",
4662 &malicious_put_frequency))
4663 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
4667 GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "bucket_size",
4670 bucket_size = (unsigned int) temp_config_num;
4674 GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "kad_alpha",
4675 &kademlia_replication))
4677 kademlia_replication = DEFAULT_KADEMLIA_REPLICATION;
4681 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_dropper"))
4683 malicious_dropper = GNUNET_YES;
4687 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "do_find_peer"))
4689 do_find_peer = GNUNET_NO;
4692 do_find_peer = GNUNET_YES;
4695 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "use_real_distance"))
4696 use_real_distance = GNUNET_YES;
4699 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
4700 "mysql_logging_extended"))
4702 debug_routes = GNUNET_YES;
4703 debug_routes_extended = GNUNET_YES;
4706 #if DEBUG_DHT_ROUTING
4707 if (GNUNET_YES == debug_routes)
4709 dhtlog_handle = GNUNET_DHTLOG_connect (cfg);
4710 if (dhtlog_handle == NULL)
4712 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4713 "Could not connect to mysql logging server, logging will not happen!");
4719 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "paper_forwarding"))
4720 paper_forwarding = GNUNET_YES;
4723 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "put_peer_identities"))
4724 put_peer_identities = GNUNET_YES;
4726 stats = GNUNET_STATISTICS_create ("dht", cfg);
4730 GNUNET_STATISTICS_set (stats, STAT_ROUTES, 0, GNUNET_NO);
4731 GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS, 0, GNUNET_NO);
4732 GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS_CLOSEST, 0, GNUNET_NO);
4733 GNUNET_STATISTICS_set (stats, STAT_RESULTS, 0, GNUNET_NO);
4734 GNUNET_STATISTICS_set (stats, STAT_RESULTS_TO_CLIENT, 0, GNUNET_NO);
4735 GNUNET_STATISTICS_set (stats, STAT_RESULT_FORWARDS, 0, GNUNET_NO);
4736 GNUNET_STATISTICS_set (stats, STAT_GETS, 0, GNUNET_NO);
4737 GNUNET_STATISTICS_set (stats, STAT_PUTS, 0, GNUNET_NO);
4738 GNUNET_STATISTICS_set (stats, STAT_PUTS_INSERTED, 0, GNUNET_NO);
4739 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER, 0, GNUNET_NO);
4740 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_START, 0, GNUNET_NO);
4741 GNUNET_STATISTICS_set (stats, STAT_GET_START, 0, GNUNET_NO);
4742 GNUNET_STATISTICS_set (stats, STAT_PUT_START, 0, GNUNET_NO);
4743 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_REPLY, 0, GNUNET_NO);
4744 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_ANSWER, 0, GNUNET_NO);
4745 GNUNET_STATISTICS_set (stats, STAT_BLOOM_FIND_PEER, 0, GNUNET_NO);
4746 GNUNET_STATISTICS_set (stats, STAT_GET_REPLY, 0, GNUNET_NO);
4747 GNUNET_STATISTICS_set (stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO);
4748 GNUNET_STATISTICS_set (stats, STAT_HELLOS_PROVIDED, 0, GNUNET_NO);
4749 GNUNET_STATISTICS_set (stats, STAT_DISCONNECTS, 0, GNUNET_NO);
4751 if (GNUNET_YES == do_find_peer)
4753 next_send_time.rel_value =
4754 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
4755 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
4756 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
4758 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
4759 find_peer_context.start = GNUNET_TIME_absolute_get ();
4760 GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
4761 &find_peer_context);
4764 /* Scheduled the task to clean up when shutdown is called */
4766 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
4767 &shutdown_task, NULL);
4772 * The main function for the dht service.
4774 * @param argc number of arguments from the command line
4775 * @param argv command line arguments
4776 * @return 0 ok, 1 on error
4779 main (int argc, char *const *argv)
4783 recent.hashmap = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT / 2);
4785 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4786 recent_find_peer_requests =
4787 GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
4790 GNUNET_SERVICE_run (argc, argv, "dht", GNUNET_SERVICE_OPTION_NONE, &run,
4792 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent.hashmap));
4793 GNUNET_CONTAINER_multihashmap_destroy (recent.hashmap);
4794 recent.hashmap = NULL;
4795 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent.minHeap));
4796 GNUNET_CONTAINER_heap_destroy (recent.minHeap);
4797 recent.minHeap = NULL;
4798 GNUNET_CONTAINER_multihashmap_destroy (recent_find_peer_requests);
4799 recent_find_peer_requests = NULL;
4803 /* end of gnunet-service-dht.c */