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 for 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_constants.h"
40 #include "gnunet_protocols.h"
41 #include "gnunet_nse_service.h"
42 #include "gnunet_core_service.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet_datacache_lib.h"
45 #include "gnunet_transport_service.h"
46 #include "gnunet_hello_lib.h"
47 #include "gnunet_dht_service.h"
48 #include "gnunet_statistics_service.h"
55 * How many buckets will we allow total.
57 #define MAX_BUCKETS sizeof (GNUNET_HashCode) * 8
60 * Should the DHT issue FIND_PEER requests to get better routing tables?
62 #define DEFAULT_DO_FIND_PEER GNUNET_YES
65 * Defines whether find peer requests send their HELLO's outgoing,
66 * or expect replies to contain hellos.
68 #define FIND_PEER_WITH_HELLO GNUNET_YES
71 * What is the maximum number of peers in a given bucket.
73 #define DEFAULT_BUCKET_SIZE 4
75 #define DEFAULT_CORE_QUEUE_SIZE 32
78 * Minimum number of peers we need for "good" routing,
79 * any less than this and we will allow messages to
80 * travel much further through the network!
82 #define MINIMUM_PEER_THRESHOLD 20
85 * Number of requests we track at most (for routing replies).
87 #define DHT_MAX_RECENT (1024 * 16)
90 * How long do we wait at most when queueing messages with core
91 * that we are sending on behalf of other peers.
93 #define DHT_DEFAULT_P2P_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
96 * Default importance for handling messages on behalf of other peers.
98 #define DHT_DEFAULT_P2P_IMPORTANCE 0
101 * How long to keep recent requests around by default.
103 #define DEFAULT_RECENT_REMOVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
106 * Default time to wait to send find peer messages sent by the dht service.
108 #define DHT_DEFAULT_FIND_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
111 * Default importance for find peer messages sent by the dht service.
113 #define DHT_DEFAULT_FIND_PEER_IMPORTANCE 8
116 * Default replication parameter for find peer messages sent by the dht service.
118 #define DHT_DEFAULT_FIND_PEER_REPLICATION 4
121 * How long at least to wait before sending another find peer request.
123 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
126 * How long at most to wait before sending another find peer request.
128 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
131 * How often to update our preference levels for peers in our routing tables.
133 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
136 * How long at most on average will we allow a reply forward to take
137 * (before we quit sending out new requests)
139 #define MAX_REQUEST_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
142 * How many initial requests to send out (in true Kademlia fashion)
144 #define DEFAULT_KADEMLIA_REPLICATION 3
147 * Default frequency for sending malicious get messages
149 #define DEFAULT_MALICIOUS_GET_FREQUENCY GNUNET_TIME_UNIT_SECONDS
152 * Default frequency for sending malicious put messages
154 #define DEFAULT_MALICIOUS_PUT_FREQUENCY GNUNET_TIME_UNIT_SECONDS
157 * How many time differences between requesting a core send and
158 * the actual callback to remember.
160 #define MAX_REPLY_TIMES 8
164 * Linked list of messages to send to clients.
166 struct P2PPendingMessage
169 * Pointer to next item in the list
171 struct P2PPendingMessage *next;
174 * Pointer to previous item in the list
176 struct P2PPendingMessage *prev;
179 * Message importance level.
181 unsigned int importance;
184 * Time when this request was scheduled to be sent.
186 struct GNUNET_TIME_Absolute scheduled;
189 * How long to wait before sending message.
191 struct GNUNET_TIME_Relative timeout;
194 * Actual message to be sent; // avoid allocation
196 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
202 * Per-peer information.
207 * Next peer entry (DLL)
209 struct PeerInfo *next;
212 * Prev peer entry (DLL)
214 struct PeerInfo *prev;
217 * Count of outstanding messages for peer.
219 unsigned int pending_count;
222 * Head of pending messages to be sent to this peer.
224 struct P2PPendingMessage *head;
227 * Tail of pending messages to be sent to this peer.
229 struct P2PPendingMessage *tail;
232 * Core handle for sending messages to this peer.
234 struct GNUNET_CORE_TransmitHandle *th;
237 * Task for scheduling message sends.
239 GNUNET_SCHEDULER_TaskIdentifier send_task;
242 * Task for scheduling preference updates
244 GNUNET_SCHEDULER_TaskIdentifier preference_task;
247 * Preference update context
249 struct GNUNET_CORE_InformationRequestContext *info_ctx;
252 * What is the identity of the peer?
254 struct GNUNET_PeerIdentity id;
258 * What is the average latency for replies received?
260 struct GNUNET_TIME_Relative latency;
263 * Transport level distance to peer.
265 unsigned int distance;
269 * Task for scheduling periodic ping messages for this peer.
271 GNUNET_SCHEDULER_TaskIdentifier ping_task;
276 * Peers are grouped into buckets.
283 struct PeerInfo *head;
288 struct PeerInfo *tail;
291 * Number of peers in the bucket.
293 unsigned int peers_size;
298 * Linked list of messages to send to clients.
300 struct PendingMessage
303 * Pointer to next item in the list
305 struct PendingMessage *next;
308 * Pointer to previous item in the list
310 struct PendingMessage *prev;
313 * Actual message to be sent; // avoid allocation
315 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
321 * Struct containing information about a client,
322 * handle to connect to it, and any pending messages
323 * that need to be sent to it.
328 * Linked list of active clients
330 struct ClientList *next;
333 * The handle to this client
335 struct GNUNET_SERVER_Client *client_handle;
338 * Handle to the current transmission request, NULL
341 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
344 * Linked list of pending messages for this client
346 struct PendingMessage *pending_head;
349 * Tail of linked list of pending messages for this client
351 struct PendingMessage *pending_tail;
356 * Context containing information about a DHT message received.
358 struct DHT_MessageContext
361 * The client this request was received from.
362 * (NULL if received from another peer)
364 struct ClientList *client;
367 * The peer this request was received from.
369 struct GNUNET_PeerIdentity peer;
372 * Bloomfilter for this routing request.
374 struct GNUNET_CONTAINER_BloomFilter *bloom;
377 * extended query (see gnunet_block_lib.h).
382 * Bloomfilter to filter out duplicate replies.
384 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
387 * The key this request was about
392 * How long should we wait to transmit this request?
394 struct GNUNET_TIME_Relative timeout;
397 * The unique identifier of this request
402 * Number of bytes in xquery.
407 * Mutator value for the reply_bf, see gnunet_block_lib.h
409 uint32_t reply_bf_mutator;
412 * Desired replication level
414 uint32_t replication;
417 * Network size estimate, either ours or the sum of
418 * those routed to thus far. =~ Log of number of peers
419 * chosen from for this request.
421 uint32_t network_size;
424 * Any message options for this request
426 uint32_t msg_options;
429 * How many hops has the message already traversed?
434 * How many peer identities are present in the path history?
436 uint32_t path_history_len;
444 * How important is this message?
446 unsigned int importance;
449 * Should we (still) forward the request on to other peers?
454 * Did we forward this message? (may need to remember it!)
459 * Are we the closest known peer to this key (out of our neighbors?)
466 * Record used for remembering what peers are waiting for what
467 * responses (based on search key).
469 struct DHTRouteSource
474 struct DHTRouteSource *next;
479 struct DHTRouteSource *prev;
482 * UID of the request, 0 if from another peer.
487 * Source of the request. Replies should be forwarded to
490 struct GNUNET_PeerIdentity source;
493 * If this was a local request, remember the client; otherwise NULL.
495 struct ClientList *client;
498 * Pointer to this nodes heap location (for removal)
500 struct GNUNET_CONTAINER_HeapNode *hnode;
503 * Back pointer to the record storing this information.
505 struct DHTQueryRecord *record;
508 * Task to remove this entry on timeout.
510 GNUNET_SCHEDULER_TaskIdentifier delete_task;
513 * Bloomfilter of peers we have already sent back as
514 * replies to the initial request. Allows us to not
515 * forward the same peer multiple times for a find peer
518 struct GNUNET_CONTAINER_BloomFilter *find_peers_responded;
524 * Entry in the DHT routing table.
526 struct DHTQueryRecord
529 * Head of DLL for result forwarding.
531 struct DHTRouteSource *head;
534 * Tail of DLL for result forwarding.
536 struct DHTRouteSource *tail;
539 * Key that the record concerns.
547 * Context used to calculate the number of find peer messages
548 * per X time units since our last scheduled find peer message
549 * was sent. If we have seen too many messages, delay or don't
552 struct FindPeerMessageContext
556 struct GNUNET_TIME_Absolute start;
558 struct GNUNET_TIME_Absolute end;
563 * DHT Routing results structure
568 * Min heap for removal upon reaching limit
570 struct GNUNET_CONTAINER_Heap *minHeap;
574 * Hashmap for fast key based lookup
576 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
581 * DHT structure for recent requests.
583 struct RecentRequests
586 * Min heap for removal upon reaching limit
588 struct GNUNET_CONTAINER_Heap *minHeap;
590 #if HAVE_UID_FOR_TESTING > 1
592 * Hashmap for key based lookup
594 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
603 * Position of this node in the min heap.
605 struct GNUNET_CONTAINER_HeapNode *heap_node;
608 * Bloomfilter containing entries for peers
609 * we forwarded this request to.
611 struct GNUNET_CONTAINER_BloomFilter *bloom;
614 * Timestamp of this request, for ordering
617 struct GNUNET_TIME_Absolute timestamp;
620 * Key of this request.
625 * Unique identifier for this request, 0 if from another peer.
630 * Task to remove this entry on timeout.
632 GNUNET_SCHEDULER_TaskIdentifier remove_task;
637 * log of the current network size estimate, used as the point where
638 * we switch between random and deterministic routing. Default
639 * value of 4.0 is used if NSE module is not available (i.e. not
642 static double log_of_network_size_estimate = 4.0;
645 * Recent requests by hash/uid and by time inserted.
647 static struct RecentRequests recent;
650 * Context to use to calculate find peer rates.
652 static struct FindPeerMessageContext find_peer_context;
655 * Don't use our routing algorithm, always route
656 * to closest peer; initially send requests to 3
659 static int strict_kademlia;
662 * Routing option to end routing when closest peer found.
664 static int stop_on_closest;
667 * Routing option to end routing when data is found.
669 static int stop_on_found;
672 * Whether DHT needs to manage find peer requests, or
673 * an external force will do it on behalf of the DHT.
675 static int do_find_peer;
678 * Use exactly the forwarding formula as described in
679 * the paper if set to GNUNET_YES, otherwise use the
680 * slightly modified version.
682 static int paper_forwarding;
685 * PUT Peer Identities of peers we know about into
688 static int put_peer_identities;
691 * Use the "real" distance metric when selecting the
692 * next routing hop. Can be less accurate.
694 static int use_real_distance;
697 * How many peers have we added since we sent out our last
700 static unsigned int newly_found_peers;
703 * Container of active queries we should remember
705 static struct DHTResults forward_list;
708 * Handle to the datacache service (for inserting/retrieving data)
710 static struct GNUNET_DATACACHE_Handle *datacache;
713 * Handle for the statistics service.
715 struct GNUNET_STATISTICS_Handle *stats;
718 * Handle to get our current HELLO.
720 static struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
723 * The configuration the DHT service is running with
725 static const struct GNUNET_CONFIGURATION_Handle *cfg;
728 * Handle to the core service
730 static struct GNUNET_CORE_Handle *coreAPI;
733 * Handle to the transport service, for getting our hello
735 static struct GNUNET_TRANSPORT_Handle *transport_handle;
738 * The identity of our peer.
740 static struct GNUNET_PeerIdentity my_identity;
743 * Short id of the peer, for printing
745 static char *my_short_id;
750 static struct GNUNET_MessageHeader *my_hello;
753 * Task to run when we shut down, cleaning up all our trash
755 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
758 * The lowest currently used bucket.
760 static unsigned int lowest_bucket; /* Initially equal to MAX_BUCKETS - 1 */
763 * The buckets (Kademlia routing table, complete with growth).
764 * Array of size MAX_BUCKET_SIZE.
766 static struct PeerBucket k_buckets[MAX_BUCKETS];
769 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
771 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
774 * Recently seen find peer requests.
776 static struct GNUNET_CONTAINER_MultiHashMap *recent_find_peer_requests;
779 * Maximum size for each bucket.
781 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
784 * List of active clients.
786 static struct ClientList *client_list;
789 * Handle to the DHT logger.
791 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
794 * Whether or not to send routing debugging information
795 * to the dht logging server
797 static unsigned int debug_routes;
800 * Whether or not to send FULL route information to
803 static unsigned int debug_routes_extended;
806 * GNUNET_YES or GNUNET_NO, whether or not to act as
807 * a malicious node which drops all messages
809 static unsigned int malicious_dropper;
812 * GNUNET_YES or GNUNET_NO, whether or not to act as
813 * a malicious node which sends out lots of GETS
815 static unsigned int malicious_getter;
818 * GNUNET_YES or GNUNET_NO, whether or not to act as
819 * a malicious node which sends out lots of PUTS
821 static unsigned int malicious_putter;
824 * Frequency for malicious get requests.
826 static struct GNUNET_TIME_Relative malicious_get_frequency;
829 * Frequency for malicious put requests.
831 static struct GNUNET_TIME_Relative malicious_put_frequency;
834 * Kademlia replication
836 static unsigned long long kademlia_replication;
839 * Reply times for requests, if we are busy, don't send any
842 static struct GNUNET_TIME_Relative reply_times[MAX_REPLY_TIMES];
845 * Current counter for replies.
847 static unsigned int reply_counter;
850 * Our handle to the BLOCK library.
852 static struct GNUNET_BLOCK_Context *block_context;
855 * Network size estimation handle.
857 static struct GNUNET_NSE_Handle *nse;
861 * Callback that is called when network size estimate is updated.
864 * @param timestamp time when the estimate was received from the server (or created by the server)
865 * @param logestimate the log(Base 2) value of the current network size estimate
866 * @param std_dev standard deviation for the estimate
870 update_network_size_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
871 double logestimate, double std_dev)
873 log_of_network_size_estimate = logestimate;
878 * Forward declaration.
881 send_generic_reply (void *cls, size_t size, void *buf);
884 /** Declare here so retry_core_send is aware of it */
886 core_transmit_notify (void *cls, size_t size, void *buf);
890 * Convert unique ID to hash code.
892 * @param uid unique ID to convert
893 * @param hash set to uid (extended with zeros)
896 hash_from_uid (uint64_t uid, GNUNET_HashCode * hash)
898 memset (hash, 0, sizeof (GNUNET_HashCode));
899 *((uint64_t *) hash) = uid;
904 * Given the largest send delay, artificially decrease it
905 * so the next time around we may have a chance at sending
909 decrease_max_send_delay (struct GNUNET_TIME_Relative max_time)
913 for (i = 0; i < MAX_REPLY_TIMES; i++)
915 if (reply_times[i].rel_value == max_time.rel_value)
917 reply_times[i].rel_value = reply_times[i].rel_value / 2;
925 * Find the maximum send time of the recently sent values.
927 * @return the average time between asking core to send a message
928 * and when the buffer for copying it is passed
930 static struct GNUNET_TIME_Relative
931 get_max_send_delay ()
934 struct GNUNET_TIME_Relative max_time;
936 max_time = GNUNET_TIME_relative_get_zero ();
938 for (i = 0; i < MAX_REPLY_TIMES; i++)
940 if (reply_times[i].rel_value > max_time.rel_value)
941 max_time.rel_value = reply_times[i].rel_value;
944 if (max_time.rel_value > MAX_REQUEST_TIME.rel_value)
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n",
946 (unsigned long long) max_time.rel_value);
953 increment_stats (const char *value)
957 GNUNET_STATISTICS_update (stats, value, 1, GNUNET_NO);
962 decrement_stats (const char *value)
966 GNUNET_STATISTICS_update (stats, value, -1, GNUNET_NO);
971 * Try to send another message from our core send list
974 try_core_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
976 struct PeerInfo *peer = cls;
977 struct P2PPendingMessage *pending;
980 peer->send_task = GNUNET_SCHEDULER_NO_TASK;
982 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
985 if (peer->th != NULL)
986 return; /* Message send already in progress */
988 pending = peer->head;
991 ssize = ntohs (pending->msg->size);
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n",
995 my_short_id, "DHT", ssize, GNUNET_i2s (&peer->id));
997 pending->scheduled = GNUNET_TIME_absolute_get ();
999 if (reply_counter >= MAX_REPLY_TIMES)
1002 GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
1003 pending->importance,
1004 pending->timeout, &peer->id, ssize,
1005 &core_transmit_notify, peer);
1006 if (peer->th == NULL)
1007 increment_stats ("# notify transmit ready failed");
1013 * Function called to send a request out to another peer.
1014 * Called both for locally initiated requests and those
1015 * received from other peers.
1017 * @param msg the encapsulated message
1018 * @param peer the peer to forward the message to
1019 * @param msg_ctx the context of the message (hop count, bloom, etc.)
1022 forward_result_message (const struct GNUNET_MessageHeader *msg,
1023 struct PeerInfo *peer,
1024 struct DHT_MessageContext *msg_ctx)
1026 struct GNUNET_DHT_P2PRouteResultMessage *result_message;
1027 struct P2PPendingMessage *pending;
1037 increment_stats (STAT_RESULT_FORWARDS);
1039 sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs (msg->size) +
1040 (sizeof (struct GNUNET_PeerIdentity) * msg_ctx->path_history_len);
1041 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1042 psize = sizeof (struct P2PPendingMessage) + msize;
1043 pending = GNUNET_malloc (psize);
1044 pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
1045 pending->importance = DHT_SEND_PRIORITY;
1046 pending->timeout = GNUNET_TIME_relative_get_forever ();
1047 result_message = (struct GNUNET_DHT_P2PRouteResultMessage *) pending->msg;
1048 result_message->header.size = htons (msize);
1049 result_message->header.type =
1050 htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT);
1051 result_message->outgoing_path_length = htonl (msg_ctx->path_history_len);
1052 if (msg_ctx->path_history_len > 0)
1054 /* End of pending is where enc_msg starts */
1055 path_start = (char *) &pending[1];
1056 /* Offset by the size of the enc_msg */
1057 path_start += ntohs (msg->size);
1058 memcpy (path_start, msg_ctx->path_history,
1059 msg_ctx->path_history_len * (sizeof (struct GNUNET_PeerIdentity)));
1061 for (i = 0; i < msg_ctx->path_history_len; i++)
1064 &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "(forward_result) Key %s Found peer %d:%s\n",
1067 GNUNET_h2s (&msg_ctx->key), i,
1068 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1072 result_message->options = htonl (msg_ctx->msg_options);
1073 result_message->hop_count = htonl (msg_ctx->hop_count + 1);
1074 #if HAVE_UID_FOR_TESTING
1075 result_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1077 memcpy (&result_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1078 /* Copy the enc_msg, then the path history as well! */
1079 memcpy (&result_message[1], msg, ntohs (msg->size));
1080 path_offset = (char *) &result_message[1];
1081 path_offset += ntohs (msg->size);
1082 /* If we have path history, copy it to the end of the whole thing */
1083 if (msg_ctx->path_history_len > 0)
1084 memcpy (path_offset, msg_ctx->path_history,
1085 msg_ctx->path_history_len * (sizeof (struct GNUNET_PeerIdentity)));
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1088 "%s:%s Adding pending message size %d for peer %s\n", my_short_id,
1089 "DHT", msize, GNUNET_i2s (&peer->id));
1091 peer->pending_count++;
1092 increment_stats ("# pending messages scheduled");
1093 GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
1095 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1096 peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1101 * Called when core is ready to send a message we asked for
1102 * out to the destination.
1104 * @param cls closure (NULL)
1105 * @param size number of bytes available in buf
1106 * @param buf where the callee should write the message
1107 * @return number of bytes written to buf
1110 core_transmit_notify (void *cls, size_t size, void *buf)
1112 struct PeerInfo *peer = cls;
1114 struct P2PPendingMessage *pending;
1122 /* client disconnected */
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n",
1125 my_short_id, "DHT");
1130 if (peer->head == NULL)
1134 pending = peer->head;
1136 reply_times[reply_counter] =
1137 GNUNET_TIME_absolute_get_difference (pending->scheduled,
1138 GNUNET_TIME_absolute_get ());
1139 msize = ntohs (pending->msg->size);
1143 memcpy (cbuf, pending->msg, msize);
1144 peer->pending_count--;
1145 increment_stats ("# pending messages sent");
1146 GNUNET_assert (peer->pending_count >= 0);
1147 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
1148 GNUNET_free (pending);
1151 while (NULL != pending &&
1152 (size - off >= (msize = ntohs (pending->msg->size))))
1154 memcpy (&cbuf[off], pending->msg, msize);
1156 peer->pending_count--;
1157 increment_stats ("# pending messages sent");
1158 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
1159 GNUNET_free (pending);
1160 pending = peer->head;
1163 if ((peer->head != NULL) && (peer->send_task == GNUNET_SCHEDULER_NO_TASK))
1164 peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1171 * Compute the distance between have and target as a 32-bit value.
1172 * Differences in the lower bits must count stronger than differences
1173 * in the higher bits.
1175 * @return 0 if have==target, otherwise a number
1176 * that is larger as the distance between
1177 * the two hash codes increases
1180 distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
1182 unsigned int bucket;
1187 /* We have to represent the distance between two 2^9 (=512)-bit
1188 * numbers as a 2^5 (=32)-bit number with "0" being used for the
1189 * two numbers being identical; furthermore, we need to
1190 * guarantee that a difference in the number of matching
1191 * bits is always represented in the result.
1193 * We use 2^32/2^9 numerical values to distinguish between
1194 * hash codes that have the same LSB bit distance and
1195 * use the highest 2^9 bits of the result to signify the
1196 * number of (mis)matching LSB bits; if we have 0 matching
1197 * and hence 512 mismatching LSB bits we return -1 (since
1198 * 512 itself cannot be represented with 9 bits) */
1200 /* first, calculate the most significant 9 bits of our
1201 * result, aka the number of LSBs */
1202 bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
1203 /* bucket is now a value between 0 and 512 */
1205 return 0; /* perfect match */
1207 return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting
1208 * below, we'd end up with max+1 (overflow)) */
1210 /* calculate the most significant bits of the final result */
1211 msb = (512 - bucket) << (32 - 9);
1212 /* calculate the 32-9 least significant bits of the final result by
1213 * looking at the differences in the 32-9 bits following the
1214 * mismatching bit at 'bucket' */
1216 for (i = bucket + 1;
1217 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
1219 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
1220 GNUNET_CRYPTO_hash_get_bit (have, i))
1221 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
1222 * last bit set will be 31 -- if
1223 * i does not reach 512 first... */
1230 * Return a number that is larger the closer the
1231 * "have" GNUNET_hash code is to the "target".
1233 * @return inverse distance metric, non-zero.
1234 * Must fudge the value if NO bits match.
1237 inverse_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
1239 if (GNUNET_CRYPTO_hash_matching_bits (target, have) == 0)
1240 return 1; /* Never return 0! */
1241 return ((unsigned int) -1) - distance (target, have);
1246 * Find the optimal bucket for this key, regardless
1247 * of the current number of buckets in use.
1249 * @param hc the hashcode to compare our identity to
1251 * @return the proper bucket index, or GNUNET_SYSERR
1252 * on error (same hashcode)
1255 find_bucket (const GNUNET_HashCode * hc)
1259 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
1260 if (bits == MAX_BUCKETS)
1261 return GNUNET_SYSERR;
1262 return MAX_BUCKETS - bits - 1;
1267 * Find which k-bucket this peer should go into,
1268 * taking into account the size of the k-bucket
1269 * array. This means that if more bits match than
1270 * there are currently buckets, lowest_bucket will
1273 * @param hc GNUNET_HashCode we are finding the bucket for.
1275 * @return the proper bucket index for this key,
1276 * or GNUNET_SYSERR on error (same hashcode)
1279 find_current_bucket (const GNUNET_HashCode * hc)
1283 actual_bucket = find_bucket (hc);
1284 if (actual_bucket == GNUNET_SYSERR) /* hc and our peer identity match! */
1285 return lowest_bucket;
1286 if (actual_bucket < lowest_bucket) /* actual_bucket not yet used */
1287 return lowest_bucket;
1288 return actual_bucket;
1293 * Find a routing table entry from a peer identity
1295 * @param peer the peer identity to look up
1297 * @return the routing table entry, or NULL if not found
1299 static struct PeerInfo *
1300 find_peer_by_id (const struct GNUNET_PeerIdentity *peer)
1303 struct PeerInfo *pos;
1305 bucket = find_current_bucket (&peer->hashPubKey);
1307 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1310 pos = k_buckets[bucket].head;
1313 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1317 return NULL; /* No such peer. */
1320 /* Forward declaration */
1322 update_core_preference (void *cls,
1323 const struct GNUNET_SCHEDULER_TaskContext *tc);
1327 * Function called with statistics about the given peer.
1329 * @param cls closure
1330 * @param peer identifies the peer
1331 * @param bpm_out set to the current bandwidth limit (sending) for this peer
1332 * @param amount set to the amount that was actually reserved or unreserved;
1333 * either the full requested amount or zero (no partial reservations)
1334 * @param res_delay if the reservation could not be satisfied (amount was 0), how
1335 * long should the client wait until re-trying?
1336 * @param preference current traffic preference for the given peer
1339 update_core_preference_finish (void *cls,
1340 const struct GNUNET_PeerIdentity *peer,
1341 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1343 struct GNUNET_TIME_Relative res_delay,
1344 uint64_t preference)
1346 struct PeerInfo *peer_info = cls;
1348 peer_info->info_ctx = NULL;
1349 GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
1350 &update_core_preference, peer_info);
1354 update_core_preference (void *cls,
1355 const struct GNUNET_SCHEDULER_TaskContext *tc)
1357 struct PeerInfo *peer = cls;
1358 uint64_t preference;
1359 unsigned int matching;
1361 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1366 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
1367 &peer->id.hashPubKey);
1371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1372 "Peer identifier matches by %u bits, only shifting as much as we can!\n",
1377 preference = 1LL << matching;
1379 GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
1380 GNUNET_TIME_UNIT_FOREVER_REL,
1381 GNUNET_BANDWIDTH_VALUE_MAX, 0,
1383 &update_core_preference_finish, peer);
1388 * Given a peer and its corresponding bucket,
1389 * remove it from that bucket. Does not free
1390 * the PeerInfo struct, nor cancel messages
1391 * or free messages waiting to be sent to this
1394 * @param peer the peer to remove
1395 * @param bucket the bucket the peer belongs to
1398 remove_peer (struct PeerInfo *peer, unsigned int bucket)
1400 GNUNET_assert (k_buckets[bucket].peers_size > 0);
1401 GNUNET_CONTAINER_DLL_remove (k_buckets[bucket].head, k_buckets[bucket].tail,
1403 k_buckets[bucket].peers_size--;
1405 if ((bucket == lowest_bucket) && (k_buckets[lowest_bucket].peers_size == 0) &&
1406 (lowest_bucket < MAX_BUCKETS - 1))
1412 * Removes peer from a bucket, then frees associated
1413 * resources and frees peer.
1415 * @param peer peer to be removed and freed
1416 * @param bucket which bucket this peer belongs to
1419 delete_peer (struct PeerInfo *peer, unsigned int bucket)
1421 struct P2PPendingMessage *pos;
1422 struct P2PPendingMessage *next;
1424 remove_peer (peer, bucket); /* First remove the peer from its bucket */
1425 if (peer->send_task != GNUNET_SCHEDULER_NO_TASK)
1426 GNUNET_SCHEDULER_cancel (peer->send_task);
1427 if ((peer->th != NULL) && (coreAPI != NULL))
1428 GNUNET_CORE_notify_transmit_ready_cancel (peer->th);
1431 while (pos != NULL) /* Remove any pending messages for this peer */
1434 ("# dht pending messages discarded (due to disconnect/shutdown)");
1440 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
1441 (all_known_peers, &peer->id.hashPubKey));
1442 GNUNET_assert (GNUNET_YES ==
1443 GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
1444 &peer->id.hashPubKey,
1447 decrement_stats (STAT_PEERS_KNOWN);
1452 * Iterator over hash map entries.
1454 * @param cls closure
1455 * @param key current key code
1456 * @param value PeerInfo of the peer to move to new lowest bucket
1457 * @return GNUNET_YES if we should continue to
1462 move_lowest_bucket (void *cls, const GNUNET_HashCode * key, void *value)
1464 struct PeerInfo *peer = value;
1467 GNUNET_assert (lowest_bucket > 0);
1468 new_bucket = lowest_bucket - 1;
1469 remove_peer (peer, lowest_bucket);
1470 GNUNET_CONTAINER_DLL_insert_after (k_buckets[new_bucket].head,
1471 k_buckets[new_bucket].tail,
1472 k_buckets[new_bucket].tail, peer);
1473 k_buckets[new_bucket].peers_size++;
1479 * The current lowest bucket is full, so change the lowest
1480 * bucket to the next lower down, and move any appropriate
1481 * entries in the current lowest bucket to the new bucket.
1484 enable_next_bucket ()
1486 struct GNUNET_CONTAINER_MultiHashMap *to_remove;
1487 struct PeerInfo *pos;
1489 GNUNET_assert (lowest_bucket > 0);
1490 to_remove = GNUNET_CONTAINER_multihashmap_create (bucket_size);
1491 pos = k_buckets[lowest_bucket].head;
1493 /* Populate the array of peers which should be in the next lowest bucket */
1496 if (find_bucket (&pos->id.hashPubKey) < lowest_bucket)
1497 GNUNET_CONTAINER_multihashmap_put (to_remove, &pos->id.hashPubKey, pos,
1498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1502 /* Remove peers from lowest bucket, insert into next lowest bucket */
1503 GNUNET_CONTAINER_multihashmap_iterate (to_remove, &move_lowest_bucket, NULL);
1504 GNUNET_CONTAINER_multihashmap_destroy (to_remove);
1505 lowest_bucket = lowest_bucket - 1;
1510 * Find the closest peer in our routing table to the
1513 * @return The closest peer in our routing table to the
1514 * key, or NULL on error.
1516 static struct PeerInfo *
1517 find_closest_peer (const GNUNET_HashCode * hc)
1519 struct PeerInfo *pos;
1520 struct PeerInfo *current_closest;
1521 unsigned int lowest_distance;
1522 unsigned int temp_distance;
1526 lowest_distance = -1;
1528 if (k_buckets[lowest_bucket].peers_size == 0)
1531 current_closest = NULL;
1532 for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
1534 pos = k_buckets[bucket].head;
1536 while ((pos != NULL) && (count < bucket_size))
1538 temp_distance = distance (&pos->id.hashPubKey, hc);
1539 if (temp_distance <= lowest_distance)
1541 lowest_distance = temp_distance;
1542 current_closest = pos;
1548 GNUNET_assert (current_closest != NULL);
1549 return current_closest;
1554 * Function called to send a request out to another peer.
1555 * Called both for locally initiated requests and those
1556 * received from other peers.
1558 * @param msg the encapsulated message
1559 * @param peer the peer to forward the message to
1560 * @param msg_ctx the context of the message (hop count, bloom, etc.)
1563 forward_message (const struct GNUNET_MessageHeader *msg, struct PeerInfo *peer,
1564 struct DHT_MessageContext *msg_ctx)
1566 struct GNUNET_DHT_P2PRouteMessage *route_message;
1567 struct P2PPendingMessage *pending;
1572 increment_stats (STAT_ROUTE_FORWARDS);
1573 GNUNET_assert (peer != NULL);
1574 if ((msg_ctx->closest != GNUNET_YES) &&
1575 (peer == find_closest_peer (&msg_ctx->key)))
1576 increment_stats (STAT_ROUTE_FORWARDS_CLOSEST);
1579 sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (msg->size) +
1580 (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1581 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1582 psize = sizeof (struct P2PPendingMessage) + msize;
1583 pending = GNUNET_malloc (psize);
1584 pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
1585 pending->importance = msg_ctx->importance;
1586 pending->timeout = msg_ctx->timeout;
1587 route_message = (struct GNUNET_DHT_P2PRouteMessage *) pending->msg;
1588 route_message->header.size = htons (msize);
1589 route_message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE);
1590 route_message->options = htonl (msg_ctx->msg_options);
1591 route_message->hop_count = htonl (msg_ctx->hop_count + 1);
1592 route_message->network_size = htonl (msg_ctx->network_size);
1593 route_message->desired_replication_level = htonl (msg_ctx->replication);
1594 #if HAVE_UID_FOR_TESTING
1595 route_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1597 if (msg_ctx->bloom != NULL)
1598 GNUNET_assert (GNUNET_OK ==
1599 GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
1603 memcpy (&route_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1604 memcpy (&route_message[1], msg, ntohs (msg->size));
1605 if (GNUNET_DHT_RO_RECORD_ROUTE ==
1606 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
1608 route_message->outgoing_path_length = htonl (msg_ctx->path_history_len);
1609 /* Set pointer to start of enc_msg */
1610 route_path = (char *) &route_message[1];
1611 /* Offset to the end of the enc_msg */
1612 route_path += ntohs (msg->size);
1613 /* Copy the route_path after enc_msg */
1614 memcpy (route_path, msg_ctx->path_history,
1615 msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "%s:%s Adding pending message size %d for peer %s\n", my_short_id,
1620 "DHT", msize, GNUNET_i2s (&peer->id));
1622 peer->pending_count++;
1623 increment_stats ("# pending messages scheduled");
1624 GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
1626 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1627 peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1632 * Task run to check for messages that need to be sent to a client.
1634 * @param client a ClientList, containing the client and any messages to be sent to it
1637 process_pending_messages (struct ClientList *client)
1639 if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
1641 client->transmit_handle =
1642 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
1643 ntohs (client->pending_head->
1645 GNUNET_TIME_UNIT_FOREVER_REL,
1646 &send_generic_reply, client);
1650 * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
1651 * request. A ClientList is passed as closure, take the head of the list
1652 * and copy it into buf, which has the result of sending the message to the
1655 * @param cls closure to this call
1656 * @param size maximum number of bytes available to send
1657 * @param buf where to copy the actual message to
1659 * @return the number of bytes actually copied, 0 indicates failure
1662 send_generic_reply (void *cls, size_t size, void *buf)
1664 struct ClientList *client = cls;
1666 struct PendingMessage *reply;
1670 client->transmit_handle = NULL;
1673 /* client disconnected */
1677 while ((NULL != (reply = client->pending_head)) &&
1678 (size >= off + (msize = ntohs (reply->msg->size))))
1680 GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
1682 memcpy (&cbuf[off], reply->msg, msize);
1683 GNUNET_free (reply);
1686 process_pending_messages (client);
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Transmitted %u bytes of replies to client\n",
1690 (unsigned int) off);
1697 * Add a PendingMessage to the clients list of messages to be sent
1699 * @param client the active client to send the message to
1700 * @param pending_message the actual message to send
1703 add_pending_message (struct ClientList *client,
1704 struct PendingMessage *pending_message)
1706 GNUNET_CONTAINER_DLL_insert_after (client->pending_head, client->pending_tail,
1707 client->pending_tail, pending_message);
1708 process_pending_messages (client);
1713 * Called when a reply needs to be sent to a client, as
1714 * a result it found to a GET or FIND PEER request.
1716 * @param client the client to send the reply to
1717 * @param message the encapsulated message to send
1718 * @param msg_ctx the context of the received message
1721 send_reply_to_client (struct ClientList *client,
1722 const struct GNUNET_MessageHeader *message,
1723 struct DHT_MessageContext *msg_ctx)
1725 struct GNUNET_DHT_RouteResultMessage *reply;
1726 struct PendingMessage *pending_message;
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': Sending reply to client.\n",
1737 my_short_id, "DHT");
1739 msize = ntohs (message->size);
1741 sizeof (struct GNUNET_DHT_RouteResultMessage) + msize +
1742 (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1743 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1745 GNUNET_break_op (0);
1748 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize);
1749 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
1750 reply = (struct GNUNET_DHT_RouteResultMessage *) &pending_message[1];
1751 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT);
1752 reply->header.size = htons (tsize);
1753 reply->outgoing_path_length = htonl (msg_ctx->path_history_len);
1754 reply->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1755 memcpy (&reply->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1756 reply_offset = (char *) &reply[1];
1757 memcpy (&reply[1], message, msize);
1758 if (msg_ctx->path_history_len > 0)
1760 reply_offset += msize;
1761 memcpy (reply_offset, msg_ctx->path_history,
1762 msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "Returning message with outgoing path length %d\n",
1767 msg_ctx->path_history_len);
1768 for (i = 0; i < msg_ctx->path_history_len; i++)
1771 &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer %d:%s\n", i,
1773 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1776 add_pending_message (client, pending_message);
1780 * Consider whether or not we would like to have this peer added to
1781 * our routing table. Check whether bucket for this peer is full,
1782 * if so return negative; if not return positive. Since peers are
1783 * only added on CORE level connect, this doesn't actually add the
1784 * peer to the routing table.
1786 * @param peer the peer we are considering adding
1788 * @return GNUNET_YES if we want this peer, GNUNET_NO if not (bucket
1792 consider_peer (struct GNUNET_PeerIdentity *peer)
1797 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
1798 &peer->hashPubKey)) ||
1799 (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))))
1800 return GNUNET_NO; /* We already know this peer (are connected even!) */
1801 bucket = find_current_bucket (&peer->hashPubKey);
1803 if ((k_buckets[bucket].peers_size < bucket_size) ||
1804 ((bucket == lowest_bucket) && (lowest_bucket > 0)))
1812 * Task used to remove forwarding entries, either
1813 * after timeout, when full, or on shutdown.
1815 * @param cls the entry to remove
1816 * @param tc context, reason, etc.
1819 remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1821 struct DHTRouteSource *source_info = cls;
1822 struct DHTQueryRecord *record;
1824 source_info = GNUNET_CONTAINER_heap_remove_node (source_info->hnode);
1825 record = source_info->record;
1826 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
1828 if (record->head == NULL) /* No more entries in DLL */
1830 GNUNET_assert (GNUNET_YES ==
1831 GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
1832 &record->key, record));
1833 GNUNET_free (record);
1835 if (source_info->find_peers_responded != NULL)
1836 GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
1837 GNUNET_free (source_info);
1841 * Main function that handles whether or not to route a result
1842 * message to other peers, or to send to our local client.
1844 * @param msg the result message to be routed
1845 * @param msg_ctx context of the message we are routing
1847 * @return the number of peers the message was routed to,
1848 * GNUNET_SYSERR on failure
1851 route_result_message (struct GNUNET_MessageHeader *msg,
1852 struct DHT_MessageContext *msg_ctx)
1854 struct GNUNET_PeerIdentity new_peer;
1855 struct DHTQueryRecord *record;
1856 struct DHTRouteSource *pos;
1857 struct PeerInfo *peer_info;
1858 const struct GNUNET_MessageHeader *hello_msg;
1864 increment_stats (STAT_RESULTS);
1866 * If a find peer result message is received and contains a valid
1867 * HELLO for another peer, offer it to the transport service.
1869 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
1871 if (ntohs (msg->size) <= sizeof (struct GNUNET_MessageHeader))
1872 GNUNET_break_op (0);
1874 hello_msg = &msg[1];
1875 if ((ntohs (hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
1877 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello_msg,
1880 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1881 "%s:%s Received non-HELLO message type in find peer result message!\n",
1882 my_short_id, "DHT");
1883 GNUNET_break_op (0);
1886 else /* We have a valid hello, and peer id stored in new_peer */
1888 find_peer_context.count++;
1889 increment_stats (STAT_FIND_PEER_REPLY);
1890 if (GNUNET_YES == consider_peer (&new_peer))
1892 increment_stats (STAT_HELLOS_PROVIDED);
1893 GNUNET_TRANSPORT_offer_hello (transport_handle, hello_msg, NULL, NULL);
1894 GNUNET_CORE_peer_request_connect (coreAPI, &new_peer, NULL, NULL);
1899 if (malicious_dropper == GNUNET_YES)
1903 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
1905 if (record == NULL) /* No record of this message! */
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "`%s:%s': Have no record of response key %s uid %llu\n",
1910 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
1911 msg_ctx->unique_id);
1913 #if DEBUG_DHT_ROUTING
1914 if ((debug_routes_extended) && (dhtlog_handle != NULL))
1916 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
1917 msg_ctx->hop_count, GNUNET_SYSERR,
1918 &my_identity, &msg_ctx->key, &msg_ctx->peer,
1928 #if STRICT_FORWARDING
1929 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) /* If we have already forwarded this peer id, don't do it again! */
1932 GNUNET_CONTAINER_bloomfilter_test (pos->find_peers_responded,
1933 &new_peer.hashPubKey))
1935 increment_stats ("# find peer responses NOT forwarded (bloom match)");
1941 GNUNET_CONTAINER_bloomfilter_add (pos->find_peers_responded,
1942 &new_peer.hashPubKey);
1947 if (0 == memcmp (&pos->source, &my_identity, sizeof (struct GNUNET_PeerIdentity))) /* Local client (or DHT) initiated request! */
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1951 "`%s:%s': Sending response key %s uid %llu to client\n",
1952 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
1953 msg_ctx->unique_id);
1955 #if DEBUG_DHT_ROUTING
1956 if ((debug_routes_extended) && (dhtlog_handle != NULL))
1958 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
1959 msg_ctx->hop_count, GNUNET_YES,
1960 &my_identity, &msg_ctx->key, &msg_ctx->peer,
1964 increment_stats (STAT_RESULTS_TO_CLIENT);
1965 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
1966 increment_stats (STAT_GET_REPLY);
1968 for (i = 0; i < msg_ctx->path_history_len; i++)
1973 &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1975 "(before client) Key %s Found peer %d:%s\n",
1976 GNUNET_h2s (&msg_ctx->key), i,
1977 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1980 send_reply_to_client (pos->client, msg, msg_ctx);
1982 else /* Send to peer */
1984 peer_info = find_peer_by_id (&pos->source);
1985 if (peer_info == NULL) /* Didn't find the peer in our routing table, perhaps peer disconnected! */
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 "`%s:%s': Forwarding response key %s uid %llu to peer %s\n",
1993 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
1994 msg_ctx->unique_id, GNUNET_i2s (&peer_info->id));
1996 #if DEBUG_DHT_ROUTING
1997 if ((debug_routes_extended) && (dhtlog_handle != NULL))
1999 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
2000 msg_ctx->hop_count, GNUNET_NO,
2001 &my_identity, &msg_ctx->key,
2002 &msg_ctx->peer, &pos->source);
2005 forward_result_message (msg, peer_info, msg_ctx);
2006 /* Try removing forward entries after sending once, only allows ONE response per request */
2007 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
2009 GNUNET_SCHEDULER_cancel (pos->delete_task);
2011 GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);
2021 * Iterator for local get request results,
2023 * @param cls closure for iterator, a DatacacheGetContext
2024 * @param exp when does this value expire?
2025 * @param key the key this data is stored under
2026 * @param size the size of the data identified by key
2027 * @param data the actual data
2028 * @param type the type of the data
2030 * @return GNUNET_OK to continue iteration, anything else
2031 * to stop iteration.
2034 datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
2035 const GNUNET_HashCode * key, size_t size,
2036 const char *data, enum GNUNET_BLOCK_Type type)
2038 struct DHT_MessageContext *msg_ctx = cls;
2039 struct DHT_MessageContext new_msg_ctx;
2040 struct GNUNET_DHT_GetResultMessage *get_result;
2041 enum GNUNET_BLOCK_EvaluationResult eval;
2042 const struct DHTPutEntry *put_entry;
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2052 "`%s:%s': Received `%s' response from datacache\n", my_short_id,
2056 put_entry = (const struct DHTPutEntry *) data;
2059 sizeof (struct DHTPutEntry) + put_entry->data_size +
2060 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)))
2062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2063 "Path + data size doesn't add up for data inserted into datacache!\nData size %d, path length %d, expected %d, got %d\n",
2064 put_entry->data_size, put_entry->path_length,
2065 sizeof (struct DHTPutEntry) + put_entry->data_size +
2066 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)),
2068 msg_ctx->do_forward = GNUNET_NO;
2073 GNUNET_BLOCK_evaluate (block_context, type, key, &msg_ctx->reply_bf,
2074 msg_ctx->reply_bf_mutator, msg_ctx->xquery,
2075 msg_ctx->xquery_size, &put_entry[1],
2076 put_entry->data_size);
2080 case GNUNET_BLOCK_EVALUATION_OK_LAST:
2081 msg_ctx->do_forward = GNUNET_NO;
2082 case GNUNET_BLOCK_EVALUATION_OK_MORE:
2083 memcpy (&new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2084 if (GNUNET_DHT_RO_RECORD_ROUTE ==
2085 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2087 new_msg_ctx.msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2089 for (i = 0; i < new_msg_ctx.path_history_len; i++)
2092 &new_msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094 "(get_iterator) Key %s Found peer %d:%s\n",
2095 GNUNET_h2s (&msg_ctx->key), i,
2096 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
2102 sizeof (struct GNUNET_DHT_GetResultMessage) + put_entry->data_size +
2103 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity));
2104 get_result = GNUNET_malloc (get_size);
2105 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
2106 get_result->header.size = htons (get_size);
2107 get_result->expiration = GNUNET_TIME_absolute_hton (exp);
2108 get_result->type = htons (type);
2109 get_result->put_path_length = htons (put_entry->path_length);
2110 path_offset = (char *) &put_entry[1];
2111 path_offset += put_entry->data_size;
2113 for (i = 0; i < put_entry->path_length; i++)
2115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2116 "(get_iterator PUT path) Key %s Found peer %d:%s\n",
2117 GNUNET_h2s (&msg_ctx->key), i,
2118 GNUNET_i2s ((struct GNUNET_PeerIdentity *)
2121 GNUNET_PeerIdentity)]));
2124 /* Copy the actual data and the path_history to the end of the get result */
2125 memcpy (&get_result[1], &put_entry[1],
2126 put_entry->data_size +
2127 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)));
2128 new_msg_ctx.peer = my_identity;
2129 new_msg_ctx.bloom = NULL;
2130 new_msg_ctx.hop_count = 0;
2131 new_msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */
2132 new_msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
2133 increment_stats (STAT_GET_RESPONSE_START);
2134 route_result_message (&get_result->header, &new_msg_ctx);
2135 GNUNET_free (get_result);
2137 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': Duplicate block error\n",
2140 my_short_id, "DHT");
2143 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
2145 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "`%s:%s': Invalid request error\n",
2146 my_short_id, "DHT");
2149 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 "`%s:%s': Valid request, no results.\n", my_short_id, "DHT");
2156 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
2157 GNUNET_break_op (0);
2158 msg_ctx->do_forward = GNUNET_NO;
2160 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
2162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2163 "`%s:%s': Unsupported block type (%u) in response!\n",
2164 my_short_id, "DHT", type);
2166 /* msg_ctx->do_forward = GNUNET_NO; // not sure... */
2174 * Main function that handles whether or not to route a message to other
2177 * @param msg the message to be routed
2178 * @param msg_ctx the context containing all pertinent information about the message
2181 route_message (const struct GNUNET_MessageHeader *msg,
2182 struct DHT_MessageContext *msg_ctx);
2186 * Server handler for all dht get requests, look for data,
2187 * if found, send response either to clients or other peers.
2189 * @param msg the actual get message
2190 * @param msg_ctx struct containing pertinent information about the get request
2192 * @return number of items found for GET request
2195 handle_dht_get (const struct GNUNET_MessageHeader *msg,
2196 struct DHT_MessageContext *msg_ctx)
2198 const struct GNUNET_DHT_GetMessage *get_msg;
2201 unsigned int results;
2203 enum GNUNET_BLOCK_Type type;
2205 msize = ntohs (msg->size);
2206 if (msize < sizeof (struct GNUNET_DHT_GetMessage))
2211 get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
2212 bf_size = ntohs (get_msg->bf_size);
2213 msg_ctx->xquery_size = ntohs (get_msg->xquery_size);
2214 msg_ctx->reply_bf_mutator = get_msg->bf_mutator;
2216 sizeof (struct GNUNET_DHT_GetMessage) + bf_size + msg_ctx->xquery_size)
2218 GNUNET_break_op (0);
2221 end = (const char *) &get_msg[1];
2222 if (msg_ctx->xquery_size == 0)
2224 msg_ctx->xquery = NULL;
2228 msg_ctx->xquery = (const void *) end;
2229 end += msg_ctx->xquery_size;
2233 msg_ctx->reply_bf = NULL;
2238 GNUNET_CONTAINER_bloomfilter_init (end, bf_size,
2239 GNUNET_DHT_GET_BLOOMFILTER_K);
2241 type = (enum GNUNET_BLOCK_Type) ntohl (get_msg->type);
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244 "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n",
2245 my_short_id, "DHT", "GET", type, GNUNET_h2s (&msg_ctx->key),
2246 msg_ctx->unique_id);
2248 increment_stats (STAT_GETS);
2251 if (type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
2253 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
2257 msg_ctx->do_forward = GNUNET_YES;
2258 if (datacache != NULL)
2260 GNUNET_DATACACHE_get (datacache, &msg_ctx->key, type,
2261 &datacache_get_iterator, msg_ctx);
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "`%s:%s': Found %d results for `%s' request uid %llu\n",
2265 my_short_id, "DHT", results, "GET", msg_ctx->unique_id);
2269 #if DEBUG_DHT_ROUTING
2270 if ((debug_routes) && (dhtlog_handle != NULL))
2272 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
2273 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2277 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2279 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2280 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2281 &msg_ctx->key, &msg_ctx->peer, NULL);
2287 /* check query valid */
2288 if (GNUNET_BLOCK_EVALUATION_REQUEST_INVALID ==
2289 GNUNET_BLOCK_evaluate (block_context, type, &msg_ctx->key,
2290 &msg_ctx->reply_bf, msg_ctx->reply_bf_mutator,
2291 msg_ctx->xquery, msg_ctx->xquery_size, NULL, 0))
2293 GNUNET_break_op (0);
2294 msg_ctx->do_forward = GNUNET_NO;
2298 if (msg_ctx->hop_count == 0) /* Locally initiated request */
2300 #if DEBUG_DHT_ROUTING
2301 if ((debug_routes) && (dhtlog_handle != NULL))
2303 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
2304 msg_ctx->hop_count, GNUNET_NO, &my_identity,
2309 if (msg_ctx->do_forward == GNUNET_YES)
2310 route_message (msg, msg_ctx);
2311 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
2317 remove_recent_find_peer (void *cls,
2318 const struct GNUNET_SCHEDULER_TaskContext *tc)
2320 GNUNET_HashCode *key = cls;
2322 GNUNET_assert (GNUNET_YES ==
2323 GNUNET_CONTAINER_multihashmap_remove
2324 (recent_find_peer_requests, key, NULL));
2330 * Server handler for initiating local dht find peer requests
2332 * @param find_msg the actual find peer message
2333 * @param msg_ctx struct containing pertinent information about the request
2337 handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
2338 struct DHT_MessageContext *msg_ctx)
2340 struct GNUNET_MessageHeader *find_peer_result;
2341 struct GNUNET_DHT_FindPeerMessage *find_peer_message;
2342 struct DHT_MessageContext *new_msg_ctx;
2343 struct GNUNET_CONTAINER_BloomFilter *incoming_bloom;
2346 GNUNET_HashCode *recent_hash;
2347 struct GNUNET_MessageHeader *other_hello;
2348 size_t other_hello_size;
2349 struct GNUNET_PeerIdentity peer_id;
2351 find_peer_message = (struct GNUNET_DHT_FindPeerMessage *) find_msg;
2352 GNUNET_break_op (ntohs (find_msg->size) >=
2353 (sizeof (struct GNUNET_DHT_FindPeerMessage)));
2354 if (ntohs (find_msg->size) < sizeof (struct GNUNET_DHT_FindPeerMessage))
2357 other_hello_size = 0;
2358 if (ntohs (find_msg->size) > sizeof (struct GNUNET_DHT_FindPeerMessage))
2361 ntohs (find_msg->size) - sizeof (struct GNUNET_DHT_FindPeerMessage);
2362 other_hello = GNUNET_malloc (other_hello_size);
2363 memcpy (other_hello, &find_peer_message[1], other_hello_size);
2364 if ((GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) other_hello) == 0)
2366 GNUNET_HELLO_get_id ((struct GNUNET_HELLO_Message *) other_hello,
2369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2370 "Received invalid HELLO message in find peer request!\n");
2371 GNUNET_free (other_hello);
2374 #if FIND_PEER_WITH_HELLO
2375 if (GNUNET_YES == consider_peer (&peer_id))
2377 increment_stats (STAT_HELLOS_PROVIDED);
2378 GNUNET_TRANSPORT_offer_hello (transport_handle, other_hello, NULL, NULL);
2379 GNUNET_CORE_peer_request_connect (coreAPI, &peer_id, NULL, NULL);
2380 route_message (find_msg, msg_ctx);
2381 GNUNET_free (other_hello);
2384 else /* We don't want this peer! */
2386 route_message (find_msg, msg_ctx);
2387 GNUNET_free (other_hello);
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 "`%s:%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
2396 my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&msg_ctx->key),
2397 ntohs (find_msg->size), sizeof (struct GNUNET_MessageHeader));
2399 if (my_hello == NULL)
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2403 "`%s': Our HELLO is null, can't return.\n", "DHT");
2405 GNUNET_free_non_null (other_hello);
2406 route_message (find_msg, msg_ctx);
2411 GNUNET_CONTAINER_bloomfilter_init (find_peer_message->bloomfilter,
2412 DHT_BLOOM_SIZE, DHT_BLOOM_K);
2414 GNUNET_CONTAINER_bloomfilter_test (incoming_bloom,
2415 &my_identity.hashPubKey))
2417 increment_stats (STAT_BLOOM_FIND_PEER);
2418 GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
2419 GNUNET_free_non_null (other_hello);
2420 route_message (find_msg, msg_ctx);
2421 return; /* We match the bloomfilter, do not send a response to this peer (they likely already know us!) */
2423 GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
2425 #if RESTRICT_FIND_PEER
2428 * Ignore any find peer requests from a peer we have seen very recently.
2430 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! */
2432 increment_stats ("# dht find peer requests ignored (recently seen!)");
2433 GNUNET_free_non_null (other_hello);
2438 * Use this check to only allow the peer to respond to find peer requests if
2439 * it would be beneficial to have the requesting peer in this peers routing
2440 * table. Can be used to thwart peers flooding the network with find peer
2441 * requests that we don't care about. However, if a new peer is joining
2442 * the network and has no other peers this is a problem (assume all buckets
2443 * full, no one will respond!).
2445 memcpy (&peer_id.hashPubKey, &msg_ctx->key, sizeof (GNUNET_HashCode));
2446 if (GNUNET_NO == consider_peer (&peer_id))
2448 increment_stats ("# dht find peer requests ignored (do not need!)");
2449 GNUNET_free_non_null (other_hello);
2450 route_message (find_msg, msg_ctx);
2455 recent_hash = GNUNET_malloc (sizeof (GNUNET_HashCode));
2456 memcpy (recent_hash, &msg_ctx->key, sizeof (GNUNET_HashCode));
2457 if (GNUNET_SYSERR !=
2458 GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests,
2459 &msg_ctx->key, NULL,
2460 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2464 "Adding recent remove task for key `%s`!\n",
2465 GNUNET_h2s (&msg_ctx->key));
2467 /* Only add a task if there wasn't one for this key already! */
2468 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2469 (GNUNET_TIME_UNIT_SECONDS, 30),
2470 &remove_recent_find_peer, recent_hash);
2474 GNUNET_free (recent_hash);
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477 "Received duplicate find peer request too soon!\n");
2481 /* Simplistic find_peer functionality, always return our hello */
2482 hello_size = ntohs (my_hello->size);
2483 tsize = hello_size + sizeof (struct GNUNET_MessageHeader);
2485 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2487 GNUNET_break_op (0);
2488 GNUNET_free_non_null (other_hello);
2492 find_peer_result = GNUNET_malloc (tsize);
2493 find_peer_result->type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT);
2494 find_peer_result->size = htons (tsize);
2495 memcpy (&find_peer_result[1], my_hello, hello_size);
2497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2498 "`%s': Sending hello size %d to requesting peer.\n", "DHT",
2502 new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
2503 memcpy (new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2504 new_msg_ctx->peer = my_identity;
2505 new_msg_ctx->bloom =
2506 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2507 new_msg_ctx->hop_count = 0;
2508 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make find peer requests a higher priority */
2509 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
2510 increment_stats (STAT_FIND_PEER_ANSWER);
2511 if (GNUNET_DHT_RO_RECORD_ROUTE ==
2512 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2514 new_msg_ctx->msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2515 new_msg_ctx->path_history_len = msg_ctx->path_history_len;
2516 /* Assign to previous msg_ctx path history, caller should free after our return */
2517 new_msg_ctx->path_history = msg_ctx->path_history;
2519 route_result_message (find_peer_result, new_msg_ctx);
2520 GNUNET_free (new_msg_ctx);
2521 #if DEBUG_DHT_ROUTING
2522 if ((debug_routes) && (dhtlog_handle != NULL))
2524 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER,
2525 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2529 GNUNET_free_non_null (other_hello);
2530 GNUNET_free (find_peer_result);
2531 route_message (find_msg, msg_ctx);
2536 * Server handler for initiating local dht put requests
2538 * @param msg the actual put message
2539 * @param msg_ctx struct containing pertinent information about the request
2542 handle_dht_put (const struct GNUNET_MessageHeader *msg,
2543 struct DHT_MessageContext *msg_ctx)
2545 const struct GNUNET_DHT_PutMessage *put_msg;
2546 struct DHTPutEntry *put_entry;
2547 unsigned int put_size;
2549 enum GNUNET_BLOCK_Type put_type;
2552 GNUNET_HashCode key;
2553 struct DHTQueryRecord *record;
2555 GNUNET_assert (ntohs (msg->size) >= sizeof (struct GNUNET_DHT_PutMessage));
2557 put_msg = (const struct GNUNET_DHT_PutMessage *) msg;
2558 put_type = (enum GNUNET_BLOCK_Type) ntohl (put_msg->type);
2560 if (put_type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
2562 #if DEBUG_DHT_ROUTING
2563 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2565 /** Log routes that die due to high load! */
2566 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2567 msg_ctx->hop_count, GNUNET_SYSERR,
2568 &my_identity, &msg_ctx->key, &msg_ctx->peer,
2576 ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
2578 GNUNET_BLOCK_get_key (block_context, put_type, &put_msg[1], data_size,
2580 if (GNUNET_NO == ret)
2582 #if DEBUG_DHT_ROUTING
2583 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2585 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2586 msg_ctx->hop_count, GNUNET_SYSERR,
2587 &my_identity, &msg_ctx->key, &msg_ctx->peer,
2592 GNUNET_break_op (0);
2595 if ((GNUNET_YES == ret) &&
2596 (0 != memcmp (&key, &msg_ctx->key, sizeof (GNUNET_HashCode))))
2598 #if DEBUG_DHT_ROUTING
2599 if ((debug_routes_extended) && (dhtlog_handle != NULL))
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,
2607 /* invalid wrapper: key mismatch! */
2608 GNUNET_break_op (0);
2611 /* ret == GNUNET_SYSERR means that there is no known relationship between
2612 * data and the key, so we cannot check it */
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2616 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
2617 msg_ctx->unique_id);
2619 #if DEBUG_DHT_ROUTING
2620 if (msg_ctx->hop_count == 0) /* Locally initiated request */
2622 if ((debug_routes) && (dhtlog_handle != NULL))
2624 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
2625 msg_ctx->hop_count, GNUNET_NO, &my_identity,
2631 record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap,
2635 struct DHTRouteSource *pos;
2636 struct GNUNET_DHT_GetResultMessage *get_result;
2637 struct DHT_MessageContext new_msg_ctx;
2643 /* TODO: do only for local started requests? or also for remote peers? */
2644 /* TODO: include this in statistics? under what? */
2645 /* TODO: reverse order of path_history? */
2646 if (NULL == pos->client)
2652 memcpy (&new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2653 if (GNUNET_DHT_RO_RECORD_ROUTE ==
2654 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2656 new_msg_ctx.msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2658 for (i = 0; i < new_msg_ctx.path_history_len; i++)
2661 &new_msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2663 "(put for active get) Key %s Found peer %d:%s\n",
2664 GNUNET_h2s (&msg_ctx->key), i,
2665 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
2671 sizeof (struct GNUNET_DHT_GetResultMessage) + data_size +
2672 (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2673 get_result = GNUNET_malloc (get_size);
2674 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
2675 get_result->header.size = htons (get_size);
2676 get_result->expiration = put_msg->expiration;
2677 get_result->type = put_msg->type;
2678 get_result->put_path_length = htons (msg_ctx->path_history_len);
2680 /* Copy the actual data and the path_history to the end of the get result */
2681 memcpy (&get_result[1], &put_msg[1], data_size);
2682 path_offset = (char *) &get_result[1];
2683 path_offset += data_size;
2684 memcpy (path_offset, msg_ctx->path_history,
2685 msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2686 new_msg_ctx.peer = my_identity;
2687 new_msg_ctx.bloom = NULL;
2688 new_msg_ctx.hop_count = 0;
2689 /* Make result routing a higher priority */
2690 new_msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;
2691 new_msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
2692 new_msg_ctx.unique_id = pos->uid;
2693 send_reply_to_client(pos->client, &get_result->header, &new_msg_ctx);
2694 GNUNET_free (get_result);
2699 if (msg_ctx->closest != GNUNET_YES)
2701 route_message (msg, msg_ctx);
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2707 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2708 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
2709 msg_ctx->unique_id);
2712 #if DEBUG_DHT_ROUTING
2713 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2715 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2716 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2717 &msg_ctx->key, &msg_ctx->peer, NULL);
2720 if ((debug_routes) && (dhtlog_handle != NULL))
2722 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
2723 msg_ctx->hop_count, GNUNET_YES, &my_identity,
2728 increment_stats (STAT_PUTS_INSERTED);
2729 if (datacache != NULL)
2731 /* Put size is actual data size plus struct overhead plus path length (if any) */
2733 data_size + sizeof (struct DHTPutEntry) +
2734 (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2735 put_entry = GNUNET_malloc (put_size);
2736 put_entry->data_size = data_size;
2737 put_entry->path_length = msg_ctx->path_history_len;
2738 /* Copy data to end of put entry */
2739 memcpy (&put_entry[1], &put_msg[1], data_size);
2740 if (msg_ctx->path_history_len > 0)
2742 /* Copy path after data */
2743 path_offset = (char *) &put_entry[1];
2744 path_offset += data_size;
2745 memcpy (path_offset, msg_ctx->path_history,
2746 msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2750 GNUNET_DATACACHE_put (datacache, &msg_ctx->key, put_size,
2751 (const char *) put_entry, put_type,
2752 GNUNET_TIME_absolute_ntoh (put_msg->expiration));
2753 GNUNET_free (put_entry);
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2757 "`%s:%s': %s request received, but have no datacache!\n",
2758 my_short_id, "DHT", "PUT");
2760 if (stop_on_closest == GNUNET_NO)
2761 route_message (msg, msg_ctx);
2766 * To how many peers should we (on average)
2767 * forward the request to obtain the desired
2768 * target_replication count (on average).
2770 * returns: target_replication / (est. hops) + (target_replication * hop_count)
2771 * where est. hops is typically 2 * the routing table depth
2773 * @param hop_count number of hops the message has traversed
2774 * @param target_replication the number of total paths desired
2776 * @return Some number of peers to forward the message to
2779 get_forward_count (unsigned int hop_count, size_t target_replication)
2781 uint32_t random_value;
2782 unsigned int forward_count;
2786 * If we are behaving in strict kademlia mode, send multiple initial requests,
2787 * but then only send to 1 or 0 peers based strictly on the number of hops.
2789 if (strict_kademlia == GNUNET_YES)
2792 return kademlia_replication;
2793 if (hop_count < log_of_network_size_estimate * 2.0)
2798 if (hop_count > log_of_network_size_estimate * 2.0)
2800 if (GNUNET_YES == paper_forwarding)
2802 /* Once we have reached our ideal number of hops, don't stop forwarding! */
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2807 "Hop count too high (est %f, lowest %d), NOT Forwarding request\n",
2808 log_of_network_size_estimate * 2.0, lowest_bucket);
2813 if (GNUNET_YES == paper_forwarding)
2815 /* FIXME: re-run replication trials with this formula */
2817 1 + (target_replication - 1.0) / (log_of_network_size_estimate +
2818 ((float) (target_replication - 1.0) *
2820 /* Set forward count to floor of target_value */
2821 forward_count = (unsigned int) target_value;
2822 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
2823 target_value = target_value - forward_count;
2825 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
2827 if (random_value < (target_value * UINT32_MAX))
2835 target_replication / (log_of_network_size_estimate +
2836 ((float) target_replication * hop_count));
2837 if (target_value > 1)
2839 /* Set forward count to floor of target_value */
2840 forward_count = (unsigned int) target_value;
2841 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
2842 target_value = target_value - forward_count;
2846 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
2848 if (random_value < (target_value * UINT32_MAX))
2852 return forward_count;
2857 * Check whether my identity is closer than any known peers.
2858 * If a non-null bloomfilter is given, check if this is the closest
2859 * peer that hasn't already been routed to.
2861 * @param target hash code to check closeness to
2862 * @param bloom bloomfilter, exclude these entries from the decision
2863 * @return GNUNET_YES if node location is closest,
2864 * GNUNET_NO otherwise.
2867 am_closest_peer (const GNUNET_HashCode * target,
2868 struct GNUNET_CONTAINER_BloomFilter *bloom)
2874 struct PeerInfo *pos;
2875 unsigned int my_distance;
2877 if (0 == memcmp (&my_identity.hashPubKey, target, sizeof (GNUNET_HashCode)))
2880 bucket_num = find_current_bucket (target);
2882 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, target);
2883 my_distance = distance (&my_identity.hashPubKey, target);
2884 pos = k_buckets[bucket_num].head;
2886 while ((pos != NULL) && (count < bucket_size))
2888 if ((bloom != NULL) &&
2890 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
2893 continue; /* Skip already checked entries */
2896 other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, target);
2897 if (other_bits > bits)
2899 else if (other_bits == bits) /* We match the same number of bits */
2901 if (strict_kademlia != GNUNET_YES) /* Return that we at as close as any other peer */
2903 if (distance (&pos->id.hashPubKey, target) < my_distance) /* Check all known peers, only return if we are the true closest */
2909 /* No peers closer, we are the closest! */
2915 * Select a peer from the routing table that would be a good routing
2916 * destination for sending a message for "target". The resulting peer
2917 * must not be in the set of blocked peers.<p>
2919 * Note that we should not ALWAYS select the closest peer to the
2920 * target, peers further away from the target should be chosen with
2921 * exponentially declining probability.
2923 * @param target the key we are selecting a peer to route to
2924 * @param bloom a bloomfilter containing entries this request has seen already
2925 * @param hops how many hops has this message traversed thus far
2927 * @return Peer to route to, or NULL on error
2929 static struct PeerInfo *
2930 select_peer (const GNUNET_HashCode * target,
2931 struct GNUNET_CONTAINER_BloomFilter *bloom, unsigned int hops)
2935 unsigned int selected;
2936 struct PeerInfo *pos;
2937 unsigned int distance;
2938 unsigned int largest_distance;
2939 struct PeerInfo *chosen;
2941 /** If we are doing kademlia routing (saves some cycles) */
2942 if ((strict_kademlia == GNUNET_YES) || (hops >= log_of_network_size_estimate))
2944 /* greedy selection (closest peer that is not in bloomfilter) */
2945 largest_distance = 0;
2947 for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
2949 pos = k_buckets[bc].head;
2951 while ((pos != NULL) && (count < bucket_size))
2953 /* If we are doing strict Kademlia routing, then checking the bloomfilter is basically cheating! */
2955 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
2957 distance = inverse_distance (target, &pos->id.hashPubKey);
2958 if (distance > largest_distance)
2961 largest_distance = distance;
2968 if ((largest_distance > 0) && (chosen != NULL))
2970 GNUNET_CONTAINER_bloomfilter_add (bloom, &chosen->id.hashPubKey);
2973 return NULL; /* no peer available or we are the closest */
2977 /* select "random" peer */
2978 /* count number of peers that are available and not filtered */
2980 for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
2982 pos = k_buckets[bc].head;
2983 while ((pos != NULL) && (count < bucket_size))
2986 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
2989 increment_stats ("# peer blocked from selection by Bloom filter");
2990 continue; /* Ignore bloomfiltered peers */
2996 if (count == 0) /* No peers to select from! */
2998 increment_stats ("# failed to select peer");
3001 /* Now actually choose a peer */
3002 selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
3004 for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
3006 pos = k_buckets[bc].head;
3007 while ((pos != NULL) && (count < bucket_size))
3010 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3013 continue; /* Ignore bloomfiltered peers */
3015 if (0 == selected--)
3026 * Task used to remove recent entries, either
3027 * after timeout, when full, or on shutdown.
3029 * @param cls the entry to remove
3030 * @param tc context, reason, etc.
3033 remove_recent (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3035 struct RecentRequest *req = cls;
3036 static GNUNET_HashCode hash;
3038 GNUNET_assert (req != NULL);
3039 hash_from_uid (req->uid, &hash);
3040 #if HAVE_UID_FOR_TESTING > 1
3041 GNUNET_assert (GNUNET_YES ==
3042 GNUNET_CONTAINER_multihashmap_remove (recent.hashmap, &hash,
3045 GNUNET_CONTAINER_heap_remove_node (req->heap_node);
3046 GNUNET_CONTAINER_bloomfilter_free (req->bloom);
3050 * if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) && (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)) && (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap)))
3052 * GNUNET_CONTAINER_multihashmap_destroy(recent.hashmap);
3053 * GNUNET_CONTAINER_heap_destroy(recent.minHeap);
3059 * Remember this routing request so that if a reply is
3060 * received we can either forward it to the correct peer
3061 * or return the result locally.
3063 * @param msg_ctx Context of the route request
3065 * @return GNUNET_YES if this response was cached, GNUNET_NO if not
3068 cache_response (struct DHT_MessageContext *msg_ctx)
3070 struct DHTQueryRecord *record;
3071 struct DHTRouteSource *source_info;
3072 struct DHTRouteSource *pos;
3073 struct GNUNET_TIME_Absolute now;
3074 unsigned int current_size;
3076 current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
3078 #if DELETE_WHEN_FULL
3079 while (current_size >= MAX_OUTSTANDING_FORWARDS)
3081 source_info = GNUNET_CONTAINER_heap_remove_root (forward_list.minHeap);
3082 GNUNET_assert (source_info != NULL);
3083 record = source_info->record;
3084 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
3085 if (record->head == NULL) /* No more entries in DLL */
3087 GNUNET_assert (GNUNET_YES ==
3088 GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
3091 GNUNET_free (record);
3093 if (source_info->delete_task != GNUNET_SCHEDULER_NO_TASK)
3095 GNUNET_SCHEDULER_cancel (source_info->delete_task);
3096 source_info->delete_task = GNUNET_SCHEDULER_NO_TASK;
3098 if (source_info->find_peers_responded != NULL)
3099 GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
3100 GNUNET_free (source_info);
3101 current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
3104 /** Non-local request and have too many outstanding forwards, discard! */
3105 if ((current_size >= MAX_OUTSTANDING_FORWARDS) && (msg_ctx->client == NULL))
3108 now = GNUNET_TIME_absolute_get ();
3110 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
3111 if (record != NULL) /* Already know this request! */
3117 memcmp (&msg_ctx->peer, &pos->source,
3118 sizeof (struct GNUNET_PeerIdentity)))
3119 break; /* Already have this peer in reply list! */
3122 if ((pos != NULL) && (pos->client == msg_ctx->client)) /* Seen this already */
3124 GNUNET_CONTAINER_heap_update_cost (forward_list.minHeap, pos->hnode,
3131 record = GNUNET_malloc (sizeof (struct DHTQueryRecord));
3132 GNUNET_assert (GNUNET_OK ==
3133 GNUNET_CONTAINER_multihashmap_put (forward_list.hashmap,
3134 &msg_ctx->key, record,
3135 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3136 memcpy (&record->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
3139 source_info = GNUNET_malloc (sizeof (struct DHTRouteSource));
3140 source_info->record = record;
3141 source_info->delete_task =
3142 GNUNET_SCHEDULER_add_delayed (DHT_FORWARD_TIMEOUT, &remove_forward_entry,
3144 source_info->find_peers_responded =
3145 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3146 source_info->source = msg_ctx->peer;
3147 GNUNET_CONTAINER_DLL_insert_after (record->head, record->tail, record->tail,
3149 if (msg_ctx->client != NULL) /* For local request, set timeout so high it effectively never gets pushed out */
3151 source_info->client = msg_ctx->client;
3152 now = GNUNET_TIME_absolute_get_forever ();
3154 source_info->hnode =
3155 GNUNET_CONTAINER_heap_insert (forward_list.minHeap, source_info,
3157 source_info->uid = msg_ctx->unique_id;
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3160 "`%s:%s': Created new forward source info for %s uid %llu\n",
3161 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
3162 msg_ctx->unique_id);
3169 * Main function that handles whether or not to route a message to other
3172 * @param msg the message to be routed
3173 * @param msg_ctx the context containing all pertinent information about the message
3176 route_message (const struct GNUNET_MessageHeader *msg,
3177 struct DHT_MessageContext *msg_ctx)
3180 struct PeerInfo *selected;
3182 #if DEBUG_DHT_ROUTING > 1
3183 struct PeerInfo *nearest;
3185 unsigned int target_forward_count;
3186 unsigned int forward_count;
3187 struct RecentRequest *recent_req;
3188 #if HAVE_UID_FOR_TESTING > 1
3189 GNUNET_HashCode unique_hash;
3191 char *stat_forward_count;
3192 char *temp_stat_str;
3194 #if DEBUG_DHT_ROUTING
3198 if (malicious_dropper == GNUNET_YES)
3200 #if DEBUG_DHT_ROUTING
3201 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3203 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3204 msg_ctx->hop_count, GNUNET_SYSERR,
3205 &my_identity, &msg_ctx->key, &msg_ctx->peer,
3209 if (msg_ctx->bloom != NULL)
3211 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
3212 msg_ctx->bloom = NULL;
3217 increment_stats (STAT_ROUTES);
3218 target_forward_count =
3219 get_forward_count (msg_ctx->hop_count, msg_ctx->replication);
3220 GNUNET_asprintf (&stat_forward_count, "# forward counts of %d",
3221 target_forward_count);
3222 increment_stats (stat_forward_count);
3223 GNUNET_free (stat_forward_count);
3224 if (msg_ctx->bloom == NULL)
3226 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3228 if ((stop_on_closest == GNUNET_YES) && (msg_ctx->closest == GNUNET_YES) &&
3229 (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
3230 target_forward_count = 0;
3233 * NOTICE: In Kademlia, a find peer request goes no further if the peer doesn't return
3234 * any closer peers (which is being checked for below). Since we are doing recursive
3235 * routing we have no choice but to stop forwarding in this case. This means that at
3236 * any given step the request may NOT be forwarded to alpha peers (because routes will
3237 * stop and the parallel route will not be aware of it). Of course, assuming that we
3238 * have fulfilled the Kademlia requirements for routing table fullness this will never
3239 * ever ever be a problem.
3241 * However, is this fair?
3243 * Since we use these requests to build our routing tables (and we build them in the
3244 * testing driver) we will ignore this restriction for FIND_PEER messages so that
3245 * routing tables still get constructed.
3247 if ((GNUNET_YES == strict_kademlia) && (msg_ctx->closest == GNUNET_YES) &&
3248 (msg_ctx->hop_count > 0) &&
3249 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER))
3250 target_forward_count = 0;
3252 #if HAVE_UID_FOR_TESTING > 1
3253 /* BUG HERE: recent uses unique_id! So if all unique-IDs are 0, we get
3254 easily into trouble!!! Also, this should not even be necessary... */
3255 hash_from_uid (msg_ctx->unique_id, &unique_hash);
3257 GNUNET_CONTAINER_multihashmap_contains (recent.hashmap, &unique_hash))
3260 GNUNET_CONTAINER_multihashmap_get (recent.hashmap, &unique_hash);
3261 GNUNET_assert (recent_req != NULL);
3262 if (0 != memcmp (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode)))
3263 increment_stats (STAT_DUPLICATE_UID);
3266 increment_stats (STAT_RECENT_SEEN);
3267 GNUNET_CONTAINER_bloomfilter_or2 (msg_ctx->bloom, recent_req->bloom,
3274 recent_req = GNUNET_malloc (sizeof (struct RecentRequest));
3275 recent_req->uid = msg_ctx->unique_id;
3276 memcpy (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
3277 recent_req->remove_task =
3278 GNUNET_SCHEDULER_add_delayed (DEFAULT_RECENT_REMOVAL, &remove_recent,
3280 recent_req->heap_node =
3281 GNUNET_CONTAINER_heap_insert (recent.minHeap, recent_req,
3282 GNUNET_TIME_absolute_get ().abs_value);
3284 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3285 #if HAVE_UID_FOR_TESTING > 1
3286 GNUNET_CONTAINER_multihashmap_put (recent.hashmap, &unique_hash, recent_req,
3287 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3291 if (GNUNET_CONTAINER_heap_get_size (recent.minHeap) > DHT_MAX_RECENT)
3293 recent_req = GNUNET_CONTAINER_heap_peek (recent.minHeap);
3294 GNUNET_assert (recent_req != NULL);
3295 GNUNET_SCHEDULER_cancel (recent_req->remove_task);
3296 recent_req->remove_task =
3297 GNUNET_SCHEDULER_add_now (&remove_recent, recent_req);
3301 for (i = 0; i < target_forward_count; i++)
3303 selected = select_peer (&msg_ctx->key, msg_ctx->bloom, msg_ctx->hop_count);
3304 if (selected == NULL)
3307 if (GNUNET_CRYPTO_hash_matching_bits
3308 (&selected->id.hashPubKey,
3310 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
3312 GNUNET_asprintf (&temp_stat_str,
3313 "# requests routed to close(r) peer hop %u",
3314 msg_ctx->hop_count);
3316 GNUNET_asprintf (&temp_stat_str,
3317 "# requests routed to less close peer hop %u",
3318 msg_ctx->hop_count);
3319 if (temp_stat_str != NULL)
3321 increment_stats (temp_stat_str);
3322 GNUNET_free (temp_stat_str);
3324 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
3325 &selected->id.hashPubKey);
3326 #if DEBUG_DHT_ROUTING > 1
3327 nearest = find_closest_peer (&msg_ctx->key);
3328 nearest_buf = GNUNET_strdup (GNUNET_i2s (&nearest->id));
3329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3330 "`%s:%s': Forwarding request key %s uid %llu to peer %s (closest %s, bits %d, distance %u)\n",
3331 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
3332 msg_ctx->unique_id, GNUNET_i2s (&selected->id), nearest_buf,
3333 GNUNET_CRYPTO_hash_matching_bits (&nearest->id.hashPubKey,
3335 distance (&nearest->id.hashPubKey, msg_ctx->key));
3336 GNUNET_free (nearest_buf);
3338 #if DEBUG_DHT_ROUTING
3339 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3341 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3342 msg_ctx->hop_count, GNUNET_NO,
3343 &my_identity, &msg_ctx->key, &msg_ctx->peer,
3347 forward_message (msg, selected, msg_ctx);
3350 if (msg_ctx->bloom != NULL)
3352 GNUNET_CONTAINER_bloomfilter_or2 (recent_req->bloom, msg_ctx->bloom,
3354 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
3355 msg_ctx->bloom = NULL;
3358 #if DEBUG_DHT_ROUTING
3359 if (forward_count == 0)
3360 ret = GNUNET_SYSERR;
3364 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3366 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3367 msg_ctx->hop_count, ret, &my_identity,
3368 &msg_ctx->key, &msg_ctx->peer, NULL);
3375 * Main function that handles whether or not to route a message to other
3378 * @param msg the message to be routed
3379 * @param msg_ctx the context containing all pertinent information about the message
3382 demultiplex_message (const struct GNUNET_MessageHeader *msg,
3383 struct DHT_MessageContext *msg_ctx)
3385 /* FIXME: Should we use closest excluding those we won't route to (the bloomfilter problem)? */
3386 msg_ctx->closest = am_closest_peer (&msg_ctx->key, msg_ctx->bloom);
3388 switch (ntohs (msg->type))
3390 case GNUNET_MESSAGE_TYPE_DHT_GET: /* Add to hashmap of requests seen, search for data (always) */
3391 cache_response (msg_ctx);
3392 handle_dht_get (msg, msg_ctx);
3394 case GNUNET_MESSAGE_TYPE_DHT_PUT: /* Check if closest, if so insert data. */
3395 increment_stats (STAT_PUTS);
3396 handle_dht_put (msg, msg_ctx);
3398 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: /* Check if closest and not started by us, check options, add to requests seen */
3399 increment_stats (STAT_FIND_PEER);
3400 if (((msg_ctx->hop_count > 0) &&
3402 memcmp (&msg_ctx->peer, &my_identity,
3403 sizeof (struct GNUNET_PeerIdentity)))) ||
3404 (msg_ctx->client != NULL))
3406 cache_response (msg_ctx);
3407 if ((msg_ctx->closest == GNUNET_YES) ||
3408 (msg_ctx->msg_options == GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE))
3409 handle_dht_find_peer (msg, msg_ctx);
3412 route_message (msg, msg_ctx);
3413 #if DEBUG_DHT_ROUTING
3414 if (msg_ctx->hop_count == 0) /* Locally initiated request */
3416 if ((debug_routes) && (dhtlog_handle != NULL))
3418 dhtlog_handle->insert_dhtkey (NULL, &msg_ctx->key);
3419 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER,
3420 msg_ctx->hop_count, GNUNET_NO,
3421 &my_identity, &msg_ctx->key);
3427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3428 "`%s': Message type (%d) not handled, forwarding anyway!\n",
3429 "DHT", ntohs (msg->type));
3430 route_message (msg, msg_ctx);
3436 * Iterator over hash map entries.
3438 * @param cls client to search for in source routes
3439 * @param key current key code (ignored)
3440 * @param value value in the hash map, a DHTQueryRecord
3441 * @return GNUNET_YES if we should continue to
3446 find_client_records (void *cls, const GNUNET_HashCode * key, void *value)
3448 struct ClientList *client = cls;
3449 struct DHTQueryRecord *record = value;
3450 struct DHTRouteSource *pos;
3455 if (pos->client == client)
3461 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, pos);
3462 GNUNET_CONTAINER_heap_remove_node (pos->hnode);
3463 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
3465 GNUNET_SCHEDULER_cancel (pos->delete_task);
3466 pos->delete_task = GNUNET_SCHEDULER_NO_TASK;
3468 if (pos->find_peers_responded != NULL)
3469 GNUNET_CONTAINER_bloomfilter_free (pos->find_peers_responded);
3472 if (record->head == NULL) /* No more entries in DLL */
3474 GNUNET_assert (GNUNET_YES ==
3475 GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
3476 &record->key, record));
3477 GNUNET_free (record);
3483 * Functions with this signature are called whenever a client
3484 * is disconnected on the network level.
3486 * @param cls closure (NULL for dht)
3487 * @param client identification of the client; NULL
3488 * for the last call when the server is destroyed
3491 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3493 struct ClientList *pos = client_list;
3494 struct ClientList *prev;
3495 struct ClientList *found;
3496 struct PendingMessage *reply;
3502 if (pos->client_handle == client)
3505 prev->next = pos->next;
3507 client_list = pos->next;
3517 if (found->transmit_handle != NULL)
3518 GNUNET_CONNECTION_notify_transmit_ready_cancel (found->transmit_handle);
3520 while (NULL != (reply = found->pending_head))
3522 GNUNET_CONTAINER_DLL_remove (found->pending_head, found->pending_tail,
3524 GNUNET_free (reply);
3526 GNUNET_CONTAINER_multihashmap_iterate (forward_list.hashmap,
3527 &find_client_records, found);
3528 GNUNET_free (found);
3533 * Find a client if it exists, add it otherwise.
3535 * @param client the server handle to the client
3537 * @return the client if found, a new client otherwise
3539 static struct ClientList *
3540 find_active_client (struct GNUNET_SERVER_Client *client)
3542 struct ClientList *pos = client_list;
3543 struct ClientList *ret;
3547 if (pos->client_handle == client)
3552 ret = GNUNET_malloc (sizeof (struct ClientList));
3553 ret->client_handle = client;
3554 ret->next = client_list;
3562 * Task to send a malicious put message across the network.
3564 * @param cls closure for this task
3565 * @param tc the context under which the task is running
3568 malicious_put_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3570 static struct GNUNET_DHT_PutMessage put_message;
3571 static struct DHT_MessageContext msg_ctx;
3572 static GNUNET_HashCode key;
3573 uint32_t random_key;
3575 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3577 put_message.header.size = htons (sizeof (struct GNUNET_DHT_PutMessage));
3578 put_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
3579 put_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
3580 put_message.expiration =
3581 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_forever ());
3582 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3584 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
3585 GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
3586 memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
3588 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3589 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
3590 msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3591 msg_ctx.msg_options = ntohl (0);
3592 msg_ctx.network_size = log_of_network_size_estimate;
3593 msg_ctx.peer = my_identity;
3594 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
3595 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3596 #if DEBUG_DHT_ROUTING
3597 if (dhtlog_handle != NULL)
3598 dhtlog_handle->insert_dhtkey (NULL, &key);
3600 increment_stats (STAT_PUT_START);
3601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3602 "%s:%s Sending malicious PUT message with hash %s\n", my_short_id,
3603 "DHT", GNUNET_h2s (&key));
3604 demultiplex_message (&put_message.header, &msg_ctx);
3605 GNUNET_SCHEDULER_add_delayed (malicious_put_frequency, &malicious_put_task,
3611 * Task to send a malicious put message across the network.
3613 * @param cls closure for this task
3614 * @param tc the context under which the task is running
3617 malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3619 static struct GNUNET_DHT_GetMessage get_message;
3620 struct DHT_MessageContext msg_ctx;
3621 static GNUNET_HashCode key;
3622 uint32_t random_key;
3624 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3627 get_message.header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
3628 get_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
3629 get_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
3630 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3632 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
3633 GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
3634 memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
3636 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3637 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
3638 msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3639 msg_ctx.msg_options = ntohl (0);
3640 msg_ctx.network_size = log_of_network_size_estimate;
3641 msg_ctx.peer = my_identity;
3642 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
3643 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3644 #if DEBUG_DHT_ROUTING
3645 if (dhtlog_handle != NULL)
3646 dhtlog_handle->insert_dhtkey (NULL, &key);
3648 increment_stats (STAT_GET_START);
3649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3650 "%s:%s Sending malicious GET message with hash %s\n", my_short_id,
3651 "DHT", GNUNET_h2s (&key));
3652 demultiplex_message (&get_message.header, &msg_ctx);
3653 GNUNET_SCHEDULER_add_delayed (malicious_get_frequency, &malicious_get_task,
3660 * Iterator over hash map entries.
3662 * @param cls closure
3663 * @param key current key code
3664 * @param value value in the hash map
3665 * @return GNUNET_YES if we should continue to
3670 add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
3672 struct GNUNET_CONTAINER_BloomFilter *bloom = cls;
3674 GNUNET_CONTAINER_bloomfilter_add (bloom, key);
3679 * Task to send a find peer message for our own peer identifier
3680 * so that we can find the closest peers in the network to ourselves
3681 * and attempt to connect to them.
3683 * @param cls closure for this task
3684 * @param tc the context under which the task is running
3687 send_find_peer_message (void *cls,
3688 const struct GNUNET_SCHEDULER_TaskContext *tc)
3690 struct GNUNET_DHT_FindPeerMessage *find_peer_msg;
3691 struct DHT_MessageContext msg_ctx;
3692 struct GNUNET_TIME_Relative next_send_time;
3693 struct GNUNET_CONTAINER_BloomFilter *temp_bloom;
3695 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3698 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! */
3700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3701 "Have %d newly found peers since last find peer message sent!\n",
3703 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
3704 &send_find_peer_message, NULL);
3705 newly_found_peers = 0;
3709 increment_stats (STAT_FIND_PEER_START);
3710 #if FIND_PEER_WITH_HELLO
3712 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage) +
3713 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *)
3715 find_peer_msg->header.size =
3716 htons (sizeof (struct GNUNET_DHT_FindPeerMessage) +
3717 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
3718 memcpy (&find_peer_msg[1], my_hello,
3719 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
3721 find_peer_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage));
3722 find_peer_msg->header.size =
3723 htons (sizeof (struct GNUNET_DHT_FindPeerMessage));
3725 find_peer_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
3727 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3728 GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, &add_known_to_bloom,
3730 GNUNET_assert (GNUNET_OK ==
3731 GNUNET_CONTAINER_bloomfilter_get_raw_data (temp_bloom,
3735 GNUNET_CONTAINER_bloomfilter_free (temp_bloom);
3736 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3737 memcpy (&msg_ctx.key, &my_identity.hashPubKey, sizeof (GNUNET_HashCode));
3739 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3740 (GNUNET_CRYPTO_QUALITY_STRONG, UINT64_MAX));
3741 msg_ctx.replication = DHT_DEFAULT_FIND_PEER_REPLICATION;
3742 msg_ctx.msg_options = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
3743 msg_ctx.network_size = log_of_network_size_estimate;
3744 msg_ctx.peer = my_identity;
3745 msg_ctx.importance = DHT_DEFAULT_FIND_PEER_IMPORTANCE;
3746 msg_ctx.timeout = DHT_DEFAULT_FIND_PEER_TIMEOUT;
3748 demultiplex_message (&find_peer_msg->header, &msg_ctx);
3749 GNUNET_free (find_peer_msg);
3750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3751 "`%s:%s': Sent `%s' request to some (?) peers\n", my_short_id,
3752 "DHT", "FIND PEER");
3753 if (newly_found_peers < bucket_size)
3755 next_send_time.rel_value =
3756 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
3757 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
3758 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
3762 next_send_time.rel_value =
3763 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
3764 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
3765 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value -
3766 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
3769 GNUNET_assert (next_send_time.rel_value != 0);
3770 find_peer_context.count = 0;
3771 newly_found_peers = 0;
3772 find_peer_context.start = GNUNET_TIME_absolute_get ();
3773 if (GNUNET_YES == do_find_peer)
3775 GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
3781 * Handler for any generic DHT messages, calls the appropriate handler
3782 * depending on message type, sends confirmation if responses aren't otherwise
3785 * @param cls closure for the service
3786 * @param client the client we received this message from
3787 * @param message the actual message received
3790 handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client,
3791 const struct GNUNET_MessageHeader *message)
3793 const struct GNUNET_DHT_RouteMessage *dht_msg =
3794 (const struct GNUNET_DHT_RouteMessage *) message;
3795 const struct GNUNET_MessageHeader *enc_msg;
3796 struct DHT_MessageContext msg_ctx;
3798 enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3801 "`%s:%s': Received `%s' request from client, message type %d, key %s, uid %llu\n",
3802 my_short_id, "DHT", "GENERIC", ntohs (message->type),
3803 GNUNET_h2s (&dht_msg->key), GNUNET_ntohll (dht_msg->unique_id));
3805 #if DEBUG_DHT_ROUTING
3806 if (dhtlog_handle != NULL)
3807 dhtlog_handle->insert_dhtkey (NULL, &dht_msg->key);
3810 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3811 msg_ctx.client = find_active_client (client);
3812 memcpy (&msg_ctx.key, &dht_msg->key, sizeof (GNUNET_HashCode));
3813 msg_ctx.unique_id = GNUNET_ntohll (dht_msg->unique_id);
3814 msg_ctx.replication = ntohl (dht_msg->desired_replication_level);
3815 msg_ctx.msg_options = ntohl (dht_msg->options);
3816 if (GNUNET_DHT_RO_RECORD_ROUTE ==
3817 (msg_ctx.msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
3819 msg_ctx.path_history = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
3820 memcpy (msg_ctx.path_history, &my_identity,
3821 sizeof (struct GNUNET_PeerIdentity));
3822 msg_ctx.path_history_len = 1;
3824 msg_ctx.network_size = log_of_network_size_estimate;
3825 msg_ctx.peer = my_identity;
3826 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 4; /* Make local routing a higher priority */
3827 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3829 if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
3830 increment_stats (STAT_GET_START);
3831 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
3832 increment_stats (STAT_PUT_START);
3833 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER)
3834 increment_stats (STAT_FIND_PEER_START);
3836 if (GNUNET_YES == malicious_dropper)
3838 if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
3840 #if DEBUG_DHT_ROUTING
3841 if ((debug_routes) && (dhtlog_handle != NULL))
3843 dhtlog_handle->insert_query (NULL, msg_ctx.unique_id, DHTLOG_GET,
3844 msg_ctx.hop_count, GNUNET_NO, &my_identity,
3849 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
3851 #if DEBUG_DHT_ROUTING
3852 if ((debug_routes) && (dhtlog_handle != NULL))
3854 dhtlog_handle->insert_query (NULL, msg_ctx.unique_id, DHTLOG_PUT,
3855 msg_ctx.hop_count, GNUNET_NO, &my_identity,
3860 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3861 GNUNET_free_non_null (msg_ctx.path_history);
3865 demultiplex_message (enc_msg, &msg_ctx);
3866 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3871 * Handler for any locally received DHT control messages,
3872 * sets malicious flags mostly for now.
3874 * @param cls closure for the service
3875 * @param client the client we received this message from
3876 * @param message the actual message received
3880 handle_dht_control_message (void *cls, struct GNUNET_SERVER_Client *client,
3881 const struct GNUNET_MessageHeader *message)
3883 const struct GNUNET_DHT_ControlMessage *dht_control_msg =
3884 (const struct GNUNET_DHT_ControlMessage *) message;
3887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3888 "`%s:%s': Received `%s' request from client, command %d\n",
3889 my_short_id, "DHT", "CONTROL", ntohs (dht_control_msg->command));
3892 switch (ntohs (dht_control_msg->command))
3894 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3896 "Sending self seeking find peer request!\n");
3897 GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
3900 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
3901 if (ntohs (dht_control_msg->variable) > 0)
3902 malicious_get_frequency.rel_value = ntohs (dht_control_msg->variable);
3903 if (malicious_get_frequency.rel_value == 0)
3904 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
3905 if (malicious_getter != GNUNET_YES)
3906 GNUNET_SCHEDULER_add_now (&malicious_get_task, NULL);
3907 malicious_getter = GNUNET_YES;
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3909 "%s:%s Initiating malicious GET behavior, frequency %llu\n",
3910 my_short_id, "DHT", malicious_get_frequency.rel_value);
3912 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
3913 if (ntohs (dht_control_msg->variable) > 0)
3914 malicious_put_frequency.rel_value = ntohs (dht_control_msg->variable);
3915 if (malicious_put_frequency.rel_value == 0)
3916 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
3917 if (malicious_putter != GNUNET_YES)
3918 GNUNET_SCHEDULER_add_now (&malicious_put_task, NULL);
3919 malicious_putter = GNUNET_YES;
3920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3921 "%s:%s Initiating malicious PUT behavior, frequency %d\n",
3922 my_short_id, "DHT", malicious_put_frequency);
3924 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
3925 #if DEBUG_DHT_ROUTING
3926 if ((malicious_dropper != GNUNET_YES) && (dhtlog_handle != NULL))
3927 dhtlog_handle->set_malicious (&my_identity);
3929 malicious_dropper = GNUNET_YES;
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3931 "%s:%s Initiating malicious DROP behavior\n", my_short_id,
3936 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3937 "%s:%s Unknown control command type `%d'!\n", my_short_id,
3938 "DHT", ntohs (dht_control_msg->command));
3942 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3946 * Handler for any generic DHT stop messages, calls the appropriate handler
3947 * depending on message type (if processed locally)
3949 * @param cls closure for the service
3950 * @param client the client we received this message from
3951 * @param message the actual message received
3955 handle_dht_local_route_stop (void *cls, struct GNUNET_SERVER_Client *client,
3956 const struct GNUNET_MessageHeader *message)
3959 const struct GNUNET_DHT_StopMessage *dht_stop_msg =
3960 (const struct GNUNET_DHT_StopMessage *) message;
3961 struct DHTQueryRecord *record;
3962 struct DHTRouteSource *pos;
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3966 "`%s:%s': Received `%s' request from client, uid %llu\n",
3967 my_short_id, "DHT", "GENERIC STOP",
3968 GNUNET_ntohll (dht_stop_msg->unique_id));
3971 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap,
3972 &dht_stop_msg->key);
3979 /* If the client is non-null (local request) and the client matches the requesting client, remove the entry. */
3980 if ((pos->client != NULL) && (pos->client->client_handle == client))
3982 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
3983 GNUNET_SCHEDULER_cancel (pos->delete_task);
3985 GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);
3991 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3996 * Core handler for p2p route requests.
3998 * @param cls closure
3999 * @param message message
4000 * @param peer peer identity this notification is about
4001 * @param atsi performance data
4002 * @return GNUNET_OK to keep the connection open,
4003 * GNUNET_SYSERR to close it (signal serious error)
4006 handle_dht_p2p_route_request (void *cls, const struct GNUNET_PeerIdentity *peer,
4007 const struct GNUNET_MessageHeader *message,
4008 const struct GNUNET_TRANSPORT_ATS_Information
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4013 "`%s:%s': Received P2P request from peer %s\n", my_short_id,
4014 "DHT", GNUNET_i2s (peer));
4016 struct GNUNET_DHT_P2PRouteMessage *incoming =
4017 (struct GNUNET_DHT_P2PRouteMessage *) message;
4018 struct GNUNET_MessageHeader *enc_msg =
4019 (struct GNUNET_MessageHeader *) &incoming[1];
4020 struct DHT_MessageContext *msg_ctx;
4024 if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4026 GNUNET_break_op (0);
4030 if (malicious_dropper == GNUNET_YES)
4032 #if DEBUG_DHT_ROUTING
4033 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4035 /** Log routes that die due to high load! */
4036 dhtlog_handle->insert_route (NULL,
4037 #if HAVE_UID_FOR_TESTING
4038 GNUNET_ntohll (incoming->unique_id),
4042 DHTLOG_ROUTE, ntohl (incoming->hop_count),
4043 GNUNET_SYSERR, &my_identity, &incoming->key,
4050 if (get_max_send_delay ().rel_value > MAX_REQUEST_TIME.rel_value)
4052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4053 "Sending of previous replies took too long, backing off!\n");
4054 increment_stats ("# route requests dropped due to high load");
4055 decrease_max_send_delay (get_max_send_delay ());
4056 #if DEBUG_DHT_ROUTING
4057 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4059 /** Log routes that die due to high load! */
4060 dhtlog_handle->insert_route (NULL,
4061 #if HAVE_UID_FOR_TESTING
4062 GNUNET_ntohll (incoming->unique_id),
4066 DHTLOG_ROUTE, ntohl (incoming->hop_count),
4067 GNUNET_SYSERR, &my_identity, &incoming->key,
4073 msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
4075 GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
4077 GNUNET_assert (msg_ctx->bloom != NULL);
4078 msg_ctx->hop_count = ntohl (incoming->hop_count);
4079 memcpy (&msg_ctx->key, &incoming->key, sizeof (GNUNET_HashCode));
4080 msg_ctx->replication = ntohl (incoming->desired_replication_level);
4081 #if HAVE_UID_FOR_TESTING
4082 msg_ctx->unique_id = GNUNET_ntohll (incoming->unique_id);
4084 msg_ctx->msg_options = ntohl (incoming->options);
4085 if (GNUNET_DHT_RO_RECORD_ROUTE ==
4086 (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
4089 ntohl (incoming->outgoing_path_length) *
4090 sizeof (struct GNUNET_PeerIdentity);
4091 if (ntohs (message->size) !=
4092 (sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (enc_msg->size) +
4095 GNUNET_break_op (0);
4096 GNUNET_free (msg_ctx);
4099 route_path = (char *) &incoming[1];
4100 route_path = route_path + ntohs (enc_msg->size);
4101 msg_ctx->path_history =
4102 GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) + path_size);
4103 memcpy (msg_ctx->path_history, route_path, path_size);
4104 memcpy (&msg_ctx->path_history[path_size], &my_identity,
4105 sizeof (struct GNUNET_PeerIdentity));
4106 msg_ctx->path_history_len = ntohl (incoming->outgoing_path_length) + 1;
4108 msg_ctx->network_size = ntohl (incoming->network_size);
4109 msg_ctx->peer = *peer;
4110 msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE;
4111 msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
4112 demultiplex_message (enc_msg, msg_ctx);
4113 if (msg_ctx->bloom != NULL)
4115 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
4116 msg_ctx->bloom = NULL;
4118 GNUNET_free (msg_ctx);
4124 * Core handler for p2p route results.
4126 * @param cls closure
4127 * @param message message
4128 * @param peer peer identity this notification is about
4129 * @param atsi performance data
4133 handle_dht_p2p_route_result (void *cls, const struct GNUNET_PeerIdentity *peer,
4134 const struct GNUNET_MessageHeader *message,
4135 const struct GNUNET_TRANSPORT_ATS_Information
4139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4140 "`%s:%s': Received request from peer %s\n", my_short_id, "DHT",
4143 const struct GNUNET_DHT_P2PRouteResultMessage *incoming =
4144 (const struct GNUNET_DHT_P2PRouteResultMessage *) message;
4145 struct GNUNET_MessageHeader *enc_msg =
4146 (struct GNUNET_MessageHeader *) &incoming[1];
4147 struct DHT_MessageContext msg_ctx;
4153 if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4155 GNUNET_break_op (0);
4159 if (malicious_dropper == GNUNET_YES)
4161 #if DEBUG_DHT_ROUTING
4162 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4164 /** Log routes that die due to high load! */
4165 dhtlog_handle->insert_route (NULL,
4166 #if HAVE_UID_FOR_TESTING
4167 GNUNET_ntohll (incoming->unique_id),
4171 DHTLOG_ROUTE, ntohl (incoming->hop_count),
4172 GNUNET_SYSERR, &my_identity, &incoming->key,
4179 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
4180 memcpy (&msg_ctx.key, &incoming->key, sizeof (GNUNET_HashCode));
4181 #if HAVE_UID_FOR_TESTING
4182 msg_ctx.unique_id = GNUNET_ntohll (incoming->unique_id);
4184 msg_ctx.msg_options = ntohl (incoming->options);
4185 msg_ctx.hop_count = ntohl (incoming->hop_count);
4186 msg_ctx.peer = *peer;
4187 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */
4188 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
4189 if ((GNUNET_DHT_RO_RECORD_ROUTE ==
4190 (msg_ctx.msg_options & GNUNET_DHT_RO_RECORD_ROUTE)) &&
4191 (ntohl (incoming->outgoing_path_length) > 0))
4193 if (ntohs (message->size) -
4194 sizeof (struct GNUNET_DHT_P2PRouteResultMessage) -
4195 ntohs (enc_msg->size) !=
4196 ntohl (incoming->outgoing_path_length) *
4197 sizeof (struct GNUNET_PeerIdentity))
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 "Return message indicated a path was included, but sizes are wrong: Total size %d, enc size %d, left %d, expected %d\n",
4202 ntohs (message->size), ntohs (enc_msg->size),
4203 ntohs (message->size) -
4204 sizeof (struct GNUNET_DHT_P2PRouteResultMessage) -
4205 ntohs (enc_msg->size),
4206 ntohl (incoming->outgoing_path_length) *
4207 sizeof (struct GNUNET_PeerIdentity));
4209 GNUNET_break_op (0);
4212 msg_ctx.path_history = (char *) &incoming[1];
4213 msg_ctx.path_history += ntohs (enc_msg->size);
4214 msg_ctx.path_history_len = ntohl (incoming->outgoing_path_length);
4216 for (i = 0; i < msg_ctx.path_history_len; i++)
4219 &msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
4220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4221 "(handle_p2p_route_result) Key %s Found peer %d:%s\n",
4222 GNUNET_h2s (&msg_ctx.key), i,
4223 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
4227 route_result_message (enc_msg, &msg_ctx);
4233 * Receive the HELLO from transport service,
4234 * free current and replace if necessary.
4237 * @param message HELLO message of peer
4240 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
4243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4244 "Received our `%s' from transport service\n", "HELLO");
4247 GNUNET_assert (message != NULL);
4248 GNUNET_free_non_null (my_hello);
4249 my_hello = GNUNET_malloc (ntohs (message->size));
4250 memcpy (my_hello, message, ntohs (message->size));
4255 * Task run during shutdown.
4261 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4264 struct PeerInfo *pos;
4268 GNUNET_TRANSPORT_get_hello_cancel (ghh);
4271 if (transport_handle != NULL)
4273 GNUNET_free_non_null (my_hello);
4274 GNUNET_TRANSPORT_disconnect (transport_handle);
4275 transport_handle = NULL;
4279 GNUNET_NSE_disconnect (nse);
4282 if (coreAPI != NULL)
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Disconnecting core!\n",
4286 my_short_id, "DHT");
4288 GNUNET_CORE_disconnect (coreAPI);
4291 for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS; bucket_count++)
4293 while (k_buckets[bucket_count].head != NULL)
4295 pos = k_buckets[bucket_count].head;
4297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4298 "%s:%s Removing peer %s from bucket %d!\n", my_short_id,
4299 "DHT", GNUNET_i2s (&pos->id), bucket_count);
4301 delete_peer (pos, bucket_count);
4304 if (datacache != NULL)
4307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Destroying datacache!\n",
4308 my_short_id, "DHT");
4310 GNUNET_DATACACHE_destroy (datacache);
4315 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
4318 if (dhtlog_handle != NULL)
4320 GNUNET_DHTLOG_disconnect (dhtlog_handle);
4321 dhtlog_handle = NULL;
4323 if (block_context != NULL)
4325 GNUNET_BLOCK_context_destroy (block_context);
4326 block_context = NULL;
4328 GNUNET_free_non_null (my_short_id);
4334 * To be called on core init/fail.
4336 * @param cls service closure
4337 * @param server handle to the server for this service
4338 * @param identity the public identity of this peer
4339 * @param publicKey the public key of this peer
4342 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4343 const struct GNUNET_PeerIdentity *identity,
4344 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
4350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Connection to core FAILED!\n",
4351 "dht", GNUNET_i2s (identity));
4353 GNUNET_SCHEDULER_cancel (cleanup_task);
4354 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
4358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4359 "%s: Core connection initialized, I am peer: %s\n", "dht",
4360 GNUNET_i2s (identity));
4363 /* Copy our identity so we can use it */
4364 memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity));
4365 if (my_short_id != NULL)
4366 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4367 "%s Receive CORE INIT message but have already been initialized! Did CORE fail?\n",
4369 my_short_id = GNUNET_strdup (GNUNET_i2s (&my_identity));
4370 if (dhtlog_handle != NULL)
4371 dhtlog_handle->insert_node (NULL, &my_identity);
4375 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
4376 {&handle_dht_local_route_request, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE,
4378 {&handle_dht_local_route_stop, NULL,
4379 GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP, 0},
4380 {&handle_dht_control_message, NULL, GNUNET_MESSAGE_TYPE_DHT_CONTROL, 0},
4385 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4386 {&handle_dht_p2p_route_request, GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE, 0},
4387 {&handle_dht_p2p_route_result, GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT, 0},
4393 * Method called whenever a peer connects.
4395 * @param cls closure
4396 * @param peer peer identity this notification is about
4397 * @param atsi performance data
4400 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4401 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4403 struct PeerInfo *ret;
4404 struct DHTPutEntry *put_entry;
4407 /* Check for connect to self message */
4408 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
4412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4413 "%s:%s Receives core connect message for peer %s distance %d!\n",
4414 my_short_id, "dht", GNUNET_i2s (peer), distance);
4418 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
4422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4423 "%s:%s Received %s message for peer %s, but already have peer in RT!",
4424 my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s (peer));
4430 if ((datacache != NULL) && (GNUNET_YES == put_peer_identities))
4433 GNUNET_malloc (sizeof (struct DHTPutEntry) +
4434 sizeof (struct GNUNET_PeerIdentity));
4435 put_entry->path_length = 0;
4436 put_entry->data_size = sizeof (struct GNUNET_PeerIdentity);
4437 memcpy (&put_entry[1], peer, sizeof (struct GNUNET_PeerIdentity));
4438 GNUNET_DATACACHE_put (datacache, &peer->hashPubKey,
4439 sizeof (struct DHTPutEntry) +
4440 sizeof (struct GNUNET_PeerIdentity),
4441 (char *) put_entry, GNUNET_BLOCK_TYPE_DHT_HELLO,
4442 GNUNET_TIME_absolute_get_forever ());
4443 GNUNET_free (put_entry);
4445 else if (datacache == NULL)
4446 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4447 "DHT has no connection to datacache!\n");
4449 peer_bucket = find_current_bucket (&peer->hashPubKey);
4450 GNUNET_assert (peer_bucket >= lowest_bucket);
4451 GNUNET_assert (peer_bucket < MAX_BUCKETS);
4452 ret = GNUNET_malloc (sizeof (struct PeerInfo));
4454 ret->latency = latency;
4455 ret->distance = distance;
4458 GNUNET_CONTAINER_DLL_insert_after (k_buckets[peer_bucket].head,
4459 k_buckets[peer_bucket].tail,
4460 k_buckets[peer_bucket].tail, ret);
4461 k_buckets[peer_bucket].peers_size++;
4462 #if DO_UPDATE_PREFERENCE
4463 if ((GNUNET_CRYPTO_hash_matching_bits
4464 (&my_identity.hashPubKey, &peer->hashPubKey) > 0) &&
4465 (k_buckets[peer_bucket].peers_size <= bucket_size))
4466 ret->preference_task =
4467 GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
4469 if ((k_buckets[lowest_bucket].peers_size) >= bucket_size)
4470 enable_next_bucket ();
4471 newly_found_peers++;
4472 GNUNET_CONTAINER_multihashmap_put (all_known_peers, &peer->hashPubKey, ret,
4473 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4474 increment_stats (STAT_PEERS_KNOWN);
4477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4478 "%s:%s Adding peer to routing list: %s\n", my_short_id, "DHT",
4479 ret == NULL ? "NOT ADDED" : "PEER ADDED");
4485 * Method called whenever a peer disconnects.
4487 * @param cls closure
4488 * @param peer peer identity this notification is about
4491 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4493 struct PeerInfo *to_remove;
4496 /* Check for disconnect from self message */
4497 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
4500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4501 "%s:%s: Received peer disconnect message for peer `%s' from %s\n",
4502 my_short_id, "DHT", GNUNET_i2s (peer), "CORE");
4506 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
4511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4512 "%s:%s: do not have peer `%s' in RT, can't disconnect!\n",
4513 my_short_id, "DHT", GNUNET_i2s (peer));
4517 increment_stats (STAT_DISCONNECTS);
4518 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
4519 (all_known_peers, &peer->hashPubKey));
4521 GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
4522 GNUNET_assert (to_remove != NULL);
4523 if (NULL != to_remove->info_ctx)
4525 GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
4526 to_remove->info_ctx = NULL;
4529 memcmp (peer, &to_remove->id,
4530 sizeof (struct GNUNET_PeerIdentity)));
4531 current_bucket = find_current_bucket (&to_remove->id.hashPubKey);
4532 delete_peer (to_remove, current_bucket);
4537 * Process dht requests.
4539 * @param cls closure
4540 * @param server the initialized server
4541 * @param c configuration to use
4544 run (void *cls, struct GNUNET_SERVER_Handle *server,
4545 const struct GNUNET_CONFIGURATION_Handle *c)
4547 struct GNUNET_TIME_Relative next_send_time;
4548 unsigned long long temp_config_num;
4551 datacache = GNUNET_DATACACHE_create (cfg, "dhtcache");
4552 GNUNET_SERVER_add_handlers (server, plugin_handlers);
4553 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
4554 nse = GNUNET_NSE_connect (cfg, &update_network_size_estimate, NULL);
4555 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
4556 DEFAULT_CORE_QUEUE_SIZE, /* queue size */
4557 NULL, /* Closure passed to DHT functions */
4558 &core_init, /* Call core_init once connected */
4559 &handle_core_connect, /* Handle connects */
4560 &handle_core_disconnect, /* remove peers on disconnects */
4561 NULL, /* Do we care about "status" updates? */
4562 NULL, /* Don't want notified about all incoming messages */
4563 GNUNET_NO, /* For header only inbound notification */
4564 NULL, /* Don't want notified about all outbound messages */
4565 GNUNET_NO, /* For header only outbound notification */
4566 core_handlers); /* Register these handlers */
4568 if (coreAPI == NULL)
4571 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
4572 if (transport_handle != NULL)
4573 ghh = GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
4575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4576 "Failed to connect to transport service!\n");
4577 block_context = GNUNET_BLOCK_context_create (cfg);
4578 lowest_bucket = MAX_BUCKETS - 1;
4579 forward_list.hashmap =
4580 GNUNET_CONTAINER_multihashmap_create (MAX_OUTSTANDING_FORWARDS / 10);
4581 forward_list.minHeap =
4582 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4583 all_known_peers = GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
4584 GNUNET_assert (all_known_peers != NULL);
4586 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
4589 debug_routes = GNUNET_YES;
4593 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "strict_kademlia"))
4595 strict_kademlia = GNUNET_YES;
4599 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_on_closest"))
4601 stop_on_closest = GNUNET_YES;
4605 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_found"))
4607 stop_on_found = GNUNET_YES;
4611 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_getter"))
4613 malicious_getter = GNUNET_YES;
4615 GNUNET_CONFIGURATION_get_value_time (cfg, "DHT",
4616 "MALICIOUS_GET_FREQUENCY",
4617 &malicious_get_frequency))
4618 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
4622 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_putter"))
4624 malicious_putter = GNUNET_YES;
4626 GNUNET_CONFIGURATION_get_value_time (cfg, "DHT",
4627 "MALICIOUS_PUT_FREQUENCY",
4628 &malicious_put_frequency))
4629 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
4633 GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "bucket_size",
4636 bucket_size = (unsigned int) temp_config_num;
4640 GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "kad_alpha",
4641 &kademlia_replication))
4643 kademlia_replication = DEFAULT_KADEMLIA_REPLICATION;
4647 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_dropper"))
4649 malicious_dropper = GNUNET_YES;
4653 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "do_find_peer"))
4655 do_find_peer = GNUNET_NO;
4658 do_find_peer = GNUNET_YES;
4661 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "use_real_distance"))
4662 use_real_distance = GNUNET_YES;
4665 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
4666 "mysql_logging_extended"))
4668 debug_routes = GNUNET_YES;
4669 debug_routes_extended = GNUNET_YES;
4672 #if DEBUG_DHT_ROUTING
4673 if (GNUNET_YES == debug_routes)
4675 dhtlog_handle = GNUNET_DHTLOG_connect (cfg);
4676 if (dhtlog_handle == NULL)
4678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4679 "Could not connect to mysql logging server, logging will not happen!");
4685 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "paper_forwarding"))
4686 paper_forwarding = GNUNET_YES;
4689 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "put_peer_identities"))
4690 put_peer_identities = GNUNET_YES;
4692 stats = GNUNET_STATISTICS_create ("dht", cfg);
4696 GNUNET_STATISTICS_set (stats, STAT_ROUTES, 0, GNUNET_NO);
4697 GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS, 0, GNUNET_NO);
4698 GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS_CLOSEST, 0, GNUNET_NO);
4699 GNUNET_STATISTICS_set (stats, STAT_RESULTS, 0, GNUNET_NO);
4700 GNUNET_STATISTICS_set (stats, STAT_RESULTS_TO_CLIENT, 0, GNUNET_NO);
4701 GNUNET_STATISTICS_set (stats, STAT_RESULT_FORWARDS, 0, GNUNET_NO);
4702 GNUNET_STATISTICS_set (stats, STAT_GETS, 0, GNUNET_NO);
4703 GNUNET_STATISTICS_set (stats, STAT_PUTS, 0, GNUNET_NO);
4704 GNUNET_STATISTICS_set (stats, STAT_PUTS_INSERTED, 0, GNUNET_NO);
4705 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER, 0, GNUNET_NO);
4706 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_START, 0, GNUNET_NO);
4707 GNUNET_STATISTICS_set (stats, STAT_GET_START, 0, GNUNET_NO);
4708 GNUNET_STATISTICS_set (stats, STAT_PUT_START, 0, GNUNET_NO);
4709 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_REPLY, 0, GNUNET_NO);
4710 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_ANSWER, 0, GNUNET_NO);
4711 GNUNET_STATISTICS_set (stats, STAT_BLOOM_FIND_PEER, 0, GNUNET_NO);
4712 GNUNET_STATISTICS_set (stats, STAT_GET_REPLY, 0, GNUNET_NO);
4713 GNUNET_STATISTICS_set (stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO);
4714 GNUNET_STATISTICS_set (stats, STAT_HELLOS_PROVIDED, 0, GNUNET_NO);
4715 GNUNET_STATISTICS_set (stats, STAT_DISCONNECTS, 0, GNUNET_NO);
4717 if (GNUNET_YES == do_find_peer)
4719 next_send_time.rel_value =
4720 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
4721 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
4722 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
4724 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
4725 find_peer_context.start = GNUNET_TIME_absolute_get ();
4726 GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
4727 &find_peer_context);
4730 /* Scheduled the task to clean up when shutdown is called */
4732 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
4733 &shutdown_task, NULL);
4738 * The main function for the dht service.
4740 * @param argc number of arguments from the command line
4741 * @param argv command line arguments
4742 * @return 0 ok, 1 on error
4745 main (int argc, char *const *argv)
4749 #if HAVE_UID_FOR_TESTING > 1
4750 recent.hashmap = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT / 2);
4753 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4754 recent_find_peer_requests =
4755 GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
4758 GNUNET_SERVICE_run (argc, argv, "dht", GNUNET_SERVICE_OPTION_NONE, &run,
4760 #if HAVE_UID_FOR_TESTING > 1
4761 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent.hashmap));
4762 GNUNET_CONTAINER_multihashmap_destroy (recent.hashmap);
4763 recent.hashmap = NULL;
4765 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent.minHeap));
4766 GNUNET_CONTAINER_heap_destroy (recent.minHeap);
4767 recent.minHeap = NULL;
4768 GNUNET_CONTAINER_multihashmap_destroy (recent_find_peer_requests);
4769 recent_find_peer_requests = NULL;
4773 /* end of gnunet-service-dht.c */