getting dht closer to being this crazy meta dht thing
[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 2, 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  * TODO: Only allow a single message until confirmed as received by
28  *       the service.  For put messages call continuation as soon as
29  *       receipt acknowledged (then remove), for GET or other messages
30  *       only call continuation when data received.
31  *       Add unique identifier to message types requesting data to be
32  *       returned.
33  */
34 #include "platform.h"
35 #include "gnunet_bandwidth_lib.h"
36 #include "gnunet_client_lib.h"
37 #include "gnunet_constants.h"
38 #include "gnunet_container_lib.h"
39 #include "gnunet_arm_service.h"
40 #include "gnunet_hello_lib.h"
41 #include "gnunet_protocols.h"
42 #include "gnunet_server_lib.h"
43 #include "gnunet_time_lib.h"
44 #include "gnunet_dht_service.h"
45 #include "dht.h"
46
47 #define DEBUG_DHT_API GNUNET_YES
48
49 #define DEFAULT_DHT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
50
51 struct PendingMessage
52 {
53   /**
54    * Message that is pending
55    */
56   struct GNUNET_MessageHeader *msg;
57
58   /**
59    * Timeout for this message
60    */
61   struct GNUNET_TIME_Relative timeout;
62
63   /**
64    * Continuation to call on message send
65    * or message receipt confirmation
66    */
67   GNUNET_DHT_MessageCallback cont;
68
69   /**
70    * Continuation closure
71    */
72   void *cont_cls;
73
74   /**
75    * Whether or not to await verification the message
76    * was received by the service
77    */
78   size_t is_unique;
79
80   /**
81    * Unique ID for this request
82    */
83   uint64_t unique_id;
84
85 };
86
87 struct GNUNET_DHT_GetContext
88 {
89
90
91   /**
92    * Iterator to call on data receipt
93    */
94   GNUNET_DHT_GetIterator iter;
95
96   /**
97    * Closure for the iterator callback
98    */
99   void *iter_cls;
100
101 };
102
103 /**
104  * Handle to control a unique operation (one that is
105  * expected to return results)
106  */
107 struct GNUNET_DHT_RouteHandle
108 {
109
110   /**
111    * Unique identifier for this request (for key collisions)
112    */
113   uint64_t uid;
114
115   /**
116    * Key that this get request is for
117    */
118   GNUNET_HashCode key;
119
120   /**
121    * Iterator to call on data receipt
122    */
123   GNUNET_DHT_ReplyProcessor iter;
124
125   /**
126    * Closure for the iterator callback
127    */
128   void *iter_cls;
129
130   /**
131    * Main handle to this DHT api
132    */
133   struct GNUNET_DHT_Handle *dht_handle;
134 };
135
136 /**
137  * Handle for a non unique request, holds callback
138  * which needs to be called before we allow other
139  * messages to be processed and sent to the DHT service
140  */
141 struct GNUNET_DHT_NonUniqueHandle
142 {
143   /**
144    * Key that this get request is for
145    */
146   GNUNET_HashCode key;
147
148   /**
149    * Type of data get request was for
150    */
151   uint32_t type;
152
153   /**
154    * Continuation to call on service
155    * confirmation of message receipt.
156    */
157   GNUNET_SCHEDULER_Task cont;
158
159   /**
160    * Send continuation cls
161    */
162   void *cont_cls;
163 };
164
165
166 /**
167  * Connection to the DHT service.
168  */
169 struct GNUNET_DHT_Handle
170 {
171   /**
172    * Our scheduler.
173    */
174   struct GNUNET_SCHEDULER_Handle *sched;
175
176   /**
177    * Configuration to use.
178    */
179   const struct GNUNET_CONFIGURATION_Handle *cfg;
180
181   /**
182    * Socket (if available).
183    */
184   struct GNUNET_CLIENT_Connection *client;
185
186   /**
187    * Currently pending transmission request.
188    */
189   struct GNUNET_CLIENT_TransmitHandle *th;
190
191   /**
192    * Message we are currently sending, only allow
193    * a single message to be queued.  If not unique
194    * (typically a put request), await a confirmation
195    * from the service that the message was received.
196    * If unique, just fire and forget.
197    */
198   struct PendingMessage *current;
199
200   /**
201    * Hash map containing the current outstanding unique requests
202    */
203   struct GNUNET_CONTAINER_MultiHashMap *outstanding_requests;
204
205   /**
206    * Non unique handle.  If set don't schedule another non
207    * unique request.
208    */
209   struct GNUNET_DHT_NonUniqueHandle *non_unique_request;
210
211   /**
212    * Kill off the connection and any pending messages.
213    */
214   int do_destroy;
215
216 };
217
218 static struct GNUNET_TIME_Relative default_request_timeout;
219
220 /* Forward declaration */
221 static void process_pending_message(struct GNUNET_DHT_Handle *handle);
222
223 static GNUNET_HashCode * hash_from_uid(uint64_t uid)
224 {
225   int count;
226   int remaining;
227   GNUNET_HashCode *hash;
228   hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
229   count = 0;
230
231   while (count < sizeof(GNUNET_HashCode))
232     {
233       remaining = sizeof(GNUNET_HashCode) - count;
234       if (remaining > sizeof(uid))
235         remaining = sizeof(uid);
236
237       memcpy(hash, &uid, remaining);
238       count += remaining;
239     }
240
241   return hash;
242 }
243
244 /**
245  * Handler for messages received from the DHT service
246  * a demultiplexer which handles numerous message types
247  *
248  */
249 void service_message_handler (void *cls,
250                               const struct GNUNET_MessageHeader *msg)
251 {
252   struct GNUNET_DHT_Handle *handle = cls;
253   struct GNUNET_DHT_Message *dht_msg = (struct GNUNET_DHT_Message *)msg;
254   struct GNUNET_MessageHeader *enc_msg;
255   struct GNUNET_DHT_RouteHandle *route_handle;
256   GNUNET_HashCode *uid_hash;
257   size_t enc_size;
258   /* TODO: find out message type, handle callbacks for different types of messages.
259    * Should be a non unique acknowledgment, or unique result. */
260
261 #if DEBUG_DHT_API
262           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263                       "`%s': Received response to message (uid %llu)\n", "DHT API", ntohl(dht_msg->unique_id));
264 #endif
265
266   if (ntohs(dht_msg->unique))
267     {
268       uid_hash = hash_from_uid(ntohl(dht_msg->unique_id));
269
270       route_handle = GNUNET_CONTAINER_multihashmap_get(handle->outstanding_requests, uid_hash);
271       if (route_handle == NULL) /* We have no recollection of this request */
272         {
273 #if DEBUG_DHT_API
274           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275                       "`%s': Received response to message (uid %llu), but have no recollection of it!\n", "DHT API", ntohl(dht_msg->unique_id));
276 #endif
277         }
278       else
279         {
280           enc_size = ntohs(dht_msg->header.size) - sizeof(struct GNUNET_DHT_Message);
281           GNUNET_assert(enc_size > 0);
282           enc_msg = (struct GNUNET_MessageHeader *)&dht_msg[1];
283           route_handle->iter(route_handle->iter_cls, enc_msg);
284         }
285     }
286   else
287     {
288       if (handle->current->unique_id == ntohl(dht_msg->unique_id))
289         {
290           handle->current->cont(handle->current->cont_cls, GNUNET_OK);
291           GNUNET_free(handle->current->msg);
292           handle->current = NULL;
293           GNUNET_free(handle->current);
294         }
295     }
296
297
298 }
299
300
301 /**
302  * Initialize the connection with the DHT service.
303  *
304  * @param cfg configuration to use
305  * @param sched scheduler to use
306  * @param ht_len size of the internal hash table to use for
307  *               processing multiple GET/FIND requests in parallel
308  * @return NULL on error
309  */
310 struct GNUNET_DHT_Handle *
311 GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
312                     const struct GNUNET_CONFIGURATION_Handle *cfg,
313                     unsigned int ht_len)
314 {
315   struct GNUNET_DHT_Handle *handle;
316
317   handle = GNUNET_malloc(sizeof(struct GNUNET_DHT_Handle));
318
319   default_request_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5);
320   handle->cfg = cfg;
321   handle->sched = sched;
322
323   handle->current = NULL;
324   handle->do_destroy = GNUNET_NO;
325   handle->th = NULL;
326
327   handle->client = GNUNET_CLIENT_connect(sched, "dht", cfg);
328   handle->outstanding_requests = GNUNET_CONTAINER_multihashmap_create(ht_len);
329
330   if (handle->client == NULL)
331     return NULL;
332 #if DEBUG_DHT_API
333   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334               "`%s': Connection to service in progress\n", "DHT API");
335 #endif
336   GNUNET_CLIENT_receive (handle->client,
337                          &service_message_handler,
338                          handle, GNUNET_TIME_UNIT_FOREVER_REL);
339
340   return handle;
341 }
342
343
344 /**
345  * Shutdown connection with the DHT service.
346  *
347  * @param h connection to shut down
348  */
349 void
350 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
351 {
352 #if DEBUG_DHT_API
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354               "`%s': Called GNUNET_DHT_disconnect\n", "DHT API");
355 #endif
356   GNUNET_assert(handle != NULL);
357
358   if (handle->th != NULL) /* We have a live transmit request in the Aether */
359     {
360       GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
361       handle->th = NULL;
362     }
363   if (handle->current != NULL) /* We are trying to send something now, clean it up */
364     GNUNET_free(handle->current);
365
366   if (handle->client != NULL) /* Finally, disconnect from the service */
367     {
368       GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
369       handle->client = NULL;
370     }
371
372   GNUNET_free (handle);
373 }
374
375
376 /**
377  * Send complete (or failed), schedule next (or don't)
378  */
379 static void
380 finish (struct GNUNET_DHT_Handle *handle, int code)
381 {
382   /* TODO: if code is not GNUNET_OK, do something! */
383   struct PendingMessage *pos = handle->current;
384
385   GNUNET_assert(pos != NULL);
386
387   if (pos->is_unique)
388     {
389       if (pos->cont != NULL)
390         pos->cont(pos->cont_cls, code);
391
392       GNUNET_free(pos->msg);
393       handle->current = NULL;
394       GNUNET_free(pos);
395     }
396   /* Otherwise we need to wait for a response to this message! */
397 }
398
399 /**
400  * Transmit the next pending message, called by notify_transmit_ready
401  */
402 static size_t
403 transmit_pending (void *cls, size_t size, void *buf)
404 {
405   struct GNUNET_DHT_Handle *handle = cls;
406   size_t tsize;
407
408   if (buf == NULL)
409     {
410 #if DEBUG_DHT_API
411       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412                   "`%s': In transmit_pending buf is NULL\n", "DHT API");
413 #endif
414       /* FIXME: free associated resources or summat */
415       finish(handle, GNUNET_SYSERR);
416       return 0;
417     }
418
419   handle->th = NULL;
420
421   if (handle->current != NULL)
422   {
423     tsize = ntohs(handle->current->msg->size);
424     if (size >= tsize)
425     {
426 #if DEBUG_DHT_API
427       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428                   "`%s': Sending message size %d\n", "DHT API", tsize);
429 #endif
430       memcpy(buf, handle->current->msg, tsize);
431       return tsize;
432     }
433     else
434     {
435       return 0;
436     }
437   }
438   /* Have no pending request */
439   return 0;
440 }
441
442
443 /**
444  * Try to (re)connect to the dht service.
445  *
446  * @return GNUNET_YES on success, GNUNET_NO on failure.
447  */
448 static int
449 try_connect (struct GNUNET_DHT_Handle *ret)
450 {
451   if (ret->client != NULL)
452     return GNUNET_OK;
453   ret->client = GNUNET_CLIENT_connect (ret->sched, "dht", ret->cfg);
454   if (ret->client != NULL)
455     return GNUNET_YES;
456 #if DEBUG_STATISTICS
457   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458               _("Failed to connect to the dht service!\n"));
459 #endif
460   return GNUNET_NO;
461 }
462
463
464 /**
465  * Try to send messages from list of messages to send
466  */
467 static void process_pending_message(struct GNUNET_DHT_Handle *handle)
468 {
469
470   if (handle->current != NULL)
471     return;                     /* action already pending */
472   if (GNUNET_YES != try_connect (handle))
473     {
474       finish (handle, GNUNET_SYSERR);
475       return;
476     }
477
478   /* TODO: set do_destroy somewhere's, see what needs to happen in that case! */
479   if (handle->do_destroy)
480     {
481       //GNUNET_DHT_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
482     }
483
484
485   if (NULL ==
486       (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
487                                                     ntohs(handle->current->msg->size),
488                                                     handle->current->timeout,
489                                                     GNUNET_YES,
490                                                     &transmit_pending, handle)))
491     {
492 #if DEBUG_DHT_API
493       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494                   "Failed to transmit request to dht service.\n");
495 #endif
496       finish (handle, GNUNET_SYSERR);
497     }
498 #if DEBUG_DHT_API
499   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500               "`%s': Scheduled sending message of size %d to service\n", "DHT API", ntohs(handle->current->msg->size));
501 #endif
502 }
503
504 /**
505  * Iterator called on each result obtained from a generic route
506  * operation
507  */
508 void get_reply_iterator (void *cls,
509                          const struct GNUNET_MessageHeader *reply)
510 {
511
512 }
513
514 /**
515  * Perform an asynchronous FIND_PEER operation on the DHT.
516  *
517  * @param handle handle to the DHT service
518  * @param key the key to look up
519  * @param desired_replication_level how many peers should ultimately receive
520  *                this message (advisory only, target may be too high for the
521  *                given DHT or not hit exactly).
522  * @param options options for routing
523  * @param enc send the encapsulated message to a peer close to the key
524  * @param iter function to call on each result, NULL if no replies are expected
525  * @param iter_cls closure for iter
526  * @param timeout when to abort with an error if we fail to get
527  *                a confirmation for the PUT from the local DHT service
528  * @param cont continuation to call when done;
529  *             reason will be TIMEOUT on error,
530  *             reason will be PREREQ_DONE on success
531  * @param cont_cls closure for cont
532  * @return handle to stop the request
533  */
534 struct GNUNET_DHT_RouteHandle *
535 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
536                         const GNUNET_HashCode *key,
537                         unsigned int desired_replication_level,
538                         enum GNUNET_DHT_RouteOption options,
539                         const struct GNUNET_MessageHeader *enc,
540                         struct GNUNET_TIME_Relative timeout,
541                         GNUNET_DHT_ReplyProcessor iter,
542                         void *iter_cls,
543                         GNUNET_DHT_MessageCallback cont,
544                         void *cont_cls)
545 {
546   struct GNUNET_DHT_RouteHandle *route_handle;
547   struct PendingMessage *pending;
548   struct GNUNET_DHT_Message *message;
549   size_t is_unique;
550   size_t msize;
551   GNUNET_HashCode *uid_key;
552   int count;
553
554   is_unique = GNUNET_YES;
555   if (iter == NULL)
556     is_unique = GNUNET_NO;
557
558   route_handle = NULL;
559
560   if (is_unique)
561     {
562       route_handle = GNUNET_malloc(sizeof(struct GNUNET_DHT_RouteHandle));
563       memcpy(&route_handle->key, key, sizeof(GNUNET_HashCode));
564       route_handle->iter = iter;
565       route_handle->iter_cls = iter_cls;
566       route_handle->dht_handle = handle;
567       route_handle->uid = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, -1);
568
569       count = 0;
570       uid_key = hash_from_uid(route_handle->uid);
571       /* While we have an outstanding request with the same identifier! */
572       while (GNUNET_CONTAINER_multihashmap_contains(handle->outstanding_requests, uid_key) == GNUNET_YES)
573         {
574           GNUNET_free(uid_key);
575           uid_key = hash_from_uid(route_handle->uid);
576         }
577       /**
578        * Store based on random identifier!
579        */
580       GNUNET_CONTAINER_multihashmap_put(handle->outstanding_requests, uid_key, route_handle, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
581       msize = sizeof(struct GNUNET_DHT_Message) + ntohs(enc->size) + sizeof(route_handle->uid);
582       GNUNET_free(uid_key);
583     }
584   else
585     {
586       msize = sizeof(struct GNUNET_DHT_Message) + ntohs(enc->size);
587     }
588
589   message = GNUNET_malloc(msize);
590   message->header.size = htons(msize);
591   message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT);
592   memcpy(&message->key, key, sizeof(GNUNET_HashCode));
593   message->options = htons(options);
594   message->desired_replication_level = htons(options);
595   message->unique = htons(is_unique);
596
597   pending = GNUNET_malloc(sizeof(struct PendingMessage));
598   pending->msg = &message->header;
599   pending->timeout = timeout;
600   pending->cont = cont;
601   pending->cont_cls = cont_cls;
602   pending->is_unique = is_unique;
603
604   GNUNET_assert(handle->current == NULL);
605
606   process_pending_message(handle);
607
608   return route_handle;
609 }
610
611 void
612 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *fph);
613
614
615 void dht_get_processor (void *cls,
616                         const struct GNUNET_MessageHeader *reply)
617 {
618
619 }
620
621 /**
622  * Perform an asynchronous GET operation on the DHT identified.
623  *
624  * @param h handle to the DHT service
625  * @param type expected type of the response object
626  * @param key the key to look up
627  * @param iter function to call on each result
628  * @param iter_cls closure for iter
629  * @return handle to stop the async get
630  */
631 struct GNUNET_DHT_RouteHandle *
632 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
633                       struct GNUNET_TIME_Relative timeout,
634                       uint32_t type,
635                       const GNUNET_HashCode * key,
636                       GNUNET_DHT_GetIterator iter,
637                       void *iter_cls)
638 {
639   struct GNUNET_DHT_GetContext *get_context;
640   struct GNUNET_DHT_GetMessage *get_msg;
641
642   if (handle->current != NULL) /* Can't send right now, we have a pending message... */
643     return NULL;
644
645   get_context = GNUNET_malloc(sizeof(struct GNUNET_DHT_GetContext));
646   get_context->iter = iter;
647   get_context->iter_cls = iter_cls;
648
649 #if DEBUG_DHT_API
650   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651               "`%s': Inserting pending get request with key %s\n", "DHT API", GNUNET_h2s(key));
652 #endif
653
654   get_msg = GNUNET_malloc(sizeof(struct GNUNET_DHT_GetMessage));
655   get_msg->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_GET);
656   get_msg->header.size = htons(sizeof(struct GNUNET_DHT_GetMessage));
657   get_msg->type = htonl(type);
658
659   return GNUNET_DHT_route_start(handle, key, 0, 0, &get_msg->header, timeout, &get_reply_iterator, get_context, NULL, NULL);
660
661 }
662
663
664 void
665 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle)
666 {
667   struct PendingMessage *pending;
668   struct GNUNET_DHT_StopMessage *message;
669   size_t msize;
670   GNUNET_HashCode *uid_key;
671
672   msize = sizeof(struct GNUNET_DHT_StopMessage);
673
674   message = GNUNET_malloc(msize);
675   message->header.size = htons(msize);
676   message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_STOP);
677   message->unique_id = htonl(route_handle->uid);
678
679   pending = GNUNET_malloc(sizeof(struct PendingMessage));
680   pending->msg = (struct GNUNET_MessageHeader *)message;
681   pending->timeout = DEFAULT_DHT_TIMEOUT;
682   pending->cont = NULL;
683   pending->cont_cls = NULL;
684   pending->is_unique = GNUNET_NO;
685
686   GNUNET_assert(route_handle->dht_handle->current == NULL);
687
688   process_pending_message(route_handle->dht_handle);
689
690   uid_key = hash_from_uid(route_handle->uid);
691
692   if (GNUNET_CONTAINER_multihashmap_remove(route_handle->dht_handle->outstanding_requests, uid_key, route_handle) != GNUNET_YES)
693     {
694 #if DEBUG_DHT_API
695       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696                   "`%s': Remove outstanding request from hashmap failed for key %s, uid %llu\n", "DHT API", GNUNET_h2s(uid_key), route_handle->uid);
697 #endif
698     }
699
700   return;
701 }
702
703
704 /**
705  * Stop async DHT-get.  Frees associated resources.
706  *
707  * @param record GET operation to stop.
708  */
709 void
710 GNUNET_DHT_get_stop (struct GNUNET_DHT_RouteHandle *handle)
711 {
712 #if OLDREMOVE
713   struct GNUNET_DHT_GetMessage *get_msg;
714   struct GNUNET_DHT_Handle *handle;
715   GNUNET_HashCode *uid_key;
716 #endif
717
718   GNUNET_DHT_route_stop(handle);
719
720 #if OLDREMOVE
721   uid_key = hash_from_uid(get_handle->uid);
722   GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(handle->outstanding_requests, uid_key, get_handle) == GNUNET_YES);
723
724   if (handle->do_destroy == GNUNET_NO)
725     {
726       get_msg = GNUNET_malloc(sizeof(struct GNUNET_DHT_GetMessage));
727       get_msg->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_GET_STOP);
728       get_msg->header.size = htons(sizeof(struct GNUNET_DHT_GetMessage));
729
730
731     }
732 #endif
733 #if DEBUG_DHT_API
734   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735               "`%s': Removing pending get request with key %s, uid %llu\n", "DHT API", GNUNET_h2s(&handle->key), handle->uid);
736 #endif
737 }
738
739
740 /**
741  * Perform a PUT operation storing data in the DHT.
742  *
743  * @param h handle to DHT service
744  * @param key the key to store under
745  * @param type type of the value
746  * @param size number of bytes in data; must be less than 64k
747  * @param data the data to store
748  * @param exp desired expiration time for the value
749  * @param cont continuation to call when done;
750  *             reason will be TIMEOUT on error,
751  *             reason will be PREREQ_DONE on success
752  * @param cont_cls closure for cont
753  *
754  * @return GNUNET_YES if put message is queued for transmission
755  */
756 void
757 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
758                 const GNUNET_HashCode * key,
759                 uint32_t type,
760                 uint32_t size,
761                 const char *data,
762                 struct GNUNET_TIME_Absolute exp,
763                 struct GNUNET_TIME_Relative timeout,
764                 GNUNET_DHT_MessageCallback cont,
765                 void *cont_cls)
766 {
767   struct GNUNET_DHT_PutMessage *put_msg;
768   size_t msize;
769
770   if (handle->current != NULL)
771     {
772       cont(cont_cls, GNUNET_SYSERR);
773       return;
774     }
775
776 #if DEBUG_DHT_API
777   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778               "`%s': Inserting pending put request with key %s\n", "DHT API", GNUNET_h2s(key));
779 #endif
780
781   msize = sizeof(struct GNUNET_DHT_PutMessage) + size;
782   put_msg = GNUNET_malloc(msize);
783   put_msg->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_PUT);
784   put_msg->header.size = htons(msize);
785   put_msg->type = htonl(type);
786   memcpy(&put_msg[1], data, size);
787
788   GNUNET_DHT_route_start(handle, key, 0, 0, &put_msg->header, timeout, NULL, NULL, cont, cont_cls);
789
790 }