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