6ecebb965ee2103220d741c67e08d1a709aea131
[oweals/gnunet.git] / src / dht / gnunet-service-dht_neighbours.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_neighbours.c
23  * @brief GNUnet DHT service's bucket and neighbour management code
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27
28 #include "platform.h"
29 #include "gnunet_block_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_nse_service.h"
35 #include "gnunet_core_service.h"
36 #include "gnunet_datacache_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_dht_service.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet-service-dht.h"
42 #include "gnunet-service-dht_clients.h"
43 #include "gnunet-service-dht_datacache.h"
44 #include "gnunet-service-dht_hello.h"
45 #include "gnunet-service-dht_neighbours.h"
46 #include "gnunet-service-dht_nse.h"
47 #include "gnunet-service-dht_routing.h"
48 #include <fenv.h>
49 #include "dht.h"
50
51 /**
52  * How many buckets will we allow total.
53  */
54 #define MAX_BUCKETS sizeof (GNUNET_HashCode) * 8
55
56 /**
57  * What is the maximum number of peers in a given bucket.
58  */
59 #define DEFAULT_BUCKET_SIZE 4
60
61 /**
62  * Desired replication level for FIND PEER requests
63  */
64 #define FIND_PEER_REPLICATION_LEVEL 4
65
66 /**
67  * Maximum allowed replication level for all requests.
68  */
69 #define MAXIMUM_REPLICATION_LEVEL 16
70
71 /**
72  * How often to update our preference levels for peers in our routing tables.
73  */
74 #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
75
76 /**
77  * How long at least to wait before sending another find peer request.
78  */
79 #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
80
81 /**
82  * How long at most to wait before sending another find peer request.
83  */
84 #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
85
86 /**
87  * How long at most to wait for transmission of a GET request to another peer?
88  */
89 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
90
91
92 /**
93  * P2P PUT message
94  */
95 struct PeerPutMessage
96 {
97   /**
98    * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_PUT
99    */
100   struct GNUNET_MessageHeader header;
101
102   /**
103    * Processing options
104    */
105   uint32_t options GNUNET_PACKED;
106
107   /**
108    * Content type.
109    */
110   uint32_t type GNUNET_PACKED;
111
112   /**
113    * Hop count
114    */
115   uint32_t hop_count GNUNET_PACKED;
116
117   /**
118    * Replication level for this message
119    */
120   uint32_t desired_replication_level GNUNET_PACKED;
121
122   /**
123    * Length of the PUT path that follows (if tracked).
124    */
125   uint32_t put_path_length GNUNET_PACKED;
126
127   /**
128    * When does the content expire?
129    */
130   struct GNUNET_TIME_AbsoluteNBO expiration_time;
131
132   /**
133    * Bloomfilter (for peer identities) to stop circular routes
134    */
135   char bloomfilter[DHT_BLOOM_SIZE];
136
137   /**
138    * The key we are storing under.
139    */
140   GNUNET_HashCode key;
141
142   /* put path (if tracked) */
143
144   /* Payload */
145
146 };
147
148
149 /**
150  * P2P Result message
151  */
152 struct PeerResultMessage
153 {
154   /**
155    * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT
156    */
157   struct GNUNET_MessageHeader header;
158
159   /**
160    * Content type.
161    */
162   uint32_t type GNUNET_PACKED;
163
164   /**
165    * Length of the PUT path that follows (if tracked).
166    */
167   uint32_t put_path_length GNUNET_PACKED;
168
169   /**
170    * Length of the GET path that follows (if tracked).
171    */
172   uint32_t get_path_length GNUNET_PACKED;
173
174   /**
175    * When does the content expire?
176    */
177   struct GNUNET_TIME_AbsoluteNBO expiration_time;
178
179   /**
180    * The key of the corresponding GET request.
181    */
182   GNUNET_HashCode key;
183
184   /* put path (if tracked) */
185
186   /* get path (if tracked) */
187
188   /* Payload */
189
190 };
191
192
193 /**
194  * P2P GET message
195  */
196 struct PeerGetMessage
197 {
198   /**
199    * Type: GNUNET_MESSAGE_TYPE_DHT_P2P_GET
200    */
201   struct GNUNET_MessageHeader header;
202
203   /**
204    * Processing options
205    */
206   uint32_t options GNUNET_PACKED;
207
208   /**
209    * Desired content type.
210    */
211   uint32_t type GNUNET_PACKED;
212
213   /**
214    * Hop count
215    */
216   uint32_t hop_count GNUNET_PACKED;
217
218   /**
219    * Desired replication level for this request.
220    */
221   uint32_t desired_replication_level GNUNET_PACKED;
222
223   /**
224    * Size of the extended query.
225    */
226   uint32_t xquery_size;
227
228   /**
229    * Bloomfilter mutator.
230    */
231   uint32_t bf_mutator;
232
233   /**
234    * Bloomfilter (for peer identities) to stop circular routes
235    */
236   char bloomfilter[DHT_BLOOM_SIZE];
237
238   /**
239    * The key we are looking for.
240    */
241   GNUNET_HashCode key;
242
243   /* xquery */
244
245   /* result bloomfilter */
246
247 };
248
249
250 /**
251  * Linked list of messages to send to a particular other peer.
252  */
253 struct P2PPendingMessage
254 {
255   /**
256    * Pointer to next item in the list
257    */
258   struct P2PPendingMessage *next;
259
260   /**
261    * Pointer to previous item in the list
262    */
263   struct P2PPendingMessage *prev;
264
265   /**
266    * Message importance level.  FIXME: used? useful?
267    */
268   unsigned int importance;
269
270   /**
271    * When does this message time out?
272    */
273   struct GNUNET_TIME_Absolute timeout;
274
275   /**
276    * Actual message to be sent, allocated at the end of the struct:
277    * // msg = (cast) &pm[1]; 
278    * // memcpy (&pm[1], data, len);
279    */
280   const struct GNUNET_MessageHeader *msg;
281
282 };
283
284
285 /**
286  * Entry for a peer in a bucket.
287  */
288 struct PeerInfo
289 {
290   /**
291    * Next peer entry (DLL)
292    */
293   struct PeerInfo *next;
294
295   /**
296    *  Prev peer entry (DLL)
297    */
298   struct PeerInfo *prev;
299
300   /**
301    * Count of outstanding messages for peer.  FIXME: NEEDED?
302    * FIXME: bound queue size!?
303    */
304   unsigned int pending_count;
305
306   /**
307    * Head of pending messages to be sent to this peer.
308    */
309   struct P2PPendingMessage *head;
310
311   /**
312    * Tail of pending messages to be sent to this peer.
313    */
314   struct P2PPendingMessage *tail;
315
316   /**
317    * Core handle for sending messages to this peer.
318    */
319   struct GNUNET_CORE_TransmitHandle *th;
320
321   /**
322    * Preference update context
323    */
324   struct GNUNET_CORE_InformationRequestContext *info_ctx;
325
326   /**
327    * Task for scheduling preference updates
328    */
329   GNUNET_SCHEDULER_TaskIdentifier preference_task;
330
331   /**
332    * What is the identity of the peer?
333    */
334   struct GNUNET_PeerIdentity id;
335
336 #if 0
337   /**
338    * What is the average latency for replies received?
339    */
340   struct GNUNET_TIME_Relative latency;
341
342   /**
343    * Transport level distance to peer.
344    */
345   unsigned int distance;
346 #endif
347
348 };
349
350
351 /**
352  * Peers are grouped into buckets.
353  */
354 struct PeerBucket
355 {
356   /**
357    * Head of DLL
358    */
359   struct PeerInfo *head;
360
361   /**
362    * Tail of DLL
363    */
364   struct PeerInfo *tail;
365
366   /**
367    * Number of peers in the bucket.
368    */
369   unsigned int peers_size;
370 };
371
372
373 /**
374  * The lowest currently used bucket, initially 0 (for 0-bits matching bucket).
375  */
376 static unsigned int closest_bucket;
377
378 /**
379  * How many peers have we added since we sent out our last
380  * find peer request?
381  */
382 static unsigned int newly_found_peers;
383
384 /**
385  * The buckets.  Array of size MAX_BUCKET_SIZE.  Offset 0 means 0 bits matching.
386  */
387 static struct PeerBucket k_buckets[MAX_BUCKETS];
388
389 /**
390  * Hash map of all known peers, for easy removal from k_buckets on disconnect.
391  */
392 static struct GNUNET_CONTAINER_MultiHashMap *all_known_peers;
393
394 /**
395  * Maximum size for each bucket.
396  */
397 static unsigned int bucket_size = DEFAULT_BUCKET_SIZE;
398
399 /**
400  * Task that sends FIND PEER requests.
401  */
402 static GNUNET_SCHEDULER_TaskIdentifier find_peer_task;
403
404 /**
405  * Identity of this peer.
406  */ 
407 static struct GNUNET_PeerIdentity my_identity;
408
409 /**
410  * Handle to GNUnet core.
411  */
412 static struct GNUNET_CORE_Handle *coreAPI;
413
414
415 /**
416  * Find the optimal bucket for this key.
417  *
418  * @param hc the hashcode to compare our identity to
419  * @return the proper bucket index, or GNUNET_SYSERR
420  *         on error (same hashcode)
421  */
422 static int
423 find_bucket (const GNUNET_HashCode * hc)
424 {
425   unsigned int bits;
426
427   bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
428   if (bits == MAX_BUCKETS)
429     {
430       /* How can all bits match? Got my own ID? */
431       GNUNET_break (0);
432       return GNUNET_SYSERR; 
433     }
434   return MAX_BUCKETS - bits - 1;
435 }
436
437
438 /**
439  * Let GNUnet core know that we like the given peer.
440  *
441  * @param cls the 'struct PeerInfo' of the peer
442  * @param tc scheduler context.
443  */ 
444 static void
445 update_core_preference (void *cls,
446                         const struct GNUNET_SCHEDULER_TaskContext *tc);
447
448
449 /**
450  * Function called with statistics about the given peer.
451  *
452  * @param cls closure
453  * @param peer identifies the peer
454  * @param bpm_out set to the current bandwidth limit (sending) for this peer
455  * @param amount set to the amount that was actually reserved or unreserved;
456  *               either the full requested amount or zero (no partial reservations)
457  * @param res_delay if the reservation could not be satisfied (amount was 0), how
458  *        long should the client wait until re-trying?
459  * @param preference current traffic preference for the given peer
460  */
461 static void
462 update_core_preference_finish (void *cls,
463                                const struct GNUNET_PeerIdentity *peer,
464                                struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
465                                int32_t amount,
466                                struct GNUNET_TIME_Relative res_delay,
467                                uint64_t preference)
468 {
469   struct PeerInfo *peer_info = cls;
470
471   peer_info->info_ctx = NULL;
472   peer_info->preference_task
473     = GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
474                                     &update_core_preference, peer_info);
475 }
476
477
478 /**
479  * Let GNUnet core know that we like the given peer.
480  *
481  * @param cls the 'struct PeerInfo' of the peer
482  * @param tc scheduler context.
483  */ 
484 static void
485 update_core_preference (void *cls,
486                         const struct GNUNET_SCHEDULER_TaskContext *tc)
487 {
488   struct PeerInfo *peer = cls;
489   uint64_t preference;
490   unsigned int matching;
491   int bucket;
492
493   peer->preference_task = GNUNET_SCHEDULER_NO_TASK;
494   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
495     return;  
496   matching =
497     GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
498                                       &peer->id.hashPubKey);
499   if (matching >= 64)
500     matching = 63;
501   bucket = find_bucket (&peer->id.hashPubKey);
502   if (bucket == GNUNET_SYSERR)
503     preference = 0;
504   else
505     preference = (1LL << matching) / k_buckets[bucket].peers_size;
506   if (preference == 0)
507     {
508       peer->preference_task
509         = GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
510                                         &update_core_preference, peer);
511       return;
512     }
513   GNUNET_STATISTICS_update (GDS_stats,
514                             gettext_noop ("# Preference updates given to core"), 1,
515                             GNUNET_NO);
516   peer->info_ctx =
517     GNUNET_CORE_peer_change_preference (coreAPI, &peer->id,
518                                         GNUNET_TIME_UNIT_FOREVER_REL,
519                                         GNUNET_BANDWIDTH_VALUE_MAX, 0,
520                                         preference,
521                                         &update_core_preference_finish, peer);
522 }
523
524
525 /**
526  * Closure for 'add_known_to_bloom'.
527  */
528 struct BloomConstructorContext
529 {
530   /**
531    * Bloom filter under construction.
532    */
533   struct GNUNET_CONTAINER_BloomFilter *bloom;
534
535   /**
536    * Mutator to use.
537    */
538   uint32_t bf_mutator;
539 };
540
541
542 /**
543  * Add each of the peers we already know to the bloom filter of
544  * the request so that we don't get duplicate HELLOs.
545  *
546  * @param cls the 'struct BloomConstructorContext'.
547  * @param key peer identity to add to the bloom filter
548  * @param value value the peer information (unused)
549  * @return GNUNET_YES (we should continue to iterate)
550  */
551 static int
552 add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
553 {
554   struct BloomConstructorContext *ctx = cls;
555   GNUNET_HashCode mh;
556
557   GNUNET_BLOCK_mingle_hash (key, ctx->bf_mutator, &mh);
558   GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh);
559   return GNUNET_YES;
560 }
561
562
563 /**
564  * Task to send a find peer message for our own peer identifier
565  * so that we can find the closest peers in the network to ourselves
566  * and attempt to connect to them.
567  *
568  * @param cls closure for this task
569  * @param tc the context under which the task is running
570  */
571 static void
572 send_find_peer_message (void *cls,
573                         const struct GNUNET_SCHEDULER_TaskContext *tc)
574 {
575   struct GNUNET_TIME_Relative next_send_time;
576   struct BloomConstructorContext bcc;
577   struct GNUNET_CONTAINER_BloomFilter *peer_bf;
578
579   find_peer_task = GNUNET_SCHEDULER_NO_TASK;
580   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
581     return;
582   if (newly_found_peers > bucket_size) 
583   {
584     /* If we are finding many peers already, no need to send out our request right now! */
585     find_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
586                                                    &send_find_peer_message, NULL);
587     newly_found_peers = 0;
588     return;
589   }
590   bcc.bf_mutator = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
591   bcc.bloom =
592     GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, GNUNET_CONSTANTS_BLOOMFILTER_K);
593   GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, 
594                                          &add_known_to_bloom,
595                                          &bcc);
596   GNUNET_STATISTICS_update (GDS_stats,
597                             gettext_noop ("# FIND PEER messages initiated"), 1,
598                             GNUNET_NO);
599   peer_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
600                                                DHT_BLOOM_SIZE,
601                                                GNUNET_CONSTANTS_BLOOMFILTER_K);
602   // FIXME: pass priority!?
603   GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO,
604                              GNUNET_DHT_RO_FIND_PEER,
605                              FIND_PEER_REPLICATION_LEVEL,
606                              0,
607                              &my_identity.hashPubKey,
608                              NULL, 0,
609                              bcc.bloom, bcc.bf_mutator, 
610                              peer_bf);
611   GNUNET_CONTAINER_bloomfilter_free (peer_bf);
612   GNUNET_CONTAINER_bloomfilter_free (bcc.bloom);
613   /* schedule next round */
614   newly_found_peers = 0;
615   next_send_time.rel_value =
616     (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
617     GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
618                               DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2);
619   find_peer_task = GNUNET_SCHEDULER_add_delayed (next_send_time, 
620                                                  &send_find_peer_message,
621                                                  NULL);  
622 }
623
624
625 /**
626  * Method called whenever a peer connects.
627  *
628  * @param cls closure
629  * @param peer peer identity this notification is about
630  * @param atsi performance data
631  */
632 static void
633 handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
634                      const struct GNUNET_TRANSPORT_ATS_Information *atsi)
635 {
636   struct PeerInfo *ret;
637   int peer_bucket;
638
639   /* Check for connect to self message */
640   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
641     return;
642   if (GNUNET_YES ==
643       GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
644                                               &peer->hashPubKey))
645   {
646     GNUNET_break (0);
647     return;
648   }
649   GNUNET_STATISTICS_update (GDS_stats,
650                             gettext_noop ("# Peers connected"), 1,
651                             GNUNET_NO);
652   peer_bucket = find_bucket (&peer->hashPubKey);
653   GNUNET_assert ( (peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS) );
654   ret = GNUNET_malloc (sizeof (struct PeerInfo));
655 #if 0
656   ret->latency = latency;
657   ret->distance = distance;
658 #endif
659   ret->id = *peer;
660   GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head,
661                                     k_buckets[peer_bucket].tail, ret);
662   k_buckets[peer_bucket].peers_size++;
663   closest_bucket = GNUNET_MAX (closest_bucket,
664                                peer_bucket);
665   if ( (peer_bucket > 0) &&
666        (k_buckets[peer_bucket].peers_size <= bucket_size) )
667     ret->preference_task = GNUNET_SCHEDULER_add_now (&update_core_preference, ret);
668   newly_found_peers++;
669   GNUNET_assert (GNUNET_OK ==
670                  GNUNET_CONTAINER_multihashmap_put (all_known_peers, 
671                                                     &peer->hashPubKey, ret,
672                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
673   if (1 == GNUNET_CONTAINER_multihashmap_size (all_known_peers))
674   {
675     /* got a first connection, good time to start with FIND PEER requests... */
676     find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message,
677                                                NULL);    
678   }
679 }
680
681
682 /**
683  * Method called whenever a peer disconnects.
684  *
685  * @param cls closure
686  * @param peer peer identity this notification is about
687  */
688 static void
689 handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
690 {
691   struct PeerInfo *to_remove;
692   int current_bucket;
693   struct P2PPendingMessage *pos;
694
695   /* Check for disconnect from self message */
696   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
697     return;
698   to_remove =
699       GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
700   if (NULL == to_remove)
701     {
702       GNUNET_break (0);
703       return;
704     }
705   GNUNET_STATISTICS_update (GDS_stats,
706                             gettext_noop ("# Peers connected"), -1,
707                             GNUNET_NO);
708   GNUNET_assert (GNUNET_YES ==
709                  GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
710                                                        &peer->hashPubKey,
711                                                        to_remove));
712   if (NULL != to_remove->info_ctx)
713   {
714     GNUNET_CORE_peer_change_preference_cancel (to_remove->info_ctx);
715     to_remove->info_ctx = NULL;
716   }
717   if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task)
718   {
719     GNUNET_SCHEDULER_cancel (to_remove->preference_task);
720     to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK;
721   }
722   current_bucket = find_bucket (&to_remove->id.hashPubKey);
723   GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head,
724                                k_buckets[current_bucket].tail,
725                                to_remove);
726   GNUNET_assert (k_buckets[current_bucket].peers_size > 0);
727   k_buckets[current_bucket].peers_size--;
728   while ( (closest_bucket > 0) &&
729           (k_buckets[closest_bucket].peers_size == 0) )
730     closest_bucket--;
731
732   if (to_remove->th != NULL) 
733   {
734     GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th);
735     to_remove->th = NULL;
736   }
737   while (NULL != (pos = to_remove->head))
738   {
739     GNUNET_CONTAINER_DLL_remove (to_remove->head,
740                                  to_remove->tail,
741                                  pos);
742     GNUNET_free (pos);
743   }
744 }
745
746
747 /**
748  * Called when core is ready to send a message we asked for
749  * out to the destination.
750  *
751  * @param cls the 'struct PeerInfo' of the target peer
752  * @param size number of bytes available in buf
753  * @param buf where the callee should write the message
754  * @return number of bytes written to buf
755  */
756 static size_t
757 core_transmit_notify (void *cls, size_t size, void *buf)
758 {
759   struct PeerInfo *peer = cls;
760   char *cbuf = buf;
761   struct P2PPendingMessage *pending;
762   size_t off;
763   size_t msize;
764
765   peer->th = NULL;
766   if (buf == NULL)
767   {
768     /* client disconnected */
769     return 0;
770   }
771   if (peer->head == NULL)
772   {
773     /* no messages pending */
774     return 0;
775   }
776   off = 0;
777   while ( (NULL != (pending = peer->head)) &&
778           (size - off >= (msize = ntohs (pending->msg->size))) )
779   {
780     GNUNET_STATISTICS_update (GDS_stats,
781                               gettext_noop ("# Bytes transmitted to other peers"), msize,
782                               GNUNET_NO);
783     memcpy (&cbuf[off], pending->msg, msize);
784     off += msize;
785     peer->pending_count--;
786     GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
787     GNUNET_free (pending);
788   }
789   if (peer->head != NULL)
790     {
791       peer->th 
792         = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
793                                              pending->importance,
794                                              GNUNET_TIME_absolute_get_remaining (pending->timeout),
795                                              &peer->id, msize,
796                                              &core_transmit_notify, peer);
797     }
798   return off;
799 }
800
801
802 /**
803  * Transmit all messages in the peer's message queue.
804  *
805  * @param peer message queue to process
806  */
807 static void
808 process_peer_queue (struct PeerInfo *peer)
809 {
810   struct P2PPendingMessage *pending;
811
812   if (NULL == (pending = peer->head))
813     return;
814   if (NULL != peer->th)
815     return;
816   GNUNET_STATISTICS_update (GDS_stats,
817                             gettext_noop ("# Bytes of bandwdith requested from core"),
818                             ntohs (pending->msg->size),
819                             GNUNET_NO);
820   peer->th 
821     = GNUNET_CORE_notify_transmit_ready (coreAPI, GNUNET_YES,
822                                          pending->importance,
823                                          GNUNET_TIME_absolute_get_remaining (pending->timeout),
824                                          &peer->id,
825                                          ntohs (pending->msg->size),
826                                          &core_transmit_notify, peer);
827 }
828
829
830 /**
831  * To how many peers should we (on average) forward the request to
832  * obtain the desired target_replication count (on average).
833  *
834  * @param hop_count number of hops the message has traversed
835  * @param target_replication the number of total paths desired
836  * @return Some number of peers to forward the message to
837  */
838 static unsigned int
839 get_forward_count (uint32_t hop_count, 
840                    uint32_t target_replication)
841 {
842   uint32_t random_value;
843   uint32_t forward_count;
844   float target_value;
845
846   if (hop_count > GDS_NSE_get () * 4.0)
847   {
848     /* forcefully terminate */
849     return 0;
850   }
851   if (hop_count > GDS_NSE_get () * 2.0)
852   {
853     /* Once we have reached our ideal number of hops, only forward to 1 peer */
854     return 1;
855   }
856   /* bound by system-wide maximum */
857   target_replication = GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL,
858                                    target_replication);
859   target_value =
860     1 + (target_replication - 1.0) / (GDS_NSE_get () +
861                                       ((float) (target_replication - 1.0) *
862                                        hop_count));
863   /* Set forward count to floor of target_value */
864   forward_count = (uint32_t) target_value;
865   /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
866   target_value = target_value - forward_count;
867   random_value =
868     GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); 
869   if (random_value < (target_value * UINT32_MAX))
870     forward_count++;
871   return forward_count;
872 }
873
874
875 /**
876  * Compute the distance between have and target as a 32-bit value.
877  * Differences in the lower bits must count stronger than differences
878  * in the higher bits.
879  *
880  * @return 0 if have==target, otherwise a number
881  *           that is larger as the distance between
882  *           the two hash codes increases
883  */
884 static unsigned int
885 get_distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
886 {
887   unsigned int bucket;
888   unsigned int msb;
889   unsigned int lsb;
890   unsigned int i;
891
892   /* We have to represent the distance between two 2^9 (=512)-bit
893    * numbers as a 2^5 (=32)-bit number with "0" being used for the
894    * two numbers being identical; furthermore, we need to
895    * guarantee that a difference in the number of matching
896    * bits is always represented in the result.
897    *
898    * We use 2^32/2^9 numerical values to distinguish between
899    * hash codes that have the same LSB bit distance and
900    * use the highest 2^9 bits of the result to signify the
901    * number of (mis)matching LSB bits; if we have 0 matching
902    * and hence 512 mismatching LSB bits we return -1 (since
903    * 512 itself cannot be represented with 9 bits) */
904
905   /* first, calculate the most significant 9 bits of our
906    * result, aka the number of LSBs */
907   bucket = GNUNET_CRYPTO_hash_matching_bits (target, have);
908   /* bucket is now a value between 0 and 512 */
909   if (bucket == 512)
910     return 0;                   /* perfect match */
911   if (bucket == 0)
912     return (unsigned int) -1;   /* LSB differs; use max (if we did the bit-shifting
913                                  * below, we'd end up with max+1 (overflow)) */
914
915   /* calculate the most significant bits of the final result */
916   msb = (512 - bucket) << (32 - 9);
917   /* calculate the 32-9 least significant bits of the final result by
918    * looking at the differences in the 32-9 bits following the
919    * mismatching bit at 'bucket' */
920   lsb = 0;
921   for (i = bucket + 1;
922        (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
923   {
924     if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
925         GNUNET_CRYPTO_hash_get_bit (have, i))
926       lsb |= (1 << (bucket + 32 - 9 - i));      /* first bit set will be 10,
927                                                  * last bit set will be 31 -- if
928                                                  * i does not reach 512 first... */
929   }
930   return msb | lsb;
931 }
932
933
934 /**
935  * Check whether my identity is closer than any known peers.  If a
936  * non-null bloomfilter is given, check if this is the closest peer
937  * that hasn't already been routed to.
938  *
939  * @param key hash code to check closeness to
940  * @param bloom bloomfilter, exclude these entries from the decision
941  * @return GNUNET_YES if node location is closest,
942  *         GNUNET_NO otherwise.
943  */
944 static int
945 am_closest_peer (const GNUNET_HashCode *key,
946                  const struct GNUNET_CONTAINER_BloomFilter *bloom)
947 {
948   int bits;
949   int other_bits;
950   int bucket_num;
951   int count;
952   struct PeerInfo *pos;
953
954   if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
955     return GNUNET_YES;
956   bucket_num = find_bucket (key);
957   bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, key);
958   pos = k_buckets[bucket_num].head;
959   count = 0;
960   while ((pos != NULL) && (count < bucket_size))
961   {
962     if ((bloom != NULL) &&
963         (GNUNET_YES ==
964          GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
965     {
966       pos = pos->next;
967       continue;                 /* Skip already checked entries */
968     }
969     other_bits = GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, key);
970     if (other_bits > bits)
971       return GNUNET_NO;
972     if (other_bits == bits)        /* We match the same number of bits */
973       return GNUNET_YES;
974     pos = pos->next;
975   }
976   /* No peers closer, we are the closest! */
977   return GNUNET_YES;
978 }
979
980
981 /**
982  * Select a peer from the routing table that would be a good routing
983  * destination for sending a message for "key".  The resulting peer
984  * must not be in the set of blocked peers.<p>
985  *
986  * Note that we should not ALWAYS select the closest peer to the
987  * target, peers further away from the target should be chosen with
988  * exponentially declining probability.
989  *
990  * FIXME: double-check that this is fine
991  * 
992  *
993  * @param key the key we are selecting a peer to route to
994  * @param bloom a bloomfilter containing entries this request has seen already
995  * @param hops how many hops has this message traversed thus far
996  * @return Peer to route to, or NULL on error
997  */
998 static struct PeerInfo *
999 select_peer (const GNUNET_HashCode *key,
1000              const struct GNUNET_CONTAINER_BloomFilter *bloom, 
1001              uint32_t hops)
1002 {
1003   unsigned int bc;
1004   unsigned int count;
1005   unsigned int selected;
1006   struct PeerInfo *pos;
1007   unsigned int dist;
1008   unsigned int smallest_distance;
1009   struct PeerInfo *chosen;
1010
1011   if (hops >= GDS_NSE_get ())
1012   {
1013     /* greedy selection (closest peer that is not in bloomfilter) */
1014     smallest_distance = UINT_MAX;
1015     chosen = NULL;
1016     for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1017     {
1018       pos = k_buckets[bc].head;
1019       count = 0;
1020       while ((pos != NULL) && (count < bucket_size))
1021       {
1022         if (GNUNET_NO ==
1023             GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
1024         {
1025           dist = get_distance (key, &pos->id.hashPubKey);
1026           if (dist < smallest_distance)
1027           {
1028             chosen = pos;
1029             smallest_distance = dist;
1030           }
1031         }
1032         else
1033         {
1034           GNUNET_STATISTICS_update (GDS_stats,
1035                                     gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
1036                                     GNUNET_NO);
1037         }
1038         count++;
1039         pos = pos->next;
1040       }
1041     }
1042     if (NULL == chosen)
1043       GNUNET_STATISTICS_update (GDS_stats,
1044                                 gettext_noop ("# Peer selection failed"), 1,
1045                                 GNUNET_NO);
1046     return chosen;
1047   }
1048
1049   /* select "random" peer */
1050   /* count number of peers that are available and not filtered */
1051   count = 0;
1052   for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1053   {
1054     pos = k_buckets[bc].head;
1055     while ((pos != NULL) && (count < bucket_size))
1056     {
1057       if ( (bloom != NULL) &&
1058            (GNUNET_YES ==
1059             GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
1060       {
1061         GNUNET_STATISTICS_update (GDS_stats,
1062                                   gettext_noop ("# Peers excluded from routing due to Bloomfilter"), 1,
1063                                   GNUNET_NO);
1064         pos = pos->next;
1065         continue;               /* Ignore bloomfiltered peers */
1066       }
1067       count++;
1068       pos = pos->next;
1069     }
1070   }
1071   if (count == 0)               /* No peers to select from! */
1072   {
1073     GNUNET_STATISTICS_update (GDS_stats,
1074                               gettext_noop ("# Peer selection failed"), 1,
1075                               GNUNET_NO);
1076     return NULL;
1077   }
1078   /* Now actually choose a peer */
1079   selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1080   count = 0;
1081   for (bc = closest_bucket; bc < MAX_BUCKETS; bc++)
1082   {
1083     pos = k_buckets[bc].head;
1084     while ((pos != NULL) && (count < bucket_size))
1085     {
1086       if ( (bloom != NULL) &&
1087            (GNUNET_YES ==
1088             GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) )
1089       {
1090         pos = pos->next;
1091         continue;               /* Ignore bloomfiltered peers */
1092       }
1093       if (0 == selected--)
1094         return pos;
1095       pos = pos->next;
1096     }
1097   }
1098   GNUNET_break (0);
1099   return NULL;
1100 }
1101
1102
1103 /**
1104  * Compute the set of peers that the given request should be
1105  * forwarded to.
1106  *
1107  * @param key routing key
1108  * @param bloom bloom filter excluding peers as targets, all selected
1109  *        peers will be added to the bloom filter
1110  * @param hop_count number of hops the request has traversed so far
1111  * @param target_replication desired number of replicas
1112  * @param targets where to store an array of target peers (to be
1113  *         free'd by the caller)
1114  * @return number of peers returned in 'targets'.
1115  */
1116 static unsigned int
1117 get_target_peers (const GNUNET_HashCode *key,
1118                   struct GNUNET_CONTAINER_BloomFilter *bloom,
1119                   uint32_t hop_count,
1120                   uint32_t target_replication,
1121                   struct PeerInfo ***targets)
1122 {
1123   unsigned int ret;
1124   unsigned int off;
1125   struct PeerInfo **rtargets;
1126   struct PeerInfo *nxt;
1127
1128   GNUNET_assert (NULL != bloom);
1129   ret = get_forward_count (hop_count, target_replication);
1130   if (ret == 0)
1131   {
1132     *targets = NULL;
1133     return 0;
1134   }
1135   rtargets = GNUNET_malloc (sizeof (struct PeerInfo*) * ret);
1136   off = 0;
1137   while (ret-- > 0)
1138   {
1139     nxt = select_peer (key, bloom, hop_count);
1140     if (nxt == NULL)
1141       break;      
1142     rtargets[off++] = nxt;
1143     GNUNET_break (GNUNET_NO ==
1144                   GNUNET_CONTAINER_bloomfilter_test (bloom, &nxt->id.hashPubKey));
1145     GNUNET_CONTAINER_bloomfilter_add (bloom, &nxt->id.hashPubKey);
1146   }
1147   if (0 == off)
1148   {
1149     GNUNET_free (rtargets);
1150     *targets = NULL;
1151     return 0;
1152   }
1153   *targets = rtargets;
1154   return off;
1155 }
1156
1157
1158 /**
1159  * Perform a PUT operation.   Forwards the given request to other
1160  * peers.   Does not store the data locally.  Does not give the
1161  * data to local clients.  May do nothing if this is the only
1162  * peer in the network (or if we are the closest peer in the
1163  * network).
1164  *
1165  * @param type type of the block
1166  * @param options routing options
1167  * @param desired_replication_level desired replication count
1168  * @param expiration_time when does the content expire
1169  * @param hop_count how many hops has this message traversed so far
1170  * @param bf Bloom filter of peers this PUT has already traversed
1171  * @param key key for the content
1172  * @param put_path_length number of entries in put_path
1173  * @param put_path peers this request has traversed so far (if tracked)
1174  * @param data payload to store
1175  * @param data_size number of bytes in data
1176  */
1177 void
1178 GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type,
1179                            enum GNUNET_DHT_RouteOption options,
1180                            uint32_t desired_replication_level,
1181                            struct GNUNET_TIME_Absolute expiration_time,
1182                            uint32_t hop_count,
1183                            struct GNUNET_CONTAINER_BloomFilter *bf,
1184                            const GNUNET_HashCode *key,
1185                            unsigned int put_path_length,
1186                            struct GNUNET_PeerIdentity *put_path,
1187                            const void *data,
1188                            size_t data_size)
1189 {
1190   unsigned int target_count;
1191   unsigned int i;
1192   struct PeerInfo **targets;
1193   struct PeerInfo *target;
1194   struct P2PPendingMessage *pending;
1195   size_t msize;
1196   struct PeerPutMessage *ppm;
1197   struct GNUNET_PeerIdentity *pp;
1198   
1199   GNUNET_assert (NULL != bf);
1200   GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity.hashPubKey);
1201   GNUNET_STATISTICS_update (GDS_stats,
1202                             gettext_noop ("# PUT requests routed"), 1,
1203                             GNUNET_NO);
1204   target_count = get_target_peers (key, bf, hop_count,
1205                                    desired_replication_level,
1206                                    &targets);
1207   if (0 == target_count)
1208     return;
1209   msize = put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + sizeof (struct PeerPutMessage);
1210   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1211   {
1212     put_path_length = 0;
1213     msize = data_size + sizeof (struct PeerPutMessage);
1214   }
1215   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1216   {
1217     GNUNET_break (0);
1218     return;
1219   }
1220   GNUNET_STATISTICS_update (GDS_stats,
1221                             gettext_noop ("# Peers selected as targets for PUT requests"), target_count,
1222                             GNUNET_NO);
1223   for (i=0;i<target_count;i++)
1224   {
1225     target = targets[i];
1226     pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize);
1227     pending->importance = 0; /* FIXME */
1228     pending->timeout = expiration_time;   
1229     ppm = (struct PeerPutMessage*) &pending[1];
1230     pending->msg = &ppm->header;
1231     ppm->header.size = htons (msize);
1232     ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT);
1233     ppm->options = htonl (options);
1234     ppm->type = htonl (type);
1235     ppm->hop_count = htonl (hop_count + 1);
1236     ppm->desired_replication_level = htonl (desired_replication_level);
1237     ppm->put_path_length = htonl (put_path_length);
1238     ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1239     GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &target->id.hashPubKey));
1240     GNUNET_assert (GNUNET_OK ==
1241                    GNUNET_CONTAINER_bloomfilter_get_raw_data (bf,
1242                                                               ppm->bloomfilter,
1243                                                               DHT_BLOOM_SIZE));
1244     ppm->key = *key;
1245     pp = (struct GNUNET_PeerIdentity*) &ppm[1];
1246     memcpy (pp, put_path, sizeof (struct GNUNET_PeerIdentity) * put_path_length);
1247     memcpy (&pp[put_path_length], data, data_size);
1248     GNUNET_CONTAINER_DLL_insert_tail (target->head,
1249                                       target->tail,
1250                                       pending);
1251     target->pending_count++;
1252     process_peer_queue (target);
1253   }
1254   GNUNET_free (targets);
1255 }
1256
1257
1258 /**
1259  * Perform a GET operation.  Forwards the given request to other
1260  * peers.  Does not lookup the key locally.  May do nothing if this is
1261  * the only peer in the network (or if we are the closest peer in the
1262  * network).
1263  *
1264  * @param type type of the block
1265  * @param options routing options
1266  * @param desired_replication_level desired replication count
1267  * @param hop_count how many hops did this request traverse so far?
1268  * @param key key for the content
1269  * @param xquery extended query
1270  * @param xquery_size number of bytes in xquery
1271  * @param reply_bf bloomfilter to filter duplicates
1272  * @param reply_bf_mutator mutator for reply_bf
1273  * @param peer_bf filter for peers not to select (again)
1274  */
1275 void
1276 GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type,
1277                            enum GNUNET_DHT_RouteOption options,
1278                            uint32_t desired_replication_level,
1279                            uint32_t hop_count,
1280                            const GNUNET_HashCode *key,
1281                            const void *xquery,
1282                            size_t xquery_size,
1283                            const struct GNUNET_CONTAINER_BloomFilter *reply_bf,
1284                            uint32_t reply_bf_mutator,
1285                            struct GNUNET_CONTAINER_BloomFilter *peer_bf)
1286 {
1287   unsigned int target_count;
1288   unsigned int i;
1289   struct PeerInfo **targets;
1290   struct PeerInfo *target;
1291   struct P2PPendingMessage *pending;
1292   size_t msize;
1293   struct PeerGetMessage *pgm;
1294   char *xq;
1295   size_t reply_bf_size;
1296
1297   GNUNET_assert (NULL != peer_bf);  
1298   GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity.hashPubKey);
1299   GNUNET_STATISTICS_update (GDS_stats,
1300                             gettext_noop ("# GET requests routed"), 1,
1301                             GNUNET_NO);
1302   target_count = get_target_peers (key, peer_bf, hop_count,
1303                                    desired_replication_level,
1304                                    &targets);
1305   if (0 == target_count)
1306     return;
1307   reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf);
1308   msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size;
1309   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1310   {
1311     GNUNET_break (0);
1312     return;
1313   }
1314   GNUNET_STATISTICS_update (GDS_stats,
1315                             gettext_noop ("# Peers selected as targets for GET requests"), target_count,
1316                             GNUNET_NO);
1317   /* forward request */
1318   for (i=0;i<target_count;i++)
1319   {
1320     target = targets[i];
1321     pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); 
1322     pending->importance = 0; /* FIXME */
1323     pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT);
1324     pgm = (struct PeerGetMessage*) &pending[1];
1325     pending->msg = &pgm->header;
1326     pgm->header.size = htons (msize);
1327     pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET);
1328     pgm->options = htonl (options);
1329     pgm->type = htonl (type);
1330     pgm->hop_count = htonl (hop_count + 1);
1331     pgm->desired_replication_level = htonl (desired_replication_level);
1332     pgm->xquery_size = htonl (xquery_size);
1333     pgm->bf_mutator = reply_bf_mutator; 
1334     GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (peer_bf, &target->id.hashPubKey));
1335     GNUNET_assert (GNUNET_OK ==
1336                    GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf,
1337                                                               pgm->bloomfilter,
1338                                                               DHT_BLOOM_SIZE));
1339     pgm->key = *key;
1340     xq = (char *) &pgm[1];
1341     memcpy (xq, xquery, xquery_size);
1342     if (NULL != reply_bf)
1343       GNUNET_assert (GNUNET_OK ==
1344                      GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf,
1345                                                                 &xq[xquery_size],
1346                                                                 reply_bf_size));
1347     GNUNET_CONTAINER_DLL_insert_tail (target->head,
1348                                       target->tail,
1349                                       pending);
1350     target->pending_count++;
1351     process_peer_queue (target);
1352   }
1353   GNUNET_free (targets);
1354 }
1355
1356
1357 /**
1358  * Handle a reply (route to origin).  Only forwards the reply back to
1359  * the given peer.  Does not do local caching or forwarding to local
1360  * clients.
1361  *
1362  * @param target neighbour that should receive the block (if still connected)
1363  * @param type type of the block
1364  * @param expiration_time when does the content expire
1365  * @param key key for the content
1366  * @param put_path_length number of entries in put_path
1367  * @param put_path peers the original PUT traversed (if tracked)
1368  * @param get_path_length number of entries in put_path
1369  * @param get_path peers this reply has traversed so far (if tracked)
1370  * @param data payload of the reply
1371  * @param data_size number of bytes in data
1372  */
1373 void
1374 GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target,
1375                              enum GNUNET_BLOCK_Type type,
1376                              struct GNUNET_TIME_Absolute expiration_time,
1377                              const GNUNET_HashCode *key,
1378                              unsigned int put_path_length,
1379                              const struct GNUNET_PeerIdentity *put_path,
1380                              unsigned int get_path_length,
1381                              const struct GNUNET_PeerIdentity *get_path,
1382                              const void *data,
1383                              size_t data_size)
1384 {
1385   struct PeerInfo *pi;
1386   struct P2PPendingMessage *pending;
1387   size_t msize;
1388   struct PeerResultMessage *prm;
1389   struct GNUNET_PeerIdentity *paths;
1390   
1391   msize = data_size + sizeof (struct PeerResultMessage) + 
1392     (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
1393   if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1394        (get_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1395        (put_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1396        (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE) )
1397   {
1398     GNUNET_break (0);
1399     return;
1400   }
1401   pi = GNUNET_CONTAINER_multihashmap_get (all_known_peers,
1402                                           &target->hashPubKey);
1403   if (NULL == pi)
1404   {
1405     /* peer disconnected in the meantime, drop reply */
1406     return;
1407   }
1408   GNUNET_STATISTICS_update (GDS_stats,
1409                             gettext_noop ("# REPLIES routed"), 1,
1410                             GNUNET_NO);
1411   pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); 
1412   pending->importance = 0; /* FIXME */
1413   pending->timeout = expiration_time;
1414   prm = (struct PeerResultMessage*) &pending[1];
1415   pending->msg = &prm->header;
1416   prm->header.size = htons (msize);
1417   prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT);
1418   prm->type = htonl (type);
1419   prm->put_path_length = htonl (put_path_length);
1420   prm->get_path_length = htonl (get_path_length);
1421   prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time);
1422   prm->key = *key;
1423   paths = (struct GNUNET_PeerIdentity*) &prm[1];
1424   memcpy (paths, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity));
1425   memcpy (&paths[put_path_length],
1426           get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
1427   memcpy (&paths[put_path_length + get_path_length],
1428           data, data_size);
1429   GNUNET_CONTAINER_DLL_insert (pi->head,
1430                                pi->tail,
1431                                pending);
1432   pi->pending_count++;
1433   process_peer_queue (pi);
1434 }
1435
1436
1437 /**
1438  * To be called on core init/fail.
1439  *
1440  * @param cls service closure
1441  * @param server handle to the server for this service
1442  * @param identity the public identity of this peer
1443  * @param publicKey the public key of this peer
1444  */
1445 static void
1446 core_init (void *cls, struct GNUNET_CORE_Handle *server,
1447            const struct GNUNET_PeerIdentity *identity,
1448            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1449 {
1450   GNUNET_assert (server != NULL);
1451   my_identity = *identity;
1452 }
1453
1454
1455 /**
1456  * Core handler for p2p put requests.
1457  *
1458  * @param cls closure
1459  * @param peer sender of the request
1460  * @param message message
1461  * @param peer peer identity this notification is about
1462  * @param atsi performance data
1463  * @return GNUNET_OK to keep the connection open,
1464  *         GNUNET_SYSERR to close it (signal serious error)
1465  */
1466 static int
1467 handle_dht_p2p_put (void *cls,
1468                     const struct GNUNET_PeerIdentity *peer,
1469                     const struct GNUNET_MessageHeader *message,
1470                     const struct GNUNET_TRANSPORT_ATS_Information
1471                     *atsi)
1472 {
1473   const struct PeerPutMessage *put;
1474   const struct GNUNET_PeerIdentity *put_path;
1475   const void *payload;
1476   uint32_t putlen;
1477   uint16_t msize;
1478   size_t payload_size;
1479   enum GNUNET_DHT_RouteOption options;
1480   struct GNUNET_CONTAINER_BloomFilter *bf;
1481   GNUNET_HashCode test_key;
1482   
1483   msize = ntohs (message->size);
1484   if (msize < sizeof (struct PeerPutMessage))
1485   {
1486     GNUNET_break_op (0);
1487     return GNUNET_YES;
1488   }
1489   put = (const struct PeerPutMessage*) message;
1490   putlen = ntohl (put->put_path_length);
1491   if ( (msize < sizeof (struct PeerPutMessage) + putlen * sizeof (struct GNUNET_PeerIdentity)) ||
1492        (putlen > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) )
1493     {
1494       GNUNET_break_op (0);
1495       return GNUNET_YES;
1496     }
1497   GNUNET_STATISTICS_update (GDS_stats,
1498                             gettext_noop ("# P2P PUT requests received"), 1,
1499                             GNUNET_NO);
1500   put_path = (const struct GNUNET_PeerIdentity*) &put[1];  
1501   payload = &put_path[putlen];
1502   options = ntohl (put->options);
1503   payload_size = msize - (sizeof (struct PeerPutMessage) + 
1504                           putlen * sizeof (struct GNUNET_PeerIdentity));
1505   switch (GNUNET_BLOCK_get_key (GDS_block_context,
1506                                 ntohl (put->type),
1507                                 payload, payload_size,
1508                                 &test_key))
1509   {
1510   case GNUNET_YES:
1511     if (0 != memcmp (&test_key, &put->key, sizeof (GNUNET_HashCode)))
1512     {
1513       GNUNET_break_op (0);
1514       return GNUNET_YES;
1515     }
1516     break;
1517   case GNUNET_NO:
1518     GNUNET_break_op (0);
1519     return GNUNET_YES;
1520   case GNUNET_SYSERR:
1521     /* cannot verify, good luck */
1522     break;
1523   }
1524   bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter,
1525                                           DHT_BLOOM_SIZE,
1526                                           GNUNET_CONSTANTS_BLOOMFILTER_K);
1527   GNUNET_break_op (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &peer->hashPubKey));
1528   {
1529     struct GNUNET_PeerIdentity pp[putlen+1];
1530   
1531     /* extend 'put path' by sender */
1532     if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE))
1533     {
1534       memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity));
1535       pp[putlen] = *peer;
1536       putlen++;
1537     }
1538     else
1539       putlen = 0;
1540     
1541     /* give to local clients */
1542     GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1543                              &put->key,
1544                              0, NULL,
1545                              putlen,
1546                              pp,
1547                              ntohl (put->type),
1548                              payload_size,
1549                              payload);
1550     /* store locally */
1551     if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1552          (am_closest_peer (&put->key,
1553                            bf) ) )
1554       GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
1555                                 &put->key,
1556                                 putlen, pp,
1557                                 ntohl (put->type),
1558                                 payload_size,
1559                                 payload);
1560     /* route to other peers */
1561     GDS_NEIGHBOURS_handle_put (ntohl (put->type),
1562                                options,
1563                                ntohl (put->desired_replication_level),
1564                                GNUNET_TIME_absolute_ntoh (put->expiration_time),
1565                                ntohl (put->hop_count) + 1 /* who adds +1? */,
1566                                bf,
1567                                &put->key,
1568                                putlen, pp,
1569                                payload,
1570                                payload_size);
1571   }
1572   GNUNET_CONTAINER_bloomfilter_free (bf);
1573   return GNUNET_YES;
1574 }
1575
1576
1577 /**
1578  * We have received a FIND PEER request.  Send matching
1579  * HELLOs back.
1580  *
1581  * @param sender sender of the FIND PEER request
1582  * @param key peers close to this key are desired
1583  * @param bf peers matching this bf are excluded
1584  * @param bf_mutator mutator for bf
1585  */
1586 static void
1587 handle_find_peer (const struct GNUNET_PeerIdentity *sender,
1588                   const GNUNET_HashCode *key,
1589                   struct GNUNET_CONTAINER_BloomFilter *bf,
1590                   uint32_t bf_mutator)
1591 {
1592   int bucket_idx;
1593   struct PeerBucket *bucket;
1594   struct PeerInfo *peer;
1595   unsigned int choice;
1596   GNUNET_HashCode mhash;
1597   const struct GNUNET_HELLO_Message *hello;
1598
1599   /* first, check about our own HELLO */
1600   if (NULL != GDS_my_hello)
1601   {
1602     GNUNET_BLOCK_mingle_hash (&my_identity.hashPubKey, bf_mutator, &mhash);
1603     if ( (NULL == bf) ||
1604          (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) )
1605     {
1606       GDS_NEIGHBOURS_handle_reply (sender,
1607                                    GNUNET_BLOCK_TYPE_DHT_HELLO,
1608                                    GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1609                                    key,
1610                                    0, NULL,
1611                                    0, NULL,
1612                                    GDS_my_hello,
1613                                    GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message*) GDS_my_hello));
1614     }
1615     else
1616     {
1617       GNUNET_STATISTICS_update (GDS_stats,
1618                                 gettext_noop ("# FIND PEER requests ignored due to Bloomfilter"), 1,
1619                                 GNUNET_NO);
1620     }
1621   }
1622   else
1623   {
1624     GNUNET_STATISTICS_update (GDS_stats,
1625                               gettext_noop ("# FIND PEER requests ignored due to lack of HELLO"), 1,
1626                               GNUNET_NO);
1627   }
1628
1629   /* then, also consider sending a random HELLO from the closest bucket */
1630   if (0 == memcmp (&my_identity.hashPubKey, key, sizeof (GNUNET_HashCode)))
1631     bucket_idx = closest_bucket;
1632   else
1633     bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key));
1634   if (bucket_idx == GNUNET_SYSERR)
1635     return;
1636   bucket = &k_buckets[bucket_idx];
1637   if (bucket->peers_size == 0)
1638     return;
1639   choice = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1640                                      bucket->peers_size);
1641   peer = bucket->head;
1642   while (choice > 0)
1643   {
1644     GNUNET_assert (peer != NULL);
1645     peer = peer->next;
1646   }
1647   choice = bucket->peers_size;
1648   do
1649     {
1650       peer = peer->next;
1651       if (choice-- == 0)
1652         return; /* no non-masked peer available */
1653       if (peer == NULL)
1654         peer = bucket->head;
1655       GNUNET_BLOCK_mingle_hash (&peer->id.hashPubKey, bf_mutator, &mhash);
1656       hello = GDS_HELLO_get (&peer->id);
1657     }
1658   while ( (hello == NULL) ||
1659           (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash)) );
1660   GDS_NEIGHBOURS_handle_reply (sender,
1661                                GNUNET_BLOCK_TYPE_DHT_HELLO,
1662                                GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION),
1663                                key,
1664                                0, NULL,
1665                                0, NULL,
1666                                hello,
1667                                GNUNET_HELLO_size (hello));    
1668 }
1669
1670
1671 /**
1672  * Core handler for p2p get requests.
1673  *
1674  * @param cls closure
1675  * @param peer sender of the request
1676  * @param message message
1677  * @param peer peer identity this notification is about
1678  * @param atsi performance data
1679  * @return GNUNET_OK to keep the connection open,
1680  *         GNUNET_SYSERR to close it (signal serious error)
1681  */
1682 static int
1683 handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
1684                     const struct GNUNET_MessageHeader *message,
1685                     const struct GNUNET_TRANSPORT_ATS_Information
1686                     *atsi)
1687 {
1688   struct PeerGetMessage *get;
1689   uint32_t xquery_size;
1690   size_t reply_bf_size;
1691   uint16_t msize;
1692   enum GNUNET_BLOCK_Type type;
1693   enum GNUNET_DHT_RouteOption options;
1694   enum GNUNET_BLOCK_EvaluationResult eval;
1695   struct GNUNET_CONTAINER_BloomFilter *reply_bf;
1696   struct GNUNET_CONTAINER_BloomFilter *peer_bf;
1697   const char *xquery;
1698
1699   GNUNET_break (0 != memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)));
1700   /* parse and validate message */
1701   msize = ntohs (message->size);
1702   if (msize < sizeof (struct PeerGetMessage))
1703   {
1704     GNUNET_break_op (0);
1705     return GNUNET_YES;
1706   }
1707   get = (struct PeerGetMessage *) message;
1708   xquery_size = ntohl (get->xquery_size);
1709   if (msize < sizeof (struct PeerGetMessage) + xquery_size)
1710   {
1711     GNUNET_break_op (0);
1712     return GNUNET_YES;
1713   }
1714   GNUNET_STATISTICS_update (GDS_stats,
1715                             gettext_noop ("# P2P GET requests received"), 1,
1716                             GNUNET_NO);
1717   reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size);
1718   type = ntohl (get->type);
1719   options = ntohl (get->options);
1720   xquery = (const char*) &get[1];
1721   reply_bf = NULL;
1722   if (reply_bf_size > 0)
1723     reply_bf = GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size],
1724                                                   reply_bf_size,
1725                                                   GNUNET_CONSTANTS_BLOOMFILTER_K);
1726   eval = GNUNET_BLOCK_evaluate (GDS_block_context,
1727                                 type,
1728                                 &get->key,
1729                                 &reply_bf,
1730                                 get->bf_mutator,
1731                                 xquery, xquery_size,
1732                                 NULL, 0);
1733   if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID)
1734   {
1735     /* request invalid or block type not supported */
1736     GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED);
1737     if (NULL != reply_bf)
1738       GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1739     return GNUNET_YES;
1740   }
1741   peer_bf =
1742     GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, 
1743                                        DHT_BLOOM_SIZE,
1744                                        GNUNET_CONSTANTS_BLOOMFILTER_K);
1745   GNUNET_break_op (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (peer_bf, &peer->hashPubKey));
1746   /* remember request for routing replies */
1747   GDS_ROUTING_add (peer,
1748                    type,
1749                    options,
1750                    &get->key,
1751                    xquery, xquery_size,
1752                    reply_bf, get->bf_mutator);
1753
1754   /* local lookup (this may update the reply_bf) */
1755   if ( (0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
1756        (am_closest_peer (&get->key,
1757                          peer_bf) ) )
1758   {
1759     if ( (0 != (options & GNUNET_DHT_RO_FIND_PEER)))
1760     {
1761       GNUNET_STATISTICS_update (GDS_stats,
1762                                 gettext_noop ("# P2P FIND PEER requests processed"), 1,
1763                                 GNUNET_NO);
1764       handle_find_peer (peer,
1765                         &get->key,
1766                         reply_bf,
1767                         get->bf_mutator);
1768     }
1769     else
1770     {
1771       eval = GDS_DATACACHE_handle_get (&get->key,
1772                                        type,
1773                                        xquery, xquery_size,
1774                                        &reply_bf, 
1775                                        get->bf_mutator);
1776     }
1777   }
1778   else
1779   {
1780     GNUNET_STATISTICS_update (GDS_stats,
1781                               gettext_noop ("# P2P GET requests ONLY routed"), 1,
1782                               GNUNET_NO);
1783   }
1784   
1785   /* P2P forwarding */
1786   if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
1787     GDS_NEIGHBOURS_handle_get (type,
1788                                options,
1789                                ntohl (get->desired_replication_level),
1790                                ntohl (get->hop_count) + 1, /* CHECK: where (else) do we do +1? */
1791                                &get->key,
1792                                xquery, xquery_size,
1793                                reply_bf,
1794                                get->bf_mutator,
1795                                peer_bf);
1796   /* clean up */
1797   if (NULL != reply_bf)
1798     GNUNET_CONTAINER_bloomfilter_free (reply_bf);
1799   GNUNET_CONTAINER_bloomfilter_free (peer_bf);  
1800   return GNUNET_YES;
1801 }
1802
1803
1804 /**
1805  * Core handler for p2p result messages.
1806  *
1807  * @param cls closure
1808  * @param message message
1809  * @param peer peer identity this notification is about
1810  * @param atsi performance data
1811  * @return GNUNET_YES (do not cut p2p connection)
1812  */
1813 static int
1814 handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
1815                        const struct GNUNET_MessageHeader *message,
1816                        const struct GNUNET_TRANSPORT_ATS_Information
1817                        *atsi)
1818 {
1819   const struct PeerResultMessage *prm;
1820   const struct GNUNET_PeerIdentity *put_path;
1821   const struct GNUNET_PeerIdentity *get_path;
1822   const void *data;
1823   uint32_t get_path_length;
1824   uint32_t put_path_length;
1825   uint16_t msize;
1826   size_t data_size;
1827   enum GNUNET_BLOCK_Type type;
1828                        
1829   /* parse and validate message */
1830   msize = ntohs (message->size);
1831   if (msize < sizeof (struct PeerResultMessage))
1832   {
1833     GNUNET_break_op (0);
1834     return GNUNET_YES;
1835   }
1836   prm = (struct PeerResultMessage *) message;
1837   put_path_length = ntohl (prm->put_path_length);
1838   get_path_length = ntohl (prm->get_path_length);
1839   if ( (msize < sizeof (struct PeerResultMessage) + 
1840         (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity)) ||
1841        (get_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
1842        (put_path_length > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) )
1843   {
1844     GNUNET_break_op (0);
1845     return GNUNET_YES;
1846   } 
1847   GNUNET_STATISTICS_update (GDS_stats,
1848                             gettext_noop ("# P2P RESULTS received"), 1,
1849                             GNUNET_NO);
1850   put_path = (const struct GNUNET_PeerIdentity*) &prm[1];
1851   get_path = &put_path[put_path_length];
1852   type = ntohl (prm->type);
1853   data = (const void*) &get_path[get_path_length];
1854   data_size = msize - (sizeof (struct PeerResultMessage) + 
1855                        (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity));
1856
1857   /* if we got a HELLO, consider it for our own routing table */
1858   if (type == GNUNET_BLOCK_TYPE_DHT_HELLO)
1859   {
1860     const struct GNUNET_MessageHeader *h;
1861     struct GNUNET_PeerIdentity pid;
1862     int bucket;
1863
1864     /* Should be a HELLO, validate and consider using it! */
1865     if (data_size < sizeof (struct GNUNET_MessageHeader))
1866     {
1867       GNUNET_break_op (0);
1868       return GNUNET_YES;
1869     }
1870     h = data;
1871     if (data_size != ntohs (h->size))
1872     {
1873       GNUNET_break_op (0);
1874       return GNUNET_YES;
1875     }
1876     if (GNUNET_OK !=
1877         GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) h,
1878                              &pid))
1879     {
1880       GNUNET_break_op (0);
1881       return GNUNET_YES;
1882     }
1883     if (0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
1884     {
1885       bucket = find_bucket (&pid.hashPubKey);
1886       if ( (bucket >= 0) &&
1887            (k_buckets[bucket].peers_size < bucket_size) )
1888         {    
1889           if (NULL != GDS_transport_handle)
1890             GNUNET_TRANSPORT_offer_hello (GDS_transport_handle,
1891                                           h, NULL, NULL);
1892           (void) GNUNET_CORE_peer_request_connect (coreAPI,
1893                                                    &pid, 
1894                                                    NULL, NULL);
1895         }   
1896     }
1897   }
1898
1899   /* append 'peer' to 'get_path' */
1900   {    
1901     struct GNUNET_PeerIdentity xget_path[get_path_length+1];
1902
1903     memcpy (xget_path, get_path, get_path_length * sizeof (struct GNUNET_PeerIdentity));
1904     xget_path[get_path_length] = *peer;
1905     get_path_length++;
1906
1907     /* forward to local clients */   
1908     GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1909                              &prm->key,
1910                              get_path_length,
1911                              xget_path,
1912                              put_path_length,
1913                              put_path,
1914                              type,
1915                              data_size, 
1916                              data);
1917
1918     /* forward to other peers */
1919     GDS_ROUTING_process (type,
1920                          GNUNET_TIME_absolute_ntoh (prm->expiration_time),
1921                          &prm->key,
1922                          put_path_length,
1923                          put_path,
1924                          get_path_length,
1925                          xget_path,
1926                          data,
1927                          data_size);                     
1928   }
1929   return GNUNET_YES;
1930 }
1931
1932
1933 /**
1934  * Initialize neighbours subsystem.
1935  *
1936  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1937  */
1938 int
1939 GDS_NEIGHBOURS_init ()
1940 {
1941   static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1942     {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0},
1943     {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0},
1944     {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0},
1945     {NULL, 0, 0}
1946   };
1947   unsigned long long temp_config_num;
1948  
1949   if (GNUNET_OK ==
1950       GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size",
1951                                              &temp_config_num))
1952     bucket_size = (unsigned int) temp_config_num;  
1953   coreAPI = GNUNET_CORE_connect (GDS_cfg,
1954                                  1,
1955                                  NULL,
1956                                  &core_init,
1957                                  &handle_core_connect,
1958                                  &handle_core_disconnect, 
1959                                  NULL,  /* Do we care about "status" updates? */
1960                                  NULL, GNUNET_NO,
1961                                  NULL, GNUNET_NO,
1962                                  core_handlers);
1963   if (coreAPI == NULL)
1964     return GNUNET_SYSERR;
1965   all_known_peers = GNUNET_CONTAINER_multihashmap_create (256);
1966   return GNUNET_OK;
1967 }
1968
1969
1970 /**
1971  * Shutdown neighbours subsystem.
1972  */
1973 void
1974 GDS_NEIGHBOURS_done ()
1975 {
1976   if (coreAPI == NULL)
1977     return;
1978   GNUNET_CORE_disconnect (coreAPI);
1979   coreAPI = NULL;    
1980   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (all_known_peers));
1981   GNUNET_CONTAINER_multihashmap_destroy (all_known_peers);
1982   all_known_peers = NULL;
1983   if (GNUNET_SCHEDULER_NO_TASK != find_peer_task)
1984   {
1985     GNUNET_SCHEDULER_cancel (find_peer_task);
1986     find_peer_task = GNUNET_SCHEDULER_NO_TASK;
1987   }
1988 }
1989
1990
1991 /* end of gnunet-service-dht_neighbours.c */