fixing 1770, towards fixing 1769
[oweals/gnunet.git] / src / dht / gnunet-service-dht.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file dht/gnunet-service-dht.c
23  * @brief GNUnet DHT service
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  *
27  * TODO:
28  * - decide which 'benchmark'/test functions to keep (malicious code, kademlia, etc.)
29  * - decide on 'stop_on_closest', 'stop_on_found', 'do_find_peer', 'paper_forwarding'
30  * - use OPTION_MULTIPLE instead of linked list ofr the forward_list.hashmap
31  * - use different 'struct DHT_MessageContext' for the different types of
32  *   messages (currently rather confusing, especially with things like
33  *   peer bloom filters occuring when processing replies).
34  * - why do we have request UIDs again?
35  */
36
37 #include "platform.h"
38 #include "gnunet_block_lib.h"
39 #include "gnunet_client_lib.h"
40 #include "gnunet_getopt_lib.h"
41 #include "gnunet_os_lib.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_service_lib.h"
44 #include "gnunet_nse_service.h"
45 #include "gnunet_core_service.h"
46 #include "gnunet_signal_lib.h"
47 #include "gnunet_util_lib.h"
48 #include "gnunet_datacache_lib.h"
49 #include "gnunet_transport_service.h"
50 #include "gnunet_hello_lib.h"
51 #include "gnunet_dht_service.h"
52 #include "gnunet_statistics_service.h"
53 #include "dhtlog.h"
54 #include "dht.h"
55 #include <fenv.h>
56
57
58 /**
59  * How many buckets will we allow total.
60  */
61 #define MAX_BUCKETS sizeof (GNUNET_HashCode) * 8
62
63 /**
64  * Should the DHT issue FIND_PEER requests to get better routing tables?
65  */
66 #define DEFAULT_DO_FIND_PEER GNUNET_YES
67
68 /**
69  * Defines whether find peer requests send their HELLO's outgoing,
70  * or expect replies to contain hellos.
71  */
72 #define FIND_PEER_WITH_HELLO GNUNET_YES
73
74 /**
75  * What is the maximum number of peers in a given bucket.
76  */
77 #define DEFAULT_BUCKET_SIZE 4
78
79 #define DEFAULT_CORE_QUEUE_SIZE 32
80
81 /**
82  * Minimum number of peers we need for "good" routing,
83  * any less than this and we will allow messages to
84  * travel much further through the network!
85  */
86 #define MINIMUM_PEER_THRESHOLD 20
87
88 /**
89  * Number of requests we track at most (for routing replies).
90  */
91 #define DHT_MAX_RECENT (1024 * 16)
92
93 /**
94  * How long do we wait at most when queueing messages with core
95  * that we are sending on behalf of other peers.
96  */
97 #define DHT_DEFAULT_P2P_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
98
99 /**
100  * Default importance for handling messages on behalf of other peers.
101  */
102 #define DHT_DEFAULT_P2P_IMPORTANCE 0
103
104 /**
105  * How long to keep recent requests around by default.
106  */
107 #define DEFAULT_RECENT_REMOVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
108
109 /**
110  * Default time to wait to send find peer messages sent by the dht service.
111  */
112 #define DHT_DEFAULT_FIND_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
113
114 /**
115  * Default importance for find peer messages sent by the dht service.
116  */
117 #define DHT_DEFAULT_FIND_PEER_IMPORTANCE 8
118
119 /**
120  * Default replication parameter for find peer messages sent by the dht service.
121  */
122 #define DHT_DEFAULT_FIND_PEER_REPLICATION 4
123
124 /**
125  * How long at least to wait before sending another find peer request.
126  */
127 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
128
129 /**
130  * How long at most to wait before sending another find peer request.
131  */
132 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
133
134 /**
135  * How often to update our preference levels for peers in our routing tables.
136  */
137 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
138
139 /**
140  * How long at most on average will we allow a reply forward to take
141  * (before we quit sending out new requests)
142  */
143 #define MAX_REQUEST_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
144
145 /**
146  * How many initial requests to send out (in true Kademlia fashion)
147  */
148 #define DEFAULT_KADEMLIA_REPLICATION 3
149
150 /**
151  * Default frequency for sending malicious get messages
152  */
153 #define DEFAULT_MALICIOUS_GET_FREQUENCY GNUNET_TIME_UNIT_SECONDS
154
155 /**
156  * Default frequency for sending malicious put messages
157  */
158 #define DEFAULT_MALICIOUS_PUT_FREQUENCY GNUNET_TIME_UNIT_SECONDS
159
160 /**
161  * How many time differences between requesting a core send and
162  * the actual callback to remember.
163  */
164 #define MAX_REPLY_TIMES 8
165
166
167 /**
168  * Linked list of messages to send to clients.
169  */
170 struct P2PPendingMessage
171 {
172   /**
173    * Pointer to next item in the list
174    */
175   struct P2PPendingMessage *next;
176
177   /**
178    * Pointer to previous item in the list
179    */
180   struct P2PPendingMessage *prev;
181
182   /**
183    * Message importance level.
184    */
185   unsigned int importance;
186
187   /**
188    * Time when this request was scheduled to be sent.
189    */
190   struct GNUNET_TIME_Absolute scheduled;
191
192   /**
193    * How long to wait before sending message.
194    */
195   struct GNUNET_TIME_Relative timeout;
196
197   /**
198    * Actual message to be sent; // avoid allocation
199    */
200   const struct GNUNET_MessageHeader *msg;       // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
201
202 };
203
204
205 /**
206  * Per-peer information.
207  */
208 struct PeerInfo
209 {
210   /**
211    * Next peer entry (DLL)
212    */
213   struct PeerInfo *next;
214
215   /**
216    *  Prev peer entry (DLL)
217    */
218   struct PeerInfo *prev;
219
220   /**
221    * Count of outstanding messages for peer.
222    */
223   unsigned int pending_count;
224
225   /**
226    * Head of pending messages to be sent to this peer.
227    */
228   struct P2PPendingMessage *head;
229
230   /**
231    * Tail of pending messages to be sent to this peer.
232    */
233   struct P2PPendingMessage *tail;
234
235   /**
236    * Core handle for sending messages to this peer.
237    */
238   struct GNUNET_CORE_TransmitHandle *th;
239
240   /**
241    * Task for scheduling message sends.
242    */
243   GNUNET_SCHEDULER_TaskIdentifier send_task;
244
245   /**
246    * Task for scheduling preference updates
247    */
248   GNUNET_SCHEDULER_TaskIdentifier preference_task;
249
250   /**
251    * Preference update context
252    */
253   struct GNUNET_CORE_InformationRequestContext *info_ctx;
254
255   /**
256    * What is the identity of the peer?
257    */
258   struct GNUNET_PeerIdentity id;
259
260 #if 0
261   /**
262    * What is the average latency for replies received?
263    */
264   struct GNUNET_TIME_Relative latency;
265
266   /**
267    * Transport level distance to peer.
268    */
269   unsigned int distance;
270 #endif
271
272   /**
273    * Task for scheduling periodic ping messages for this peer.
274    */
275   GNUNET_SCHEDULER_TaskIdentifier ping_task;
276 };
277
278
279 /**
280  * Peers are grouped into buckets.
281  */
282 struct PeerBucket
283 {
284   /**
285    * Head of DLL
286    */
287   struct PeerInfo *head;
288
289   /**
290    * Tail of DLL
291    */
292   struct PeerInfo *tail;
293
294   /**
295    * Number of peers in the bucket.
296    */
297   unsigned int peers_size;
298 };
299
300
301 /**
302  * Linked list of messages to send to clients.
303  */
304 struct PendingMessage
305 {
306   /**
307    * Pointer to next item in the list
308    */
309   struct PendingMessage *next;
310
311   /**
312    * Pointer to previous item in the list
313    */
314   struct PendingMessage *prev;
315
316   /**
317    * Actual message to be sent; // avoid allocation
318    */
319   const struct GNUNET_MessageHeader *msg;       // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
320
321 };
322
323
324 /**
325  * Struct containing information about a client,
326  * handle to connect to it, and any pending messages
327  * that need to be sent to it.
328  */
329 struct ClientList
330 {
331   /**
332    * Linked list of active clients
333    */
334   struct ClientList *next;
335
336   /**
337    * The handle to this client
338    */
339   struct GNUNET_SERVER_Client *client_handle;
340
341   /**
342    * Handle to the current transmission request, NULL
343    * if none pending.
344    */
345   struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
346
347   /**
348    * Linked list of pending messages for this client
349    */
350   struct PendingMessage *pending_head;
351
352   /**
353    * Tail of linked list of pending messages for this client
354    */
355   struct PendingMessage *pending_tail;
356 };
357
358
359 /**
360  * Context containing information about a DHT message received.
361  */
362 struct DHT_MessageContext
363 {
364   /**
365    * The client this request was received from.
366    * (NULL if received from another peer)
367    */
368   struct ClientList *client;
369
370   /**
371    * The peer this request was received from.
372    */
373   struct GNUNET_PeerIdentity peer;
374
375   /**
376    * Bloomfilter for this routing request.
377    */
378   struct GNUNET_CONTAINER_BloomFilter *bloom;
379
380   /**
381    * extended query (see gnunet_block_lib.h).
382    */
383   const void *xquery;
384
385   /**
386    * Bloomfilter to filter out duplicate replies.
387    */
388   struct GNUNET_CONTAINER_BloomFilter *reply_bf;
389
390   /**
391    * The key this request was about
392    */
393   GNUNET_HashCode key;
394
395   /**
396    * How long should we wait to transmit this request?
397    */
398   struct GNUNET_TIME_Relative timeout;
399
400   /**
401    * The unique identifier of this request
402    */
403   uint64_t unique_id;
404
405   /**
406    * Number of bytes in xquery.
407    */
408   size_t xquery_size;
409
410   /**
411    * Mutator value for the reply_bf, see gnunet_block_lib.h
412    */
413   uint32_t reply_bf_mutator;
414
415   /**
416    * Desired replication level
417    */
418   uint32_t replication;
419
420   /**
421    * Network size estimate, either ours or the sum of
422    * those routed to thus far. =~ Log of number of peers
423    * chosen from for this request.
424    */
425   uint32_t network_size;
426
427   /**
428    * Any message options for this request
429    */
430   uint32_t msg_options;
431
432   /**
433    * How many hops has the message already traversed?
434    */
435   uint32_t hop_count;
436
437   /**
438    * How many peer identities are present in the path history?
439    */
440   uint32_t path_history_len;
441
442   /**
443    * Path history.
444    */
445   char *path_history;
446
447   /**
448    * How important is this message?
449    */
450   unsigned int importance;
451
452   /**
453    * Should we (still) forward the request on to other peers?
454    */
455   int do_forward;
456
457   /**
458    * Did we forward this message? (may need to remember it!)
459    */
460   int forwarded;
461
462   /**
463    * Are we the closest known peer to this key (out of our neighbors?)
464    */
465   int closest;
466 };
467
468
469 /**
470  * Record used for remembering what peers are waiting for what
471  * responses (based on search key).
472  */
473 struct DHTRouteSource
474 {
475   /**
476    * This is a DLL.
477    */
478   struct DHTRouteSource *next;
479
480   /**
481    * This is a DLL.
482    */
483   struct DHTRouteSource *prev;
484
485   /**
486    * UID of the request, 0 if from another peer.
487    */
488   uint64_t uid;
489
490   /**
491    * Source of the request.  Replies should be forwarded to
492    * this peer.
493    */
494   struct GNUNET_PeerIdentity source;
495
496   /**
497    * If this was a local request, remember the client; otherwise NULL.
498    */
499   struct ClientList *client;
500
501   /**
502    * Pointer to this nodes heap location (for removal)
503    */
504   struct GNUNET_CONTAINER_HeapNode *hnode;
505
506   /**
507    * Back pointer to the record storing this information.
508    */
509   struct DHTQueryRecord *record;
510
511   /**
512    * Task to remove this entry on timeout.
513    */
514   GNUNET_SCHEDULER_TaskIdentifier delete_task;
515
516   /**
517    * Bloomfilter of peers we have already sent back as
518    * replies to the initial request.  Allows us to not
519    * forward the same peer multiple times for a find peer
520    * request.
521    */
522   struct GNUNET_CONTAINER_BloomFilter *find_peers_responded;
523
524 };
525
526
527 /**
528  * Entry in the DHT routing table.
529  */
530 struct DHTQueryRecord
531 {
532   /**
533    * Head of DLL for result forwarding.
534    */
535   struct DHTRouteSource *head;
536
537   /**
538    * Tail of DLL for result forwarding.
539    */
540   struct DHTRouteSource *tail;
541
542   /**
543    * Key that the record concerns.
544    */
545   GNUNET_HashCode key;
546
547 };
548
549
550 /**
551  * Context used to calculate the number of find peer messages
552  * per X time units since our last scheduled find peer message
553  * was sent.  If we have seen too many messages, delay or don't
554  * send our own out.
555  */
556 struct FindPeerMessageContext
557 {
558   unsigned int count;
559
560   struct GNUNET_TIME_Absolute start;
561
562   struct GNUNET_TIME_Absolute end;
563 };
564
565
566 /**
567  * DHT Routing results structure
568  */
569 struct DHTResults
570 {
571   /*
572    * Min heap for removal upon reaching limit
573    */
574   struct GNUNET_CONTAINER_Heap *minHeap;
575
576   /*
577    * Hashmap for fast key based lookup
578    */
579   struct GNUNET_CONTAINER_MultiHashMap *hashmap;
580
581 };
582
583
584 /**
585  * DHT structure for recent requests.
586  */
587 struct RecentRequests
588 {
589   /*
590    * Min heap for removal upon reaching limit
591    */
592   struct GNUNET_CONTAINER_Heap *minHeap;
593
594   /*
595    * Hashmap for key based lookup
596    */
597   struct GNUNET_CONTAINER_MultiHashMap *hashmap;
598 };
599
600
601 struct RecentRequest
602 {
603   /**
604    * Position of this node in the min heap.
605    */
606   struct GNUNET_CONTAINER_HeapNode *heap_node;
607
608   /**
609    * Bloomfilter containing entries for peers
610    * we forwarded this request to.
611    */
612   struct GNUNET_CONTAINER_BloomFilter *bloom;
613
614   /**
615    * Timestamp of this request, for ordering
616    * the min heap.
617    */
618   struct GNUNET_TIME_Absolute timestamp;
619
620   /**
621    * Key of this request.
622    */
623   GNUNET_HashCode key;
624
625   /**
626    * Unique identifier for this request, 0 if from another peer.
627    */
628   uint64_t uid;
629
630   /**
631    * Task to remove this entry on timeout.
632    */
633   GNUNET_SCHEDULER_TaskIdentifier remove_task;
634 };
635
636
637 /**
638  * log of the current network size estimate, used as the point where
639  * we switch between random and deterministic routing.  Default
640  * value of 4.0 is used if NSE module is not available (i.e. not
641  * configured).
642  */
643 static double log_of_network_size_estimate = 4.0;
644
645 /**
646  * Recent requests by hash/uid and by time inserted.
647  */
648 static struct RecentRequests recent;
649
650 /**
651  * Context to use to calculate find peer rates.
652  */
653 static struct FindPeerMessageContext find_peer_context;
654
655 /**
656  * Don't use our routing algorithm, always route
657  * to closest peer; initially send requests to 3
658  * peers.
659  */
660 static int strict_kademlia;
661
662 /**
663  * Routing option to end routing when closest peer found.
664  */
665 static int stop_on_closest;
666
667 /**
668  * Routing option to end routing when data is found.
669  */
670 static int stop_on_found;
671
672 /**
673  * Whether DHT needs to manage find peer requests, or
674  * an external force will do it on behalf of the DHT.
675  */
676 static int do_find_peer;
677
678 /**
679  * Use exactly the forwarding formula as described in
680  * the paper if set to GNUNET_YES, otherwise use the
681  * slightly modified version.
682  */
683 static int paper_forwarding;
684
685 /**
686  * PUT Peer Identities of peers we know about into
687  * the datacache.
688  */
689 static int put_peer_identities;
690
691 /**
692  * Use the "real" distance metric when selecting the
693  * next routing hop.  Can be less accurate.
694  */
695 static int use_real_distance;
696
697 /**
698  * How many peers have we added since we sent out our last
699  * find peer request?
700  */
701 static unsigned int newly_found_peers;
702
703 /**
704  * Container of active queries we should remember
705  */
706 static struct DHTResults forward_list;
707
708 /**
709  * Handle to the datacache service (for inserting/retrieving data)
710  */
711 static struct GNUNET_DATACACHE_Handle *datacache;
712
713 /**
714  * Handle for the statistics service.
715  */
716 struct GNUNET_STATISTICS_Handle *stats;
717
718 /**
719  * Handle to get our current HELLO.
720  */
721 static struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
722
723 /**
724  * The configuration the DHT service is running with
725  */
726 static const struct GNUNET_CONFIGURATION_Handle *cfg;
727
728 /**
729  * Handle to the core service
730  */
731 static struct GNUNET_CORE_Handle *coreAPI;
732
733 /**
734  * Handle to the transport service, for getting our hello
735  */
736 static struct GNUNET_TRANSPORT_Handle *transport_handle;
737
738 /**
739  * The identity of our peer.
740  */
741 static struct GNUNET_PeerIdentity my_identity;
742
743 /**
744  * Short id of the peer, for printing
745  */
746 static char *my_short_id;
747
748 /**
749  * Our HELLO
750  */
751 static struct GNUNET_MessageHeader *my_hello;
752
753 /**
754  * Task to run when we shut down, cleaning up all our trash
755  */
756 static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
757
758 /**
759  * The lowest currently used bucket.
760  */
761 static unsigned int lowest_bucket;      /* Initially equal to MAX_BUCKETS - 1 */
762
763 /**
764  * The buckets (Kademlia routing table, complete with growth).
765  * Array of size MAX_BUCKET_SIZE.
766  */
767 static struct PeerBucket k_buckets[MAX_BUCKETS];
768
769 /**
770  * Hash map of all known peers, for easy removal from k_buckets on disconnect.
771  */
772 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
773
774 /**
775  * Recently seen find peer requests.
776  */
777 static struct GNUNET_CONTAINER_MultiHashMap *recent_find_peer_requests;
778
779 /**
780  * Maximum size for each bucket.
781  */
782 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
783
784 /**
785  * List of active clients.
786  */
787 static struct ClientList *client_list;
788
789 /**
790  * Handle to the DHT logger.
791  */
792 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
793
794 /**
795  * Whether or not to send routing debugging information
796  * to the dht logging server
797  */
798 static unsigned int debug_routes;
799
800 /**
801  * Whether or not to send FULL route information to
802  * logging server
803  */
804 static unsigned int debug_routes_extended;
805
806 /**
807  * GNUNET_YES or GNUNET_NO, whether or not to act as
808  * a malicious node which drops all messages
809  */
810 static unsigned int malicious_dropper;
811
812 /**
813  * GNUNET_YES or GNUNET_NO, whether or not to act as
814  * a malicious node which sends out lots of GETS
815  */
816 static unsigned int malicious_getter;
817
818 /**
819  * GNUNET_YES or GNUNET_NO, whether or not to act as
820  * a malicious node which sends out lots of PUTS
821  */
822 static unsigned int malicious_putter;
823
824 /**
825  * Frequency for malicious get requests.
826  */
827 static struct GNUNET_TIME_Relative malicious_get_frequency;
828
829 /**
830  * Frequency for malicious put requests.
831  */
832 static struct GNUNET_TIME_Relative malicious_put_frequency;
833
834 /**
835  * Kademlia replication
836  */
837 static unsigned long long kademlia_replication;
838
839 /**
840  * Reply times for requests, if we are busy, don't send any
841  * more requests!
842  */
843 static struct GNUNET_TIME_Relative reply_times[MAX_REPLY_TIMES];
844
845 /**
846  * Current counter for replies.
847  */
848 static unsigned int reply_counter;
849
850 /**
851  * Our handle to the BLOCK library.
852  */
853 static struct GNUNET_BLOCK_Context *block_context;
854
855 /**
856  * Network size estimation handle.
857  */
858 static struct GNUNET_NSE_Handle *nse;
859
860
861 /**
862  * Callback that is called when network size estimate is updated.
863  *
864  * @param cls closure
865  * @param timestamp time when the estimate was received from the server (or created by the server)
866  * @param logestimate the log(Base 2) value of the current network size estimate
867  * @param std_dev standard deviation for the estimate
868  *
869  */
870 static void
871 update_network_size_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
872                               double logestimate, double std_dev)
873 {
874   log_of_network_size_estimate = logestimate;
875 }
876
877
878 /**
879  * Forward declaration.
880  */
881 static size_t
882 send_generic_reply (void *cls, size_t size, void *buf);
883
884
885 /** Declare here so retry_core_send is aware of it */
886 static size_t
887 core_transmit_notify (void *cls, size_t size, void *buf);
888
889
890 /**
891  * Convert unique ID to hash code.
892  *
893  * @param uid unique ID to convert
894  * @param hash set to uid (extended with zeros)
895  */
896 static void
897 hash_from_uid (uint64_t uid, GNUNET_HashCode * hash)
898 {
899   memset (hash, 0, sizeof (GNUNET_HashCode));
900   *((uint64_t *) hash) = uid;
901 }
902
903
904 /**
905  * Given the largest send delay, artificially decrease it
906  * so the next time around we may have a chance at sending
907  * again.
908  */
909 static void
910 decrease_max_send_delay (struct GNUNET_TIME_Relative max_time)
911 {
912   unsigned int i;
913
914   for (i = 0; i < MAX_REPLY_TIMES; i++)
915   {
916     if (reply_times[i].rel_value == max_time.rel_value)
917     {
918       reply_times[i].rel_value = reply_times[i].rel_value / 2;
919       return;
920     }
921   }
922 }
923
924
925 /**
926  * Find the maximum send time of the recently sent values.
927  *
928  * @return the average time between asking core to send a message
929  *         and when the buffer for copying it is passed
930  */
931 static struct GNUNET_TIME_Relative
932 get_max_send_delay ()
933 {
934   unsigned int i;
935   struct GNUNET_TIME_Relative max_time;
936
937   max_time = GNUNET_TIME_relative_get_zero ();
938
939   for (i = 0; i < MAX_REPLY_TIMES; i++)
940   {
941     if (reply_times[i].rel_value > max_time.rel_value)
942       max_time.rel_value = reply_times[i].rel_value;
943   }
944 #if DEBUG_DHT
945   if (max_time.rel_value > MAX_REQUEST_TIME.rel_value)
946     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n",
947                 (unsigned long long) max_time.rel_value);
948 #endif
949   return max_time;
950 }
951
952
953 static void
954 increment_stats (const char *value)
955 {
956   if (stats == NULL)
957     return;
958   GNUNET_STATISTICS_update (stats, value, 1, GNUNET_NO);
959 }
960
961
962 static void
963 decrement_stats (const char *value)
964 {
965   if (stats == NULL)
966     return;
967   GNUNET_STATISTICS_update (stats, value, -1, GNUNET_NO);
968 }
969
970
971 /**
972  *  Try to send another message from our core send list
973  */
974 static void
975 try_core_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
976 {
977   struct PeerInfo *peer = cls;
978   struct P2PPendingMessage *pending;
979   size_t ssize;
980
981   peer->send_task = GNUNET_SCHEDULER_NO_TASK;
982
983   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
984     return;
985
986   if (peer->th != NULL)
987     return;                     /* Message send already in progress */
988
989   pending = peer->head;
990   if (pending != NULL)
991   {
992     ssize = ntohs (pending->msg->size);
993 #if DEBUG_DHT > 1
994     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995                 "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n",
996                 my_short_id, "DHT", ssize, GNUNET_i2s (&peer->id));
997 #endif
998     pending->scheduled = GNUNET_TIME_absolute_get ();
999     reply_counter++;
1000     if (reply_counter >= MAX_REPLY_TIMES)
1001       reply_counter = 0;
1002     peer->th =
1003         GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
1004                                            pending->importance,
1005                                            pending->timeout, &peer->id, ssize,
1006                                            &core_transmit_notify, peer);
1007     if (peer->th == NULL)
1008       increment_stats ("# notify transmit ready failed");
1009   }
1010 }
1011
1012
1013 /**
1014  * Function called to send a request out to another peer.
1015  * Called both for locally initiated requests and those
1016  * received from other peers.
1017  *
1018  * @param msg the encapsulated message
1019  * @param peer the peer to forward the message to
1020  * @param msg_ctx the context of the message (hop count, bloom, etc.)
1021  */
1022 static void
1023 forward_result_message (const struct GNUNET_MessageHeader *msg,
1024                         struct PeerInfo *peer,
1025                         struct DHT_MessageContext *msg_ctx)
1026 {
1027   struct GNUNET_DHT_P2PRouteResultMessage *result_message;
1028   struct P2PPendingMessage *pending;
1029   size_t msize;
1030   size_t psize;
1031   char *path_start;
1032   char *path_offset;
1033
1034 #if DEBUG_PATH
1035   unsigned int i;
1036 #endif
1037
1038   increment_stats (STAT_RESULT_FORWARDS);
1039   msize =
1040       sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs (msg->size) +
1041       (sizeof (struct GNUNET_PeerIdentity) * msg_ctx->path_history_len);
1042   GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1043   psize = sizeof (struct P2PPendingMessage) + msize;
1044   pending = GNUNET_malloc (psize);
1045   pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
1046   pending->importance = DHT_SEND_PRIORITY;
1047   pending->timeout = GNUNET_TIME_relative_get_forever ();
1048   result_message = (struct GNUNET_DHT_P2PRouteResultMessage *) pending->msg;
1049   result_message->header.size = htons (msize);
1050   result_message->header.type =
1051       htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT);
1052   result_message->outgoing_path_length = htonl (msg_ctx->path_history_len);
1053   if (msg_ctx->path_history_len > 0)
1054   {
1055     /* End of pending is where enc_msg starts */
1056     path_start = (char *) &pending[1];
1057     /* Offset by the size of the enc_msg */
1058     path_start += ntohs (msg->size);
1059     memcpy (path_start, msg_ctx->path_history,
1060             msg_ctx->path_history_len * (sizeof (struct GNUNET_PeerIdentity)));
1061 #if DEBUG_PATH
1062     for (i = 0; i < msg_ctx->path_history_len; i++)
1063     {
1064       path_offset =
1065           &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1066       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067                   "(forward_result) Key %s Found peer %d:%s\n",
1068                   GNUNET_h2s (&msg_ctx->key), i,
1069                   GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1070     }
1071 #endif
1072   }
1073   result_message->options = htonl (msg_ctx->msg_options);
1074   result_message->hop_count = htonl (msg_ctx->hop_count + 1);
1075 #if HAVE_REPLY_BLOOMFILTER
1076   GNUNET_assert (GNUNET_OK ==
1077                  GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
1078                                                             result_message->
1079                                                             bloomfilter,
1080                                                             DHT_BLOOM_SIZE));
1081 #endif
1082 #if HAVE_UID_FOR_TESTING 
1083   result_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1084 #endif
1085   memcpy (&result_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1086   /* Copy the enc_msg, then the path history as well! */
1087   memcpy (&result_message[1], msg, ntohs (msg->size));
1088   path_offset = (char *) &result_message[1];
1089   path_offset += ntohs (msg->size);
1090   /* If we have path history, copy it to the end of the whole thing */
1091   if (msg_ctx->path_history_len > 0)
1092     memcpy (path_offset, msg_ctx->path_history,
1093             msg_ctx->path_history_len * (sizeof (struct GNUNET_PeerIdentity)));
1094 #if DEBUG_DHT > 1
1095   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096               "%s:%s Adding pending message size %d for peer %s\n", my_short_id,
1097               "DHT", msize, GNUNET_i2s (&peer->id));
1098 #endif
1099   peer->pending_count++;
1100   increment_stats ("# pending messages scheduled");
1101   GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
1102                                      pending);
1103   if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1104     peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1105 }
1106
1107
1108 /**
1109  * Called when core is ready to send a message we asked for
1110  * out to the destination.
1111  *
1112  * @param cls closure (NULL)
1113  * @param size number of bytes available in buf
1114  * @param buf where the callee should write the message
1115  * @return number of bytes written to buf
1116  */
1117 static size_t
1118 core_transmit_notify (void *cls, size_t size, void *buf)
1119 {
1120   struct PeerInfo *peer = cls;
1121   char *cbuf = buf;
1122   struct P2PPendingMessage *pending;
1123
1124   size_t off;
1125   size_t msize;
1126
1127   peer->th = NULL;
1128   if (buf == NULL)
1129   {
1130     /* client disconnected */
1131 #if DEBUG_DHT
1132     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n",
1133                 my_short_id, "DHT");
1134 #endif
1135     return 0;
1136   }
1137
1138   if (peer->head == NULL)
1139     return 0;
1140
1141   off = 0;
1142   pending = peer->head;
1143 #if DUMB
1144   reply_times[reply_counter] =
1145       GNUNET_TIME_absolute_get_difference (pending->scheduled,
1146                                            GNUNET_TIME_absolute_get ());
1147   msize = ntohs (pending->msg->size);
1148   if (msize <= size)
1149   {
1150     off = msize;
1151     memcpy (cbuf, pending->msg, msize);
1152     peer->pending_count--;
1153     increment_stats ("# pending messages sent");
1154     GNUNET_assert (peer->pending_count >= 0);
1155     GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
1156     GNUNET_free (pending);
1157   }
1158 #else
1159   while (NULL != pending &&
1160          (size - off >= (msize = ntohs (pending->msg->size))))
1161   {
1162     memcpy (&cbuf[off], pending->msg, msize);
1163     off += msize;
1164     peer->pending_count--;
1165     increment_stats ("# pending messages sent");
1166     GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
1167     GNUNET_free (pending);
1168     pending = peer->head;
1169   }
1170 #endif
1171   if ((peer->head != NULL) && (peer->send_task == GNUNET_SCHEDULER_NO_TASK))
1172     peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1173
1174   return off;
1175 }
1176
1177
1178 /**
1179  * Compute the distance between have and target as a 32-bit value.
1180  * Differences in the lower bits must count stronger than differences
1181  * in the higher bits.
1182  *
1183  * @return 0 if have==target, otherwise a number
1184  *           that is larger as the distance between
1185  *           the two hash codes increases
1186  */
1187 static unsigned int
1188 distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
1189 {
1190   unsigned int bucket;
1191   unsigned int msb;
1192   unsigned int lsb;
1193   unsigned int i;
1194
1195   /* We have to represent the distance between two 2^9 (=512)-bit
1196    * numbers as a 2^5 (=32)-bit number with "0" being used for the
1197    * two numbers being identical; furthermore, we need to
1198    * guarantee that a difference in the number of matching
1199    * bits is always represented in the result.
1200    *
1201    * We use 2^32/2^9 numerical values to distinguish between
1202    * hash codes that have the same LSB bit distance and
1203    * use the highest 2^9 bits of the result to signify the
1204    * number of (mis)matching LSB bits; if we have 0 matching
1205    * and hence 512 mismatching LSB bits we return -1 (since
1206    * 512 itself cannot be represented with 9 bits) */
1207
1208   /* first, calculate the most significant 9 bits of our
1209    * result, aka the number of LSBs */
1210   bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
1211   /* bucket is now a value between 0 and 512 */
1212   if (bucket == 512)
1213     return 0;                   /* perfect match */
1214   if (bucket == 0)
1215     return (unsigned int) -1;   /* LSB differs; use max (if we did the bit-shifting
1216                                  * below, we'd end up with max+1 (overflow)) */
1217
1218   /* calculate the most significant bits of the final result */
1219   msb = (512 - bucket) << (32 - 9);
1220   /* calculate the 32-9 least significant bits of the final result by
1221    * looking at the differences in the 32-9 bits following the
1222    * mismatching bit at 'bucket' */
1223   lsb = 0;
1224   for (i = bucket + 1;
1225        (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
1226   {
1227     if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
1228         GNUNET_CRYPTO_hash_get_bit (have, i))
1229       lsb |= (1 << (bucket + 32 - 9 - i));      /* first bit set will be 10,
1230                                                  * last bit set will be 31 -- if
1231                                                  * i does not reach 512 first... */
1232   }
1233   return msb | lsb;
1234 }
1235
1236
1237 /**
1238  * Return a number that is larger the closer the
1239  * "have" GNUNET_hash code is to the "target".
1240  *
1241  * @return inverse distance metric, non-zero.
1242  *         Must fudge the value if NO bits match.
1243  */
1244 static unsigned int
1245 inverse_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
1246 {
1247   if (GNUNET_CRYPTO_hash_matching_bits (target, have) == 0)
1248     return 1;                   /* Never return 0! */
1249   return ((unsigned int) -1) - distance (target, have);
1250 }
1251
1252
1253 /**
1254  * Find the optimal bucket for this key, regardless
1255  * of the current number of buckets in use.
1256  *
1257  * @param hc the hashcode to compare our identity to
1258  *
1259  * @return the proper bucket index, or GNUNET_SYSERR
1260  *         on error (same hashcode)
1261  */
1262 static int
1263 find_bucket (const GNUNET_HashCode * hc)
1264 {
1265   unsigned int bits;
1266
1267   bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
1268   if (bits == MAX_BUCKETS)
1269     return GNUNET_SYSERR;
1270   return MAX_BUCKETS - bits - 1;
1271 }
1272
1273
1274 /**
1275  * Find which k-bucket this peer should go into,
1276  * taking into account the size of the k-bucket
1277  * array.  This means that if more bits match than
1278  * there are currently buckets, lowest_bucket will
1279  * be returned.
1280  *
1281  * @param hc GNUNET_HashCode we are finding the bucket for.
1282  *
1283  * @return the proper bucket index for this key,
1284  *         or GNUNET_SYSERR on error (same hashcode)
1285  */
1286 static int
1287 find_current_bucket (const GNUNET_HashCode * hc)
1288 {
1289   int actual_bucket;
1290
1291   actual_bucket = find_bucket (hc);
1292   if (actual_bucket == GNUNET_SYSERR)   /* hc and our peer identity match! */
1293     return lowest_bucket;
1294   if (actual_bucket < lowest_bucket)    /* actual_bucket not yet used */
1295     return lowest_bucket;
1296   return actual_bucket;
1297 }
1298
1299
1300 /**
1301  * Find a routing table entry from a peer identity
1302  *
1303  * @param peer the peer identity to look up
1304  *
1305  * @return the routing table entry, or NULL if not found
1306  */
1307 static struct PeerInfo *
1308 find_peer_by_id (const struct GNUNET_PeerIdentity *peer)
1309 {
1310   int bucket;
1311   struct PeerInfo *pos;
1312
1313   bucket = find_current_bucket (&peer->hashPubKey);
1314
1315   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1316     return NULL;
1317
1318   pos = k_buckets[bucket].head;
1319   while (pos != NULL)
1320   {
1321     if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1322       return pos;
1323     pos = pos->next;
1324   }
1325   return NULL;                  /* No such peer. */
1326 }
1327
1328 /* Forward declaration */
1329 static void
1330 update_core_preference (void *cls,
1331                         const struct GNUNET_SCHEDULER_TaskContext *tc);
1332
1333
1334 /**
1335  * Function called with statistics about the given peer.
1336  *
1337  * @param cls closure
1338  * @param peer identifies the peer
1339  * @param bpm_out set to the current bandwidth limit (sending) for this peer
1340  * @param amount set to the amount that was actually reserved or unreserved;
1341  *               either the full requested amount or zero (no partial reservations)
1342  * @param res_delay if the reservation could not be satisfied (amount was 0), how
1343  *        long should the client wait until re-trying?
1344  * @param preference current traffic preference for the given peer
1345  */
1346 static void
1347 update_core_preference_finish (void *cls,
1348                                const struct GNUNET_PeerIdentity *peer,
1349                                struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1350                                int32_t amount,
1351                                struct GNUNET_TIME_Relative res_delay,
1352                                uint64_t preference)
1353 {
1354   struct PeerInfo *peer_info = cls;
1355
1356   peer_info->info_ctx = NULL;
1357   GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
1358                                 &update_core_preference, peer_info);
1359 }
1360
1361 static void
1362 update_core_preference (void *cls,
1363                         const struct GNUNET_SCHEDULER_TaskContext *tc)
1364 {
1365   struct PeerInfo *peer = cls;
1366   uint64_t preference;
1367   unsigned int matching;
1368
1369   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1370   {
1371     return;
1372   }
1373   matching =
1374       GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
1375                                         &peer->id.hashPubKey);
1376   if (matching >= 64)
1377   {
1378 #if DEBUG_DHT
1379     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1380                 "Peer identifier matches by %u bits, only shifting as much as we can!\n",
1381                 matching);
1382 #endif
1383     matching = 63;
1384   }
1385   preference = 1LL << matching;
1386   peer->info_ctx =
1387       GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
1388                                           GNUNET_TIME_UNIT_FOREVER_REL,
1389                                           GNUNET_BANDWIDTH_VALUE_MAX, 0,
1390                                           preference,
1391                                           &update_core_preference_finish, peer);
1392 }
1393
1394
1395 /**
1396  * Given a peer and its corresponding bucket,
1397  * remove it from that bucket.  Does not free
1398  * the PeerInfo struct, nor cancel messages
1399  * or free messages waiting to be sent to this
1400  * peer!
1401  *
1402  * @param peer the peer to remove
1403  * @param bucket the bucket the peer belongs to
1404  */
1405 static void
1406 remove_peer (struct PeerInfo *peer, unsigned int bucket)
1407 {
1408   GNUNET_assert (k_buckets[bucket].peers_size > 0);
1409   GNUNET_CONTAINER_DLL_remove (k_buckets[bucket].head, k_buckets[bucket].tail,
1410                                peer);
1411   k_buckets[bucket].peers_size--;
1412 #if CHANGE_LOWEST
1413   if ((bucket == lowest_bucket) && (k_buckets[lowest_bucket].peers_size == 0) &&
1414       (lowest_bucket < MAX_BUCKETS - 1))
1415     lowest_bucket++;
1416 #endif
1417 }
1418
1419 /**
1420  * Removes peer from a bucket, then frees associated
1421  * resources and frees peer.
1422  *
1423  * @param peer peer to be removed and freed
1424  * @param bucket which bucket this peer belongs to
1425  */
1426 static void
1427 delete_peer (struct PeerInfo *peer, unsigned int bucket)
1428 {
1429   struct P2PPendingMessage *pos;
1430   struct P2PPendingMessage *next;
1431
1432   remove_peer (peer, bucket);   /* First remove the peer from its bucket */
1433   if (peer->send_task != GNUNET_SCHEDULER_NO_TASK)
1434     GNUNET_SCHEDULER_cancel (peer->send_task);
1435   if ((peer->th != NULL) && (coreAPI != NULL))
1436     GNUNET_CORE_notify_transmit_ready_cancel (peer->th);
1437
1438   pos = peer->head;
1439   while (pos != NULL)           /* Remove any pending messages for this peer */
1440   {
1441     increment_stats
1442         ("# dht pending messages discarded (due to disconnect/shutdown)");
1443     next = pos->next;
1444     GNUNET_free (pos);
1445     pos = next;
1446   }
1447
1448   GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
1449                  (all_known_peers, &peer->id.hashPubKey));
1450   GNUNET_assert (GNUNET_YES ==
1451                  GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
1452                                                        &peer->id.hashPubKey,
1453                                                        peer));
1454   GNUNET_free (peer);
1455   decrement_stats (STAT_PEERS_KNOWN);
1456 }
1457
1458
1459 /**
1460  * Iterator over hash map entries.
1461  *
1462  * @param cls closure
1463  * @param key current key code
1464  * @param value PeerInfo of the peer to move to new lowest bucket
1465  * @return GNUNET_YES if we should continue to
1466  *         iterate,
1467  *         GNUNET_NO if not.
1468  */
1469 static int
1470 move_lowest_bucket (void *cls, const GNUNET_HashCode * key, void *value)
1471 {
1472   struct PeerInfo *peer = value;
1473   int new_bucket;
1474
1475   GNUNET_assert (lowest_bucket > 0);
1476   new_bucket = lowest_bucket - 1;
1477   remove_peer (peer, lowest_bucket);
1478   GNUNET_CONTAINER_DLL_insert_after (k_buckets[new_bucket].head,
1479                                      k_buckets[new_bucket].tail,
1480                                      k_buckets[new_bucket].tail, peer);
1481   k_buckets[new_bucket].peers_size++;
1482   return GNUNET_YES;
1483 }
1484
1485
1486 /**
1487  * The current lowest bucket is full, so change the lowest
1488  * bucket to the next lower down, and move any appropriate
1489  * entries in the current lowest bucket to the new bucket.
1490  */
1491 static void
1492 enable_next_bucket ()
1493 {
1494   struct GNUNET_CONTAINER_MultiHashMap *to_remove;
1495   struct PeerInfo *pos;
1496
1497   GNUNET_assert (lowest_bucket > 0);
1498   to_remove = GNUNET_CONTAINER_multihashmap_create (bucket_size);
1499   pos = k_buckets[lowest_bucket].head;
1500
1501   /* Populate the array of peers which should be in the next lowest bucket */
1502   while (pos != NULL)
1503   {
1504     if (find_bucket (&pos->id.hashPubKey) < lowest_bucket)
1505       GNUNET_CONTAINER_multihashmap_put (to_remove, &pos->id.hashPubKey, pos,
1506                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1507     pos = pos->next;
1508   }
1509
1510   /* Remove peers from lowest bucket, insert into next lowest bucket */
1511   GNUNET_CONTAINER_multihashmap_iterate (to_remove, &move_lowest_bucket, NULL);
1512   GNUNET_CONTAINER_multihashmap_destroy (to_remove);
1513   lowest_bucket = lowest_bucket - 1;
1514 }
1515
1516
1517 /**
1518  * Find the closest peer in our routing table to the
1519  * given hashcode.
1520  *
1521  * @return The closest peer in our routing table to the
1522  *         key, or NULL on error.
1523  */
1524 static struct PeerInfo *
1525 find_closest_peer (const GNUNET_HashCode * hc)
1526 {
1527   struct PeerInfo *pos;
1528   struct PeerInfo *current_closest;
1529   unsigned int lowest_distance;
1530   unsigned int temp_distance;
1531   int bucket;
1532   int count;
1533
1534   lowest_distance = -1;
1535
1536   if (k_buckets[lowest_bucket].peers_size == 0)
1537     return NULL;
1538
1539   current_closest = NULL;
1540   for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
1541   {
1542     pos = k_buckets[bucket].head;
1543     count = 0;
1544     while ((pos != NULL) && (count < bucket_size))
1545     {
1546       temp_distance = distance (&pos->id.hashPubKey, hc);
1547       if (temp_distance <= lowest_distance)
1548       {
1549         lowest_distance = temp_distance;
1550         current_closest = pos;
1551       }
1552       pos = pos->next;
1553       count++;
1554     }
1555   }
1556   GNUNET_assert (current_closest != NULL);
1557   return current_closest;
1558 }
1559
1560
1561 /**
1562  * Function called to send a request out to another peer.
1563  * Called both for locally initiated requests and those
1564  * received from other peers.
1565  *
1566  * @param msg the encapsulated message
1567  * @param peer the peer to forward the message to
1568  * @param msg_ctx the context of the message (hop count, bloom, etc.)
1569  */
1570 static void
1571 forward_message (const struct GNUNET_MessageHeader *msg, struct PeerInfo *peer,
1572                  struct DHT_MessageContext *msg_ctx)
1573 {
1574   struct GNUNET_DHT_P2PRouteMessage *route_message;
1575   struct P2PPendingMessage *pending;
1576   size_t msize;
1577   size_t psize;
1578   char *route_path;
1579
1580   increment_stats (STAT_ROUTE_FORWARDS);
1581   GNUNET_assert (peer != NULL);
1582   if ((msg_ctx->closest != GNUNET_YES) &&
1583       (peer == find_closest_peer (&msg_ctx->key)))
1584     increment_stats (STAT_ROUTE_FORWARDS_CLOSEST);
1585
1586   msize =
1587       sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (msg->size) +
1588       (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1589   GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1590   psize = sizeof (struct P2PPendingMessage) + msize;
1591   pending = GNUNET_malloc (psize);
1592   pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
1593   pending->importance = msg_ctx->importance;
1594   pending->timeout = msg_ctx->timeout;
1595   route_message = (struct GNUNET_DHT_P2PRouteMessage *) pending->msg;
1596   route_message->header.size = htons (msize);
1597   route_message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE);
1598   route_message->options = htonl (msg_ctx->msg_options);
1599   route_message->hop_count = htonl (msg_ctx->hop_count + 1);
1600   route_message->network_size = htonl (msg_ctx->network_size);
1601   route_message->desired_replication_level = htonl (msg_ctx->replication);
1602 #if HAVE_UID_FOR_TESTING 
1603   route_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1604 #endif
1605   if (msg_ctx->bloom != NULL)
1606     GNUNET_assert (GNUNET_OK ==
1607                    GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
1608                                                               route_message->
1609                                                               bloomfilter,
1610                                                               DHT_BLOOM_SIZE));
1611   memcpy (&route_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1612   memcpy (&route_message[1], msg, ntohs (msg->size));
1613   if (GNUNET_DHT_RO_RECORD_ROUTE ==
1614       (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
1615   {
1616     route_message->outgoing_path_length = htonl (msg_ctx->path_history_len);
1617     /* Set pointer to start of enc_msg */
1618     route_path = (char *) &route_message[1];
1619     /* Offset to the end of the enc_msg */
1620     route_path += ntohs (msg->size);
1621     /* Copy the route_path after enc_msg */
1622     memcpy (route_path, msg_ctx->path_history,
1623             msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1624   }
1625 #if DEBUG_DHT > 1
1626   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627               "%s:%s Adding pending message size %d for peer %s\n", my_short_id,
1628               "DHT", msize, GNUNET_i2s (&peer->id));
1629 #endif
1630   peer->pending_count++;
1631   increment_stats ("# pending messages scheduled");
1632   GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
1633                                      pending);
1634   if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1635     peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1636 }
1637
1638
1639 /**
1640  * Task run to check for messages that need to be sent to a client.
1641  *
1642  * @param client a ClientList, containing the client and any messages to be sent to it
1643  */
1644 static void
1645 process_pending_messages (struct ClientList *client)
1646 {
1647   if ((client->pending_head == NULL) || (client->transmit_handle != NULL))
1648     return;
1649   client->transmit_handle =
1650       GNUNET_SERVER_notify_transmit_ready (client->client_handle,
1651                                            ntohs (client->pending_head->
1652                                                   msg->size),
1653                                            GNUNET_TIME_UNIT_FOREVER_REL,
1654                                            &send_generic_reply, client);
1655 }
1656
1657 /**
1658  * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
1659  * request.  A ClientList is passed as closure, take the head of the list
1660  * and copy it into buf, which has the result of sending the message to the
1661  * client.
1662  *
1663  * @param cls closure to this call
1664  * @param size maximum number of bytes available to send
1665  * @param buf where to copy the actual message to
1666  *
1667  * @return the number of bytes actually copied, 0 indicates failure
1668  */
1669 static size_t
1670 send_generic_reply (void *cls, size_t size, void *buf)
1671 {
1672   struct ClientList *client = cls;
1673   char *cbuf = buf;
1674   struct PendingMessage *reply;
1675   size_t off;
1676   size_t msize;
1677
1678   client->transmit_handle = NULL;
1679   if (buf == NULL)
1680   {
1681     /* client disconnected */
1682     return 0;
1683   }
1684   off = 0;
1685   while ((NULL != (reply = client->pending_head)) &&
1686          (size >= off + (msize = ntohs (reply->msg->size))))
1687   {
1688     GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
1689                                  reply);
1690     memcpy (&cbuf[off], reply->msg, msize);
1691     GNUNET_free (reply);
1692     off += msize;
1693   }
1694   process_pending_messages (client);
1695 #if DEBUG_DHT
1696   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697               "Transmitted %u bytes of replies to client\n",
1698               (unsigned int) off);
1699 #endif
1700   return off;
1701 }
1702
1703
1704 /**
1705  * Add a PendingMessage to the clients list of messages to be sent
1706  *
1707  * @param client the active client to send the message to
1708  * @param pending_message the actual message to send
1709  */
1710 static void
1711 add_pending_message (struct ClientList *client,
1712                      struct PendingMessage *pending_message)
1713 {
1714   GNUNET_CONTAINER_DLL_insert_after (client->pending_head, client->pending_tail,
1715                                      client->pending_tail, pending_message);
1716   process_pending_messages (client);
1717 }
1718
1719
1720 /**
1721  * Called when a reply needs to be sent to a client, as
1722  * a result it found to a GET or FIND PEER request.
1723  *
1724  * @param client the client to send the reply to
1725  * @param message the encapsulated message to send
1726  * @param msg_ctx the context of the received message
1727  */
1728 static void
1729 send_reply_to_client (struct ClientList *client,
1730                       const struct GNUNET_MessageHeader *message,
1731                       struct DHT_MessageContext *msg_ctx)
1732 {
1733   struct GNUNET_DHT_RouteResultMessage *reply;
1734   struct PendingMessage *pending_message;
1735   uint16_t msize;
1736   size_t tsize;
1737   char *reply_offset;
1738
1739 #if DEBUG_PATH
1740   char *path_offset;
1741   unsigned int i;
1742 #endif
1743 #if DEBUG_DHT
1744   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': Sending reply to client.\n",
1745               my_short_id, "DHT");
1746 #endif
1747   msize = ntohs (message->size);
1748   tsize =
1749       sizeof (struct GNUNET_DHT_RouteResultMessage) + msize +
1750       (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1751   if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1752   {
1753     GNUNET_break_op (0);
1754     return;
1755   }
1756   pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize);
1757   pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
1758   reply = (struct GNUNET_DHT_RouteResultMessage *) &pending_message[1];
1759   reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT);
1760   reply->header.size = htons (tsize);
1761   reply->outgoing_path_length = htonl (msg_ctx->path_history_len);
1762   reply->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1763   memcpy (&reply->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1764   reply_offset = (char *) &reply[1];
1765   memcpy (&reply[1], message, msize);
1766   if (msg_ctx->path_history_len > 0)
1767   {
1768     reply_offset += msize;
1769     memcpy (reply_offset, msg_ctx->path_history,
1770             msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
1771   }
1772 #if DEBUG_PATH
1773   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1774               "Returning message with outgoing path length %d\n",
1775               msg_ctx->path_history_len);
1776   for (i = 0; i < msg_ctx->path_history_len; i++)
1777   {
1778     path_offset =
1779         &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1780     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer %d:%s\n", i,
1781                 GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1782   }
1783 #endif
1784   add_pending_message (client, pending_message);
1785 }
1786
1787 /**
1788  * Consider whether or not we would like to have this peer added to
1789  * our routing table.  Check whether bucket for this peer is full,
1790  * if so return negative; if not return positive.  Since peers are
1791  * only added on CORE level connect, this doesn't actually add the
1792  * peer to the routing table.
1793  *
1794  * @param peer the peer we are considering adding
1795  *
1796  * @return GNUNET_YES if we want this peer, GNUNET_NO if not (bucket
1797  *         already full)
1798  */
1799 static int
1800 consider_peer (struct GNUNET_PeerIdentity *peer)
1801 {
1802   int bucket;
1803
1804   if ((GNUNET_YES ==
1805        GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
1806                                                &peer->hashPubKey)) ||
1807       (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))))
1808     return GNUNET_NO;           /* We already know this peer (are connected even!) */
1809   bucket = find_current_bucket (&peer->hashPubKey);
1810
1811   if ((k_buckets[bucket].peers_size < bucket_size) ||
1812       ((bucket == lowest_bucket) && (lowest_bucket > 0)))
1813     return GNUNET_YES;
1814
1815   return GNUNET_NO;
1816 }
1817
1818
1819 /**
1820  * Task used to remove forwarding entries, either
1821  * after timeout, when full, or on shutdown.
1822  *
1823  * @param cls the entry to remove
1824  * @param tc context, reason, etc.
1825  */
1826 static void
1827 remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1828 {
1829   struct DHTRouteSource *source_info = cls;
1830   struct DHTQueryRecord *record;
1831
1832   source_info = GNUNET_CONTAINER_heap_remove_node (source_info->hnode);
1833   record = source_info->record;
1834   GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
1835
1836   if (record->head == NULL)     /* No more entries in DLL */
1837   {
1838     GNUNET_assert (GNUNET_YES ==
1839                    GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
1840                                                          &record->key, record));
1841     GNUNET_free (record);
1842   }
1843   if (source_info->find_peers_responded != NULL)
1844     GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
1845   GNUNET_free (source_info);
1846 }
1847
1848 /**
1849  * Main function that handles whether or not to route a result
1850  * message to other peers, or to send to our local client.
1851  *
1852  * @param msg the result message to be routed
1853  * @param msg_ctx context of the message we are routing
1854  *
1855  * @return the number of peers the message was routed to,
1856  *         GNUNET_SYSERR on failure
1857  */
1858 static int
1859 route_result_message (struct GNUNET_MessageHeader *msg,
1860                       struct DHT_MessageContext *msg_ctx)
1861 {
1862   struct GNUNET_PeerIdentity new_peer;
1863   struct DHTQueryRecord *record;
1864   struct DHTRouteSource *pos;
1865   struct PeerInfo *peer_info;
1866   const struct GNUNET_MessageHeader *hello_msg;
1867
1868 #if DEBUG_DHT > 1
1869   unsigned int i;
1870 #endif
1871
1872   increment_stats (STAT_RESULTS);
1873   /**
1874    * If a find peer result message is received and contains a valid
1875    * HELLO for another peer, offer it to the transport service.
1876    */
1877   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
1878   {
1879     if (ntohs (msg->size) <= sizeof (struct GNUNET_MessageHeader))
1880       GNUNET_break_op (0);
1881
1882     hello_msg = &msg[1];
1883     if ((ntohs (hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
1884         (GNUNET_SYSERR ==
1885          GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello_msg,
1886                               &new_peer)))
1887     {
1888       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1889                   "%s:%s Received non-HELLO message type in find peer result message!\n",
1890                   my_short_id, "DHT");
1891       GNUNET_break_op (0);
1892       return GNUNET_NO;
1893     }
1894     else                        /* We have a valid hello, and peer id stored in new_peer */
1895     {
1896       find_peer_context.count++;
1897       increment_stats (STAT_FIND_PEER_REPLY);
1898       if (GNUNET_YES == consider_peer (&new_peer))
1899       {
1900         increment_stats (STAT_HELLOS_PROVIDED);
1901         GNUNET_TRANSPORT_offer_hello (transport_handle, hello_msg, NULL, NULL);
1902         GNUNET_CORE_peer_request_connect (coreAPI, &new_peer, NULL, NULL);
1903       }
1904     }
1905   }
1906
1907   if (malicious_dropper == GNUNET_YES)
1908     record = NULL;
1909   else
1910     record =
1911         GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
1912
1913   if (record == NULL)           /* No record of this message! */
1914   {
1915 #if DEBUG_DHT
1916     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917                 "`%s:%s': Have no record of response key %s uid %llu\n",
1918                 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
1919                 msg_ctx->unique_id);
1920 #endif
1921 #if DEBUG_DHT_ROUTING
1922     if ((debug_routes_extended) && (dhtlog_handle != NULL))
1923     {
1924       dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
1925                                    msg_ctx->hop_count, GNUNET_SYSERR,
1926                                    &my_identity, &msg_ctx->key, &msg_ctx->peer,
1927                                    NULL);
1928     }
1929 #endif
1930 #if HAVE_REPLY_BLOOMFILTER
1931     if (msg_ctx->bloom != NULL)
1932     {
1933       GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
1934       msg_ctx->bloom = NULL;
1935     }
1936 #endif
1937     return 0;
1938   }
1939
1940   pos = record->head;
1941   while (pos != NULL)
1942   {
1943 #if STRICT_FORWARDING
1944     if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)  /* If we have already forwarded this peer id, don't do it again! */
1945     {
1946       if (GNUNET_YES ==
1947           GNUNET_CONTAINER_bloomfilter_test (pos->find_peers_responded,
1948                                              &new_peer.hashPubKey))
1949       {
1950         increment_stats ("# find peer responses NOT forwarded (bloom match)");
1951         pos = pos->next;
1952         continue;
1953       }
1954       else
1955         GNUNET_CONTAINER_bloomfilter_add (pos->find_peers_responded,
1956                                           &new_peer.hashPubKey);
1957     }
1958 #endif
1959
1960     if (0 == memcmp (&pos->source, &my_identity, sizeof (struct GNUNET_PeerIdentity)))  /* Local client (or DHT) initiated request! */
1961     {
1962 #if DEBUG_DHT
1963       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964                   "`%s:%s': Sending response key %s uid %llu to client\n",
1965                   my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
1966                   msg_ctx->unique_id);
1967 #endif
1968 #if DEBUG_DHT_ROUTING
1969       if ((debug_routes_extended) && (dhtlog_handle != NULL))
1970       {
1971         dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
1972                                      msg_ctx->hop_count, GNUNET_YES,
1973                                      &my_identity, &msg_ctx->key, &msg_ctx->peer,
1974                                      NULL);
1975       }
1976 #endif
1977       increment_stats (STAT_RESULTS_TO_CLIENT);
1978       if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
1979         increment_stats (STAT_GET_REPLY);
1980 #if DEBUG_DHT > 1
1981       for (i = 0; i < msg_ctx->path_history_len; i++)
1982       {
1983         char *path_offset;
1984
1985         path_offset =
1986             &msg_ctx->path_history[i * sizeof (struct GNUNET_PeerIdentity)];
1987         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1988                     "(before client) Key %s Found peer %d:%s\n",
1989                     GNUNET_h2s (&msg_ctx->key), i,
1990                     GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
1991       }
1992 #endif
1993       send_reply_to_client (pos->client, msg, msg_ctx);
1994     }
1995     else                        /* Send to peer */
1996     {
1997       peer_info = find_peer_by_id (&pos->source);
1998       if (peer_info == NULL)    /* Didn't find the peer in our routing table, perhaps peer disconnected! */
1999       {
2000         pos = pos->next;
2001         continue;
2002       }
2003 #if HAVE_REPLY_BLOOMFILTER
2004       if (msg_ctx->bloom == NULL)
2005         msg_ctx->bloom =
2006             GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
2007                                                DHT_BLOOM_K);
2008       GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
2009                                         &my_identity.hashPubKey);
2010       if ((GNUNET_NO ==
2011            GNUNET_CONTAINER_bloomfilter_test (msg_ctx->bloom,
2012                                               &peer_info->id.hashPubKey)))
2013         {{{
2014 #endif
2015 #if DEBUG_DHT
2016       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017                   "`%s:%s': Forwarding response key %s uid %llu to peer %s\n",
2018                   my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
2019                   msg_ctx->unique_id, GNUNET_i2s (&peer_info->id));
2020 #endif
2021 #if DEBUG_DHT_ROUTING
2022       if ((debug_routes_extended) && (dhtlog_handle != NULL))
2023       {
2024         dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT,
2025                                      msg_ctx->hop_count, GNUNET_NO,
2026                                      &my_identity, &msg_ctx->key,
2027                                      &msg_ctx->peer, &pos->source);
2028       }
2029 #endif
2030       forward_result_message (msg, peer_info, msg_ctx);
2031       /* Try removing forward entries after sending once, only allows ONE response per request */
2032       if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
2033       {
2034         GNUNET_SCHEDULER_cancel (pos->delete_task);
2035         pos->delete_task =
2036           GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);        
2037       }
2038 #if HAVE_REPLY_BLOOMFILTER
2039         }}}
2040 #endif
2041     }
2042     pos = pos->next;
2043   }
2044 #if HAVE_REPLY_BLOOMFILTER
2045   if (msg_ctx->bloom != NULL)
2046   {
2047     GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
2048     msg_ctx->bloom = NULL;
2049   }
2050 #endif
2051   return 0;
2052 }
2053
2054
2055 /**
2056  * Iterator for local get request results,
2057  *
2058  * @param cls closure for iterator, a DatacacheGetContext
2059  * @param exp when does this value expire?
2060  * @param key the key this data is stored under
2061  * @param size the size of the data identified by key
2062  * @param data the actual data
2063  * @param type the type of the data
2064  *
2065  * @return GNUNET_OK to continue iteration, anything else
2066  * to stop iteration.
2067  */
2068 static int
2069 datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
2070                         const GNUNET_HashCode * key, size_t size,
2071                         const char *data, enum GNUNET_BLOCK_Type type)
2072 {
2073   struct DHT_MessageContext *msg_ctx = cls;
2074   struct DHT_MessageContext new_msg_ctx;
2075   struct GNUNET_DHT_GetResultMessage *get_result;
2076   enum GNUNET_BLOCK_EvaluationResult eval;
2077   const struct DHTPutEntry *put_entry;
2078   int get_size;
2079   char *path_offset;
2080
2081 #if DEBUG_PATH
2082   unsigned int i;
2083 #endif
2084
2085 #if DEBUG_DHT
2086   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2087               "`%s:%s': Received `%s' response from datacache\n", my_short_id,
2088               "DHT", "GET");
2089 #endif
2090
2091   put_entry = (const struct DHTPutEntry *) data;
2092
2093   if (size !=
2094       sizeof (struct DHTPutEntry) + put_entry->data_size +
2095       (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)))
2096   {
2097     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2098                 "Path + data size doesn't add up for data inserted into datacache!\nData size %d, path length %d, expected %d, got %d\n",
2099                 put_entry->data_size, put_entry->path_length,
2100                 sizeof (struct DHTPutEntry) + put_entry->data_size +
2101                 (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)),
2102                 size);
2103     msg_ctx->do_forward = GNUNET_NO;
2104     return GNUNET_OK;
2105   }
2106
2107   eval =
2108       GNUNET_BLOCK_evaluate (block_context, type, key, &msg_ctx->reply_bf,
2109                              msg_ctx->reply_bf_mutator, msg_ctx->xquery,
2110                              msg_ctx->xquery_size, &put_entry[1],
2111                              put_entry->data_size);
2112
2113   switch (eval)
2114   {
2115   case GNUNET_BLOCK_EVALUATION_OK_LAST:
2116     msg_ctx->do_forward = GNUNET_NO;
2117   case GNUNET_BLOCK_EVALUATION_OK_MORE:
2118     memcpy (&new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2119     if (GNUNET_DHT_RO_RECORD_ROUTE ==
2120         (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2121     {
2122       new_msg_ctx.msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2123 #if DEBUG_PATH
2124       for (i = 0; i < new_msg_ctx.path_history_len; i++)
2125       {
2126         path_offset =
2127             &new_msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
2128         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129                     "(get_iterator) Key %s Found peer %d:%s\n",
2130                     GNUNET_h2s (&msg_ctx->key), i,
2131                     GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
2132       }
2133 #endif
2134     }
2135
2136     get_size =
2137         sizeof (struct GNUNET_DHT_GetResultMessage) + put_entry->data_size +
2138         (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity));
2139     get_result = GNUNET_malloc (get_size);
2140     get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
2141     get_result->header.size = htons (get_size);
2142     get_result->expiration = GNUNET_TIME_absolute_hton (exp);
2143     get_result->type = htons (type);
2144     get_result->put_path_length = htons (put_entry->path_length);
2145     path_offset = (char *) &put_entry[1];
2146     path_offset += put_entry->data_size;
2147 #if DEBUG_PATH
2148     for (i = 0; i < put_entry->path_length; i++)
2149     {
2150       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151                   "(get_iterator PUT path) Key %s Found peer %d:%s\n",
2152                   GNUNET_h2s (&msg_ctx->key), i,
2153                   GNUNET_i2s ((struct GNUNET_PeerIdentity *)
2154                               &path_offset[i *
2155                                            sizeof (struct
2156                                                    GNUNET_PeerIdentity)]));
2157     }
2158 #endif
2159     /* Copy the actual data and the path_history to the end of the get result */
2160     memcpy (&get_result[1], &put_entry[1],
2161             put_entry->data_size +
2162             (put_entry->path_length * sizeof (struct GNUNET_PeerIdentity)));
2163     new_msg_ctx.peer = my_identity;
2164     new_msg_ctx.bloom = NULL;
2165     new_msg_ctx.hop_count = 0;
2166     new_msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;   /* Make result routing a higher priority */
2167     new_msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
2168     increment_stats (STAT_GET_RESPONSE_START);
2169     route_result_message (&get_result->header, &new_msg_ctx);
2170     GNUNET_free (get_result);
2171     break;
2172   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
2173 #if DEBUG_DHT
2174     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': Duplicate block error\n",
2175                 my_short_id, "DHT");
2176 #endif
2177     break;
2178   case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
2179 #if DEBUG_DHT
2180     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "`%s:%s': Invalid request error\n",
2181                 my_short_id, "DHT");
2182 #endif
2183     break;
2184   case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
2185 #if DEBUG_DHT
2186     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2187                 "`%s:%s': Valid request, no results.\n", my_short_id, "DHT");
2188 #endif
2189     GNUNET_break (0);
2190     break;
2191   case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
2192     GNUNET_break_op (0);
2193     msg_ctx->do_forward = GNUNET_NO;
2194     break;
2195   case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
2196 #if DEBUG_DHT
2197     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2198                 "`%s:%s': Unsupported block type (%u) in response!\n",
2199                 my_short_id, "DHT", type);
2200 #endif
2201     /* msg_ctx->do_forward = GNUNET_NO;  // not sure... */
2202     break;
2203   }
2204   return GNUNET_OK;
2205 }
2206
2207
2208 /**
2209  * Main function that handles whether or not to route a message to other
2210  * peers.
2211  *
2212  * @param msg the message to be routed
2213  * @param msg_ctx the context containing all pertinent information about the message
2214  */
2215 static void
2216 route_message (const struct GNUNET_MessageHeader *msg,
2217                struct DHT_MessageContext *msg_ctx);
2218
2219
2220 /**
2221  * Server handler for all dht get requests, look for data,
2222  * if found, send response either to clients or other peers.
2223  *
2224  * @param msg the actual get message
2225  * @param msg_ctx struct containing pertinent information about the get request
2226  *
2227  * @return number of items found for GET request
2228  */
2229 static unsigned int
2230 handle_dht_get (const struct GNUNET_MessageHeader *msg,
2231                 struct DHT_MessageContext *msg_ctx)
2232 {
2233   const struct GNUNET_DHT_GetMessage *get_msg;
2234   uint16_t msize;
2235   uint16_t bf_size;
2236   unsigned int results;
2237   const char *end;
2238   enum GNUNET_BLOCK_Type type;
2239
2240   msize = ntohs (msg->size);
2241   if (msize < sizeof (struct GNUNET_DHT_GetMessage))
2242   {
2243     GNUNET_break (0);
2244     return 0;
2245   }
2246   get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
2247   bf_size = ntohs (get_msg->bf_size);
2248   msg_ctx->xquery_size = ntohs (get_msg->xquery_size);
2249   msg_ctx->reply_bf_mutator = get_msg->bf_mutator;
2250   if (msize !=
2251       sizeof (struct GNUNET_DHT_GetMessage) + bf_size + msg_ctx->xquery_size)
2252   {
2253     GNUNET_break_op (0);
2254     return 0;
2255   }
2256   end = (const char *) &get_msg[1];
2257   if (msg_ctx->xquery_size == 0)
2258   {
2259     msg_ctx->xquery = NULL;
2260   }
2261   else
2262   {
2263     msg_ctx->xquery = (const void *) end;
2264     end += msg_ctx->xquery_size;
2265   }
2266   if (bf_size == 0)
2267   {
2268     msg_ctx->reply_bf = NULL;
2269   }
2270   else
2271   {
2272     msg_ctx->reply_bf =
2273         GNUNET_CONTAINER_bloomfilter_init (end, bf_size,
2274                                            GNUNET_DHT_GET_BLOOMFILTER_K);
2275   }
2276   type = (enum GNUNET_BLOCK_Type) ntohl (get_msg->type);
2277 #if DEBUG_DHT
2278   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279               "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n",
2280               my_short_id, "DHT", "GET", type, GNUNET_h2s (&msg_ctx->key),
2281               msg_ctx->unique_id);
2282 #endif
2283   increment_stats (STAT_GETS);
2284   results = 0;
2285 #if HAVE_MALICIOUS
2286   if (type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
2287   {
2288     GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
2289     return results;
2290   }
2291 #endif
2292   msg_ctx->do_forward = GNUNET_YES;
2293   if (datacache != NULL)
2294     results =
2295         GNUNET_DATACACHE_get (datacache, &msg_ctx->key, type,
2296                               &datacache_get_iterator, msg_ctx);
2297 #if DEBUG_DHT
2298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2299               "`%s:%s': Found %d results for `%s' request uid %llu\n",
2300               my_short_id, "DHT", results, "GET", msg_ctx->unique_id);
2301 #endif
2302   if (results >= 1)
2303   {
2304 #if DEBUG_DHT_ROUTING
2305     if ((debug_routes) && (dhtlog_handle != NULL))
2306     {
2307       dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
2308                                    msg_ctx->hop_count, GNUNET_YES, &my_identity,
2309                                    &msg_ctx->key);
2310     }
2311
2312     if ((debug_routes_extended) && (dhtlog_handle != NULL))
2313     {
2314       dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2315                                    msg_ctx->hop_count, GNUNET_YES, &my_identity,
2316                                    &msg_ctx->key, &msg_ctx->peer, NULL);
2317     }
2318 #endif
2319   }
2320   else
2321   {
2322     /* check query valid */
2323     if (GNUNET_BLOCK_EVALUATION_REQUEST_INVALID ==
2324         GNUNET_BLOCK_evaluate (block_context, type, &msg_ctx->key,
2325                                &msg_ctx->reply_bf, msg_ctx->reply_bf_mutator,
2326                                msg_ctx->xquery, msg_ctx->xquery_size, NULL, 0))
2327     {
2328       GNUNET_break_op (0);
2329       msg_ctx->do_forward = GNUNET_NO;
2330     }
2331   }
2332
2333   if (msg_ctx->hop_count == 0)  /* Locally initiated request */
2334   {
2335 #if DEBUG_DHT_ROUTING
2336     if ((debug_routes) && (dhtlog_handle != NULL))
2337     {
2338       dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
2339                                    msg_ctx->hop_count, GNUNET_NO, &my_identity,
2340                                    &msg_ctx->key);
2341     }
2342 #endif
2343   }
2344   if (msg_ctx->do_forward == GNUNET_YES)
2345     route_message (msg, msg_ctx);
2346   GNUNET_CONTAINER_bloomfilter_free (msg_ctx->reply_bf);
2347   return results;
2348 }
2349
2350
2351 static void
2352 remove_recent_find_peer (void *cls,
2353                          const struct GNUNET_SCHEDULER_TaskContext *tc)
2354 {
2355   GNUNET_HashCode *key = cls;
2356
2357   GNUNET_assert (GNUNET_YES ==
2358                  GNUNET_CONTAINER_multihashmap_remove
2359                  (recent_find_peer_requests, key, NULL));
2360   GNUNET_free (key);
2361 }
2362
2363
2364 /**
2365  * Server handler for initiating local dht find peer requests
2366  *
2367  * @param find_msg the actual find peer message
2368  * @param msg_ctx struct containing pertinent information about the request
2369  *
2370  */
2371 static void
2372 handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
2373                       struct DHT_MessageContext *msg_ctx)
2374 {
2375   struct GNUNET_MessageHeader *find_peer_result;
2376   struct GNUNET_DHT_FindPeerMessage *find_peer_message;
2377   struct DHT_MessageContext *new_msg_ctx;
2378   struct GNUNET_CONTAINER_BloomFilter *incoming_bloom;
2379   size_t hello_size;
2380   size_t tsize;
2381   GNUNET_HashCode *recent_hash;
2382   struct GNUNET_MessageHeader *other_hello;
2383   size_t other_hello_size;
2384   struct GNUNET_PeerIdentity peer_id;
2385
2386   find_peer_message = (struct GNUNET_DHT_FindPeerMessage *) find_msg;
2387   GNUNET_break_op (ntohs (find_msg->size) >=
2388                    (sizeof (struct GNUNET_DHT_FindPeerMessage)));
2389   if (ntohs (find_msg->size) < sizeof (struct GNUNET_DHT_FindPeerMessage))
2390     return;
2391   other_hello = NULL;
2392   other_hello_size = 0;
2393   if (ntohs (find_msg->size) > sizeof (struct GNUNET_DHT_FindPeerMessage))
2394   {
2395     other_hello_size =
2396         ntohs (find_msg->size) - sizeof (struct GNUNET_DHT_FindPeerMessage);
2397     other_hello = GNUNET_malloc (other_hello_size);
2398     memcpy (other_hello, &find_peer_message[1], other_hello_size);
2399     if ((GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) other_hello) == 0)
2400         || (GNUNET_OK !=
2401             GNUNET_HELLO_get_id ((struct GNUNET_HELLO_Message *) other_hello,
2402                                  &peer_id)))
2403     {
2404       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2405                   "Received invalid HELLO message in find peer request!\n");
2406       GNUNET_free (other_hello);
2407       return;
2408     }
2409 #if FIND_PEER_WITH_HELLO
2410     if (GNUNET_YES == consider_peer (&peer_id))
2411     {
2412       increment_stats (STAT_HELLOS_PROVIDED);
2413       GNUNET_TRANSPORT_offer_hello (transport_handle, other_hello, NULL, NULL);
2414       GNUNET_CORE_peer_request_connect (coreAPI, &peer_id, NULL, NULL);
2415       route_message (find_msg, msg_ctx);
2416       GNUNET_free (other_hello);
2417       return;
2418     }
2419     else                        /* We don't want this peer! */
2420     {
2421       route_message (find_msg, msg_ctx);
2422       GNUNET_free (other_hello);
2423       return;
2424     }
2425 #endif
2426   }
2427
2428 #if DEBUG_DHT
2429   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2430               "`%s:%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
2431               my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&msg_ctx->key),
2432               ntohs (find_msg->size), sizeof (struct GNUNET_MessageHeader));
2433 #endif
2434   if (my_hello == NULL)
2435   {
2436 #if DEBUG_DHT
2437     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2438                 "`%s': Our HELLO is null, can't return.\n", "DHT");
2439 #endif
2440     GNUNET_free_non_null (other_hello);
2441     route_message (find_msg, msg_ctx);
2442     return;
2443   }
2444
2445   incoming_bloom =
2446       GNUNET_CONTAINER_bloomfilter_init (find_peer_message->bloomfilter,
2447                                          DHT_BLOOM_SIZE, DHT_BLOOM_K);
2448   if (GNUNET_YES ==
2449       GNUNET_CONTAINER_bloomfilter_test (incoming_bloom,
2450                                          &my_identity.hashPubKey))
2451   {
2452     increment_stats (STAT_BLOOM_FIND_PEER);
2453     GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
2454     GNUNET_free_non_null (other_hello);
2455     route_message (find_msg, msg_ctx);
2456     return;                     /* We match the bloomfilter, do not send a response to this peer (they likely already know us!) */
2457   }
2458   GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
2459
2460 #if RESTRICT_FIND_PEER
2461
2462   /**
2463    * Ignore any find peer requests from a peer we have seen very recently.
2464    */
2465   if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (recent_find_peer_requests, &msg_ctx->key))  /* We have recently responded to a find peer request for this peer! */
2466   {
2467     increment_stats ("# dht find peer requests ignored (recently seen!)");
2468     GNUNET_free_non_null (other_hello);
2469     return;
2470   }
2471
2472   /**
2473    * Use this check to only allow the peer to respond to find peer requests if
2474    * it would be beneficial to have the requesting peer in this peers routing
2475    * table.  Can be used to thwart peers flooding the network with find peer
2476    * requests that we don't care about.  However, if a new peer is joining
2477    * the network and has no other peers this is a problem (assume all buckets
2478    * full, no one will respond!).
2479    */
2480   memcpy (&peer_id.hashPubKey, &msg_ctx->key, sizeof (GNUNET_HashCode));
2481   if (GNUNET_NO == consider_peer (&peer_id))
2482   {
2483     increment_stats ("# dht find peer requests ignored (do not need!)");
2484     GNUNET_free_non_null (other_hello);
2485     route_message (find_msg, msg_ctx);
2486     return;
2487   }
2488 #endif
2489
2490   recent_hash = GNUNET_malloc (sizeof (GNUNET_HashCode));
2491   memcpy (recent_hash, &msg_ctx->key, sizeof (GNUNET_HashCode));
2492   if (GNUNET_SYSERR !=
2493       GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests,
2494                                          &msg_ctx->key, NULL,
2495                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2496   {
2497 #if DEBUG_DHT
2498     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499                 "Adding recent remove task for key `%s`!\n",
2500                 GNUNET_h2s (&msg_ctx->key));
2501 #endif
2502     /* Only add a task if there wasn't one for this key already! */
2503     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2504                                   (GNUNET_TIME_UNIT_SECONDS, 30),
2505                                   &remove_recent_find_peer, recent_hash);
2506   }
2507   else
2508   {
2509     GNUNET_free (recent_hash);
2510 #if DEBUG_DHT
2511     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512                 "Received duplicate find peer request too soon!\n");
2513 #endif
2514   }
2515
2516   /* Simplistic find_peer functionality, always return our hello */
2517   hello_size = ntohs (my_hello->size);
2518   tsize = hello_size + sizeof (struct GNUNET_MessageHeader);
2519
2520   if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2521   {
2522     GNUNET_break_op (0);
2523     GNUNET_free_non_null (other_hello);
2524     return;
2525   }
2526
2527   find_peer_result = GNUNET_malloc (tsize);
2528   find_peer_result->type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT);
2529   find_peer_result->size = htons (tsize);
2530   memcpy (&find_peer_result[1], my_hello, hello_size);
2531 #if DEBUG_DHT
2532   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533               "`%s': Sending hello size %d to requesting peer.\n", "DHT",
2534               hello_size);
2535 #endif
2536
2537   new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
2538   memcpy (new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2539   new_msg_ctx->peer = my_identity;
2540   new_msg_ctx->bloom =
2541       GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2542   new_msg_ctx->hop_count = 0;
2543   new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;     /* Make find peer requests a higher priority */
2544   new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
2545   increment_stats (STAT_FIND_PEER_ANSWER);
2546   if (GNUNET_DHT_RO_RECORD_ROUTE ==
2547       (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2548   {
2549     new_msg_ctx->msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2550     new_msg_ctx->path_history_len = msg_ctx->path_history_len;
2551     /* Assign to previous msg_ctx path history, caller should free after our return */
2552     new_msg_ctx->path_history = msg_ctx->path_history;
2553   }
2554   route_result_message (find_peer_result, new_msg_ctx);
2555   GNUNET_free (new_msg_ctx);
2556 #if DEBUG_DHT_ROUTING
2557   if ((debug_routes) && (dhtlog_handle != NULL))
2558   {
2559     dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER,
2560                                  msg_ctx->hop_count, GNUNET_YES, &my_identity,
2561                                  &msg_ctx->key);
2562   }
2563 #endif
2564   GNUNET_free_non_null (other_hello);
2565   GNUNET_free (find_peer_result);
2566   route_message (find_msg, msg_ctx);
2567 }
2568
2569
2570 /**
2571  * Server handler for initiating local dht put requests
2572  *
2573  * @param msg the actual put message
2574  * @param msg_ctx struct containing pertinent information about the request
2575  */
2576 static void
2577 handle_dht_put (const struct GNUNET_MessageHeader *msg,
2578                 struct DHT_MessageContext *msg_ctx)
2579 {
2580   const struct GNUNET_DHT_PutMessage *put_msg;
2581   struct DHTPutEntry *put_entry;
2582   unsigned int put_size;
2583   char *path_offset;
2584   enum GNUNET_BLOCK_Type put_type;
2585   size_t data_size;
2586   int ret;
2587   GNUNET_HashCode key;
2588   struct DHTQueryRecord *record;
2589
2590   GNUNET_assert (ntohs (msg->size) >= sizeof (struct GNUNET_DHT_PutMessage));
2591
2592   put_msg = (const struct GNUNET_DHT_PutMessage *) msg;
2593   put_type = (enum GNUNET_BLOCK_Type) ntohl (put_msg->type);
2594 #if HAVE_MALICIOUS
2595   if (put_type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
2596   {
2597 #if DEBUG_DHT_ROUTING
2598     if ((debug_routes_extended) && (dhtlog_handle != NULL))
2599     {
2600           /** Log routes that die due to high load! */
2601       dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2602                                    msg_ctx->hop_count, GNUNET_SYSERR,
2603                                    &my_identity, &msg_ctx->key, msg_ctx->peer,
2604                                    NULL);
2605     }
2606 #endif
2607     return;
2608   }
2609 #endif
2610   data_size =
2611       ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
2612   ret =
2613       GNUNET_BLOCK_get_key (block_context, put_type, &put_msg[1], data_size,
2614                             &key);
2615   if (GNUNET_NO == ret)
2616   {
2617 #if DEBUG_DHT_ROUTING
2618     if ((debug_routes_extended) && (dhtlog_handle != NULL))
2619     {
2620       dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2621                                    msg_ctx->hop_count, GNUNET_SYSERR,
2622                                    &my_identity, &msg_ctx->key, &msg_ctx->peer,
2623                                    NULL);
2624     }
2625 #endif
2626     /* invalid reply */
2627     GNUNET_break_op (0);
2628     return;
2629   }
2630   if ((GNUNET_YES == ret) &&
2631       (0 != memcmp (&key, &msg_ctx->key, sizeof (GNUNET_HashCode))))
2632   {
2633 #if DEBUG_DHT_ROUTING
2634     if ((debug_routes_extended) && (dhtlog_handle != NULL))
2635     {
2636       dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2637                                    msg_ctx->hop_count, GNUNET_SYSERR,
2638                                    &my_identity, &msg_ctx->key, &msg_ctx->peer,
2639                                    NULL);
2640     }
2641 #endif
2642     /* invalid wrapper: key mismatch! */
2643     GNUNET_break_op (0);
2644     return;
2645   }
2646   /* ret == GNUNET_SYSERR means that there is no known relationship between
2647    * data and the key, so we cannot check it */
2648 #if DEBUG_DHT
2649   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650               "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2651               my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
2652               msg_ctx->unique_id);
2653 #endif
2654 #if DEBUG_DHT_ROUTING
2655   if (msg_ctx->hop_count == 0)  /* Locally initiated request */
2656   {
2657     if ((debug_routes) && (dhtlog_handle != NULL))
2658     {
2659       dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
2660                                    msg_ctx->hop_count, GNUNET_NO, &my_identity,
2661                                    &msg_ctx->key);
2662     }
2663   }
2664 #endif
2665
2666   record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap,
2667                                              &msg_ctx->key);
2668   if (NULL != record)
2669   {
2670     struct DHTRouteSource *pos;
2671     struct GNUNET_DHT_GetResultMessage *get_result;
2672     struct DHT_MessageContext new_msg_ctx;
2673     size_t get_size;
2674
2675     pos = record->head;
2676     while (pos != NULL)
2677     {
2678       /* TODO: do only for local started requests? or also for remote peers? */
2679       /* TODO: include this in statistics? under what? */
2680       /* TODO: reverse order of path_history? */
2681       if (NULL == pos->client)
2682       {
2683         pos = pos->next;
2684         continue;
2685       }
2686
2687       memcpy (&new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2688       if (GNUNET_DHT_RO_RECORD_ROUTE ==
2689           (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
2690       {
2691         new_msg_ctx.msg_options = GNUNET_DHT_RO_RECORD_ROUTE;
2692 #if DEBUG_PATH
2693         for (i = 0; i < new_msg_ctx.path_history_len; i++)
2694         {
2695           path_offset =
2696               &new_msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
2697           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698                       "(put for active get) Key %s Found peer %d:%s\n",
2699                       GNUNET_h2s (&msg_ctx->key), i,
2700                       GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
2701         }
2702 #endif
2703       }
2704
2705       get_size =
2706           sizeof (struct GNUNET_DHT_GetResultMessage) + data_size +
2707           (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2708       get_result = GNUNET_malloc (get_size);
2709       get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
2710       get_result->header.size = htons (get_size);
2711       get_result->expiration = put_msg->expiration;
2712       get_result->type = put_msg->type;
2713       get_result->put_path_length = htons (msg_ctx->path_history_len);
2714
2715       /* Copy the actual data and the path_history to the end of the get result */
2716       memcpy (&get_result[1], &put_msg[1], data_size);
2717       path_offset = (char *) &get_result[1];
2718       path_offset += data_size;
2719       memcpy (path_offset, msg_ctx->path_history,
2720               msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2721       new_msg_ctx.peer = my_identity;
2722       new_msg_ctx.bloom = NULL;
2723       new_msg_ctx.hop_count = 0;
2724       /* Make result routing a higher priority */
2725       new_msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;
2726       new_msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
2727       new_msg_ctx.unique_id = pos->uid;
2728       send_reply_to_client(pos->client, &get_result->header, &new_msg_ctx);
2729       GNUNET_free (get_result);
2730       pos = pos->next;
2731     }
2732   }
2733
2734   if (msg_ctx->closest != GNUNET_YES)
2735   {
2736     route_message (msg, msg_ctx);
2737     return;
2738   }
2739
2740 #if DEBUG_DHT
2741   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2742               "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2743               my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
2744               msg_ctx->unique_id);
2745 #endif
2746
2747 #if DEBUG_DHT_ROUTING
2748   if ((debug_routes_extended) && (dhtlog_handle != NULL))
2749   {
2750     dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2751                                  msg_ctx->hop_count, GNUNET_YES, &my_identity,
2752                                  &msg_ctx->key, &msg_ctx->peer, NULL);
2753   }
2754
2755   if ((debug_routes) && (dhtlog_handle != NULL))
2756   {
2757     dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
2758                                  msg_ctx->hop_count, GNUNET_YES, &my_identity,
2759                                  &msg_ctx->key);
2760   }
2761 #endif
2762
2763   increment_stats (STAT_PUTS_INSERTED);
2764   if (datacache != NULL)
2765   {
2766     /* Put size is actual data size plus struct overhead plus path length (if any) */
2767     put_size =
2768         data_size + sizeof (struct DHTPutEntry) +
2769         (msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2770     put_entry = GNUNET_malloc (put_size);
2771     put_entry->data_size = data_size;
2772     put_entry->path_length = msg_ctx->path_history_len;
2773     /* Copy data to end of put entry */
2774     memcpy (&put_entry[1], &put_msg[1], data_size);
2775     if (msg_ctx->path_history_len > 0)
2776     {
2777       /* Copy path after data */
2778       path_offset = (char *) &put_entry[1];
2779       path_offset += data_size;
2780       memcpy (path_offset, msg_ctx->path_history,
2781               msg_ctx->path_history_len * sizeof (struct GNUNET_PeerIdentity));
2782     }
2783
2784     ret =
2785         GNUNET_DATACACHE_put (datacache, &msg_ctx->key, put_size,
2786                               (const char *) put_entry, put_type,
2787                               GNUNET_TIME_absolute_ntoh (put_msg->expiration));
2788     GNUNET_free (put_entry);
2789   }
2790   else
2791     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2792                 "`%s:%s': %s request received, but have no datacache!\n",
2793                 my_short_id, "DHT", "PUT");
2794
2795   if (stop_on_closest == GNUNET_NO)
2796     route_message (msg, msg_ctx);
2797 }
2798
2799
2800 /**
2801  * To how many peers should we (on average)
2802  * forward the request to obtain the desired
2803  * target_replication count (on average).
2804  *
2805  * returns: target_replication / (est. hops) + (target_replication * hop_count)
2806  * where est. hops is typically 2 * the routing table depth
2807  *
2808  * @param hop_count number of hops the message has traversed
2809  * @param target_replication the number of total paths desired
2810  *
2811  * @return Some number of peers to forward the message to
2812  */
2813 static unsigned int
2814 get_forward_count (unsigned int hop_count, size_t target_replication)
2815 {
2816   uint32_t random_value;
2817   unsigned int forward_count;
2818   float target_value;
2819
2820   /**
2821    * If we are behaving in strict kademlia mode, send multiple initial requests,
2822    * but then only send to 1 or 0 peers based strictly on the number of hops.
2823    */
2824   if (strict_kademlia == GNUNET_YES)
2825   {
2826     if (hop_count == 0)
2827       return kademlia_replication;
2828     if (hop_count < log_of_network_size_estimate * 2.0)
2829       return 1;
2830     return 0;
2831   }
2832
2833   if (hop_count > log_of_network_size_estimate * 2.0)
2834   {
2835     if (GNUNET_YES == paper_forwarding)
2836     {
2837       /* Once we have reached our ideal number of hops, don't stop forwarding! */
2838       return 1;
2839     }
2840 #if DEBUG_DHT
2841     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2842                 "Hop count too high (est %f, lowest %d), NOT Forwarding request\n",
2843                 log_of_network_size_estimate * 2.0, lowest_bucket);
2844 #endif
2845     return 0;
2846   }
2847
2848   if (GNUNET_YES == paper_forwarding)
2849   {
2850     /* FIXME: re-run replication trials with this formula */
2851     target_value =
2852         1 + (target_replication - 1.0) / (log_of_network_size_estimate +
2853                                           ((float) (target_replication - 1.0) *
2854                                            hop_count));
2855     /* Set forward count to floor of target_value */
2856     forward_count = (unsigned int) target_value;
2857     /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
2858     target_value = target_value - forward_count;
2859     random_value =
2860         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
2861
2862     if (random_value < (target_value * UINT32_MAX))
2863       forward_count += 1;
2864   }
2865   else
2866   {
2867     random_value = 0;
2868     forward_count = 1;
2869     target_value =
2870         target_replication / (log_of_network_size_estimate +
2871                               ((float) target_replication * hop_count));
2872     if (target_value > 1)
2873     {
2874       /* Set forward count to floor of target_value */
2875       forward_count = (unsigned int) target_value;
2876       /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
2877       target_value = target_value - forward_count;
2878     }
2879     else
2880       random_value =
2881           GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
2882
2883     if (random_value < (target_value * UINT32_MAX))
2884       forward_count += 1;
2885   }
2886
2887   return forward_count;
2888 }
2889
2890
2891 /**
2892  * Check whether my identity is closer than any known peers.
2893  * If a non-null bloomfilter is given, check if this is the closest
2894  * peer that hasn't already been routed to.
2895  *
2896  * @param target hash code to check closeness to
2897  * @param bloom bloomfilter, exclude these entries from the decision
2898  * @return GNUNET_YES if node location is closest,
2899  *         GNUNET_NO otherwise.
2900  */
2901 static int
2902 am_closest_peer (const GNUNET_HashCode * target,
2903                  struct GNUNET_CONTAINER_BloomFilter *bloom)
2904 {
2905   int bits;
2906   int other_bits;
2907   int bucket_num;
2908   int count;
2909   struct PeerInfo *pos;
2910   unsigned int my_distance;
2911
2912   if (0 == memcmp (&my_identity.hashPubKey, target, sizeof (GNUNET_HashCode)))
2913     return GNUNET_YES;
2914
2915   bucket_num = find_current_bucket (target);
2916
2917   bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, target);
2918   my_distance = distance (&my_identity.hashPubKey, target);
2919   pos = k_buckets[bucket_num].head;
2920   count = 0;
2921   while ((pos != NULL) && (count < bucket_size))
2922   {
2923     if ((bloom != NULL) &&
2924         (GNUNET_YES ==
2925          GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
2926     {
2927       pos = pos->next;
2928       continue;                 /* Skip already checked entries */
2929     }
2930
2931     other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, target);
2932     if (other_bits > bits)
2933       return GNUNET_NO;
2934     else if (other_bits == bits)        /* We match the same number of bits */
2935     {
2936       if (strict_kademlia != GNUNET_YES)        /* Return that we at as close as any other peer */
2937         return GNUNET_YES;
2938       if (distance (&pos->id.hashPubKey, target) < my_distance) /* Check all known peers, only return if we are the true closest */
2939         return GNUNET_NO;
2940     }
2941     pos = pos->next;
2942   }
2943
2944   /* No peers closer, we are the closest! */
2945   return GNUNET_YES;
2946 }
2947
2948
2949 /**
2950  * Select a peer from the routing table that would be a good routing
2951  * destination for sending a message for "target".  The resulting peer
2952  * must not be in the set of blocked peers.<p>
2953  *
2954  * Note that we should not ALWAYS select the closest peer to the
2955  * target, peers further away from the target should be chosen with
2956  * exponentially declining probability.
2957  *
2958  * @param target the key we are selecting a peer to route to
2959  * @param bloom a bloomfilter containing entries this request has seen already
2960  * @param hops how many hops has this message traversed thus far
2961  *
2962  * @return Peer to route to, or NULL on error
2963  */
2964 static struct PeerInfo *
2965 select_peer (const GNUNET_HashCode * target,
2966              struct GNUNET_CONTAINER_BloomFilter *bloom, unsigned int hops)
2967 {
2968   unsigned int bc;
2969   unsigned int count;
2970   unsigned int selected;
2971   struct PeerInfo *pos;
2972   unsigned int distance;
2973   unsigned int largest_distance;
2974   struct PeerInfo *chosen;
2975
2976   /** If we are doing kademlia routing (saves some cycles) */
2977   if ((strict_kademlia == GNUNET_YES) || (hops >= log_of_network_size_estimate))
2978   {
2979     /* greedy selection (closest peer that is not in bloomfilter) */
2980     largest_distance = 0;
2981     chosen = NULL;
2982     for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
2983     {
2984       pos = k_buckets[bc].head;
2985       count = 0;
2986       while ((pos != NULL) && (count < bucket_size))
2987       {
2988         /* If we are doing strict Kademlia routing, then checking the bloomfilter is basically cheating! */
2989         if (GNUNET_NO ==
2990             GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
2991         {
2992           distance = inverse_distance (target, &pos->id.hashPubKey);
2993           if (distance > largest_distance)
2994           {
2995             chosen = pos;
2996             largest_distance = distance;
2997           }
2998         }
2999         count++;
3000         pos = pos->next;
3001       }
3002     }
3003     if ((largest_distance > 0) && (chosen != NULL))
3004     {
3005       GNUNET_CONTAINER_bloomfilter_add (bloom, &chosen->id.hashPubKey);
3006       return chosen;
3007     }
3008     return NULL;                /* no peer available or we are the closest */
3009   }
3010
3011
3012   /* select "random" peer */
3013   /* count number of peers that are available and not filtered */
3014   count = 0;
3015   for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
3016   {
3017     pos = k_buckets[bc].head;
3018     while ((pos != NULL) && (count < bucket_size))
3019     {
3020       if (GNUNET_YES ==
3021           GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3022       {
3023         pos = pos->next;
3024         continue;               /* Ignore bloomfiltered peers */
3025       }
3026       count++;
3027       pos = pos->next;
3028     }
3029   }
3030   if (count == 0)               /* No peers to select from! */
3031   {
3032     increment_stats ("# failed to select peer");
3033     return NULL;
3034   }
3035   /* Now actually choose a peer */
3036   selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
3037   count = 0;
3038   for (bc = lowest_bucket; bc < MAX_BUCKETS; bc++)
3039   {
3040     pos = k_buckets[bc].head;
3041     while ((pos != NULL) && (count < bucket_size))
3042     {
3043       if (GNUNET_YES ==
3044           GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3045       {
3046         pos = pos->next;
3047         continue;               /* Ignore bloomfiltered peers */
3048       }
3049       if (0 == selected--)
3050         return pos;
3051       pos = pos->next;
3052     }
3053   }
3054   GNUNET_break (0);
3055   return NULL;
3056 }
3057
3058
3059 /**
3060  * Task used to remove recent entries, either
3061  * after timeout, when full, or on shutdown.
3062  *
3063  * @param cls the entry to remove
3064  * @param tc context, reason, etc.
3065  */
3066 static void
3067 remove_recent (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3068 {
3069   struct RecentRequest *req = cls;
3070   static GNUNET_HashCode hash;
3071
3072   GNUNET_assert (req != NULL);
3073   hash_from_uid (req->uid, &hash);
3074   GNUNET_assert (GNUNET_YES ==
3075                  GNUNET_CONTAINER_multihashmap_remove (recent.hashmap, &hash,
3076                                                        req));
3077   GNUNET_CONTAINER_heap_remove_node (req->heap_node);
3078   GNUNET_CONTAINER_bloomfilter_free (req->bloom);
3079   GNUNET_free (req);
3080
3081   /*
3082    * if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) && (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)) && (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap)))
3083    * {
3084    * GNUNET_CONTAINER_multihashmap_destroy(recent.hashmap);
3085    * GNUNET_CONTAINER_heap_destroy(recent.minHeap);
3086    * }
3087    */
3088 }
3089
3090 /**
3091  * Remember this routing request so that if a reply is
3092  * received we can either forward it to the correct peer
3093  * or return the result locally.
3094  *
3095  * @param msg_ctx Context of the route request
3096  *
3097  * @return GNUNET_YES if this response was cached, GNUNET_NO if not
3098  */
3099 static int
3100 cache_response (struct DHT_MessageContext *msg_ctx)
3101 {
3102   struct DHTQueryRecord *record;
3103   struct DHTRouteSource *source_info;
3104   struct DHTRouteSource *pos;
3105   struct GNUNET_TIME_Absolute now;
3106   unsigned int current_size;
3107
3108   current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
3109
3110 #if DELETE_WHEN_FULL
3111   while (current_size >= MAX_OUTSTANDING_FORWARDS)
3112   {
3113     source_info = GNUNET_CONTAINER_heap_remove_root (forward_list.minHeap);
3114     GNUNET_assert (source_info != NULL);
3115     record = source_info->record;
3116     GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
3117     if (record->head == NULL)   /* No more entries in DLL */
3118     {
3119       GNUNET_assert (GNUNET_YES ==
3120                      GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
3121                                                            &record->key,
3122                                                            record));
3123       GNUNET_free (record);
3124     }
3125     if (source_info->delete_task != GNUNET_SCHEDULER_NO_TASK)
3126     {
3127       GNUNET_SCHEDULER_cancel (source_info->delete_task);
3128       source_info->delete_task = GNUNET_SCHEDULER_NO_TASK;
3129     }
3130     if (source_info->find_peers_responded != NULL)
3131       GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
3132     GNUNET_free (source_info);
3133     current_size = GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
3134   }
3135 #endif
3136   /** Non-local request and have too many outstanding forwards, discard! */
3137   if ((current_size >= MAX_OUTSTANDING_FORWARDS) && (msg_ctx->client == NULL))
3138     return GNUNET_NO;
3139
3140   now = GNUNET_TIME_absolute_get ();
3141   record =
3142       GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
3143   if (record != NULL)           /* Already know this request! */
3144   {
3145     pos = record->head;
3146     while (pos != NULL)
3147     {
3148       if (0 ==
3149           memcmp (&msg_ctx->peer, &pos->source,
3150                   sizeof (struct GNUNET_PeerIdentity)))
3151         break;                  /* Already have this peer in reply list! */
3152       pos = pos->next;
3153     }
3154     if ((pos != NULL) && (pos->client == msg_ctx->client))      /* Seen this already */
3155     {
3156       GNUNET_CONTAINER_heap_update_cost (forward_list.minHeap, pos->hnode,
3157                                          now.abs_value);
3158       return GNUNET_NO;
3159     }
3160   }
3161   else
3162   {
3163     record = GNUNET_malloc (sizeof (struct DHTQueryRecord));
3164     GNUNET_assert (GNUNET_OK ==
3165                    GNUNET_CONTAINER_multihashmap_put (forward_list.hashmap,
3166                                                       &msg_ctx->key, record,
3167                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3168     memcpy (&record->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
3169   }
3170
3171   source_info = GNUNET_malloc (sizeof (struct DHTRouteSource));
3172   source_info->record = record;
3173   source_info->delete_task =
3174       GNUNET_SCHEDULER_add_delayed (DHT_FORWARD_TIMEOUT, &remove_forward_entry,
3175                                     source_info);
3176   source_info->find_peers_responded =
3177       GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3178   source_info->source = msg_ctx->peer;
3179   GNUNET_CONTAINER_DLL_insert_after (record->head, record->tail, record->tail,
3180                                      source_info);
3181   if (msg_ctx->client != NULL)  /* For local request, set timeout so high it effectively never gets pushed out */
3182   {
3183     source_info->client = msg_ctx->client;
3184     now = GNUNET_TIME_absolute_get_forever ();
3185   }
3186   source_info->hnode =
3187       GNUNET_CONTAINER_heap_insert (forward_list.minHeap, source_info,
3188                                     now.abs_value);
3189   source_info->uid = msg_ctx->unique_id;
3190 #if DEBUG_DHT > 1
3191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192               "`%s:%s': Created new forward source info for %s uid %llu\n",
3193               my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
3194               msg_ctx->unique_id);
3195 #endif
3196   return GNUNET_YES;
3197 }
3198
3199
3200 /**
3201  * Main function that handles whether or not to route a message to other
3202  * peers.
3203  *
3204  * @param msg the message to be routed
3205  * @param msg_ctx the context containing all pertinent information about the message
3206  */
3207 static void
3208 route_message (const struct GNUNET_MessageHeader *msg,
3209                struct DHT_MessageContext *msg_ctx)
3210 {
3211   int i;
3212   struct PeerInfo *selected;
3213
3214 #if DEBUG_DHT_ROUTING > 1
3215   struct PeerInfo *nearest;
3216 #endif
3217   unsigned int target_forward_count;
3218   unsigned int forward_count;
3219   struct RecentRequest *recent_req;
3220   GNUNET_HashCode unique_hash;
3221   char *stat_forward_count;
3222   char *temp_stat_str;
3223
3224 #if DEBUG_DHT_ROUTING
3225   int ret;
3226 #endif
3227
3228   if (malicious_dropper == GNUNET_YES)
3229   {
3230 #if DEBUG_DHT_ROUTING
3231     if ((debug_routes_extended) && (dhtlog_handle != NULL))
3232     {
3233       dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3234                                    msg_ctx->hop_count, GNUNET_SYSERR,
3235                                    &my_identity, &msg_ctx->key, &msg_ctx->peer,
3236                                    NULL);
3237     }
3238 #endif
3239     if (msg_ctx->bloom != NULL)
3240     {
3241       GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
3242       msg_ctx->bloom = NULL;
3243     }
3244     return;
3245   }
3246
3247   increment_stats (STAT_ROUTES);
3248   target_forward_count =
3249       get_forward_count (msg_ctx->hop_count, msg_ctx->replication);
3250   GNUNET_asprintf (&stat_forward_count, "# forward counts of %d",
3251                    target_forward_count);
3252   increment_stats (stat_forward_count);
3253   GNUNET_free (stat_forward_count);
3254   if (msg_ctx->bloom == NULL)
3255     msg_ctx->bloom =
3256         GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3257
3258   if ((stop_on_closest == GNUNET_YES) && (msg_ctx->closest == GNUNET_YES) &&
3259       (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
3260     target_forward_count = 0;
3261
3262   /**
3263    * NOTICE:  In Kademlia, a find peer request goes no further if the peer doesn't return
3264    * any closer peers (which is being checked for below).  Since we are doing recursive
3265    * routing we have no choice but to stop forwarding in this case.  This means that at
3266    * any given step the request may NOT be forwarded to alpha peers (because routes will
3267    * stop and the parallel route will not be aware of it).  Of course, assuming that we
3268    * have fulfilled the Kademlia requirements for routing table fullness this will never
3269    * ever ever be a problem.
3270    *
3271    * However, is this fair?
3272    *
3273    * Since we use these requests to build our routing tables (and we build them in the
3274    * testing driver) we will ignore this restriction for FIND_PEER messages so that
3275    * routing tables still get constructed.
3276    */
3277   if ((GNUNET_YES == strict_kademlia) && (msg_ctx->closest == GNUNET_YES) &&
3278       (msg_ctx->hop_count > 0) &&
3279       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER))
3280     target_forward_count = 0;
3281
3282
3283   GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom, &my_identity.hashPubKey);
3284   hash_from_uid (msg_ctx->unique_id, &unique_hash);
3285   if (GNUNET_YES ==
3286       GNUNET_CONTAINER_multihashmap_contains (recent.hashmap, &unique_hash))
3287   {
3288     recent_req =
3289         GNUNET_CONTAINER_multihashmap_get (recent.hashmap, &unique_hash);
3290     GNUNET_assert (recent_req != NULL);
3291     if (0 != memcmp (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode)))
3292       increment_stats (STAT_DUPLICATE_UID);
3293     else
3294     {
3295       increment_stats (STAT_RECENT_SEEN);
3296       GNUNET_CONTAINER_bloomfilter_or2 (msg_ctx->bloom, recent_req->bloom,
3297                                         DHT_BLOOM_SIZE);
3298     }
3299   }
3300   else
3301   {
3302     recent_req = GNUNET_malloc (sizeof (struct RecentRequest));
3303     recent_req->uid = msg_ctx->unique_id;
3304     memcpy (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
3305     recent_req->remove_task =
3306         GNUNET_SCHEDULER_add_delayed (DEFAULT_RECENT_REMOVAL, &remove_recent,
3307                                       recent_req);
3308     recent_req->heap_node =
3309         GNUNET_CONTAINER_heap_insert (recent.minHeap, recent_req,
3310                                       GNUNET_TIME_absolute_get ().abs_value);
3311     recent_req->bloom =
3312         GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3313     GNUNET_CONTAINER_multihashmap_put (recent.hashmap, &unique_hash, recent_req,
3314                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3315   }
3316
3317   if (GNUNET_CONTAINER_multihashmap_size (recent.hashmap) > DHT_MAX_RECENT)
3318   {
3319     recent_req = GNUNET_CONTAINER_heap_peek (recent.minHeap);
3320     GNUNET_assert (recent_req != NULL);
3321     GNUNET_SCHEDULER_cancel (recent_req->remove_task);
3322     recent_req->remove_task =
3323         GNUNET_SCHEDULER_add_now (&remove_recent, recent_req);
3324   }
3325
3326   forward_count = 0;
3327   for (i = 0; i < target_forward_count; i++)
3328   {
3329     selected = select_peer (&msg_ctx->key, msg_ctx->bloom, msg_ctx->hop_count);
3330
3331     if (selected != NULL)
3332     {
3333       forward_count++;
3334       if (GNUNET_CRYPTO_hash_matching_bits
3335           (&selected->id.hashPubKey,
3336            &msg_ctx->key) >=
3337           GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
3338                                             &msg_ctx->key))
3339         GNUNET_asprintf (&temp_stat_str,
3340                          "# requests routed to close(r) peer hop %u",
3341                          msg_ctx->hop_count);
3342       else
3343         GNUNET_asprintf (&temp_stat_str,
3344                          "# requests routed to less close peer hop %u",
3345                          msg_ctx->hop_count);
3346       if (temp_stat_str != NULL)
3347       {
3348         increment_stats (temp_stat_str);
3349         GNUNET_free (temp_stat_str);
3350       }
3351       GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
3352                                         &selected->id.hashPubKey);
3353 #if DEBUG_DHT_ROUTING > 1
3354       nearest = find_closest_peer (&msg_ctx->key);
3355       nearest_buf = GNUNET_strdup (GNUNET_i2s (&nearest->id));
3356       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3357                   "`%s:%s': Forwarding request key %s uid %llu to peer %s (closest %s, bits %d, distance %u)\n",
3358                   my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
3359                   msg_ctx->unique_id, GNUNET_i2s (&selected->id), nearest_buf,
3360                   GNUNET_CRYPTO_hash_matching_bits (&nearest->id.hashPubKey,
3361                                                     msg_ctx->key),
3362                   distance (&nearest->id.hashPubKey, msg_ctx->key));
3363       GNUNET_free (nearest_buf);
3364 #endif
3365 #if DEBUG_DHT_ROUTING
3366       if ((debug_routes_extended) && (dhtlog_handle != NULL))
3367       {
3368         dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3369                                      msg_ctx->hop_count, GNUNET_NO,
3370                                      &my_identity, &msg_ctx->key, &msg_ctx->peer,
3371                                      &selected->id);
3372       }
3373 #endif
3374       forward_message (msg, selected, msg_ctx);
3375     }
3376   }
3377
3378   if (msg_ctx->bloom != NULL)
3379   {
3380     GNUNET_CONTAINER_bloomfilter_or2 (recent_req->bloom, msg_ctx->bloom,
3381                                       DHT_BLOOM_SIZE);
3382     GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
3383     msg_ctx->bloom = NULL;
3384   }
3385
3386 #if DEBUG_DHT_ROUTING
3387   if (forward_count == 0)
3388     ret = GNUNET_SYSERR;
3389   else
3390     ret = GNUNET_NO;
3391
3392   if ((debug_routes_extended) && (dhtlog_handle != NULL))
3393   {
3394     dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3395                                  msg_ctx->hop_count, ret, &my_identity,
3396                                  &msg_ctx->key, &msg_ctx->peer, NULL);
3397   }
3398 #endif
3399 }
3400
3401
3402 /**
3403  * Main function that handles whether or not to route a message to other
3404  * peers.
3405  *
3406  * @param msg the message to be routed
3407  * @param msg_ctx the context containing all pertinent information about the message
3408  */
3409 static void
3410 demultiplex_message (const struct GNUNET_MessageHeader *msg,
3411                      struct DHT_MessageContext *msg_ctx)
3412 {
3413   /* FIXME: Should we use closest excluding those we won't route to (the bloomfilter problem)? */
3414   msg_ctx->closest = am_closest_peer (&msg_ctx->key, msg_ctx->bloom);
3415
3416   switch (ntohs (msg->type))
3417   {
3418   case GNUNET_MESSAGE_TYPE_DHT_GET:    /* Add to hashmap of requests seen, search for data (always) */
3419     cache_response (msg_ctx);
3420     handle_dht_get (msg, msg_ctx);
3421     break;
3422   case GNUNET_MESSAGE_TYPE_DHT_PUT:    /* Check if closest, if so insert data. */
3423     increment_stats (STAT_PUTS);
3424     handle_dht_put (msg, msg_ctx);
3425     break;
3426   case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:      /* Check if closest and not started by us, check options, add to requests seen */
3427     increment_stats (STAT_FIND_PEER);
3428     if (((msg_ctx->hop_count > 0) &&
3429          (0 !=
3430           memcmp (&msg_ctx->peer, &my_identity,
3431                   sizeof (struct GNUNET_PeerIdentity)))) ||
3432         (msg_ctx->client != NULL))
3433     {
3434       cache_response (msg_ctx);
3435       if ((msg_ctx->closest == GNUNET_YES) ||
3436           (msg_ctx->msg_options == GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE))
3437         handle_dht_find_peer (msg, msg_ctx);
3438     }
3439     else
3440       route_message (msg, msg_ctx);
3441 #if DEBUG_DHT_ROUTING
3442     if (msg_ctx->hop_count == 0)        /* Locally initiated request */
3443     {
3444       if ((debug_routes) && (dhtlog_handle != NULL))
3445       {
3446         dhtlog_handle->insert_dhtkey (NULL, &msg_ctx->key);
3447         dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER,
3448                                      msg_ctx->hop_count, GNUNET_NO,
3449                                      &my_identity, &msg_ctx->key);
3450       }
3451     }
3452 #endif
3453     break;
3454   default:
3455     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3456                 "`%s': Message type (%d) not handled, forwarding anyway!\n",
3457                 "DHT", ntohs (msg->type));
3458     route_message (msg, msg_ctx);
3459   }
3460 }
3461
3462
3463 /**
3464  * Iterator over hash map entries.
3465  *
3466  * @param cls client to search for in source routes
3467  * @param key current key code (ignored)
3468  * @param value value in the hash map, a DHTQueryRecord
3469  * @return GNUNET_YES if we should continue to
3470  *         iterate,
3471  *         GNUNET_NO if not.
3472  */
3473 static int
3474 find_client_records (void *cls, const GNUNET_HashCode * key, void *value)
3475 {
3476   struct ClientList *client = cls;
3477   struct DHTQueryRecord *record = value;
3478   struct DHTRouteSource *pos;
3479
3480   pos = record->head;
3481   while (pos != NULL)
3482   {
3483     if (pos->client == client)
3484       break;
3485     pos = pos->next;
3486   }
3487   if (pos != NULL)
3488   {
3489     GNUNET_CONTAINER_DLL_remove (record->head, record->tail, pos);
3490     GNUNET_CONTAINER_heap_remove_node (pos->hnode);
3491     if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
3492     {
3493       GNUNET_SCHEDULER_cancel (pos->delete_task);
3494       pos->delete_task = GNUNET_SCHEDULER_NO_TASK;
3495     }
3496     if (pos->find_peers_responded != NULL)
3497       GNUNET_CONTAINER_bloomfilter_free (pos->find_peers_responded);
3498     GNUNET_free (pos);
3499   }
3500   if (record->head == NULL)     /* No more entries in DLL */
3501   {
3502     GNUNET_assert (GNUNET_YES ==
3503                    GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap,
3504                                                          &record->key, record));
3505     GNUNET_free (record);
3506   }
3507   return GNUNET_YES;
3508 }
3509
3510 /**
3511  * Functions with this signature are called whenever a client
3512  * is disconnected on the network level.
3513  *
3514  * @param cls closure (NULL for dht)
3515  * @param client identification of the client; NULL
3516  *        for the last call when the server is destroyed
3517  */
3518 static void
3519 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3520 {
3521   struct ClientList *pos = client_list;
3522   struct ClientList *prev;
3523   struct ClientList *found;
3524   struct PendingMessage *reply;
3525
3526   prev = NULL;
3527   found = NULL;
3528   while (pos != NULL)
3529   {
3530     if (pos->client_handle == client)
3531     {
3532       if (prev != NULL)
3533         prev->next = pos->next;
3534       else
3535         client_list = pos->next;
3536       found = pos;
3537       break;
3538     }
3539     prev = pos;
3540     pos = pos->next;
3541   }
3542
3543   if (found != NULL)
3544   {
3545     if (found->transmit_handle != NULL)
3546       GNUNET_CONNECTION_notify_transmit_ready_cancel (found->transmit_handle);
3547
3548     while (NULL != (reply = found->pending_head))
3549     {
3550       GNUNET_CONTAINER_DLL_remove (found->pending_head, found->pending_tail,
3551                                    reply);
3552       GNUNET_free (reply);
3553     }
3554     GNUNET_CONTAINER_multihashmap_iterate (forward_list.hashmap,
3555                                            &find_client_records, found);
3556     GNUNET_free (found);
3557   }
3558 }
3559
3560 /**
3561  * Find a client if it exists, add it otherwise.
3562  *
3563  * @param client the server handle to the client
3564  *
3565  * @return the client if found, a new client otherwise
3566  */
3567 static struct ClientList *
3568 find_active_client (struct GNUNET_SERVER_Client *client)
3569 {
3570   struct ClientList *pos = client_list;
3571   struct ClientList *ret;
3572
3573   while (pos != NULL)
3574   {
3575     if (pos->client_handle == client)
3576       return pos;
3577     pos = pos->next;
3578   }
3579
3580   ret = GNUNET_malloc (sizeof (struct ClientList));
3581   ret->client_handle = client;
3582   ret->next = client_list;
3583   client_list = ret;
3584
3585   return ret;
3586 }
3587
3588 #if HAVE_MALICIOUS
3589 /**
3590  * Task to send a malicious put message across the network.
3591  *
3592  * @param cls closure for this task
3593  * @param tc the context under which the task is running
3594  */
3595 static void
3596 malicious_put_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3597 {
3598   static struct GNUNET_DHT_PutMessage put_message;
3599   static struct DHT_MessageContext msg_ctx;
3600   static GNUNET_HashCode key;
3601   uint32_t random_key;
3602
3603   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3604     return;
3605   put_message.header.size = htons (sizeof (struct GNUNET_DHT_PutMessage));
3606   put_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
3607   put_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
3608   put_message.expiration =
3609       GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_forever ());
3610   memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3611   random_key =
3612       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
3613   GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
3614   memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
3615   msg_ctx.unique_id =
3616       GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3617                      (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
3618   msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3619   msg_ctx.msg_options = ntohl (0);
3620   msg_ctx.network_size = log_of_network_size_estimate;
3621   msg_ctx.peer = &my_identity;
3622   msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
3623   msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3624 #if DEBUG_DHT_ROUTING
3625   if (dhtlog_handle != NULL)
3626     dhtlog_handle->insert_dhtkey (NULL, &key);
3627 #endif
3628   increment_stats (STAT_PUT_START);
3629   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3630               "%s:%s Sending malicious PUT message with hash %s\n", my_short_id,
3631               "DHT", GNUNET_h2s (&key));
3632   demultiplex_message (&put_message.header, &msg_ctx);
3633   GNUNET_SCHEDULER_add_delayed (malicious_put_frequency, &malicious_put_task,
3634                                 NULL);
3635 }
3636
3637
3638 /**
3639  * Task to send a malicious put message across the network.
3640  *
3641  * @param cls closure for this task
3642  * @param tc the context under which the task is running
3643  */
3644 static void
3645 malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3646 {
3647   static struct GNUNET_DHT_GetMessage get_message;
3648   struct DHT_MessageContext msg_ctx;
3649   static GNUNET_HashCode key;
3650   uint32_t random_key;
3651
3652   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3653     return;
3654
3655   get_message.header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
3656   get_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
3657   get_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
3658   memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3659   random_key =
3660       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
3661   GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
3662   memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
3663   msg_ctx.unique_id =
3664       GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3665                      (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX));
3666   msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3667   msg_ctx.msg_options = ntohl (0);
3668   msg_ctx.network_size = log_of_network_size_estimate;
3669   msg_ctx.peer = &my_identity;
3670   msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
3671   msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3672 #if DEBUG_DHT_ROUTING
3673   if (dhtlog_handle != NULL)
3674     dhtlog_handle->insert_dhtkey (NULL, &key);
3675 #endif
3676   increment_stats (STAT_GET_START);
3677   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3678               "%s:%s Sending malicious GET message with hash %s\n", my_short_id,
3679               "DHT", GNUNET_h2s (&key));
3680   demultiplex_message (&get_message.header, &msg_ctx);
3681   GNUNET_SCHEDULER_add_delayed (malicious_get_frequency, &malicious_get_task,
3682                                 NULL);
3683 }
3684 #endif
3685
3686
3687 /**
3688  * Iterator over hash map entries.
3689  *
3690  * @param cls closure
3691  * @param key current key code
3692  * @param value value in the hash map
3693  * @return GNUNET_YES if we should continue to
3694  *         iterate,
3695  *         GNUNET_NO if not.
3696  */
3697 static int
3698 add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
3699 {
3700   struct GNUNET_CONTAINER_BloomFilter *bloom = cls;
3701
3702   GNUNET_CONTAINER_bloomfilter_add (bloom, key);
3703   return GNUNET_YES;
3704 }
3705
3706 /**
3707  * Task to send a find peer message for our own peer identifier
3708  * so that we can find the closest peers in the network to ourselves
3709  * and attempt to connect to them.
3710  *
3711  * @param cls closure for this task
3712  * @param tc the context under which the task is running
3713  */
3714 static void
3715 send_find_peer_message (void *cls,
3716                         const struct GNUNET_SCHEDULER_TaskContext *tc)
3717 {
3718   struct GNUNET_DHT_FindPeerMessage *find_peer_msg;
3719   struct DHT_MessageContext msg_ctx;
3720   struct GNUNET_TIME_Relative next_send_time;
3721   struct GNUNET_CONTAINER_BloomFilter *temp_bloom;
3722
3723   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3724     return;
3725
3726   if ((newly_found_peers > bucket_size) && (GNUNET_YES == do_find_peer))        /* If we are finding peers already, no need to send out our request right now! */
3727   {
3728     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3729                 "Have %d newly found peers since last find peer message sent!\n",
3730                 newly_found_peers);
3731     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
3732                                   &send_find_peer_message, NULL);
3733     newly_found_peers = 0;
3734     return;
3735   }
3736
3737   increment_stats (STAT_FIND_PEER_START);
3738 #if FIND_PEER_WITH_HELLO
3739   find_peer_msg =
3740       GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage) +
3741                      GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *)
3742                                         my_hello));
3743   find_peer_msg->header.size =
3744       htons (sizeof (struct GNUNET_DHT_FindPeerMessage) +
3745              GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
3746   memcpy (&find_peer_msg[1], my_hello,
3747           GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
3748 #else
3749   find_peer_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage));
3750   find_peer_msg->header.size =
3751       htons (sizeof (struct GNUNET_DHT_FindPeerMessage));
3752 #endif
3753   find_peer_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
3754   temp_bloom =
3755       GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3756   GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, &add_known_to_bloom,
3757                                          temp_bloom);
3758   GNUNET_assert (GNUNET_OK ==
3759                  GNUNET_CONTAINER_bloomfilter_get_raw_data (temp_bloom,
3760                                                             find_peer_msg->
3761                                                             bloomfilter,
3762                                                             DHT_BLOOM_SIZE));
3763   GNUNET_CONTAINER_bloomfilter_free (temp_bloom);
3764   memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3765   memcpy (&msg_ctx.key, &my_identity.hashPubKey, sizeof (GNUNET_HashCode));
3766   msg_ctx.unique_id =
3767       GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3768                      (GNUNET_CRYPTO_QUALITY_STRONG, UINT64_MAX));
3769   msg_ctx.replication = DHT_DEFAULT_FIND_PEER_REPLICATION;
3770   msg_ctx.msg_options = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
3771   msg_ctx.network_size = log_of_network_size_estimate;
3772   msg_ctx.peer = my_identity;
3773   msg_ctx.importance = DHT_DEFAULT_FIND_PEER_IMPORTANCE;
3774   msg_ctx.timeout = DHT_DEFAULT_FIND_PEER_TIMEOUT;
3775
3776   demultiplex_message (&find_peer_msg->header, &msg_ctx);
3777   GNUNET_free (find_peer_msg);
3778   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3779               "`%s:%s': Sent `%s' request to some (?) peers\n", my_short_id,
3780               "DHT", "FIND PEER");
3781   if (newly_found_peers < bucket_size)
3782   {
3783     next_send_time.rel_value =
3784         (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
3785         GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
3786                                   DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
3787   }
3788   else
3789   {
3790     next_send_time.rel_value =
3791         DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
3792         GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
3793                                   DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value -
3794                                   DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
3795   }
3796
3797   GNUNET_assert (next_send_time.rel_value != 0);
3798   find_peer_context.count = 0;
3799   newly_found_peers = 0;
3800   find_peer_context.start = GNUNET_TIME_absolute_get ();
3801   if (GNUNET_YES == do_find_peer)
3802   {
3803     GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
3804                                   NULL);
3805   }
3806 }
3807
3808 /**
3809  * Handler for any generic DHT messages, calls the appropriate handler
3810  * depending on message type, sends confirmation if responses aren't otherwise
3811  * expected.
3812  *
3813  * @param cls closure for the service
3814  * @param client the client we received this message from
3815  * @param message the actual message received
3816  */
3817 static void
3818 handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client,
3819                                 const struct GNUNET_MessageHeader *message)
3820 {
3821   const struct GNUNET_DHT_RouteMessage *dht_msg =
3822       (const struct GNUNET_DHT_RouteMessage *) message;
3823   const struct GNUNET_MessageHeader *enc_msg;
3824   struct DHT_MessageContext msg_ctx;
3825
3826   enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
3827 #if DEBUG_DHT
3828   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3829               "`%s:%s': Received `%s' request from client, message type %d, key %s, uid %llu\n",
3830               my_short_id, "DHT", "GENERIC", ntohs (message->type),
3831               GNUNET_h2s (&dht_msg->key), GNUNET_ntohll (dht_msg->unique_id));
3832 #endif
3833 #if DEBUG_DHT_ROUTING
3834   if (dhtlog_handle != NULL)
3835     dhtlog_handle->insert_dhtkey (NULL, &dht_msg->key);
3836 #endif
3837
3838   memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3839   msg_ctx.client = find_active_client (client);
3840   memcpy (&msg_ctx.key, &dht_msg->key, sizeof (GNUNET_HashCode));
3841   msg_ctx.unique_id = GNUNET_ntohll (dht_msg->unique_id);
3842   msg_ctx.replication = ntohl (dht_msg->desired_replication_level);
3843   msg_ctx.msg_options = ntohl (dht_msg->options);
3844   if (GNUNET_DHT_RO_RECORD_ROUTE ==
3845       (msg_ctx.msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
3846   {
3847     msg_ctx.path_history = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
3848     memcpy (msg_ctx.path_history, &my_identity,
3849             sizeof (struct GNUNET_PeerIdentity));
3850     msg_ctx.path_history_len = 1;
3851   }
3852   msg_ctx.network_size = log_of_network_size_estimate;
3853   msg_ctx.peer = my_identity;
3854   msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 4;  /* Make local routing a higher priority */
3855   msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3856
3857   if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
3858     increment_stats (STAT_GET_START);
3859   else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
3860     increment_stats (STAT_PUT_START);
3861   else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER)
3862     increment_stats (STAT_FIND_PEER_START);
3863
3864   if (GNUNET_YES == malicious_dropper)
3865   {
3866     if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
3867     {
3868 #if DEBUG_DHT_ROUTING
3869       if ((debug_routes) && (dhtlog_handle != NULL))
3870       {
3871         dhtlog_handle->insert_query (NULL, msg_ctx.unique_id, DHTLOG_GET,
3872                                      msg_ctx.hop_count, GNUNET_NO, &my_identity,
3873                                      &msg_ctx.key);
3874       }
3875 #endif
3876     }
3877     else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
3878     {
3879 #if DEBUG_DHT_ROUTING
3880       if ((debug_routes) && (dhtlog_handle != NULL))
3881       {
3882         dhtlog_handle->insert_query (NULL, msg_ctx.unique_id, DHTLOG_PUT,
3883                                      msg_ctx.hop_count, GNUNET_NO, &my_identity,
3884                                      &msg_ctx.key);
3885       }
3886 #endif
3887     }
3888     GNUNET_SERVER_receive_done (client, GNUNET_OK);
3889     GNUNET_free_non_null (msg_ctx.path_history);
3890     return;
3891   }
3892
3893   demultiplex_message (enc_msg, &msg_ctx);
3894   GNUNET_SERVER_receive_done (client, GNUNET_OK);
3895
3896 }
3897
3898 /**
3899  * Handler for any locally received DHT control messages,
3900  * sets malicious flags mostly for now.
3901  *
3902  * @param cls closure for the service
3903  * @param client the client we received this message from
3904  * @param message the actual message received
3905  *
3906  */
3907 static void
3908 handle_dht_control_message (void *cls, struct GNUNET_SERVER_Client *client,
3909                             const struct GNUNET_MessageHeader *message)
3910 {
3911   const struct GNUNET_DHT_ControlMessage *dht_control_msg =
3912       (const struct GNUNET_DHT_ControlMessage *) message;
3913
3914 #if DEBUG_DHT
3915   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3916               "`%s:%s': Received `%s' request from client, command %d\n",
3917               my_short_id, "DHT", "CONTROL", ntohs (dht_control_msg->command));
3918 #endif
3919
3920   switch (ntohs (dht_control_msg->command))
3921   {
3922   case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
3923     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3924                 "Sending self seeking find peer request!\n");
3925     GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
3926     break;
3927 #if HAVE_MALICIOUS
3928   case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
3929     if (ntohs (dht_control_msg->variable) > 0)
3930       malicious_get_frequency.rel_value = ntohs (dht_control_msg->variable);
3931     if (malicious_get_frequency.rel_value == 0)
3932       malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
3933     if (malicious_getter != GNUNET_YES)
3934       GNUNET_SCHEDULER_add_now (&malicious_get_task, NULL);
3935     malicious_getter = GNUNET_YES;
3936     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3937                 "%s:%s Initiating malicious GET behavior, frequency %llu\n",
3938                 my_short_id, "DHT", malicious_get_frequency.rel_value);
3939     break;
3940   case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
3941     if (ntohs (dht_control_msg->variable) > 0)
3942       malicious_put_frequency.rel_value = ntohs (dht_control_msg->variable);
3943     if (malicious_put_frequency.rel_value == 0)
3944       malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
3945     if (malicious_putter != GNUNET_YES)
3946       GNUNET_SCHEDULER_add_now (&malicious_put_task, NULL);
3947     malicious_putter = GNUNET_YES;
3948     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3949                 "%s:%s Initiating malicious PUT behavior, frequency %d\n",
3950                 my_short_id, "DHT", malicious_put_frequency);
3951     break;
3952   case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
3953 #if DEBUG_DHT_ROUTING
3954     if ((malicious_dropper != GNUNET_YES) && (dhtlog_handle != NULL))
3955       dhtlog_handle->set_malicious (&my_identity);
3956 #endif
3957     malicious_dropper = GNUNET_YES;
3958     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3959                 "%s:%s Initiating malicious DROP behavior\n", my_short_id,
3960                 "DHT");
3961     break;
3962 #endif
3963   default:
3964     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3965                 "%s:%s Unknown control command type `%d'!\n", my_short_id,
3966                 "DHT", ntohs (dht_control_msg->command));
3967     break;
3968   }
3969
3970   GNUNET_SERVER_receive_done (client, GNUNET_OK);
3971 }
3972
3973 /**
3974  * Handler for any generic DHT stop messages, calls the appropriate handler
3975  * depending on message type (if processed locally)
3976  *
3977  * @param cls closure for the service
3978  * @param client the client we received this message from
3979  * @param message the actual message received
3980  *
3981  */
3982 static void
3983 handle_dht_local_route_stop (void *cls, struct GNUNET_SERVER_Client *client,
3984                              const struct GNUNET_MessageHeader *message)
3985 {
3986
3987   const struct GNUNET_DHT_StopMessage *dht_stop_msg =
3988       (const struct GNUNET_DHT_StopMessage *) message;
3989   struct DHTQueryRecord *record;
3990   struct DHTRouteSource *pos;
3991
3992 #if DEBUG_DHT
3993   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3994               "`%s:%s': Received `%s' request from client, uid %llu\n",
3995               my_short_id, "DHT", "GENERIC STOP",
3996               GNUNET_ntohll (dht_stop_msg->unique_id));
3997 #endif
3998   record =
3999       GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap,
4000                                          &dht_stop_msg->key);
4001   if (record != NULL)
4002   {
4003     pos = record->head;
4004
4005     while (pos != NULL)
4006     {
4007       /* If the client is non-null (local request) and the client matches the requesting client, remove the entry. */
4008       if ((pos->client != NULL) && (pos->client->client_handle == client))
4009       {
4010         if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
4011           GNUNET_SCHEDULER_cancel (pos->delete_task);
4012         pos->delete_task =
4013             GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);
4014       }
4015       pos = pos->next;
4016     }
4017   }
4018
4019   GNUNET_SERVER_receive_done (client, GNUNET_OK);
4020 }
4021
4022
4023 /**
4024  * Core handler for p2p route requests.
4025  *
4026  * @param cls closure
4027  * @param message message
4028  * @param peer peer identity this notification is about
4029  * @param atsi performance data
4030  * @return GNUNET_OK to keep the connection open,
4031  *         GNUNET_SYSERR to close it (signal serious error)
4032  */
4033 static int
4034 handle_dht_p2p_route_request (void *cls, const struct GNUNET_PeerIdentity *peer,
4035                               const struct GNUNET_MessageHeader *message,
4036                               const struct GNUNET_TRANSPORT_ATS_Information
4037                               *atsi)
4038 {
4039 #if DEBUG_DHT
4040   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4041               "`%s:%s': Received P2P request from peer %s\n", my_short_id,
4042               "DHT", GNUNET_i2s (peer));
4043 #endif
4044   struct GNUNET_DHT_P2PRouteMessage *incoming =
4045       (struct GNUNET_DHT_P2PRouteMessage *) message;
4046   struct GNUNET_MessageHeader *enc_msg =
4047       (struct GNUNET_MessageHeader *) &incoming[1];
4048   struct DHT_MessageContext *msg_ctx;
4049   char *route_path;
4050   int path_size;
4051
4052   if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4053   {
4054     GNUNET_break_op (0);
4055     return GNUNET_YES;
4056   }
4057
4058   if (malicious_dropper == GNUNET_YES)
4059   {
4060 #if DEBUG_DHT_ROUTING
4061     if ((debug_routes_extended) && (dhtlog_handle != NULL))
4062     {
4063           /** Log routes that die due to high load! */
4064       dhtlog_handle->insert_route (NULL, 
4065 #if HAVE_UID_FOR_TESTING
4066                                    GNUNET_ntohll (incoming->unique_id),
4067 #else
4068                                    0,
4069 #endif
4070                                    DHTLOG_ROUTE, ntohl (incoming->hop_count),
4071                                    GNUNET_SYSERR, &my_identity, &incoming->key,
4072                                    peer, NULL);
4073     }
4074 #endif
4075     return GNUNET_YES;
4076   }
4077
4078   if (get_max_send_delay ().rel_value > MAX_REQUEST_TIME.rel_value)
4079   {
4080     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4081                 "Sending of previous replies took too long, backing off!\n");
4082     increment_stats ("# route requests dropped due to high load");
4083     decrease_max_send_delay (get_max_send_delay ());
4084 #if DEBUG_DHT_ROUTING
4085     if ((debug_routes_extended) && (dhtlog_handle != NULL))
4086     {
4087         /** Log routes that die due to high load! */
4088       dhtlog_handle->insert_route (NULL, 
4089 #if HAVE_UID_FOR_TESTING
4090                                    GNUNET_ntohll (incoming->unique_id),
4091 #else
4092                                    0,
4093 #endif
4094                                    DHTLOG_ROUTE, ntohl (incoming->hop_count),
4095                                    GNUNET_SYSERR, &my_identity, &incoming->key,
4096                                    peer, NULL);
4097     }
4098 #endif
4099     return GNUNET_YES;
4100   }
4101   msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
4102   msg_ctx->bloom =
4103       GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
4104                                          DHT_BLOOM_K);
4105   GNUNET_assert (msg_ctx->bloom != NULL);
4106   msg_ctx->hop_count = ntohl (incoming->hop_count);
4107   memcpy (&msg_ctx->key, &incoming->key, sizeof (GNUNET_HashCode));
4108   msg_ctx->replication = ntohl (incoming->desired_replication_level);
4109 #if HAVE_UID_FOR_TESTING
4110   msg_ctx->unique_id = GNUNET_ntohll (incoming->unique_id);
4111 #endif
4112   msg_ctx->msg_options = ntohl (incoming->options);
4113   if (GNUNET_DHT_RO_RECORD_ROUTE ==
4114       (msg_ctx->msg_options & GNUNET_DHT_RO_RECORD_ROUTE))
4115   {
4116     path_size =
4117         ntohl (incoming->outgoing_path_length) *
4118         sizeof (struct GNUNET_PeerIdentity);
4119     if (ntohs (message->size) !=
4120         (sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (enc_msg->size) +
4121          path_size))
4122     {
4123       GNUNET_break_op (0);
4124       GNUNET_free (msg_ctx);
4125       return GNUNET_YES;
4126     }
4127     route_path = (char *) &incoming[1];
4128     route_path = route_path + ntohs (enc_msg->size);
4129     msg_ctx->path_history =
4130         GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) + path_size);
4131     memcpy (msg_ctx->path_history, route_path, path_size);
4132     memcpy (&msg_ctx->path_history[path_size], &my_identity,
4133             sizeof (struct GNUNET_PeerIdentity));
4134     msg_ctx->path_history_len = ntohl (incoming->outgoing_path_length) + 1;
4135   }
4136   msg_ctx->network_size = ntohl (incoming->network_size);
4137   msg_ctx->peer = *peer;
4138   msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE;
4139   msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
4140   demultiplex_message (enc_msg, msg_ctx);
4141   if (msg_ctx->bloom != NULL)
4142   {
4143     GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
4144     msg_ctx->bloom = NULL;
4145   }
4146   GNUNET_free (msg_ctx);
4147   return GNUNET_YES;
4148 }
4149
4150
4151 /**
4152  * Core handler for p2p route results.
4153  *
4154  * @param cls closure
4155  * @param message message
4156  * @param peer peer identity this notification is about
4157  * @param atsi performance data
4158  *
4159  */
4160 static int
4161 handle_dht_p2p_route_result (void *cls, const struct GNUNET_PeerIdentity *peer,
4162                              const struct GNUNET_MessageHeader *message,
4163                              const struct GNUNET_TRANSPORT_ATS_Information
4164                              *atsi)
4165 {
4166 #if DEBUG_DHT
4167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4168               "`%s:%s': Received request from peer %s\n", my_short_id, "DHT",
4169               GNUNET_i2s (peer));
4170 #endif
4171   const struct GNUNET_DHT_P2PRouteResultMessage *incoming =
4172       (const struct GNUNET_DHT_P2PRouteResultMessage *) message;
4173   struct GNUNET_MessageHeader *enc_msg =
4174       (struct GNUNET_MessageHeader *) &incoming[1];
4175   struct DHT_MessageContext msg_ctx;
4176
4177 #if DEBUG_PATH
4178   char *path_offset;
4179   unsigned int i;
4180 #endif
4181   if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4182   {
4183     GNUNET_break_op (0);
4184     return GNUNET_YES;
4185   }
4186
4187   if (malicious_dropper == GNUNET_YES)
4188   {
4189 #if DEBUG_DHT_ROUTING
4190     if ((debug_routes_extended) && (dhtlog_handle != NULL))
4191     {
4192           /** Log routes that die due to high load! */
4193       dhtlog_handle->insert_route (NULL, 
4194 #if HAVE_UID_FOR_TESTING
4195                                    GNUNET_ntohll (incoming->unique_id),
4196 #else
4197                                    0,
4198 #endif
4199                                    DHTLOG_ROUTE, ntohl (incoming->hop_count),
4200                                    GNUNET_SYSERR, &my_identity, &incoming->key,
4201                                    peer, NULL);
4202     }
4203 #endif
4204     return GNUNET_YES;
4205   }
4206
4207   memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
4208   memcpy (&msg_ctx.key, &incoming->key, sizeof (GNUNET_HashCode));
4209 #if HAVE_UID_FOR_TESTING
4210   msg_ctx.unique_id = GNUNET_ntohll (incoming->unique_id);
4211 #endif
4212   msg_ctx.msg_options = ntohl (incoming->options);
4213   msg_ctx.hop_count = ntohl (incoming->hop_count);
4214   msg_ctx.peer = *peer;
4215   msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2;  /* Make result routing a higher priority */
4216   msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
4217   if ((GNUNET_DHT_RO_RECORD_ROUTE ==
4218        (msg_ctx.msg_options & GNUNET_DHT_RO_RECORD_ROUTE)) &&
4219       (ntohl (incoming->outgoing_path_length) > 0))
4220   {
4221     if (ntohs (message->size) -
4222         sizeof (struct GNUNET_DHT_P2PRouteResultMessage) -
4223         ntohs (enc_msg->size) !=
4224         ntohl (incoming->outgoing_path_length) *
4225         sizeof (struct GNUNET_PeerIdentity))
4226     {
4227 #if DEBUG_DHT
4228       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4229                   "Return message indicated a path was included, but sizes are wrong: Total size %d, enc size %d, left %d, expected %d\n",
4230                   ntohs (message->size), ntohs (enc_msg->size),
4231                   ntohs (message->size) -
4232                   sizeof (struct GNUNET_DHT_P2PRouteResultMessage) -
4233                   ntohs (enc_msg->size),
4234                   ntohl (incoming->outgoing_path_length) *
4235                   sizeof (struct GNUNET_PeerIdentity));
4236 #endif
4237       GNUNET_break_op (0);
4238       return GNUNET_NO;
4239     }
4240     msg_ctx.path_history = (char *) &incoming[1];
4241     msg_ctx.path_history += ntohs (enc_msg->size);
4242     msg_ctx.path_history_len = ntohl (incoming->outgoing_path_length);
4243 #if DEBUG_PATH
4244     for (i = 0; i < msg_ctx.path_history_len; i++)
4245     {
4246       path_offset =
4247           &msg_ctx.path_history[i * sizeof (struct GNUNET_PeerIdentity)];
4248       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4249                   "(handle_p2p_route_result) Key %s Found peer %d:%s\n",
4250                   GNUNET_h2s (&msg_ctx.key), i,
4251                   GNUNET_i2s ((struct GNUNET_PeerIdentity *) path_offset));
4252     }
4253 #endif
4254   }
4255 #if HAVE_REPLY_BLOOMFILTER
4256   msg_ctx.bloom =
4257       GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
4258                                          DHT_BLOOM_K);
4259   GNUNET_assert (msg_ctx.bloom != NULL);
4260 #endif
4261   route_result_message (enc_msg, &msg_ctx);
4262   return GNUNET_YES;
4263 }
4264
4265
4266 /**
4267  * Receive the HELLO from transport service,
4268  * free current and replace if necessary.
4269  *
4270  * @param cls NULL
4271  * @param message HELLO message of peer
4272  */
4273 static void
4274 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
4275 {
4276 #if DEBUG_DHT
4277   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4278               "Received our `%s' from transport service\n", "HELLO");
4279 #endif
4280
4281   GNUNET_assert (message != NULL);
4282   GNUNET_free_non_null (my_hello);
4283   my_hello = GNUNET_malloc (ntohs (message->size));
4284   memcpy (my_hello, message, ntohs (message->size));
4285 }
4286
4287
4288 /**
4289  * Task run during shutdown.
4290  *
4291  * @param cls unused
4292  * @param tc unused
4293  */
4294 static void
4295 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4296 {
4297   int bucket_count;
4298   struct PeerInfo *pos;
4299
4300   if (NULL != ghh)
4301   {
4302     GNUNET_TRANSPORT_get_hello_cancel (ghh);
4303     ghh = NULL;
4304   }
4305   if (transport_handle != NULL)
4306   {
4307     GNUNET_free_non_null (my_hello);
4308     GNUNET_TRANSPORT_disconnect (transport_handle);
4309     transport_handle = NULL;
4310   }
4311   if (NULL != nse)
4312   {
4313     GNUNET_NSE_disconnect (nse);
4314     nse = NULL;
4315   }
4316   if (coreAPI != NULL)
4317   {
4318 #if DEBUG_DHT
4319     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Disconnecting core!\n",
4320                 my_short_id, "DHT");
4321 #endif
4322     GNUNET_CORE_disconnect (coreAPI);
4323     coreAPI = NULL;
4324   }
4325   for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS; bucket_count++)
4326   {
4327     while (k_buckets[bucket_count].head != NULL)
4328     {
4329       pos = k_buckets[bucket_count].head;
4330 #if DEBUG_DHT
4331       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4332                   "%s:%s Removing peer %s from bucket %d!\n", my_short_id,
4333                   "DHT", GNUNET_i2s (&pos->id), bucket_count);
4334 #endif
4335       delete_peer (pos, bucket_count);
4336     }
4337   }
4338   if (datacache != NULL)
4339   {
4340 #if DEBUG_DHT
4341     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Destroying datacache!\n",
4342                 my_short_id, "DHT");
4343 #endif
4344     GNUNET_DATACACHE_destroy (datacache);
4345     datacache = NULL;
4346   }
4347   if (stats != NULL)
4348   {
4349     GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
4350     stats = NULL;
4351   }
4352   if (dhtlog_handle != NULL)
4353   {
4354     GNUNET_DHTLOG_disconnect (dhtlog_handle);
4355     dhtlog_handle = NULL;
4356   }
4357   if (block_context != NULL)
4358   {
4359     GNUNET_BLOCK_context_destroy (block_context);
4360     block_context = NULL;
4361   }
4362   GNUNET_free_non_null (my_short_id);
4363   my_short_id = NULL;
4364 }
4365
4366
4367 /**
4368  * To be called on core init/fail.
4369  *
4370  * @param cls service closure
4371  * @param server handle to the server for this service
4372  * @param identity the public identity of this peer
4373  * @param publicKey the public key of this peer
4374  */
4375 void
4376 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4377            const struct GNUNET_PeerIdentity *identity,
4378            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
4379 {
4380
4381   if (server == NULL)
4382   {
4383 #if DEBUG_DHT
4384     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s: Connection to core FAILED!\n",
4385                 "dht", GNUNET_i2s (identity));
4386 #endif
4387     GNUNET_SCHEDULER_cancel (cleanup_task);
4388     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
4389     return;
4390   }
4391 #if DEBUG_DHT
4392   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4393               "%s: Core connection initialized, I am peer: %s\n", "dht",
4394               GNUNET_i2s (identity));
4395 #endif
4396
4397   /* Copy our identity so we can use it */
4398   memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity));
4399   if (my_short_id != NULL)
4400     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4401                 "%s Receive CORE INIT message but have already been initialized! Did CORE fail?\n",
4402                 "DHT SERVICE");
4403   my_short_id = GNUNET_strdup (GNUNET_i2s (&my_identity));
4404   if (dhtlog_handle != NULL)
4405     dhtlog_handle->insert_node (NULL, &my_identity);
4406 }
4407
4408
4409 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
4410   {&handle_dht_local_route_request, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE,
4411    0},
4412   {&handle_dht_local_route_stop, NULL,
4413    GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP, 0},
4414   {&handle_dht_control_message, NULL, GNUNET_MESSAGE_TYPE_DHT_CONTROL, 0},
4415   {NULL, NULL, 0, 0}
4416 };
4417
4418
4419 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4420   {&handle_dht_p2p_route_request, GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE, 0},
4421   {&handle_dht_p2p_route_result, GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT, 0},
4422   {NULL, 0, 0}
4423 };
4424
4425
4426 /**
4427  * Method called whenever a peer connects.
4428  *
4429  * @param cls closure
4430  * @param peer peer identity this notification is about
4431  * @param atsi performance data
4432  */
4433 static void
4434 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4435                      const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4436 {
4437   struct PeerInfo *ret;
4438   struct DHTPutEntry *put_entry;
4439   int peer_bucket;
4440
4441   /* Check for connect to self message */
4442   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
4443     return;
4444
4445 #if DEBUG_DHT
4446   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4447               "%s:%s Receives core connect message for peer %s distance %d!\n",
4448               my_short_id, "dht", GNUNET_i2s (peer), distance);
4449 #endif
4450
4451   if (GNUNET_YES ==
4452       GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
4453                                               &peer->hashPubKey))
4454   {
4455 #if DEBUG_DHT
4456     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4457                 "%s:%s Received %s message for peer %s, but already have peer in RT!",
4458                 my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s (peer));
4459 #endif
4460     GNUNET_break (0);
4461     return;
4462   }
4463
4464   if ((datacache != NULL) && (GNUNET_YES == put_peer_identities))
4465   {
4466     put_entry =
4467         GNUNET_malloc (sizeof (struct DHTPutEntry) +
4468                        sizeof (struct GNUNET_PeerIdentity));
4469     put_entry->path_length = 0;
4470     put_entry->data_size = sizeof (struct GNUNET_PeerIdentity);
4471     memcpy (&put_entry[1], peer, sizeof (struct GNUNET_PeerIdentity));
4472     GNUNET_DATACACHE_put (datacache, &peer->hashPubKey,
4473                           sizeof (struct DHTPutEntry) +
4474                           sizeof (struct GNUNET_PeerIdentity),
4475                           (char *) put_entry, GNUNET_BLOCK_TYPE_DHT_HELLO,
4476                           GNUNET_TIME_absolute_get_forever ());
4477     GNUNET_free (put_entry);
4478   }
4479   else if (datacache == NULL)
4480     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4481                 "DHT has no connection to datacache!\n");
4482
4483   peer_bucket = find_current_bucket (&peer->hashPubKey);
4484   GNUNET_assert (peer_bucket >= lowest_bucket);
4485   GNUNET_assert (peer_bucket < MAX_BUCKETS);
4486   ret = GNUNET_malloc (sizeof (struct PeerInfo));
4487 #if 0
4488   ret->latency = latency;
4489   ret->distance = distance;
4490 #endif
4491   ret->id = *peer;
4492   GNUNET_CONTAINER_DLL_insert_after (k_buckets[peer_bucket].head,
4493                                      k_buckets[peer_bucket].tail,
4494                                      k_buckets[peer_bucket].tail, ret);
4495   k_buckets[peer_bucket].peers_size++;
4496 #if DO_UPDATE_PREFERENCE
4497   if ((GNUNET_CRYPTO_hash_matching_bits
4498        (&my_identity.hashPubKey, &peer->hashPubKey) > 0) &&
4499       (k_buckets[peer_bucket].peers_size <= bucket_size))
4500     ret->preference_task =
4501         GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
4502 #endif
4503   if ((k_buckets[lowest_bucket].peers_size) >= bucket_size)
4504     enable_next_bucket ();
4505   newly_found_peers++;
4506   GNUNET_CONTAINER_multihashmap_put (all_known_peers, &peer->hashPubKey, ret,
4507                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4508   increment_stats (STAT_PEERS_KNOWN);
4509
4510 #if DEBUG_DHT
4511   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4512               "%s:%s Adding peer to routing list: %s\n", my_short_id, "DHT",
4513               ret == NULL ? "NOT ADDED" : "PEER ADDED");
4514 #endif
4515 }
4516
4517
4518 /**
4519  * Method called whenever a peer disconnects.
4520  *
4521  * @param cls closure
4522  * @param peer peer identity this notification is about
4523  */
4524 static void
4525 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4526 {
4527   struct PeerInfo *to_remove;
4528   int current_bucket;
4529
4530   /* Check for disconnect from self message */
4531   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
4532     return;
4533 #if DEBUG_DHT
4534   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4535               "%s:%s: Received peer disconnect message for peer `%s' from %s\n",
4536               my_short_id, "DHT", GNUNET_i2s (peer), "CORE");
4537 #endif
4538
4539   if (GNUNET_YES !=
4540       GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
4541                                               &peer->hashPubKey))
4542   {
4543     GNUNET_break (0);
4544 #if DEBUG_DHT
4545     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4546                 "%s:%s: do not have peer `%s' in RT, can't disconnect!\n",
4547                 my_short_id, "DHT", GNUNET_i2s (peer));
4548 #endif
4549     return;
4550   }
4551   increment_stats (STAT_DISCONNECTS);
4552   GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
4553                  (all_known_peers, &peer->hashPubKey));
4554   to_remove =
4555       GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
4556   GNUNET_assert (to_remove != NULL);
4557   if (NULL != to_remove->info_ctx)
4558   {
4559     GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
4560     to_remove->info_ctx = NULL;
4561   }
4562   GNUNET_assert (0 ==
4563                  memcmp (peer, &to_remove->id,
4564                          sizeof (struct GNUNET_PeerIdentity)));
4565   current_bucket = find_current_bucket (&to_remove->id.hashPubKey);
4566   delete_peer (to_remove, current_bucket);
4567 }
4568
4569
4570 /**
4571  * Process dht requests.
4572  *
4573  * @param cls closure
4574  * @param server the initialized server
4575  * @param c configuration to use
4576  */
4577 static void
4578 run (void *cls, struct GNUNET_SERVER_Handle *server,
4579      const struct GNUNET_CONFIGURATION_Handle *c)
4580 {
4581   struct GNUNET_TIME_Relative next_send_time;
4582   unsigned long long temp_config_num;
4583
4584   cfg = c;
4585   datacache = GNUNET_DATACACHE_create (cfg, "dhtcache");
4586   GNUNET_SERVER_add_handlers (server, plugin_handlers);
4587   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
4588   nse = GNUNET_NSE_connect (cfg, &update_network_size_estimate, NULL);
4589   coreAPI = GNUNET_CORE_connect (cfg,   /* Main configuration */
4590                                  DEFAULT_CORE_QUEUE_SIZE,       /* queue size */
4591                                  NULL,  /* Closure passed to DHT functions */
4592                                  &core_init,    /* Call core_init once connected */
4593                                  &handle_core_connect,  /* Handle connects */
4594                                  &handle_core_disconnect,       /* remove peers on disconnects */
4595                                  NULL,  /* Do we care about "status" updates? */
4596                                  NULL,  /* Don't want notified about all incoming messages */
4597                                  GNUNET_NO,     /* For header only inbound notification */
4598                                  NULL,  /* Don't want notified about all outbound messages */
4599                                  GNUNET_NO,     /* For header only outbound notification */
4600                                  core_handlers);        /* Register these handlers */
4601
4602   if (coreAPI == NULL)
4603     return;
4604   transport_handle =
4605       GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
4606   if (transport_handle != NULL)
4607     ghh = GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
4608   else
4609     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4610                 "Failed to connect to transport service!\n");
4611   block_context = GNUNET_BLOCK_context_create (cfg);
4612   lowest_bucket = MAX_BUCKETS - 1;
4613   forward_list.hashmap =
4614       GNUNET_CONTAINER_multihashmap_create (MAX_OUTSTANDING_FORWARDS / 10);
4615   forward_list.minHeap =
4616       GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4617   all_known_peers = GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
4618   GNUNET_assert (all_known_peers != NULL);
4619   if (GNUNET_YES ==
4620       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
4621                                             "mysql_logging"))
4622   {
4623     debug_routes = GNUNET_YES;
4624   }
4625
4626   if (GNUNET_YES ==
4627       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "strict_kademlia"))
4628   {
4629     strict_kademlia = GNUNET_YES;
4630   }
4631
4632   if (GNUNET_YES ==
4633       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_on_closest"))
4634   {
4635     stop_on_closest = GNUNET_YES;
4636   }
4637
4638   if (GNUNET_YES ==
4639       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_found"))
4640   {
4641     stop_on_found = GNUNET_YES;
4642   }
4643
4644   if (GNUNET_YES ==
4645       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_getter"))
4646   {
4647     malicious_getter = GNUNET_YES;
4648     if (GNUNET_NO ==
4649         GNUNET_CONFIGURATION_get_value_time (cfg, "DHT",
4650                                              "MALICIOUS_GET_FREQUENCY",
4651                                              &malicious_get_frequency))
4652       malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
4653   }
4654
4655   if (GNUNET_YES ==
4656       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_putter"))
4657   {
4658     malicious_putter = GNUNET_YES;
4659     if (GNUNET_NO ==
4660         GNUNET_CONFIGURATION_get_value_time (cfg, "DHT",
4661                                              "MALICIOUS_PUT_FREQUENCY",
4662                                              &malicious_put_frequency))
4663       malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
4664   }
4665
4666   if (GNUNET_OK ==
4667       GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "bucket_size",
4668                                              &temp_config_num))
4669   {
4670     bucket_size = (unsigned int) temp_config_num;
4671   }
4672
4673   if (GNUNET_OK !=
4674       GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "kad_alpha",
4675                                              &kademlia_replication))
4676   {
4677     kademlia_replication = DEFAULT_KADEMLIA_REPLICATION;
4678   }
4679
4680   if (GNUNET_YES ==
4681       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_dropper"))
4682   {
4683     malicious_dropper = GNUNET_YES;
4684   }
4685
4686   if (GNUNET_NO ==
4687       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "do_find_peer"))
4688   {
4689     do_find_peer = GNUNET_NO;
4690   }
4691   else
4692     do_find_peer = GNUNET_YES;
4693
4694   if (GNUNET_YES ==
4695       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "use_real_distance"))
4696     use_real_distance = GNUNET_YES;
4697
4698   if (GNUNET_YES ==
4699       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
4700                                             "mysql_logging_extended"))
4701   {
4702     debug_routes = GNUNET_YES;
4703     debug_routes_extended = GNUNET_YES;
4704   }
4705
4706 #if DEBUG_DHT_ROUTING
4707   if (GNUNET_YES == debug_routes)
4708   {
4709     dhtlog_handle = GNUNET_DHTLOG_connect (cfg);
4710     if (dhtlog_handle == NULL)
4711     {
4712       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4713                   "Could not connect to mysql logging server, logging will not happen!");
4714     }
4715   }
4716 #endif
4717
4718   if (GNUNET_YES ==
4719       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "paper_forwarding"))
4720     paper_forwarding = GNUNET_YES;
4721
4722   if (GNUNET_YES ==
4723       GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "put_peer_identities"))
4724     put_peer_identities = GNUNET_YES;
4725
4726   stats = GNUNET_STATISTICS_create ("dht", cfg);
4727
4728   if (stats != NULL)
4729   {
4730     GNUNET_STATISTICS_set (stats, STAT_ROUTES, 0, GNUNET_NO);
4731     GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS, 0, GNUNET_NO);
4732     GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS_CLOSEST, 0, GNUNET_NO);
4733     GNUNET_STATISTICS_set (stats, STAT_RESULTS, 0, GNUNET_NO);
4734     GNUNET_STATISTICS_set (stats, STAT_RESULTS_TO_CLIENT, 0, GNUNET_NO);
4735     GNUNET_STATISTICS_set (stats, STAT_RESULT_FORWARDS, 0, GNUNET_NO);
4736     GNUNET_STATISTICS_set (stats, STAT_GETS, 0, GNUNET_NO);
4737     GNUNET_STATISTICS_set (stats, STAT_PUTS, 0, GNUNET_NO);
4738     GNUNET_STATISTICS_set (stats, STAT_PUTS_INSERTED, 0, GNUNET_NO);
4739     GNUNET_STATISTICS_set (stats, STAT_FIND_PEER, 0, GNUNET_NO);
4740     GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_START, 0, GNUNET_NO);
4741     GNUNET_STATISTICS_set (stats, STAT_GET_START, 0, GNUNET_NO);
4742     GNUNET_STATISTICS_set (stats, STAT_PUT_START, 0, GNUNET_NO);
4743     GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_REPLY, 0, GNUNET_NO);
4744     GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_ANSWER, 0, GNUNET_NO);
4745     GNUNET_STATISTICS_set (stats, STAT_BLOOM_FIND_PEER, 0, GNUNET_NO);
4746     GNUNET_STATISTICS_set (stats, STAT_GET_REPLY, 0, GNUNET_NO);
4747     GNUNET_STATISTICS_set (stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO);
4748     GNUNET_STATISTICS_set (stats, STAT_HELLOS_PROVIDED, 0, GNUNET_NO);
4749     GNUNET_STATISTICS_set (stats, STAT_DISCONNECTS, 0, GNUNET_NO);
4750   }
4751   if (GNUNET_YES == do_find_peer)
4752   {
4753     next_send_time.rel_value =
4754         DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
4755         GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
4756                                   (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
4757                                    2) -
4758                                   DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
4759     find_peer_context.start = GNUNET_TIME_absolute_get ();
4760     GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message,
4761                                   &find_peer_context);
4762   }
4763
4764   /* Scheduled the task to clean up when shutdown is called */
4765   cleanup_task =
4766       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
4767                                     &shutdown_task, NULL);
4768 }
4769
4770
4771 /**
4772  * The main function for the dht service.
4773  *
4774  * @param argc number of arguments from the command line
4775  * @param argv command line arguments
4776  * @return 0 ok, 1 on error
4777  */
4778 int
4779 main (int argc, char *const *argv)
4780 {
4781   int ret;
4782
4783   recent.hashmap = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT / 2);
4784   recent.minHeap =
4785       GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4786   recent_find_peer_requests =
4787       GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
4788   ret =
4789       (GNUNET_OK ==
4790        GNUNET_SERVICE_run (argc, argv, "dht", GNUNET_SERVICE_OPTION_NONE, &run,
4791                            NULL)) ? 0 : 1;
4792   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent.hashmap));
4793   GNUNET_CONTAINER_multihashmap_destroy (recent.hashmap);
4794   recent.hashmap = NULL;
4795   GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent.minHeap));
4796   GNUNET_CONTAINER_heap_destroy (recent.minHeap);
4797   recent.minHeap = NULL;
4798   GNUNET_CONTAINER_multihashmap_destroy (recent_find_peer_requests);
4799   recent_find_peer_requests = NULL;
4800   return ret;
4801 }
4802
4803 /* end of gnunet-service-dht.c */