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_YES
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;
665 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
668 GNUNET_assert (ntohs (reply->size) >=
669 sizeof (struct GNUNET_DHT_FindPeerResultMessage));
670 result = (struct GNUNET_DHT_FindPeerResultMessage *) reply;
671 data_size = ntohs (result->data_size);
672 GNUNET_assert (ntohs (reply->size) ==
673 sizeof (struct GNUNET_DHT_GetResultMessage) + data_size);
676 result_data = (struct GNUNET_MessageHeader *) &result[1]; /* Set data pointer to end of message */
680 find_peer_handle->find_peer_context.proc (find_peer_handle->
681 find_peer_context.proc_cls,
682 &result->peer, result_data);
686 * Perform an asynchronous FIND_PEER operation on the DHT.
688 * @param handle handle to the DHT service
689 * @param key the key to look up
690 * @param desired_replication_level how many peers should ultimately receive
691 * this message (advisory only, target may be too high for the
692 * given DHT or not hit exactly).
693 * @param options options for routing
694 * @param enc send the encapsulated message to a peer close to the key
695 * @param iter function to call on each result, NULL if no replies are expected
696 * @param iter_cls closure for iter
697 * @param timeout when to abort with an error if we fail to get
698 * a confirmation for the request (when necessary) or how long
699 * to wait for tramission to the service
700 * @param cont continuation to call when done;
701 * reason will be TIMEOUT on error,
702 * reason will be PREREQ_DONE on success
703 * @param cont_cls closure for cont
705 * @return handle to stop the request, NULL if the request is "fire and forget"
707 struct GNUNET_DHT_RouteHandle *
708 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
709 const GNUNET_HashCode * key,
710 unsigned int desired_replication_level,
711 enum GNUNET_DHT_RouteOption options,
712 const struct GNUNET_MessageHeader *enc,
713 struct GNUNET_TIME_Relative timeout,
714 GNUNET_DHT_ReplyProcessor iter,
716 GNUNET_SCHEDULER_Task cont, void *cont_cls)
718 struct GNUNET_DHT_RouteHandle *route_handle;
719 struct PendingMessage *pending;
720 struct GNUNET_DHT_Message *message;
723 GNUNET_HashCode *uid_key;
726 is_unique = GNUNET_YES;
728 is_unique = GNUNET_NO;
735 GNUNET_free_non_null (uid_key);
736 uid = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, -1);
737 uid_key = hash_from_uid (uid);
739 while (GNUNET_CONTAINER_multihashmap_contains
740 (handle->outstanding_requests, uid_key) == GNUNET_YES);
744 route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
745 memcpy (&route_handle->key, key, sizeof (GNUNET_HashCode));
746 route_handle->iter = iter;
747 route_handle->iter_cls = iter_cls;
748 route_handle->dht_handle = handle;
749 route_handle->uid = uid;
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "`%s': Unique ID is %llu\n", "DHT API", uid);
755 * Store based on random identifier!
757 GNUNET_CONTAINER_multihashmap_put (handle->outstanding_requests,
758 uid_key, route_handle,
759 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
760 msize = sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size);
765 msize = sizeof (struct GNUNET_DHT_Message) + ntohs (enc->size);
768 GNUNET_free (uid_key);
769 message = GNUNET_malloc (msize);
770 message->header.size = htons (msize);
771 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT);
772 memcpy (&message->key, key, sizeof (GNUNET_HashCode));
773 message->options = htons (options);
774 message->desired_replication_level = htons (options);
775 message->unique = htons (is_unique);
776 message->unique_id = GNUNET_htonll (uid);
777 memcpy (&message[1], enc, ntohs (enc->size));
779 pending = GNUNET_malloc (sizeof (struct PendingMessage));
780 pending->msg = &message->header;
781 pending->timeout = timeout;
782 pending->cont = cont;
783 pending->cont_cls = cont_cls;
784 pending->is_unique = is_unique;
785 pending->unique_id = uid;
787 GNUNET_assert (handle->current == NULL);
789 handle->current = pending;
791 process_pending_message (handle);
797 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
798 GNUNET_SCHEDULER_Task cont, void *cont_cls);
801 * Perform an asynchronous GET operation on the DHT identified.
803 * @param handle handle to the DHT service
804 * @param timeout how long to wait for transmission of this request to the service
805 * @param type expected type of the response object
806 * @param key the key to look up
807 * @param iter function to call on each result
808 * @param iter_cls closure for iter
809 * @param cont continuation to call once message sent
810 * @param cont_cls closure for continuation
812 * @return handle to stop the async get
814 struct GNUNET_DHT_GetHandle *
815 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
816 struct GNUNET_TIME_Relative timeout,
818 const GNUNET_HashCode * key,
819 GNUNET_DHT_GetIterator iter,
821 GNUNET_SCHEDULER_Task cont, void *cont_cls)
823 struct GNUNET_DHT_GetHandle *get_handle;
824 struct GNUNET_DHT_GetMessage *get_msg;
826 if (handle->current != NULL) /* Can't send right now, we have a pending message... */
829 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
830 get_handle->get_context.iter = iter;
831 get_handle->get_context.iter_cls = iter_cls;
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "`%s': Inserting pending get request with key %s\n", "DHT API",
839 get_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetMessage));
840 get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
841 get_msg->header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
842 get_msg->type = htonl (type);
844 get_handle->route_handle =
845 GNUNET_DHT_route_start (handle, key, 0, 0, &get_msg->header, timeout,
846 &get_reply_iterator, get_handle, cont, cont_cls);
851 * Stop a previously issued routing request
853 * @param route_handle handle to the request to stop
854 * @param cont continuation to call once this message is sent to the service or times out
855 * @param cont_cls closure for the continuation
859 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
860 GNUNET_SCHEDULER_Task cont, void *cont_cls)
862 struct PendingMessage *pending;
863 struct GNUNET_DHT_StopMessage *message;
865 GNUNET_HashCode *uid_key;
867 msize = sizeof (struct GNUNET_DHT_StopMessage);
869 message = GNUNET_malloc (msize);
870 message->header.size = htons (msize);
871 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_STOP);
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "`%s': Remove outstanding request for uid %llu\n", "DHT API",
877 message->unique_id = GNUNET_htonll (route_handle->uid);
879 GNUNET_assert (route_handle->dht_handle->current == NULL);
881 pending = GNUNET_malloc (sizeof (struct PendingMessage));
882 pending->msg = (struct GNUNET_MessageHeader *) message;
883 pending->timeout = DEFAULT_DHT_TIMEOUT;
884 pending->cont = cont;
885 pending->cont_cls = cont_cls;
886 pending->is_unique = GNUNET_NO;
887 pending->unique_id = route_handle->uid;
889 GNUNET_assert (route_handle->dht_handle->current == NULL);
891 route_handle->dht_handle->current = pending;
893 process_pending_message (route_handle->dht_handle);
895 uid_key = hash_from_uid (route_handle->uid);
897 if (GNUNET_CONTAINER_multihashmap_remove
898 (route_handle->dht_handle->outstanding_requests, uid_key,
899 route_handle) != GNUNET_YES)
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "`%s': Remove outstanding request from hashmap failed for key %s, uid %llu\n",
904 "DHT API", GNUNET_h2s (uid_key), route_handle->uid);
907 GNUNET_free (uid_key);
913 * Stop async DHT-get.
915 * @param get_handle handle to the GET operation to stop
916 * @param cont continuation to call once this message is sent to the service or times out
917 * @param cont_cls closure for the continuation
920 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle,
921 GNUNET_SCHEDULER_Task cont, void *cont_cls)
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925 "`%s': Removing pending get request with key %s, uid %llu\n",
926 "DHT API", GNUNET_h2s (&get_handle->route_handle->key),
927 get_handle->route_handle->uid);
929 GNUNET_DHT_route_stop (get_handle->route_handle, cont, cont_cls);
930 GNUNET_free (get_handle);
936 * Perform an asynchronous FIND PEER operation on the DHT.
938 * @param handle handle to the DHT service
939 * @param timeout timeout for this request to be sent to the
941 * @param options routing options for this message
942 * @param message a message to inject at found peers (may be null)
943 * @param key the key to look up
944 * @param proc function to call on each result
945 * @param proc_cls closure for proc
946 * @param cont continuation to call once message sent
947 * @param cont_cls closure for continuation
949 * @return handle to stop the async get, NULL on error
951 struct GNUNET_DHT_FindPeerHandle *
952 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
953 struct GNUNET_TIME_Relative timeout,
954 enum GNUNET_DHT_RouteOption options,
955 struct GNUNET_MessageHeader *message,
956 const GNUNET_HashCode * key,
957 GNUNET_DHT_FindPeerProcessor proc,
959 GNUNET_SCHEDULER_Task cont, void *cont_cls)
961 struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
962 struct GNUNET_DHT_FindPeerMessage *find_peer_msg;
965 if (handle->current != NULL) /* Can't send right now, we have a pending message... */
969 msize = ntohs (message->size);
974 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerHandle));
975 find_peer_handle->find_peer_context.proc = proc;
976 find_peer_handle->find_peer_context.proc_cls = proc_cls;
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "`%s': Inserting pending `%s' request with key %s\n", "DHT API",
981 "FIND PEER", GNUNET_h2s (key));
985 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage) + msize);
986 find_peer_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
987 find_peer_msg->header.size =
988 htons (sizeof (struct GNUNET_DHT_FindPeerMessage));
989 find_peer_msg->msg_len = msize;
993 memcpy (&find_peer_msg[1], message, msize);
996 find_peer_handle->route_handle =
997 GNUNET_DHT_route_start (handle, key, 0, options, &find_peer_msg->header,
998 timeout, &find_peer_reply_iterator,
999 find_peer_handle, cont, cont_cls);
1000 return find_peer_handle;
1004 * Stop async find peer. Frees associated resources.
1006 * @param find_peer_handle GET operation to stop.
1007 * @param cont continuation to call once this message is sent to the service or times out
1008 * @param cont_cls closure for the continuation
1011 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle,
1012 GNUNET_SCHEDULER_Task cont, void *cont_cls)
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "`%s': Removing pending `%s' request with key %s, uid %llu\n",
1017 "DHT API", "FIND PEER",
1018 GNUNET_h2s (&find_peer_handle->route_handle->key),
1019 find_peer_handle->route_handle->uid);
1021 GNUNET_DHT_route_stop (find_peer_handle->route_handle, cont, cont_cls);
1022 GNUNET_free (find_peer_handle);
1028 * Perform a PUT operation storing data in the DHT.
1030 * @param h handle to DHT service
1031 * @param key the key to store under
1032 * @param type type of the value
1033 * @param size number of bytes in data; must be less than 64k
1034 * @param data the data to store
1035 * @param exp desired expiration time for the value
1036 * @param cont continuation to call when done;
1037 * reason will be TIMEOUT on error,
1038 * reason will be PREREQ_DONE on success
1039 * @param cont_cls closure for cont
1041 * @return GNUNET_YES if put message is queued for transmission
1044 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
1045 const GNUNET_HashCode * key,
1049 struct GNUNET_TIME_Absolute exp,
1050 struct GNUNET_TIME_Relative timeout,
1051 GNUNET_SCHEDULER_Task cont, void *cont_cls)
1053 struct GNUNET_DHT_PutMessage *put_msg;
1056 if (handle->current != NULL)
1058 GNUNET_SCHEDULER_add_continuation (handle->sched, cont, cont_cls,
1059 GNUNET_SCHEDULER_REASON_TIMEOUT);
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 "`%s': Inserting pending put request with key %s\n", "DHT API",
1069 msize = sizeof (struct GNUNET_DHT_PutMessage) + size;
1070 put_msg = GNUNET_malloc (msize);
1071 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
1072 put_msg->header.size = htons (msize);
1073 put_msg->type = htonl (type);
1074 put_msg->data_size = htons (size);
1075 put_msg->expiration = exp;
1076 memcpy (&put_msg[1], data, size);
1078 GNUNET_DHT_route_start (handle, key, 0, 0, &put_msg->header, timeout, NULL,
1079 NULL, cont, cont_cls);
1081 GNUNET_free (put_msg);