use long long
[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_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_dht_service.h"
35 #include "dht.h"
36
37 #define DEBUG_DHT_API GNUNET_EXTRA_LOGGING
38
39 #define LOG(kind,...) GNUNET_log_from (kind, "dht-api",__VA_ARGS__)
40
41 /**
42  * Entry in our list of messages to be (re-)transmitted.
43  */
44 struct PendingMessage
45 {
46   /**
47    * This is a doubly-linked list.
48    */
49   struct PendingMessage *prev;
50
51   /**
52    * This is a doubly-linked list.
53    */
54   struct PendingMessage *next;
55
56   /**
57    * Message that is pending, allocated at the end
58    * of this struct.
59    */
60   const struct GNUNET_MessageHeader *msg;
61
62   /**
63    * Handle to the DHT API context.
64    */
65   struct GNUNET_DHT_Handle *handle;
66
67   /**
68    * Continuation to call when the request has been
69    * transmitted (for the first time) to the service; can be NULL.
70    */
71   GNUNET_SCHEDULER_Task cont;
72
73   /**
74    * Closure for 'cont'.
75    */
76   void *cont_cls;
77
78   /**
79    * Timeout task for this message
80    */
81   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
82
83   /**
84    * Unique ID for this request
85    */
86   uint64_t unique_id;
87
88   /**
89    * Free the saved message once sent, set to GNUNET_YES for messages
90    * that do not receive responses; GNUNET_NO if this pending message
91    * is aliased from a 'struct GNUNET_DHT_RouteHandle' and will be freed
92    * from there.
93    */
94   int free_on_send;
95
96   /**
97    * GNUNET_YES if this message is in our pending queue right now.
98    */
99   int in_pending_queue;
100
101 };
102
103
104 /**
105  * Handle to a GET request
106  */
107 struct GNUNET_DHT_GetHandle
108 {
109
110   /**
111    * Iterator to call on data receipt
112    */
113   GNUNET_DHT_GetIterator iter;
114
115   /**
116    * Closure for the iterator callback
117    */
118   void *iter_cls;
119
120   /**
121    * Main handle to this DHT api
122    */
123   struct GNUNET_DHT_Handle *dht_handle;
124
125   /**
126    * The actual message sent for this request,
127    * used for retransmitting requests on service
128    * failure/reconnect.  Freed on route_stop.
129    */
130   struct PendingMessage *message;
131
132   /**
133    * Key that this get request is for
134    */
135   GNUNET_HashCode key;
136
137   /**
138    * Unique identifier for this request (for key collisions).
139    */
140   uint64_t unique_id;
141
142 };
143
144
145 /**
146  * Connection to the DHT service.
147  */
148 struct GNUNET_DHT_Handle
149 {
150
151   /**
152    * Configuration to use.
153    */
154   const struct GNUNET_CONFIGURATION_Handle *cfg;
155
156   /**
157    * Socket (if available).
158    */
159   struct GNUNET_CLIENT_Connection *client;
160
161   /**
162    * Currently pending transmission request (or NULL).
163    */
164   struct GNUNET_CLIENT_TransmitHandle *th;
165
166   /**
167    * Head of linked list of messages we would like to transmit.
168    */
169   struct PendingMessage *pending_head;
170
171   /**
172    * Tail of linked list of messages we would like to transmit.
173    */
174   struct PendingMessage *pending_tail;
175
176   /**
177    * Hash map containing the current outstanding unique requests
178    * (values are of type 'struct GNUNET_DHT_RouteHandle').
179    */
180   struct GNUNET_CONTAINER_MultiHashMap *active_requests;
181
182   /**
183    * Task for trying to reconnect.
184    */
185   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
186
187   /**
188    * How quickly should we retry?  Used for exponential back-off on
189    * connect-errors.
190    */
191   struct GNUNET_TIME_Relative retry_time;
192
193   /**
194    * Generator for unique ids.
195    */
196   uint64_t uid_gen;
197
198 };
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, const struct GNUNET_MessageHeader *msg);
208
209
210 /**
211  * Try to (re)connect to the DHT service.
212  *
213  * @return GNUNET_YES on success, GNUNET_NO on failure.
214  */
215 static int
216 try_connect (struct GNUNET_DHT_Handle *handle)
217 {
218   if (handle->client != NULL)
219     return GNUNET_OK;
220   handle->client = GNUNET_CLIENT_connect ("dht", handle->cfg);
221   if (handle->client == NULL)
222   {
223     LOG (GNUNET_ERROR_TYPE_WARNING,
224          _("Failed to connect to the DHT service!\n"));
225     return GNUNET_NO;
226   }
227 #if DEBUG_DHT
228   LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting to process replies from DHT\n");
229 #endif
230   GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
231                          GNUNET_TIME_UNIT_FOREVER_REL);
232   return GNUNET_YES;
233 }
234
235
236 /**
237  * Add the request corresponding to the given route handle
238  * to the pending queue (if it is not already in there).
239  *
240  * @param cls the 'struct GNUNET_DHT_Handle*'
241  * @param key key for the request (not used)
242  * @param value the 'struct GNUNET_DHT_GetHandle*'
243  * @return GNUNET_YES (always)
244  */
245 static int
246 add_request_to_pending (void *cls, const GNUNET_HashCode * key, void *value)
247 {
248   struct GNUNET_DHT_Handle *handle = cls;
249   struct GNUNET_DHT_GetHandle *rh = value;
250
251   if (GNUNET_NO == rh->message->in_pending_queue)
252   {
253     GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
254                                  rh->message);
255     rh->message->in_pending_queue = GNUNET_YES;
256   }
257   return GNUNET_YES;
258 }
259
260
261 /**
262  * Try to send messages from list of messages to send
263  * @param handle DHT_Handle
264  */
265 static void
266 process_pending_messages (struct GNUNET_DHT_Handle *handle);
267
268
269 /**
270  * Try reconnecting to the dht service.
271  *
272  * @param cls GNUNET_DHT_Handle
273  * @param tc scheduler context
274  */
275 static void
276 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
277 {
278   struct GNUNET_DHT_Handle *handle = cls;
279
280   handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
281   if (handle->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
282     handle->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
283   else
284     handle->retry_time = GNUNET_TIME_relative_multiply (handle->retry_time, 2);
285   if (handle->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
286     handle->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
287   handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
288   handle->client = GNUNET_CLIENT_connect ("dht", handle->cfg);
289   if (handle->client == NULL)
290   {
291     LOG (GNUNET_ERROR_TYPE_DEBUG, "dht reconnect failed(!)\n");
292     return;
293   }
294   GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
295                                          &add_request_to_pending, handle);
296   process_pending_messages (handle);
297 }
298
299
300 /**
301  * Try reconnecting to the DHT service.
302  *
303  * @param handle handle to dht to (possibly) disconnect and reconnect
304  */
305 static void
306 do_disconnect (struct GNUNET_DHT_Handle *handle)
307 {
308   if (handle->client == NULL)
309     return;
310   GNUNET_assert (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
311   if (NULL != handle->th)
312     GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
313   handle->th = NULL;
314   GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
315   handle->client = NULL;
316   handle->reconnect_task =
317       GNUNET_SCHEDULER_add_delayed (handle->retry_time, &try_reconnect, handle);
318 }
319
320
321 /**
322  * Transmit the next pending message, called by notify_transmit_ready
323  */
324 static size_t
325 transmit_pending (void *cls, size_t size, void *buf);
326
327
328 /**
329  * Try to send messages from list of messages to send
330  */
331 static void
332 process_pending_messages (struct GNUNET_DHT_Handle *handle)
333 {
334   struct PendingMessage *head;
335
336   if (handle->client == NULL)
337   {
338     do_disconnect (handle);
339     return;
340   }
341   if (handle->th != NULL)
342     return;
343   if (NULL == (head = handle->pending_head))
344     return;
345   handle->th =
346       GNUNET_CLIENT_notify_transmit_ready (handle->client,
347                                            ntohs (head->msg->size),
348                                            GNUNET_TIME_UNIT_FOREVER_REL,
349                                            GNUNET_YES, &transmit_pending,
350                                            handle);
351   if (NULL != handle->th)
352     return;
353   do_disconnect (handle);
354 }
355
356
357 /**
358  * Transmit the next pending message, called by notify_transmit_ready
359  */
360 static size_t
361 transmit_pending (void *cls, size_t size, void *buf)
362 {
363   struct GNUNET_DHT_Handle *handle = cls;
364   struct PendingMessage *head;
365   size_t tsize;
366
367   handle->th = NULL;
368   if (buf == NULL)
369   {
370     LOG (GNUNET_ERROR_TYPE_DEBUG,
371          "Transmission to DHT service failed!  Reconnecting!\n");
372     do_disconnect (handle);
373     return 0;
374   }
375   if (NULL == (head = handle->pending_head))
376     return 0;
377
378   tsize = ntohs (head->msg->size);
379   if (size < tsize)
380   {
381     process_pending_messages (handle);
382     return 0;
383   }
384   memcpy (buf, head->msg, tsize);
385   GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
386                                head);
387   if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
388   {
389     GNUNET_SCHEDULER_cancel (head->timeout_task);
390     head->timeout_task = GNUNET_SCHEDULER_NO_TASK;
391   }
392   if (NULL != head->cont)
393   {
394     GNUNET_SCHEDULER_add_continuation (head->cont, head->cont_cls,
395                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
396     head->cont = NULL;
397     head->cont_cls = NULL;
398   }
399   head->in_pending_queue = GNUNET_NO;
400   if (GNUNET_YES == head->free_on_send)
401     GNUNET_free (head);
402   process_pending_messages (handle);
403 #if DEBUG_DHT
404   LOG (GNUNET_ERROR_TYPE_DEBUG,
405        "Forwarded request of %u bytes to DHT service\n", (unsigned int) tsize);
406 #endif
407   return tsize;
408 }
409
410
411 /**
412  * Process a given reply that might match the given
413  * request.
414  *
415  * @param cls the 'struct GNUNET_DHT_ClientResultMessage'
416  * @param key query of the request
417  * @param value the 'struct GNUNET_DHT_RouteHandle' of a request matching the same key
418  * @return GNUNET_YES to continue to iterate over all results,
419  *         GNUNET_NO if the reply is malformed
420  */
421 static int
422 process_reply (void *cls, const GNUNET_HashCode * key, void *value)
423 {
424   const struct GNUNET_DHT_ClientResultMessage *dht_msg = cls;
425   struct GNUNET_DHT_GetHandle *get_handle = value;
426   const struct GNUNET_PeerIdentity *put_path;
427   const struct GNUNET_PeerIdentity *get_path;
428   uint32_t put_path_length;
429   uint32_t get_path_length;
430   size_t data_length;
431   size_t msize;
432   size_t meta_length;
433   const void *data;
434
435   if (dht_msg->unique_id != get_handle->unique_id)
436   {
437     /* UID mismatch */
438     LOG (GNUNET_ERROR_TYPE_DEBUG, "Ignoring reply (UID mismatch: %llu/%llu)\n",
439          dht_msg->unique_id, get_handle->unique_id);
440     return GNUNET_YES;
441   }
442   msize = ntohs (dht_msg->header.size);
443   put_path_length = ntohl (dht_msg->put_path_length);
444   get_path_length = ntohl (dht_msg->get_path_length);
445   meta_length =
446       sizeof (struct GNUNET_DHT_ClientResultMessage) +
447       sizeof (struct GNUNET_PeerIdentity) * (get_path_length + put_path_length);
448   if ((msize < meta_length) ||
449       (get_path_length >
450        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
451       (put_path_length >
452        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
453   {
454     GNUNET_break (0);
455     return GNUNET_NO;
456   }
457   data_length = msize - meta_length;
458   put_path = (const struct GNUNET_PeerIdentity *) &dht_msg[1];
459   get_path = &put_path[put_path_length];
460   data = &get_path[get_path_length];
461   get_handle->iter (get_handle->iter_cls,
462                     GNUNET_TIME_absolute_ntoh (dht_msg->expiration), key,
463                     get_path, get_path_length, put_path, put_path_length,
464                     ntohl (dht_msg->type), data_length, data);
465   return GNUNET_YES;
466 }
467
468
469 /**
470  * Handler for messages received from the DHT service
471  * a demultiplexer which handles numerous message types
472  *
473  * @param cls the 'struct GNUNET_DHT_Handle'
474  * @param msg the incoming message
475  */
476 static void
477 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
478 {
479   struct GNUNET_DHT_Handle *handle = cls;
480   const struct GNUNET_DHT_ClientResultMessage *dht_msg;
481
482   if (msg == NULL)
483   {
484     LOG (GNUNET_ERROR_TYPE_DEBUG,
485          "Error receiving data from DHT service, reconnecting\n");
486     do_disconnect (handle);
487     return;
488   }
489   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT)
490   {
491     GNUNET_break (0);
492     do_disconnect (handle);
493     return;
494   }
495   if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_ClientResultMessage))
496   {
497     GNUNET_break (0);
498     do_disconnect (handle);
499     return;
500   }
501   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply from DHT service\n");
502   dht_msg = (const struct GNUNET_DHT_ClientResultMessage *) msg;
503   GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
504                                               &dht_msg->key, &process_reply,
505                                               (void *) dht_msg);
506   GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
507                          GNUNET_TIME_UNIT_FOREVER_REL);
508 }
509
510
511 /**
512  * Initialize the connection with the DHT service.
513  *
514  * @param cfg configuration to use
515  * @param ht_len size of the internal hash table to use for
516  *               processing multiple GET/FIND requests in parallel
517  *
518  * @return handle to the DHT service, or NULL on error
519  */
520 struct GNUNET_DHT_Handle *
521 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
522                     unsigned int ht_len)
523 {
524   struct GNUNET_DHT_Handle *handle;
525
526   handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
527   handle->cfg = cfg;
528   handle->uid_gen =
529       GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
530   handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
531   if (GNUNET_NO == try_connect (handle))
532   {
533     GNUNET_DHT_disconnect (handle);
534     return NULL;
535   }
536   return handle;
537 }
538
539
540 /**
541  * Shutdown connection with the DHT service.
542  *
543  * @param handle handle of the DHT connection to stop
544  */
545 void
546 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
547 {
548   struct PendingMessage *pm;
549
550   GNUNET_assert (handle != NULL);
551   GNUNET_assert (0 ==
552                  GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
553   if (handle->th != NULL)
554   {
555     GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
556     handle->th = NULL;
557   }
558   while (NULL != (pm = handle->pending_head))
559   {
560     GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
561                                  pm);
562     GNUNET_assert (GNUNET_YES == pm->free_on_send);
563     if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
564       GNUNET_SCHEDULER_cancel (pm->timeout_task);
565     if (NULL != pm->cont)
566       GNUNET_SCHEDULER_add_continuation (pm->cont, pm->cont_cls,
567                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
568     pm->in_pending_queue = GNUNET_NO;
569     GNUNET_free (pm);
570   }
571   if (handle->client != NULL)
572   {
573     GNUNET_CLIENT_disconnect (handle->client, GNUNET_YES);
574     handle->client = NULL;
575   }
576   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
577     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
578   GNUNET_CONTAINER_multihashmap_destroy (handle->active_requests);
579   GNUNET_free (handle);
580 }
581
582
583 /**
584  * Timeout for the transmission of a fire&forget-request.  Clean it up.
585  *
586  * @param cls the 'struct PendingMessage'
587  * @param tc scheduler context
588  */
589 static void
590 timeout_put_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
591 {
592   struct PendingMessage *pending = cls;
593   struct GNUNET_DHT_Handle *handle;
594
595   handle = pending->handle;
596   GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
597                                pending);
598   if (pending->cont != NULL)
599     pending->cont (pending->cont_cls, tc);
600   GNUNET_free (pending);
601 }
602
603
604 /**
605  * Perform a PUT operation storing data in the DHT.
606  *
607  * @param handle handle to DHT service
608  * @param key the key to store under
609  * @param desired_replication_level estimate of how many
610  *                nearest peers this request should reach
611  * @param options routing options for this message
612  * @param type type of the value
613  * @param size number of bytes in data; must be less than 64k
614  * @param data the data to store
615  * @param exp desired expiration time for the value
616  * @param timeout how long to wait for transmission of this request
617  * @param cont continuation to call when done (transmitting request to service)
618  * @param cont_cls closure for cont
619  */
620 void
621 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const GNUNET_HashCode * key,
622                 uint32_t desired_replication_level,
623                 enum GNUNET_DHT_RouteOption options,
624                 enum GNUNET_BLOCK_Type type, size_t size, const char *data,
625                 struct GNUNET_TIME_Absolute exp,
626                 struct GNUNET_TIME_Relative timeout, GNUNET_SCHEDULER_Task cont,
627                 void *cont_cls)
628 {
629   struct GNUNET_DHT_ClientPutMessage *put_msg;
630   size_t msize;
631   struct PendingMessage *pending;
632
633   msize = sizeof (struct GNUNET_DHT_ClientPutMessage) + size;
634   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
635       (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
636   {
637     GNUNET_break (0);
638     if (NULL != cont)
639       cont (cont_cls, NULL);
640     return;
641   }
642   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
643   put_msg = (struct GNUNET_DHT_ClientPutMessage *) &pending[1];
644   pending->msg = &put_msg->header;
645   pending->handle = handle;
646   pending->cont = cont;
647   pending->cont_cls = cont_cls;
648   pending->free_on_send = GNUNET_YES;
649   pending->timeout_task =
650       GNUNET_SCHEDULER_add_delayed (timeout, &timeout_put_request, pending);
651   put_msg->header.size = htons (msize);
652   put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT);
653   put_msg->type = htonl (type);
654   put_msg->options = htonl ((uint32_t) options);
655   put_msg->desired_replication_level = htonl (desired_replication_level);
656   put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
657   put_msg->key = *key;
658   memcpy (&put_msg[1], data, size);
659   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
660                                pending);
661   pending->in_pending_queue = GNUNET_YES;
662   process_pending_messages (handle);
663 }
664
665
666 /**
667  * Perform an asynchronous GET operation on the DHT identified. See
668  * also "GNUNET_BLOCK_evaluate".
669  *
670  * @param handle handle to the DHT service
671  * @param timeout how long to wait for transmission of this request to the service
672  * @param type expected type of the response object
673  * @param key the key to look up
674  * @param desired_replication_level estimate of how many
675                   nearest peers this request should reach
676  * @param options routing options for this message
677  * @param xquery extended query data (can be NULL, depending on type)
678  * @param xquery_size number of bytes in xquery
679  * @param iter function to call on each result
680  * @param iter_cls closure for iter
681  * @return handle to stop the async get
682  */
683 struct GNUNET_DHT_GetHandle *
684 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
685                       struct GNUNET_TIME_Relative timeout,
686                       enum GNUNET_BLOCK_Type type, const GNUNET_HashCode * key,
687                       uint32_t desired_replication_level,
688                       enum GNUNET_DHT_RouteOption options, const void *xquery,
689                       size_t xquery_size, GNUNET_DHT_GetIterator iter,
690                       void *iter_cls)
691 {
692   struct GNUNET_DHT_ClientGetMessage *get_msg;
693   struct GNUNET_DHT_GetHandle *get_handle;
694   size_t msize;
695   struct PendingMessage *pending;
696
697   msize = sizeof (struct GNUNET_DHT_ClientGetMessage) + xquery_size;
698   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
699       (xquery_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
700   {
701     GNUNET_break (0);
702     return NULL;
703   }
704   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
705   get_msg = (struct GNUNET_DHT_ClientGetMessage *) &pending[1];
706   pending->msg = &get_msg->header;
707   pending->handle = handle;
708   pending->free_on_send = GNUNET_NO;
709   get_msg->header.size = htons (msize);
710   get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET);
711   get_msg->options = htonl ((uint32_t) options);
712   get_msg->desired_replication_level = htonl (desired_replication_level);
713   get_msg->type = htonl (type);
714   get_msg->key = *key;
715   handle->uid_gen++;
716   get_msg->unique_id = handle->uid_gen;
717   memcpy (&get_msg[1], xquery, xquery_size);
718   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
719                                pending);
720   pending->in_pending_queue = GNUNET_YES;
721   get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
722   get_handle->iter = iter;
723   get_handle->iter_cls = iter_cls;
724   get_handle->message = pending;
725   get_handle->unique_id = get_msg->unique_id;
726   GNUNET_CONTAINER_multihashmap_put (handle->active_requests, key, get_handle,
727                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
728   process_pending_messages (handle);
729   return get_handle;
730 }
731
732
733 /**
734  * Stop async DHT-get.
735  *
736  * @param get_handle handle to the GET operation to stop
737  */
738 void
739 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
740 {
741   struct GNUNET_DHT_Handle *handle;
742   const struct GNUNET_DHT_ClientGetMessage *get_msg;
743   struct GNUNET_DHT_ClientGetStopMessage *stop_msg;
744   struct PendingMessage *pending;
745
746   handle = get_handle->message->handle;
747   get_msg =
748       (const struct GNUNET_DHT_ClientGetMessage *) get_handle->message->msg;
749
750   /* generate STOP */
751   pending =
752       GNUNET_malloc (sizeof (struct PendingMessage) +
753                      sizeof (struct GNUNET_DHT_ClientGetStopMessage));
754   stop_msg = (struct GNUNET_DHT_ClientGetStopMessage *) &pending[1];
755   pending->msg = &stop_msg->header;
756   pending->handle = handle;
757   pending->free_on_send = GNUNET_YES;
758   stop_msg->header.size =
759       htons (sizeof (struct GNUNET_DHT_ClientGetStopMessage));
760   stop_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP);
761   stop_msg->reserved = htonl (0);
762   stop_msg->unique_id = get_msg->unique_id;
763   stop_msg->key = get_msg->key;
764   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
765                                pending);
766   pending->in_pending_queue = GNUNET_YES;
767
768   /* remove 'GET' from active status */
769   GNUNET_assert (GNUNET_YES ==
770                  GNUNET_CONTAINER_multihashmap_remove (handle->active_requests,
771                                                        &get_msg->key,
772                                                        get_handle));
773   if (GNUNET_YES == get_handle->message->in_pending_queue)
774   {
775     GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
776                                  get_handle->message);
777     get_handle->message->in_pending_queue = GNUNET_NO;
778   }
779   GNUNET_free (get_handle->message);
780   GNUNET_free (get_handle);
781
782   process_pending_messages (handle);
783 }
784
785
786 /* end of dht_api.c */