561aa0a778540e7f36d14065336273eb79f550ef
[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). FIXME: redundant!?
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_log (GNUNET_ERROR_TYPE_DEBUG,
235               "Starting to process replies from DHT\n");
236   GNUNET_CLIENT_receive (handle->client,
237                          &service_message_handler,
238                          handle, 
239                          GNUNET_TIME_UNIT_FOREVER_REL);
240   return GNUNET_YES;
241 }
242
243
244 /**
245  * Add the request corresponding to the given route handle
246  * to the pending queue (if it is not already in there).
247  *
248  * @param cls the 'struct GNUNET_DHT_Handle*'
249  * @param key key for the request (not used)
250  * @param value the 'struct GNUNET_DHT_RouteHandle*'
251  * @return GNUNET_YES (always)
252  */
253 static int
254 add_request_to_pending (void *cls,
255                         const GNUNET_HashCode *key,
256                         void *value)
257 {
258   struct GNUNET_DHT_Handle *handle = cls;
259   struct GNUNET_DHT_RouteHandle *rh = value;
260
261   if (GNUNET_NO == rh->message->in_pending_queue)
262     {
263       GNUNET_CONTAINER_DLL_insert (handle->pending_head,
264                                    handle->pending_tail,
265                                    rh->message);
266       rh->message->in_pending_queue = GNUNET_YES;
267     }
268   return GNUNET_YES;
269 }
270
271
272 /**
273  * Re-connect to the DHT, re-issue all pending requests if needed.
274  */
275 static void
276 reconnect (struct GNUNET_DHT_Handle *handle)
277 {
278   if (handle->client != NULL)
279     {
280       GNUNET_CLIENT_disconnect (handle->client, 
281                                 GNUNET_NO);
282       handle->client = NULL;
283     }
284   if (GNUNET_YES != try_connect (handle))
285     return;
286   GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
287                                          &add_request_to_pending,
288                                          handle);
289   if (handle->pending_head == NULL)
290     return;
291   GNUNET_CLIENT_notify_transmit_ready (handle->client,
292                                        ntohs(handle->pending_head->msg->size),
293                                        GNUNET_TIME_UNIT_FOREVER_REL,
294                                        GNUNET_NO,
295                                        &transmit_pending,
296                                        handle);
297                                        
298 }
299
300
301 /**
302  * Try to send messages from list of messages to send
303  */
304 static void
305 process_pending_messages (struct GNUNET_DHT_Handle *handle)
306 {
307   struct PendingMessage *head;
308
309   if (GNUNET_YES != try_connect (handle))
310     return;      
311   if (handle->th != NULL)
312     return;
313   if (NULL == (head = handle->pending_head))
314     return;
315   handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
316                                                     ntohs (head->msg->size),
317                                                     GNUNET_TIME_UNIT_FOREVER_REL, 
318                                                     GNUNET_YES,
319                                                     &transmit_pending,
320                                                     handle);
321   if (NULL == handle->th)    
322     {
323       reconnect (handle);
324       return;
325     }
326 }
327
328
329 /**
330  * Transmit the next pending message, called by notify_transmit_ready
331  */
332 static size_t
333 transmit_pending (void *cls,
334                   size_t size, 
335                   void *buf)
336 {
337   struct GNUNET_DHT_Handle *handle = cls;
338   struct PendingMessage *head;
339   size_t tsize;
340
341   handle->th = NULL;
342   if (buf == NULL)
343     {
344       reconnect (handle);
345       return 0;
346     }
347   if (NULL == (head = handle->pending_head))
348     return 0;
349   
350   tsize = ntohs (head->msg->size);
351   if (size < tsize)
352     {
353       process_pending_messages (handle);
354       return 0;
355     }
356   memcpy (buf, head->msg, tsize);
357   GNUNET_CONTAINER_DLL_remove (handle->pending_head,
358                                handle->pending_tail,
359                                head);
360   if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
361     {
362       GNUNET_SCHEDULER_cancel (handle->sched,
363                                head->timeout_task);
364       head->timeout_task = GNUNET_SCHEDULER_NO_TASK;
365     }
366   if (NULL != head->cont)
367     {
368       GNUNET_SCHEDULER_add_continuation (handle->sched,
369                                          head->cont,
370                                          head->cont_cls,
371                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
372       head->cont = NULL;
373       head->cont_cls = NULL;
374     }
375   head->in_pending_queue = GNUNET_NO;
376   if (GNUNET_YES == head->free_on_send)
377     GNUNET_free (head);
378   process_pending_messages (handle);
379   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380               "Forwarded request of %u bytes to DHT service\n",
381               (unsigned int) tsize);
382   return tsize;
383 }
384
385
386
387
388 /**
389  * Process a given reply that might match the given
390  * request.
391  */
392 static int
393 process_reply (void *cls,
394                const GNUNET_HashCode *key,
395                void *value)
396 {
397   const struct GNUNET_DHT_RouteResultMessage *dht_msg = cls;
398   struct GNUNET_DHT_RouteHandle *rh = value;
399   const struct GNUNET_MessageHeader *enc_msg;
400   size_t enc_size;
401   uint64_t uid;
402   const struct GNUNET_PeerIdentity **get_path;
403   const struct GNUNET_PeerIdentity **put_path;
404   const struct GNUNET_PeerIdentity *pos;
405   uint16_t gpl;
406   uint16_t ppl;
407   unsigned int i;
408
409   uid = GNUNET_ntohll (dht_msg->unique_id);
410   if (uid != rh->uid)
411     {
412       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413                   "Reply UID did not match request UID\n");
414       return GNUNET_YES;
415     }
416   enc_size = ntohs (dht_msg->header.size) - sizeof (struct GNUNET_DHT_RouteResultMessage);
417   if (enc_size < sizeof (struct GNUNET_MessageHeader))
418     {
419       GNUNET_break (0);
420       return GNUNET_NO;
421     }
422   pos = (const struct GNUNET_PeerIdentity *) &dht_msg[1];
423   ppl = ntohs (dht_msg->put_path_length);
424   gpl = ntohs (dht_msg->get_path_length);
425   if ( (ppl + gpl) * sizeof (struct GNUNET_PeerIdentity) > enc_size)
426     {
427       GNUNET_break (0);
428       return GNUNET_NO;
429     }
430   if (ppl > 0)
431     {
432       put_path = GNUNET_malloc ((ppl+1) * sizeof (struct GNUNET_PeerIdentity*));
433       for (i=0;i<ppl;i++)
434         {
435           put_path[i] = pos;
436           pos++;
437         }
438       put_path[ppl] = NULL;
439     }
440   else
441     put_path = NULL;
442   if (gpl > 0)
443     {
444       get_path = GNUNET_malloc ((gpl+1) * sizeof (struct GNUNET_PeerIdentity*));
445       for (i=0;i<gpl;i++)
446         {
447           get_path[i] = pos;
448           pos++;
449         }
450       get_path[gpl] = NULL;
451     }
452   else
453     get_path = NULL;
454   enc_size -= (ppl + gpl) * sizeof (struct GNUNET_PeerIdentity);
455   enc_msg = (const struct GNUNET_MessageHeader *) pos;
456   if (enc_size != ntohs (enc_msg->size))
457     {
458       GNUNET_break (0);
459       GNUNET_free_non_null (get_path);
460       GNUNET_free_non_null (put_path);
461       return GNUNET_NO;
462     }
463   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464               "Processing reply.\n");
465   rh->iter (rh->iter_cls, 
466             &rh->key,
467             get_path,
468             put_path,
469             enc_msg);
470   GNUNET_free_non_null (get_path);
471   GNUNET_free_non_null (put_path);
472   return GNUNET_YES;
473 }
474
475
476 /**
477  * Handler for messages received from the DHT service
478  * a demultiplexer which handles numerous message types
479  *
480  * @param cls the 'struct GNUNET_DHT_Handle'
481  * @param msg the incoming message
482  */
483 static void
484 service_message_handler (void *cls,
485                          const struct GNUNET_MessageHeader *msg)
486 {
487   struct GNUNET_DHT_Handle *handle = cls;
488   const struct GNUNET_DHT_RouteResultMessage *dht_msg;
489
490   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491               "%s called\n",
492               __FUNCTION__);  
493   if (msg == NULL)
494     {
495       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496                   "Error receiving data from DHT service, reconnecting\n");
497       reconnect (handle);
498       return;
499     }
500   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT)
501     {
502       GNUNET_break (0);
503       reconnect (handle);
504       return;
505     }
506   if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_RouteResultMessage))
507     {
508       GNUNET_break (0);
509       reconnect (handle);
510       return;
511     }
512   dht_msg = (const struct GNUNET_DHT_RouteResultMessage *) msg;
513   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514               "Comparing reply `%s' against %u pending requests.\n",
515               GNUNET_h2s (&dht_msg->key),
516               GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
517   GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
518                                               &dht_msg->key,
519                                               &process_reply,
520                                               (void*) dht_msg);
521   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522               "Continuing to process replies from DHT\n");
523   GNUNET_CLIENT_receive (handle->client,
524                          &service_message_handler,
525                          handle, GNUNET_TIME_UNIT_FOREVER_REL);
526
527 }
528
529
530 /**
531  * Initialize the connection with the DHT service.
532  *
533  * @param sched scheduler to use
534  * @param cfg configuration to use
535  * @param ht_len size of the internal hash table to use for
536  *               processing multiple GET/FIND requests in parallel
537  *
538  * @return handle to the DHT service, or NULL on error
539  */
540 struct GNUNET_DHT_Handle *
541 GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
542                     const struct GNUNET_CONFIGURATION_Handle *cfg,
543                     unsigned int ht_len)
544 {
545   struct GNUNET_DHT_Handle *handle;
546
547   handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
548   handle->cfg = cfg;
549   handle->sched = sched;
550   handle->uid_gen = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
551   handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
552   if (GNUNET_NO == try_connect (handle))
553     {
554       GNUNET_DHT_disconnect (handle);
555       return NULL;
556     }
557   return handle;
558 }
559
560
561 /**
562  * Shutdown connection with the DHT service.
563  *
564  * @param handle handle of the DHT connection to stop
565  */
566 void
567 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
568 {
569   struct PendingMessage *pm;
570
571   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size(handle->active_requests));
572   if (handle->th != NULL)
573     {
574       GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
575       handle->th = NULL;
576     }
577   while (NULL != (pm = handle->pending_head))
578     {
579       GNUNET_CONTAINER_DLL_remove (handle->pending_head,
580                                    handle->pending_tail,
581                                    pm);
582       GNUNET_assert (GNUNET_YES == pm->free_on_send);
583       if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
584         GNUNET_SCHEDULER_cancel (handle->sched,
585                                  pm->timeout_task);
586       if (NULL != pm->cont)
587         GNUNET_SCHEDULER_add_continuation (handle->sched,
588                                            pm->cont,
589                                            pm->cont_cls,
590                                            GNUNET_SCHEDULER_REASON_TIMEOUT);
591       pm->in_pending_queue = GNUNET_NO;
592       GNUNET_free (pm);
593     }
594   if (handle->client != NULL)
595     {
596       GNUNET_CLIENT_disconnect (handle->client, GNUNET_YES);
597       handle->client = NULL;
598     }  
599   GNUNET_CONTAINER_multihashmap_destroy(handle->active_requests);
600   GNUNET_free (handle);
601 }
602
603
604
605
606 /* ***** Special low-level API providing generic routing abstraction ***** */
607
608
609 /**
610  * Timeout for the transmission of a fire&forget-request.  Clean it up.
611  *
612  * @param cls the 'struct PendingMessage'
613  * @param tc scheduler context
614  */
615 static void
616 timeout_route_request (void *cls,
617                        const struct GNUNET_SCHEDULER_TaskContext *tc)
618 {
619   struct PendingMessage *pending = cls;
620   struct GNUNET_DHT_Handle *handle;
621
622   if (pending->free_on_send != GNUNET_YES)
623     {
624       /* timeouts should only apply to fire & forget requests! */
625       GNUNET_break (0);
626       return;
627     }
628   handle = pending->handle;
629   GNUNET_CONTAINER_DLL_remove (handle->pending_head,
630                                handle->pending_tail,
631                                pending);
632   if (pending->cont != NULL)
633     pending->cont (pending->cont_cls,
634                    tc);
635   GNUNET_free (pending);
636 }
637
638
639 /**
640  * Initiate a generic DHT route operation.
641  *
642  * @param handle handle to the DHT service
643  * @param key the key to look up
644  * @param desired_replication_level how many peers should ultimately receive
645  *                this message (advisory only, target may be too high for the
646  *                given DHT or not hit exactly).
647  * @param options options for routing
648  * @param enc send the encapsulated message to a peer close to the key
649  * @param iter function to call on each result, NULL if no replies are expected
650  * @param iter_cls closure for iter
651  * @param timeout when to abort with an error if we fail to get
652  *                a confirmation for the request (when necessary) or how long
653  *                to wait for tramission to the service; only applies
654  *                if 'iter' is NULL
655  * @param cont continuation to call when the request has been transmitted
656  *             the first time to the service
657  * @param cont_cls closure for cont
658  * @return handle to stop the request, NULL if the request is "fire and forget"
659  */
660 struct GNUNET_DHT_RouteHandle *
661 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
662                         const GNUNET_HashCode *key,
663                         uint32_t desired_replication_level,
664                         enum GNUNET_DHT_RouteOption options,
665                         const struct GNUNET_MessageHeader *enc,
666                         struct GNUNET_TIME_Relative timeout,
667                         GNUNET_DHT_ReplyProcessor iter,
668                         void *iter_cls,
669                         GNUNET_SCHEDULER_Task cont,
670                         void *cont_cls)
671 {
672   struct PendingMessage *pending;
673   struct GNUNET_DHT_RouteMessage *message;
674   struct GNUNET_DHT_RouteHandle *route_handle;
675   uint16_t msize;
676   uint16_t esize;
677
678   esize = ntohs (enc->size);
679   if (sizeof (struct GNUNET_DHT_RouteMessage) + esize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
680     {
681       GNUNET_break (0);
682       return NULL;
683     }
684   msize = sizeof (struct GNUNET_DHT_RouteMessage) + esize;
685   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
686   message = (struct GNUNET_DHT_RouteMessage*) &pending[1];
687   pending->msg = &message->header;
688   pending->handle = handle;
689   pending->cont = cont;
690   pending->cont_cls = cont_cls;
691   
692   message->header.size = htons (msize);
693   message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE);
694   message->key = *key;
695   message->options = htonl ((uint32_t) options);
696   message->desired_replication_level = htonl (desired_replication_level);
697   memcpy (&message[1], enc, esize);
698   if (iter != NULL)
699     {
700       route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
701       route_handle->key = *key;
702       route_handle->iter = iter;
703       route_handle->iter_cls = iter_cls;
704       route_handle->dht_handle = handle;
705       route_handle->uid = handle->uid_gen++;
706       route_handle->message = pending;
707       message->unique_id = GNUNET_htonll (route_handle->uid);
708       GNUNET_CONTAINER_multihashmap_put (handle->active_requests,
709                                          key,
710                                          route_handle,
711                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
712     }
713   else
714     {
715       route_handle = NULL;
716       pending->free_on_send = GNUNET_YES;
717       pending->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched,
718                                                             timeout,
719                                                             &timeout_route_request,
720                                                             pending);
721     }
722   GNUNET_CONTAINER_DLL_insert (handle->pending_head,
723                                handle->pending_tail,
724                                pending);
725   pending->in_pending_queue = GNUNET_YES;
726   process_pending_messages (handle);
727   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728               "DHT route start request processed, returning %p\n",
729               route_handle);
730   return route_handle;
731 }
732
733
734 /**
735  * Stop a previously issued routing request
736  *
737  * @param route_handle handle to the request to stop
738  */
739 void
740 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle)
741 {
742   struct GNUNET_DHT_Handle *handle;
743   struct PendingMessage *pending;
744   struct GNUNET_DHT_StopMessage *message;
745   size_t msize;
746
747   handle = route_handle->dht_handle;
748   if (GNUNET_NO == route_handle->message->in_pending_queue)
749     {
750       /* need to send stop message */
751       msize = sizeof (struct GNUNET_DHT_StopMessage);
752       pending = GNUNET_malloc (sizeof (struct PendingMessage) + 
753                                msize);
754       message = (struct GNUNET_DHT_StopMessage*) &pending[1];
755       pending->msg = &message->header;
756       message->header.size = htons (msize);
757       message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP);
758       message->unique_id = GNUNET_htonll (route_handle->uid);
759       message->key = route_handle->key;
760       pending->handle = handle;
761       pending->free_on_send = GNUNET_YES;
762       pending->in_pending_queue = GNUNET_YES;      
763       GNUNET_CONTAINER_DLL_insert (handle->pending_head,
764                                    handle->pending_tail,
765                                    pending);
766       process_pending_messages (handle);
767     }
768   else
769     {
770       /* simply remove pending request from message queue before
771          transmission, no need to transmit STOP request! */
772       GNUNET_CONTAINER_DLL_remove (handle->pending_head,
773                                    handle->pending_tail,
774                                    route_handle->message);
775     }
776   GNUNET_assert (GNUNET_YES ==
777                  GNUNET_CONTAINER_multihashmap_remove (route_handle->dht_handle->active_requests,
778                                                        &route_handle->key,
779                                                        route_handle));
780   GNUNET_free(route_handle->message);
781   GNUNET_free(route_handle);
782   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783               "DHT route stop request processed\n");
784 }
785
786
787
788 /* ***** Special API for controlling DHT routing maintenance ******* */
789
790
791 /**
792  * Send a control message to the DHT.
793  *
794  * @param handle handle to the DHT service
795  * @param command command
796  * @param variable variable to the command
797  * @param cont continuation to call when done (transmitting request to service)
798  * @param cont_cls closure for cont
799  */
800 static void
801 send_control_message (struct GNUNET_DHT_Handle *handle,
802                       uint16_t command,
803                       uint16_t variable,
804                       GNUNET_SCHEDULER_Task cont,
805                       void *cont_cls)
806 {
807   struct GNUNET_DHT_ControlMessage *msg;
808   struct PendingMessage *pending;
809
810   pending = GNUNET_malloc (sizeof (struct PendingMessage) + 
811                            sizeof(struct GNUNET_DHT_ControlMessage)); 
812   msg = (struct GNUNET_DHT_ControlMessage*) &pending[1];
813   pending->msg = &msg->header;
814   msg->header.size = htons (sizeof(struct GNUNET_DHT_ControlMessage));
815   msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CONTROL);
816   msg->command = htons (command);
817   msg->variable = htons (variable);
818   pending->free_on_send = GNUNET_YES;
819   pending->cont = cont;
820   pending->cont_cls = cont_cls;
821   pending->in_pending_queue = GNUNET_YES;      
822   GNUNET_CONTAINER_DLL_insert (handle->pending_head,
823                                handle->pending_tail,
824                                pending);
825   process_pending_messages (handle);
826 }
827
828
829 /**
830  * Send a message to the DHT telling it to issue a single find
831  * peer request using the peers unique identifier as key.  This
832  * is used to fill the routing table, and is normally controlled
833  * by the DHT itself.  However, for testing and perhaps more
834  * close control over the DHT, this can be explicitly managed.
835  *
836  * @param handle handle to the DHT service
837  * @param cont continuation to call when done (transmitting request to service)
838  * @param cont_cls closure for cont
839  */
840 void
841 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
842                        GNUNET_SCHEDULER_Task cont,
843                        void *cont_cls)
844 {
845   send_control_message (handle,
846                         GNUNET_MESSAGE_TYPE_DHT_FIND_PEER, 0,
847                         cont, cont_cls);
848 }
849
850
851
852 #if HAVE_MALICIOUS
853
854 /**
855  * Send a message to the DHT telling it to start issuing random GET
856  * requests every 'frequency' milliseconds.
857  *
858  * @param handle handle to the DHT service
859  * @param frequency delay between sending malicious messages
860  */
861 void
862 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
863                                  struct GNUNET_TIME_Relative frequency)
864 {
865   if (frequency.value > UINT16_MAX)
866     {
867       GNUNET_break (0);
868       return;
869     }
870   send_control_message (handle,
871                         GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET, frequency.value,
872                         NULL, NULL);
873 }
874
875 /**
876  * Send a message to the DHT telling it to start issuing random PUT
877  * requests every 'frequency' milliseconds.
878  *
879  * @param handle handle to the DHT service
880  * @param frequency delay between sending malicious messages
881  */
882 void 
883 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle, 
884                                  struct GNUNET_TIME_Relative frequency)
885 {
886   if (frequency.value > UINT16_MAX)
887     {
888       GNUNET_break (0);
889       return;
890     }
891   send_control_message (handle,
892                         GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT, frequency.value,
893                         NULL, NULL);
894 }
895
896
897 /**
898  * Send a message to the DHT telling it to start dropping
899  * all requests received.
900  *
901  * @param handle handle to the DHT service
902  */
903 void 
904 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle)
905 {
906   send_control_message (handle,
907                         GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP, 0,
908                         NULL, NULL);
909 }
910
911 #endif
912
913 /* end of dht_api.c */