2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
23 * @brief library to access the DHT service
24 * @author Christian Grothoff
25 * @author Nathan Evans
30 #include "gnunet_bandwidth_lib.h"
31 #include "gnunet_client_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_container_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_hello_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_server_lib.h"
38 #include "gnunet_time_lib.h"
39 #include "gnunet_dht_service.h"
42 #define DEBUG_DHT_API GNUNET_NO
44 #define DEFAULT_DHT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
49 * Message that is pending
51 struct GNUNET_MessageHeader *msg;
54 * Timeout for this message
56 struct GNUNET_TIME_Relative timeout;
59 * Continuation to call on message send
60 * or message receipt confirmation
62 GNUNET_SCHEDULER_Task cont;
65 * Continuation closure
70 * Whether or not to await verification the message
71 * was received by the service
76 * Unique ID for this request
82 struct GNUNET_DHT_GetContext
85 * Iterator to call on data receipt
87 GNUNET_DHT_GetIterator iter;
90 * Closure for the iterator callback
96 struct GNUNET_DHT_FindPeerContext
99 * Iterator to call on data receipt
101 GNUNET_DHT_FindPeerProcessor proc;
104 * Closure for the iterator callback
111 * Handle to control a unique operation (one that is
112 * expected to return results)
114 struct GNUNET_DHT_RouteHandle
118 * Unique identifier for this request (for key collisions)
123 * Key that this get request is for
128 * Iterator to call on data receipt
130 GNUNET_DHT_ReplyProcessor iter;
133 * Closure for the iterator callback
138 * Main handle to this DHT api
140 struct GNUNET_DHT_Handle *dht_handle;
144 * Handle for a non unique request, holds callback
145 * which needs to be called before we allow other
146 * messages to be processed and sent to the DHT service
148 struct GNUNET_DHT_NonUniqueHandle
151 * Key that this get request is for
156 * Type of data get request was for
161 * Continuation to call on service
162 * confirmation of message receipt.
164 GNUNET_SCHEDULER_Task cont;
167 * Send continuation cls
173 * Handle to control a get operation.
175 struct GNUNET_DHT_GetHandle
178 * Handle to the actual route operation for the get
180 struct GNUNET_DHT_RouteHandle *route_handle;
183 * The context of the get request
185 struct GNUNET_DHT_GetContext get_context;
189 * Handle to control a find peer operation.
191 struct GNUNET_DHT_FindPeerHandle
194 * Handle to the actual route operation for the request
196 struct GNUNET_DHT_RouteHandle *route_handle;
199 * The context of the get request
201 struct GNUNET_DHT_FindPeerContext find_peer_context;
206 * Connection to the DHT service.
208 struct GNUNET_DHT_Handle
213 struct GNUNET_SCHEDULER_Handle *sched;
216 * Configuration to use.
218 const struct GNUNET_CONFIGURATION_Handle *cfg;
221 * Socket (if available).
223 struct GNUNET_CLIENT_Connection *client;
226 * Currently pending transmission request.
228 struct GNUNET_CLIENT_TransmitHandle *th;
231 * Message we are currently sending, only allow
232 * a single message to be queued. If not unique
233 * (typically a put request), await a confirmation
234 * from the service that the message was received.
235 * If unique, just fire and forget.
237 struct PendingMessage *current;
240 * Hash map containing the current outstanding unique requests
242 struct GNUNET_CONTAINER_MultiHashMap *outstanding_requests;
245 * Non unique handle. If set don't schedule another non
248 struct GNUNET_DHT_NonUniqueHandle *non_unique_request;
251 * Kill off the connection and any pending messages.
257 static struct GNUNET_TIME_Relative default_request_timeout;
259 /* Forward declaration */
260 static void process_pending_message (struct GNUNET_DHT_Handle *handle);
262 static GNUNET_HashCode *
263 hash_from_uid (uint64_t uid)
267 GNUNET_HashCode *hash;
268 hash = GNUNET_malloc (sizeof (GNUNET_HashCode));
271 while (count < sizeof (GNUNET_HashCode))
273 remaining = sizeof (GNUNET_HashCode) - count;
274 if (remaining > sizeof (uid))
275 remaining = sizeof (uid);
277 memcpy (hash, &uid, remaining);
285 * Handler for messages received from the DHT service
286 * a demultiplexer which handles numerous message types
290 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
292 struct GNUNET_DHT_Handle *handle = cls;
293 struct GNUNET_DHT_Message *dht_msg;
294 struct GNUNET_DHT_StopMessage *stop_msg;
295 struct GNUNET_MessageHeader *enc_msg;
296 struct GNUNET_DHT_RouteHandle *route_handle;
298 GNUNET_HashCode *uid_hash;
300 /* TODO: find out message type, handle callbacks for different types of messages.
301 * Should be a non unique acknowledgment, or unique result. */
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "`%s': Received NULL from server, connection down?\n",
313 switch (ntohs (msg->type))
315 case GNUNET_MESSAGE_TYPE_DHT:
317 dht_msg = (struct GNUNET_DHT_Message *) msg;
318 uid = GNUNET_ntohll (dht_msg->unique_id);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "`%s': Received response to message (uid %llu)\n",
324 if (ntohs (dht_msg->unique))
326 uid_hash = hash_from_uid (uid);
328 GNUNET_CONTAINER_multihashmap_get (handle->outstanding_requests,
330 GNUNET_free (uid_hash);
331 if (route_handle == NULL) /* We have no recollection of this request */
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "`%s': Received response to message (uid %llu), but have no recollection of it!\n",
342 ntohs (dht_msg->header.size) -
343 sizeof (struct GNUNET_DHT_Message);
344 GNUNET_assert (enc_size > 0);
345 enc_msg = (struct GNUNET_MessageHeader *) &dht_msg[1];
346 route_handle->iter (route_handle->iter_cls, enc_msg);
352 case GNUNET_MESSAGE_TYPE_DHT_STOP:
354 stop_msg = (struct GNUNET_DHT_StopMessage *) msg;
355 uid = GNUNET_ntohll (stop_msg->unique_id);
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "`%s': Received response to message (uid %llu), current uid %llu\n",
359 "DHT API", uid, handle->current->unique_id);
361 if (handle->current->unique_id == uid)
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "`%s': Have pending confirmation for this message!\n",
368 if (handle->current->cont != NULL)
369 GNUNET_SCHEDULER_add_continuation (handle->sched,
370 handle->current->cont,
371 handle->current->cont_cls,
372 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
374 GNUNET_free (handle->current->msg);
375 GNUNET_free (handle->current);
376 handle->current = NULL;
382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
383 "`%s': Received unknown message type %d\n", "DHT API",
387 GNUNET_CLIENT_receive (handle->client,
388 &service_message_handler,
389 handle, GNUNET_TIME_UNIT_FOREVER_REL);
395 * Initialize the connection with the DHT service.
397 * @param sched scheduler to use
398 * @param cfg configuration to use
399 * @param ht_len size of the internal hash table to use for
400 * processing multiple GET/FIND requests in parallel
402 * @return handle to the DHT service, or NULL on error
404 struct GNUNET_DHT_Handle *
405 GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
406 const struct GNUNET_CONFIGURATION_Handle *cfg,
409 struct GNUNET_DHT_Handle *handle;
411 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
413 default_request_timeout =
414 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
416 handle->sched = sched;
418 handle->current = NULL;
419 handle->do_destroy = GNUNET_NO;
422 handle->client = GNUNET_CLIENT_connect (sched, "dht", cfg);
423 handle->outstanding_requests =
424 GNUNET_CONTAINER_multihashmap_create (ht_len);
426 if (handle->client == NULL)
428 GNUNET_free (handle);
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "`%s': Connection to service in progress\n", "DHT API");
435 GNUNET_CLIENT_receive (handle->client,
436 &service_message_handler,
437 handle, GNUNET_TIME_UNIT_FOREVER_REL);
444 * Shutdown connection with the DHT service.
446 * @param handle handle of the DHT connection to stop
449 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "`%s': Called GNUNET_DHT_disconnect\n", "DHT API");
455 GNUNET_assert (handle != NULL);
457 if (handle->th != NULL) /* We have a live transmit request in the Aether */
459 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
462 if (handle->current != NULL) /* We are trying to send something now, clean it up */
463 GNUNET_free (handle->current);
465 if (handle->client != NULL) /* Finally, disconnect from the service */
467 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
468 handle->client = NULL;
471 GNUNET_free (handle);
476 * Send complete (or failed), schedule next (or don't)
479 finish (struct GNUNET_DHT_Handle *handle, int code)
481 /* TODO: if code is not GNUNET_OK, do something! */
482 struct PendingMessage *pos = handle->current;
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': Finish called!\n", "DHT API");
486 GNUNET_assert (pos != NULL);
490 if (pos->cont != NULL)
492 if (code == GNUNET_SYSERR)
493 GNUNET_SCHEDULER_add_continuation (handle->sched, pos->cont,
495 GNUNET_SCHEDULER_REASON_TIMEOUT);
497 GNUNET_SCHEDULER_add_continuation (handle->sched, pos->cont,
499 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
502 GNUNET_free (pos->msg);
503 handle->current = NULL;
506 /* Otherwise we need to wait for a response to this message! */
510 * Transmit the next pending message, called by notify_transmit_ready
513 transmit_pending (void *cls, size_t size, void *buf)
515 struct GNUNET_DHT_Handle *handle = cls;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "`%s': In transmit_pending\n", "DHT API");
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "`%s': In transmit_pending buf is NULL\n", "DHT API");
528 /* FIXME: free associated resources or summat */
529 finish (handle, GNUNET_SYSERR);
535 if (handle->current != NULL)
537 tsize = ntohs (handle->current->msg->size);
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "`%s': Sending message size %d\n", "DHT API", tsize);
544 memcpy (buf, handle->current->msg, tsize);
545 finish (handle, GNUNET_OK);
553 /* Have no pending request */
559 * Try to (re)connect to the dht service.
561 * @return GNUNET_YES on success, GNUNET_NO on failure.
564 try_connect (struct GNUNET_DHT_Handle *handle)
566 if (handle->client != NULL)
568 handle->client = GNUNET_CLIENT_connect (handle->sched, "dht", handle->cfg);
569 if (handle->client != NULL)
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 _("Failed to connect to the dht service!\n"));
580 * Try to send messages from list of messages to send
583 process_pending_message (struct GNUNET_DHT_Handle *handle)
586 if (handle->current == NULL)
587 return; /* action already pending */
588 if (GNUNET_YES != try_connect (handle))
590 finish (handle, GNUNET_SYSERR);
594 /* TODO: set do_destroy somewhere's, see what needs to happen in that case! */
595 if (handle->do_destroy)
597 //GNUNET_DHT_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
602 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
613 "Failed to transmit request to dht service.\n");
615 finish (handle, GNUNET_SYSERR);
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "`%s': Scheduled sending message of size %d to service\n",
620 "DHT API", ntohs (handle->current->msg->size));
625 * Iterator called on each result obtained from a generic route
629 get_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply)
631 struct GNUNET_DHT_GetHandle *get_handle = cls;
632 struct GNUNET_DHT_GetResultMessage *result;
636 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
639 GNUNET_assert (ntohs (reply->size) >=
640 sizeof (struct GNUNET_DHT_GetResultMessage));
641 result = (struct GNUNET_DHT_GetResultMessage *) reply;
642 data_size = ntohs (result->data_size);
643 GNUNET_assert (ntohs (reply->size) ==
644 sizeof (struct GNUNET_DHT_GetResultMessage) + data_size);
645 result_data = (char *) &result[1]; /* Set data pointer to end of message */
647 get_handle->get_context.iter (get_handle->get_context.iter_cls,
648 result->expiration, &result->key,
649 ntohs (result->type), data_size, result_data);
654 * Iterator called on each result obtained from a generic route
658 find_peer_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply)
660 struct GNUNET_DHT_FindPeerHandle *find_peer_handle = cls;
661 struct GNUNET_DHT_FindPeerResultMessage *result;
663 struct GNUNET_MessageHeader *result_data;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Find peer iterator called.\n");
669 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
672 GNUNET_assert (ntohs (reply->size) >=
673 sizeof (struct GNUNET_DHT_FindPeerResultMessage));
674 result = (struct GNUNET_DHT_FindPeerResultMessage *) reply;
675 data_size = ntohs (result->data_size);
676 GNUNET_assert (ntohs (reply->size) ==
677 sizeof (struct GNUNET_DHT_FindPeerResultMessage) + data_size);
680 result_data = (struct GNUNET_MessageHeader *) &result[1]; /* Set data pointer to end of message */
684 find_peer_handle->find_peer_context.proc (find_peer_handle->
685 find_peer_context.proc_cls,
686 &result->peer, result_data);
690 * Perform an asynchronous FIND_PEER operation on the DHT.
692 * @param handle handle to the DHT service
693 * @param key the key to look up
694 * @param desired_replication_level how many peers should ultimately receive
695 * this message (advisory only, target may be too high for the
696 * given DHT or not hit exactly).
697 * @param options options for routing
698 * @param enc send the encapsulated message to a peer close to the key
699 * @param iter function to call on each result, NULL if no replies are expected
700 * @param iter_cls closure for iter
701 * @param timeout when to abort with an error if we fail to get
702 * a confirmation for the request (when necessary) or how long
703 * to wait for tramission to the service
704 * @param cont continuation to call when done;
705 * reason will be TIMEOUT on error,
706 * reason will be PREREQ_DONE on success
707 * @param cont_cls closure for cont
709 * @return handle to stop the request, NULL if the request is "fire and forget"
711 struct GNUNET_DHT_RouteHandle *
712 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
713 const GNUNET_HashCode * key,
714 unsigned int desired_replication_level,
715 enum GNUNET_DHT_RouteOption options,
716 const struct GNUNET_MessageHeader *enc,
717 struct GNUNET_TIME_Relative timeout,
718 GNUNET_DHT_ReplyProcessor iter,
720 GNUNET_SCHEDULER_Task cont, void *cont_cls)
722 struct GNUNET_DHT_RouteHandle *route_handle;
723 struct PendingMessage *pending;
724 struct GNUNET_DHT_Message *message;
727 GNUNET_HashCode *uid_key;
730 is_unique = GNUNET_YES;
732 is_unique = GNUNET_NO;
739 GNUNET_free_non_null (uid_key);
740 uid = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
741 uid_key = hash_from_uid (uid);
743 while (GNUNET_CONTAINER_multihashmap_contains
744 (handle->outstanding_requests, uid_key) == GNUNET_YES);
748 route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
749 memcpy (&route_handle->key, key, sizeof (GNUNET_HashCode));
750 route_handle->iter = iter;
751 route_handle->iter_cls = iter_cls;
752 route_handle->dht_handle = handle;
753 route_handle->uid = uid;
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "`%s': Unique ID is %llu\n", "DHT API", uid);
759 * Store based on random identifier!
761 GNUNET_CONTAINER_multihashmap_put (handle->outstanding_requests,
762 uid_key, route_handle,
763 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
764 msize = sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size);
769 msize = sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size);
772 GNUNET_free (uid_key);
773 message = GNUNET_malloc (msize);
774 message->header.size = htons (msize);
775 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT);
776 memcpy (&message->key, key, sizeof (GNUNET_HashCode));
777 message->options = htons (options);
778 message->desired_replication_level = htons (options);
779 message->unique = htons (is_unique);
780 message->unique_id = GNUNET_htonll (uid);
781 memcpy (&message[1], enc, ntohs (enc->size));
783 pending = GNUNET_malloc (sizeof (struct PendingMessage));
784 pending->msg = &message->header;
785 pending->timeout = timeout;
786 pending->cont = cont;
787 pending->cont_cls = cont_cls;
788 pending->is_unique = is_unique;
789 pending->unique_id = uid;
791 GNUNET_assert (handle->current == NULL);
793 handle->current = pending;
795 process_pending_message (handle);
801 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
802 GNUNET_SCHEDULER_Task cont, void *cont_cls);
805 * Perform an asynchronous GET operation on the DHT identified.
807 * @param handle handle to the DHT service
808 * @param timeout how long to wait for transmission of this request to the service
809 * @param type expected type of the response object
810 * @param key the key to look up
811 * @param iter function to call on each result
812 * @param iter_cls closure for iter
813 * @param cont continuation to call once message sent
814 * @param cont_cls closure for continuation
816 * @return handle to stop the async get
818 struct GNUNET_DHT_GetHandle *
819 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
820 struct GNUNET_TIME_Relative timeout,
822 const GNUNET_HashCode * key,
823 GNUNET_DHT_GetIterator iter,
825 GNUNET_SCHEDULER_Task cont, void *cont_cls)
827 struct GNUNET_DHT_GetHandle *get_handle;
828 struct GNUNET_DHT_GetMessage *get_msg;
830 if (handle->current != NULL) /* Can't send right now, we have a pending message... */
833 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
834 get_handle->get_context.iter = iter;
835 get_handle->get_context.iter_cls = iter_cls;
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 "`%s': Inserting pending get request with key %s\n", "DHT API",
843 get_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetMessage));
844 get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
845 get_msg->header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
846 get_msg->type = htons (type);
848 get_handle->route_handle =
849 GNUNET_DHT_route_start (handle, key, 0, 0, &get_msg->header, timeout,
850 &get_reply_iterator, get_handle, cont, cont_cls);
855 * Stop a previously issued routing request
857 * @param route_handle handle to the request to stop
858 * @param cont continuation to call once this message is sent to the service or times out
859 * @param cont_cls closure for the continuation
863 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
864 GNUNET_SCHEDULER_Task cont, void *cont_cls)
866 struct PendingMessage *pending;
867 struct GNUNET_DHT_StopMessage *message;
869 GNUNET_HashCode *uid_key;
871 msize = sizeof (struct GNUNET_DHT_StopMessage);
873 message = GNUNET_malloc (msize);
874 message->header.size = htons (msize);
875 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_STOP);
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "`%s': Remove outstanding request for uid %llu\n", "DHT API",
881 message->unique_id = GNUNET_htonll (route_handle->uid);
883 GNUNET_assert (route_handle->dht_handle->current == NULL);
885 pending = GNUNET_malloc (sizeof (struct PendingMessage));
886 pending->msg = (struct GNUNET_MessageHeader *) message;
887 pending->timeout = DEFAULT_DHT_TIMEOUT;
888 pending->cont = cont;
889 pending->cont_cls = cont_cls;
890 pending->is_unique = GNUNET_NO;
891 pending->unique_id = route_handle->uid;
893 GNUNET_assert (route_handle->dht_handle->current == NULL);
895 route_handle->dht_handle->current = pending;
897 process_pending_message (route_handle->dht_handle);
899 uid_key = hash_from_uid (route_handle->uid);
901 if (GNUNET_CONTAINER_multihashmap_remove
902 (route_handle->dht_handle->outstanding_requests, uid_key,
903 route_handle) != GNUNET_YES)
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "`%s': Remove outstanding request from hashmap failed for key %s, uid %llu\n",
908 "DHT API", GNUNET_h2s (uid_key), route_handle->uid);
911 GNUNET_free (uid_key);
917 * Stop async DHT-get.
919 * @param get_handle handle to the GET operation to stop
920 * @param cont continuation to call once this message is sent to the service or times out
921 * @param cont_cls closure for the continuation
924 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle,
925 GNUNET_SCHEDULER_Task cont, void *cont_cls)
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "`%s': Removing pending get request with key %s, uid %llu\n",
930 "DHT API", GNUNET_h2s (&get_handle->route_handle->key),
931 get_handle->route_handle->uid);
933 GNUNET_DHT_route_stop (get_handle->route_handle, cont, cont_cls);
934 GNUNET_free (get_handle);
940 * Perform an asynchronous FIND PEER operation on the DHT.
942 * @param handle handle to the DHT service
943 * @param timeout timeout for this request to be sent to the
945 * @param options routing options for this message
946 * @param message a message to inject at found peers (may be null)
947 * @param key the key to look up
948 * @param proc function to call on each result
949 * @param proc_cls closure for proc
950 * @param cont continuation to call once message sent
951 * @param cont_cls closure for continuation
953 * @return handle to stop the async get, NULL on error
955 struct GNUNET_DHT_FindPeerHandle *
956 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
957 struct GNUNET_TIME_Relative timeout,
958 enum GNUNET_DHT_RouteOption options,
959 struct GNUNET_MessageHeader *message,
960 const GNUNET_HashCode * key,
961 GNUNET_DHT_FindPeerProcessor proc,
963 GNUNET_SCHEDULER_Task cont, void *cont_cls)
965 struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
966 struct GNUNET_DHT_FindPeerMessage *find_peer_msg;
969 if (handle->current != NULL) /* Can't send right now, we have a pending message... */
973 msize = ntohs (message->size);
978 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerHandle));
979 find_peer_handle->find_peer_context.proc = proc;
980 find_peer_handle->find_peer_context.proc_cls = proc_cls;
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984 "`%s': Inserting pending `%s' request with key %s\n", "DHT API",
985 "FIND PEER", GNUNET_h2s (key));
989 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage) + msize);
990 find_peer_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
991 find_peer_msg->header.size =
992 htons (sizeof (struct GNUNET_DHT_FindPeerMessage));
993 find_peer_msg->msg_len = msize;
997 memcpy (&find_peer_msg[1], message, msize);
1000 find_peer_handle->route_handle =
1001 GNUNET_DHT_route_start (handle, key, 0, options, &find_peer_msg->header,
1002 timeout, &find_peer_reply_iterator,
1003 find_peer_handle, cont, cont_cls);
1004 return find_peer_handle;
1008 * Stop async find peer. Frees associated resources.
1010 * @param find_peer_handle GET operation to stop.
1011 * @param cont continuation to call once this message is sent to the service or times out
1012 * @param cont_cls closure for the continuation
1015 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle,
1016 GNUNET_SCHEDULER_Task cont, void *cont_cls)
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020 "`%s': Removing pending `%s' request with key %s, uid %llu\n",
1021 "DHT API", "FIND PEER",
1022 GNUNET_h2s (&find_peer_handle->route_handle->key),
1023 find_peer_handle->route_handle->uid);
1025 GNUNET_DHT_route_stop (find_peer_handle->route_handle, cont, cont_cls);
1026 GNUNET_free (find_peer_handle);
1032 * Perform a PUT operation storing data in the DHT.
1034 * @param handle handle to DHT service
1035 * @param key the key to store under
1036 * @param type type of the value
1037 * @param size number of bytes in data; must be less than 64k
1038 * @param data the data to store
1039 * @param exp desired expiration time for the value
1040 * @param timeout how long to wait for transmission of this request
1041 * @param cont continuation to call when done;
1042 * reason will be TIMEOUT on error,
1043 * reason will be PREREQ_DONE on success
1044 * @param cont_cls closure for cont
1046 * @return GNUNET_YES if put message is queued for transmission
1049 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
1050 const GNUNET_HashCode * key,
1054 struct GNUNET_TIME_Absolute exp,
1055 struct GNUNET_TIME_Relative timeout,
1056 GNUNET_SCHEDULER_Task cont, void *cont_cls)
1058 struct GNUNET_DHT_PutMessage *put_msg;
1061 if (handle->current != NULL)
1063 GNUNET_SCHEDULER_add_continuation (handle->sched, cont, cont_cls,
1064 GNUNET_SCHEDULER_REASON_TIMEOUT);
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1070 "`%s': Inserting pending put request with key %s\n", "DHT API",
1074 msize = sizeof (struct GNUNET_DHT_PutMessage) + size;
1075 put_msg = GNUNET_malloc (msize);
1076 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
1077 put_msg->header.size = htons (msize);
1078 put_msg->type = htons (type);
1079 put_msg->data_size = htons (size);
1080 put_msg->expiration = exp;
1081 memcpy (&put_msg[1], data, size);
1083 GNUNET_DHT_route_start (handle, key, 0, 0, &put_msg->header, timeout, NULL,
1084 NULL, cont, cont_cls);
1086 GNUNET_free (put_msg);