c89a92dd53b12394b9d6a14d7bffbf0283a8845d
[oweals/gnunet.git] / src / dht / dht_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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/dht_api.c
23  * @brief library to access the DHT service
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27
28 #include "platform.h"
29 #include "gnunet_bandwidth_lib.h"
30 #include "gnunet_client_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_container_lib.h"
33 #include "gnunet_arm_service.h"
34 #include "gnunet_hello_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_time_lib.h"
38 #include "gnunet_dht_service.h"
39 #include "dht.h"
40
41 #define DEBUG_DHT_API GNUNET_NO
42
43 /**
44  * Entry in our list of messages to be (re-)transmitted.
45  */
46 struct PendingMessage
47 {
48   /**
49    * This is a doubly-linked list.
50    */
51   struct PendingMessage *prev;
52
53   /**
54    * This is a doubly-linked list.
55    */
56   struct PendingMessage *next;
57
58   /**
59    * Message that is pending, allocated at the end
60    * of this struct.
61    */
62   const struct GNUNET_MessageHeader *msg;
63   
64   /**
65    * Handle to the DHT API context.
66    */
67   struct GNUNET_DHT_Handle *handle;
68                        
69   /**
70    * Continuation to call when the request has been
71    * transmitted (for the first time) to the service; can be NULL.
72    */
73   GNUNET_SCHEDULER_Task cont;
74
75   /**
76    * Closure for 'cont'.
77    */
78   void *cont_cls;
79
80   /**
81    * Timeout task for this message
82    */
83   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
84
85   /**
86    * Unique ID for this request
87    */
88   uint64_t unique_id;
89
90   /**
91    * Free the saved message once sent, set to GNUNET_YES for messages
92    * that do not receive responses; GNUNET_NO if this pending message
93    * is aliased from a 'struct GNUNET_DHT_RouteHandle' and will be freed
94    * from there.
95    */
96   int free_on_send;
97
98   /**
99    * GNUNET_YES if this message is in our pending queue right now.
100    */
101   int in_pending_queue;
102
103 };
104
105
106 /**
107  * Handle to a route request
108  */
109 struct GNUNET_DHT_RouteHandle
110 {
111
112   /**
113    * Iterator to call on data receipt
114    */
115   GNUNET_DHT_ReplyProcessor iter;
116
117   /**
118    * Closure for the iterator callback
119    */
120   void *iter_cls;
121
122   /**
123    * Main handle to this DHT api
124    */
125   struct GNUNET_DHT_Handle *dht_handle;
126
127   /**
128    * The actual message sent for this request,
129    * used for retransmitting requests on service
130    * failure/reconnect.  Freed on route_stop.
131    */
132   struct PendingMessage *message;
133
134   /**
135    * Key that this get request is for
136    */
137   GNUNET_HashCode key;
138
139   /**
140    * Unique identifier for this request (for key collisions)
141    */
142   uint64_t uid;
143
144 };
145
146
147 /**
148  * Connection to the DHT service.
149  */
150 struct GNUNET_DHT_Handle
151 {
152   /**
153    * Our scheduler.
154    */
155   struct GNUNET_SCHEDULER_Handle *sched;
156
157   /**
158    * Configuration to use.
159    */
160   const struct GNUNET_CONFIGURATION_Handle *cfg;
161
162   /**
163    * Socket (if available).
164    */
165   struct GNUNET_CLIENT_Connection *client;
166
167   /**
168    * Currently pending transmission request (or NULL).
169    */
170   struct GNUNET_CLIENT_TransmitHandle *th;
171
172   /**
173    * Head of linked list of messages we would like to transmit.
174    */
175   struct PendingMessage *pending_head;
176
177   /**
178    * Tail of linked list of messages we would like to transmit.
179    */
180   struct PendingMessage *pending_tail;
181
182   /**
183    * Hash map containing the current outstanding unique requests
184    * (values are of type 'struct GNUNET_DHT_RouteHandle').
185    */
186   struct GNUNET_CONTAINER_MultiHashMap *active_requests;
187
188   /**
189    * Generator for unique ids.
190    */
191   uint64_t uid_gen;
192
193 };
194
195
196 /**
197  * Transmit the next pending message, called by notify_transmit_ready
198  */
199 static size_t
200 transmit_pending (void *cls,
201                   size_t size, 
202                   void *buf);
203
204
205 /**
206  * Handler for messages received from the DHT service
207  * a demultiplexer which handles numerous message types
208  *
209  */
210 static void
211 service_message_handler (void *cls,
212                          const struct GNUNET_MessageHeader *msg);
213
214
215
216
217 /**
218  * Try to (re)connect to the DHT service.
219  *
220  * @return GNUNET_YES on success, GNUNET_NO on failure.
221  */
222 static int
223 try_connect (struct GNUNET_DHT_Handle *handle)
224 {
225   if (handle->client != NULL)
226     return GNUNET_OK;
227   handle->client = GNUNET_CLIENT_connect (handle->sched, "dht", handle->cfg);
228   if (handle->client == NULL)
229     { 
230       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
231                   _("Failed to connect to the DHT service!\n"));
232       return GNUNET_NO;
233     }
234   GNUNET_CLIENT_receive (handle->client,
235                          &service_message_handler,
236                          handle, 
237                          GNUNET_TIME_UNIT_FOREVER_REL);
238   return GNUNET_YES;
239 }
240
241
242 /**
243  * Add the request corresponding to the given route handle
244  * to the pending queue (if it is not already in there).
245  *
246  * @param cls the 'struct GNUNET_DHT_Handle*'
247  * @param key key for the request (not used)
248  * @param value the 'struct GNUNET_DHT_RouteHandle*'
249  * @return GNUNET_YES (always)
250  */
251 static int
252 add_request_to_pending (void *cls,
253                         const GNUNET_HashCode *key,
254                         void *value)
255 {
256   struct GNUNET_DHT_Handle *handle = cls;
257   struct GNUNET_DHT_RouteHandle *rh = value;
258
259   if (GNUNET_NO == rh->message->in_pending_queue)
260     {
261       GNUNET_CONTAINER_DLL_insert (handle->pending_head,
262                                    handle->pending_tail,
263                                    rh->message);
264       rh->message->in_pending_queue = GNUNET_YES;
265     }
266   return GNUNET_YES;
267 }
268
269
270 /**
271  * Re-connect to the DHT, re-issue all pending requests if needed.
272  */
273 static void
274 reconnect (struct GNUNET_DHT_Handle *handle)
275 {
276   if (handle->client != NULL)
277     {
278       GNUNET_CLIENT_disconnect (handle->client, 
279                                 GNUNET_NO);
280       handle->client = NULL;
281     }
282   if (GNUNET_YES != try_connect (handle))
283     return;
284   GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
285                                          &add_request_to_pending,
286                                          handle);
287   if (handle->pending_head == NULL)
288     return;
289   GNUNET_CLIENT_notify_transmit_ready (handle->client,
290                                        ntohs(handle->pending_head->msg->size),
291                                        GNUNET_TIME_UNIT_FOREVER_REL,
292                                        GNUNET_NO,
293                                        &transmit_pending,
294                                        handle);
295                                        
296 }
297
298
299 /**
300  * Try to send messages from list of messages to send
301  */
302 static void
303 process_pending_messages (struct GNUNET_DHT_Handle *handle)
304 {
305   struct PendingMessage *head;
306
307   if (GNUNET_YES != try_connect (handle))
308     return;      
309   if (handle->th != NULL)
310     return;
311   if (NULL == (head = handle->pending_head))
312     return;
313   handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
314                                                     ntohs (head->msg->size),
315                                                     GNUNET_TIME_UNIT_FOREVER_REL, 
316                                                     GNUNET_YES,
317                                                     &transmit_pending,
318                                                     handle);
319   if (NULL == handle->th)    
320     {
321       reconnect (handle);
322       return;
323     }
324 }
325
326
327 /**
328  * Transmit the next pending message, called by notify_transmit_ready
329  */
330 static size_t
331 transmit_pending (void *cls,
332                   size_t size, 
333                   void *buf)
334 {
335   struct GNUNET_DHT_Handle *handle = cls;
336   struct PendingMessage *head;
337   size_t tsize;
338
339   handle->th = NULL;
340   if (buf == NULL)
341     {
342       reconnect (handle);
343       return 0;
344     }
345   if (NULL == (head = handle->pending_head))
346     return 0;
347   
348   tsize = ntohs (head->msg->size);
349   if (size < tsize)
350     {
351       process_pending_messages (handle);
352       return 0;
353     }
354   memcpy (buf, head->msg, tsize);
355   GNUNET_CONTAINER_DLL_remove (handle->pending_head,
356                                handle->pending_tail,
357                                head);
358   if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
359     {
360       GNUNET_SCHEDULER_cancel (handle->sched,
361                                head->timeout_task);
362       head->timeout_task = GNUNET_SCHEDULER_NO_TASK;
363     }
364   if (NULL != head->cont)
365     {
366       GNUNET_SCHEDULER_add_now (handle->sched,
367                                 head->cont,
368                                 head->cont_cls);                             
369       head->cont = NULL;
370       head->cont_cls = NULL;
371     }
372   head->in_pending_queue = GNUNET_NO;
373   if (GNUNET_YES == head->free_on_send)
374     GNUNET_free (head);
375   process_pending_messages (handle);
376   return tsize;
377 }
378
379
380
381
382 /**
383  * Process a given reply that might match the given
384  * request.
385  */
386 static int
387 process_reply (void *cls,
388                const GNUNET_HashCode *key,
389                void *value)
390 {
391   const struct GNUNET_DHT_RouteResultMessage *dht_msg = cls;
392   struct GNUNET_DHT_RouteHandle *rh = value;
393   const struct GNUNET_MessageHeader *enc_msg;
394   size_t enc_size;
395   uint64_t uid;
396
397   uid = GNUNET_ntohll (dht_msg->unique_id);
398   if (uid != rh->uid)
399     return GNUNET_YES;
400   enc_size = ntohs (dht_msg->header.size) - sizeof (struct GNUNET_DHT_RouteResultMessage);
401   if (enc_size < sizeof (struct GNUNET_MessageHeader))
402     {
403       GNUNET_break (0);
404       return GNUNET_NO;
405     }
406   enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
407   if (enc_size != ntohs (enc_msg->size))
408     {
409       GNUNET_break (0);
410       return GNUNET_NO;
411     }
412   rh->iter (rh->iter_cls, 
413             &rh->key,
414             enc_msg);
415   return GNUNET_YES;
416 }
417
418
419 /**
420  * Handler for messages received from the DHT service
421  * a demultiplexer which handles numerous message types
422  *
423  * @param cls the 'struct GNUNET_DHT_Handle'
424  * @param msg the incoming message
425  */
426 static void
427 service_message_handler (void *cls,
428                          const struct GNUNET_MessageHeader *msg)
429 {
430   struct GNUNET_DHT_Handle *handle = cls;
431   const struct GNUNET_DHT_RouteResultMessage *dht_msg;
432   
433   if (msg == NULL)
434     {
435       reconnect (handle);
436       return;
437     }
438   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT)
439     {
440       GNUNET_break (0);
441       reconnect (handle);
442       return;
443     }
444   if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_RouteResultMessage))
445     {
446       GNUNET_break (0);
447       reconnect (handle);
448       return;
449     }
450   dht_msg = (const struct GNUNET_DHT_RouteResultMessage *) msg;
451   GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
452                                               &dht_msg->key,
453                                               &process_reply,
454                                               (void*) dht_msg);
455   GNUNET_CLIENT_receive (handle->client,
456                          &service_message_handler,
457                          handle, GNUNET_TIME_UNIT_FOREVER_REL);
458
459 }
460
461
462 /**
463  * Initialize the connection with the DHT service.
464  *
465  * @param sched scheduler to use
466  * @param cfg configuration to use
467  * @param ht_len size of the internal hash table to use for
468  *               processing multiple GET/FIND requests in parallel
469  *
470  * @return handle to the DHT service, or NULL on error
471  */
472 struct GNUNET_DHT_Handle *
473 GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
474                     const struct GNUNET_CONFIGURATION_Handle *cfg,
475                     unsigned int ht_len)
476 {
477   struct GNUNET_DHT_Handle *handle;
478
479   handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
480   handle->cfg = cfg;
481   handle->sched = sched;
482   handle->uid_gen = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
483   handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
484   if (GNUNET_NO == try_connect (handle))
485     {
486       GNUNET_DHT_disconnect (handle);
487       return NULL;
488     }
489   return handle;
490 }
491
492
493 /**
494  * Shutdown connection with the DHT service.
495  *
496  * @param handle handle of the DHT connection to stop
497  */
498 void
499 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
500 {
501   struct PendingMessage *pm;
502
503   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size(handle->active_requests));
504   if (handle->th != NULL)
505     {
506       GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
507       handle->th = NULL;
508     }
509   while (NULL != (pm = handle->pending_head))
510     {
511       GNUNET_CONTAINER_DLL_remove (handle->pending_head,
512                                    handle->pending_tail,
513                                    pm);
514       GNUNET_assert (GNUNET_YES == pm->free_on_send);
515       if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
516         GNUNET_SCHEDULER_cancel (handle->sched,
517                                  pm->timeout_task);
518       if (NULL != pm->cont)
519         GNUNET_SCHEDULER_add_continuation (handle->sched,
520                                            pm->cont,
521                                            pm->cont_cls,
522                                            GNUNET_SCHEDULER_REASON_TIMEOUT);
523       pm->in_pending_queue = GNUNET_NO;
524       GNUNET_free (pm);
525     }
526   if (handle->client != NULL)
527     {
528       GNUNET_CLIENT_disconnect (handle->client, GNUNET_YES);
529       handle->client = NULL;
530     }  
531   GNUNET_CONTAINER_multihashmap_destroy(handle->active_requests);
532   GNUNET_free (handle);
533 }
534
535
536
537
538 /* ***** Special low-level API providing generic routeing abstraction ***** */
539
540
541 /**
542  * Timeout for the transmission of a fire&forget-request.  Clean it up.
543  *
544  * @param cls the 'struct PendingMessage'
545  * @param tc scheduler context
546  */
547 static void
548 timeout_route_request (void *cls,
549                        const struct GNUNET_SCHEDULER_TaskContext *tc)
550 {
551   struct PendingMessage *pending = cls;
552   struct GNUNET_DHT_Handle *handle;
553
554   if (pending->free_on_send != GNUNET_YES)
555     {
556       /* timeouts should only apply to fire & forget requests! */
557       GNUNET_break (0);
558       return;
559     }
560   handle = pending->handle;
561   GNUNET_CONTAINER_DLL_remove (handle->pending_head,
562                                handle->pending_tail,
563                                pending);
564   if (pending->cont != NULL)
565     pending->cont (pending->cont_cls,
566                    tc);
567   GNUNET_free (pending);
568 }
569
570
571 /**
572  * Initiate a generic DHT route operation.
573  *
574  * @param handle handle to the DHT service
575  * @param key the key to look up
576  * @param desired_replication_level how many peers should ultimately receive
577  *                this message (advisory only, target may be too high for the
578  *                given DHT or not hit exactly).
579  * @param options options for routing
580  * @param enc send the encapsulated message to a peer close to the key
581  * @param iter function to call on each result, NULL if no replies are expected
582  * @param iter_cls closure for iter
583  * @param timeout when to abort with an error if we fail to get
584  *                a confirmation for the request (when necessary) or how long
585  *                to wait for tramission to the service; only applies
586  *                if 'iter' is NULL
587  * @param cont continuation to call when the request has been transmitted
588  *             the first time to the service
589  * @param cont_cls closure for cont
590  * @return handle to stop the request, NULL if the request is "fire and forget"
591  */
592 struct GNUNET_DHT_RouteHandle *
593 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
594                         const GNUNET_HashCode * key,
595                         uint32_t desired_replication_level,
596                         enum GNUNET_DHT_RouteOption options,
597                         const struct GNUNET_MessageHeader *enc,
598                         struct GNUNET_TIME_Relative timeout,
599                         GNUNET_DHT_ReplyProcessor iter,
600                         void *iter_cls,
601                         GNUNET_SCHEDULER_Task cont,
602                         void *cont_cls)
603 {
604   struct PendingMessage *pending;
605   struct GNUNET_DHT_RouteMessage *message;
606   struct GNUNET_DHT_RouteHandle *route_handle;
607   uint16_t msize;
608   uint16_t esize;
609
610   esize = ntohs (enc->size);
611   if (sizeof (struct GNUNET_DHT_RouteMessage) + esize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
612     {
613       GNUNET_break (0);
614       return NULL;
615     }
616   msize = sizeof (struct GNUNET_DHT_RouteMessage) + esize;
617   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
618   message = (struct GNUNET_DHT_RouteMessage*) &pending[1];
619   pending->msg = &message->header;
620   pending->handle = handle;
621   pending->cont = cont;
622   pending->cont_cls = cont_cls;
623   
624   message->header.size = htons (msize);
625   message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE);
626   message->key = *key;
627   message->options = htonl ((uint32_t) options);
628   message->desired_replication_level = htonl (desired_replication_level);
629   memcpy (&message[1], enc, esize);
630   if (iter != NULL)
631     {
632       route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
633       route_handle->key = *key;
634       route_handle->iter = iter;
635       route_handle->iter_cls = iter_cls;
636       route_handle->dht_handle = handle;
637       route_handle->uid = handle->uid_gen++;
638       route_handle->message = pending;
639       message->unique_id = GNUNET_htonll (route_handle->uid);
640       GNUNET_CONTAINER_multihashmap_put (handle->active_requests,
641                                          key,
642                                          route_handle,
643                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
644     }
645   else
646     {
647       route_handle = NULL;
648       pending->free_on_send = GNUNET_YES;
649       pending->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched,
650                                                             timeout,
651                                                             &timeout_route_request,
652                                                             pending);
653     }
654   GNUNET_CONTAINER_DLL_insert (handle->pending_head,
655                                handle->pending_tail,
656                                pending);
657   pending->in_pending_queue = GNUNET_YES;
658   process_pending_messages (handle);
659   return route_handle;
660 }
661
662
663 /**
664  * Stop a previously issued routing request
665  *
666  * @param route_handle handle to the request to stop
667  */
668 void
669 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle)
670 {
671   struct GNUNET_DHT_Handle *handle;
672   struct PendingMessage *pending;
673   struct GNUNET_DHT_StopMessage *message;
674   size_t msize;
675
676   handle = route_handle->dht_handle;
677   if (GNUNET_NO == route_handle->message->in_pending_queue)
678     {
679       /* need to send stop message */
680       msize = sizeof (struct GNUNET_DHT_StopMessage);
681       pending = GNUNET_malloc (sizeof (struct PendingMessage) + 
682                                msize);
683       message = (struct GNUNET_DHT_StopMessage*) &pending[1];
684       pending->msg = &message->header;
685       message->header.size = htons (msize);
686       message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP);
687       message->unique_id = GNUNET_htonll (route_handle->uid);
688       message->key = route_handle->key;
689       pending->handle = handle;
690       pending->free_on_send = GNUNET_YES;
691       pending->in_pending_queue = GNUNET_YES;      
692       GNUNET_CONTAINER_DLL_insert (handle->pending_head,
693                                    handle->pending_tail,
694                                    pending);
695       process_pending_messages (handle);
696     }
697   else
698     {
699       /* simply remove pending request from message queue before
700          transmission, no need to transmit STOP request! */
701       GNUNET_CONTAINER_DLL_remove (handle->pending_head,
702                                    handle->pending_tail,
703                                    route_handle->message);
704     }
705   GNUNET_assert (GNUNET_YES ==
706                  GNUNET_CONTAINER_multihashmap_remove (route_handle->dht_handle->active_requests,
707                                                        &route_handle->key,
708                                                        route_handle));
709   GNUNET_free(route_handle->message);
710   GNUNET_free(route_handle);
711 }
712
713
714
715 /* ***** Special API for controlling DHT routing maintenance ******* */
716
717
718 /**
719  * Send a control message to the DHT.
720  *
721  * @param handle handle to the DHT service
722  * @param command command
723  * @param variable variable to the command
724  * @param cont continuation to call when done (transmitting request to service)
725  * @param cont_cls closure for cont
726  */
727 static void
728 send_control_message (struct GNUNET_DHT_Handle *handle,
729                       uint16_t command,
730                       uint16_t variable,
731                       GNUNET_SCHEDULER_Task cont,
732                       void *cont_cls)
733 {
734   struct GNUNET_DHT_ControlMessage *msg;
735   struct PendingMessage *pending;
736
737   pending = GNUNET_malloc (sizeof (struct PendingMessage) + 
738                            sizeof(struct GNUNET_DHT_ControlMessage)); 
739   msg = (struct GNUNET_DHT_ControlMessage*) &pending[1];
740   pending->msg = &msg->header;
741   msg->header.size = htons (sizeof(struct GNUNET_DHT_ControlMessage));
742   msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CONTROL);
743   msg->command = htons (command);
744   msg->variable = htons (variable);
745   pending->free_on_send = GNUNET_YES;
746   pending->cont = cont;
747   pending->cont_cls = cont_cls;
748   pending->in_pending_queue = GNUNET_YES;      
749   GNUNET_CONTAINER_DLL_insert (handle->pending_head,
750                                handle->pending_tail,
751                                pending);
752   process_pending_messages (handle);
753 }
754
755
756 /**
757  * Send a message to the DHT telling it to issue a single find
758  * peer request using the peers unique identifier as key.  This
759  * is used to fill the routing table, and is normally controlled
760  * by the DHT itself.  However, for testing and perhaps more
761  * close control over the DHT, this can be explicitly managed.
762  *
763  * @param handle handle to the DHT service
764  * @param cont continuation to call when done (transmitting request to service)
765  * @param cont_cls closure for cont
766  */
767 void
768 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
769                        GNUNET_SCHEDULER_Task cont,
770                        void *cont_cls)
771 {
772   send_control_message (handle,
773                         GNUNET_MESSAGE_TYPE_DHT_FIND_PEER, 0,
774                         cont, cont_cls);
775 }
776
777
778
779 #if HAVE_MALICIOUS
780
781 /**
782  * Send a message to the DHT telling it to start issuing random GET
783  * requests every 'frequency' milliseconds.
784  *
785  * @param handle handle to the DHT service
786  * @param frequency delay between sending malicious messages
787  */
788 void
789 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
790                                  struct GNUNET_TIME_Relative frequency)
791 {
792   if (frequency.value > UINT16_MAX)
793     {
794       GNUNET_break (0);
795       return;
796     }
797   send_control_message (handle,
798                         GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET, frequency.value,
799                         NULL, NULL);
800 }
801
802 /**
803  * Send a message to the DHT telling it to start issuing random PUT
804  * requests every 'frequency' milliseconds.
805  *
806  * @param handle handle to the DHT service
807  * @param frequency delay between sending malicious messages
808  */
809 void 
810 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle, 
811                                  struct GNUNET_TIME_Relative frequency)
812 {
813   if (frequency.value > UINT16_MAX)
814     {
815       GNUNET_break (0);
816       return;
817     }
818   send_control_message (handle,
819                         GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT, frequency.value,
820                         NULL, NULL);
821 }
822
823
824 /**
825  * Send a message to the DHT telling it to start dropping
826  * all requests received.
827  *
828  * @param handle handle to the DHT service
829  */
830 void 
831 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle)
832 {
833   send_control_message (handle,
834                         GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP, 0,
835                         NULL, NULL);
836 }
837
838 #endif
839
840 /* end of dht_api.c */