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 3, 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
27 * TODO: retransmission of pending requests maybe happens now, at least
28 * the code is in place to do so. Need to add checks when api calls
29 * happen to check if retransmission is in progress, and if so set
30 * the single pending message for transmission once the list of
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"
47 #define DEBUG_DHT_API GNUNET_NO
52 * Message that is pending
54 struct GNUNET_MessageHeader *msg;
57 * Timeout for this message
59 struct GNUNET_TIME_Relative timeout;
62 * Continuation to call on message send
63 * or message receipt confirmation
65 GNUNET_SCHEDULER_Task cont;
68 * Continuation closure
73 * Unique ID for this request
79 struct PendingMessageList
82 * This is a singly linked list.
84 struct PendingMessageList *next;
87 * The pending message.
89 struct PendingMessage *message;
92 struct GNUNET_DHT_GetContext
95 * Iterator to call on data receipt
97 GNUNET_DHT_GetIterator iter;
100 * Closure for the iterator callback
106 struct GNUNET_DHT_FindPeerContext
109 * Iterator to call on data receipt
111 GNUNET_DHT_FindPeerProcessor proc;
114 * Closure for the iterator callback
121 * Handle to a route request
123 struct GNUNET_DHT_RouteHandle
127 * Unique identifier for this request (for key collisions)
132 * Key that this get request is for
137 * Iterator to call on data receipt
139 GNUNET_DHT_ReplyProcessor iter;
142 * Closure for the iterator callback
147 * Main handle to this DHT api
149 struct GNUNET_DHT_Handle *dht_handle;
152 * The actual message sent for this request,
153 * used for retransmitting requests on service
154 * failure/reconnect. Freed on route_stop.
156 struct GNUNET_DHT_RouteMessage *message;
161 * Handle to control a get operation.
163 struct GNUNET_DHT_GetHandle
166 * Handle to the actual route operation for the get
168 struct GNUNET_DHT_RouteHandle *route_handle;
171 * The context of the get request
173 struct GNUNET_DHT_GetContext get_context;
178 * Handle to control a find peer operation.
180 struct GNUNET_DHT_FindPeerHandle
183 * Handle to the actual route operation for the request
185 struct GNUNET_DHT_RouteHandle *route_handle;
188 * The context of the find peer request
190 struct GNUNET_DHT_FindPeerContext find_peer_context;
194 enum DHT_Retransmit_Stage
197 * The API is not retransmitting anything at this time.
199 DHT_NOT_RETRANSMITTING,
202 * The API is retransmitting, and nothing has been single
203 * queued for sending.
208 * The API is retransmitting, and a single message has been
209 * queued for transmission once finished.
211 DHT_RETRANSMITTING_MESSAGE_QUEUED
216 * Connection to the DHT service.
218 struct GNUNET_DHT_Handle
223 struct GNUNET_SCHEDULER_Handle *sched;
226 * Configuration to use.
228 const struct GNUNET_CONFIGURATION_Handle *cfg;
231 * Socket (if available).
233 struct GNUNET_CLIENT_Connection *client;
236 * Currently pending transmission request.
238 struct GNUNET_CLIENT_TransmitHandle *th;
241 * Message we are currently sending, only allow
242 * a single message to be queued. If not unique
243 * (typically a put request), await a confirmation
244 * from the service that the message was received.
245 * If unique, just fire and forget.
247 struct PendingMessage *current;
250 * Hash map containing the current outstanding unique requests
252 struct GNUNET_CONTAINER_MultiHashMap *outstanding_requests;
255 * Generator for unique ids.
260 * Are we currently retransmitting requests? If so queue a _single_
261 * new request when received.
263 enum DHT_Retransmit_Stage retransmit_stage;
266 * Linked list of retranmissions, to be used in the event
267 * of a dht service disconnect/reconnect.
269 struct PendingMessageList *retransmissions;
272 * A single pending message allowed to be scheduled
273 * during retransmission phase.
275 struct PendingMessage *retransmission_buffer;
280 * Convert unique ID to hash code.
282 * @param uid unique ID to convert
283 * @param hash set to uid (extended with zeros)
286 hash_from_uid (uint64_t uid,
287 GNUNET_HashCode *hash)
289 memset (hash, 0, sizeof(GNUNET_HashCode));
290 *((uint64_t*)hash) = uid;
295 * Iterator callback to retransmit each outstanding request
296 * because the connection to the DHT service went down (and
301 static int retransmit_iterator (void *cls,
302 const GNUNET_HashCode * key,
305 struct GNUNET_DHT_RouteHandle *route_handle = value;
306 struct PendingMessageList *pending_message_list;
308 pending_message_list = GNUNET_malloc(sizeof(struct PendingMessageList) + sizeof(struct PendingMessage));
309 pending_message_list->message = (struct PendingMessage *)&pending_message_list[1];
310 pending_message_list->message->msg = &route_handle->message->header;
311 pending_message_list->message->timeout = GNUNET_TIME_relative_get_forever();
312 pending_message_list->message->cont = NULL;
313 pending_message_list->message->cont_cls = NULL;
314 pending_message_list->message->unique_id = route_handle->uid;
315 /* Add the new pending message to the front of the retransmission list */
316 pending_message_list->next = route_handle->dht_handle->retransmissions;
317 route_handle->dht_handle->retransmissions = pending_message_list;
324 * Try to (re)connect to the dht service.
326 * @return GNUNET_YES on success, GNUNET_NO on failure.
329 try_connect (struct GNUNET_DHT_Handle *handle)
331 if (handle->client != NULL)
333 handle->client = GNUNET_CLIENT_connect (handle->sched, "dht", handle->cfg);
334 if (handle->client != NULL)
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 _("Failed to connect to the dht service!\n"));
344 * Send complete (or failed), call continuation if we have one.
347 finish (struct GNUNET_DHT_Handle *handle, int code)
349 struct PendingMessage *pos = handle->current;
350 GNUNET_HashCode uid_hash;
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': Finish called!\n", "DHT API");
354 GNUNET_assert (pos != NULL);
355 hash_from_uid (pos->unique_id, &uid_hash);
356 if (pos->cont != NULL)
358 if (code == GNUNET_SYSERR)
359 GNUNET_SCHEDULER_add_continuation (handle->sched, pos->cont,
361 GNUNET_SCHEDULER_REASON_TIMEOUT);
363 GNUNET_SCHEDULER_add_continuation (handle->sched, pos->cont,
365 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
368 if (pos->unique_id == 0)
369 GNUNET_free(pos->msg);
371 handle->current = NULL;
375 * Transmit the next pending message, called by notify_transmit_ready
378 transmit_pending (void *cls, size_t size, void *buf)
380 struct GNUNET_DHT_Handle *handle = cls;
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "`%s': In transmit_pending\n", "DHT API");
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "`%s': In transmit_pending buf is NULL\n", "DHT API");
393 finish (handle, GNUNET_SYSERR);
399 if (handle->current != NULL)
401 tsize = ntohs (handle->current->msg->size);
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "`%s': Sending message size %d\n", "DHT API", tsize);
408 memcpy (buf, handle->current->msg, tsize);
409 finish (handle, GNUNET_OK);
417 /* Have no pending request */
422 * Try to send messages from list of messages to send
425 process_pending_message (struct GNUNET_DHT_Handle *handle)
428 if (handle->current == NULL)
429 return; /* action already pending */
430 if (GNUNET_YES != try_connect (handle))
432 finish (handle, GNUNET_SYSERR);
437 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Failed to transmit request to dht service.\n");
450 finish (handle, GNUNET_SYSERR);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "`%s': Scheduled sending message of size %d to service\n",
456 "DHT API", ntohs (handle->current->msg->size));
461 * Send complete (or failed), call continuation if we have one.
462 * Forward declaration.
465 finish_retransmission (struct GNUNET_DHT_Handle *handle, int code);
467 /* Forward declaration */
469 transmit_pending_retransmission (void *cls, size_t size, void *buf);
472 * Try to send messages from list of messages to send
475 process_pending_retransmissions (struct GNUNET_DHT_Handle *handle)
478 if (handle->current == NULL)
479 return; /* action already pending */
480 if (GNUNET_YES != try_connect (handle))
482 finish_retransmission (handle, GNUNET_SYSERR);
487 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
493 &transmit_pending_retransmission,
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Failed to transmit request to dht service.\n");
500 finish_retransmission (handle, GNUNET_SYSERR);
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "`%s': Scheduled sending message of size %d to service\n",
506 "DHT API", ntohs (handle->current->msg->size));
511 * Send complete (or failed), call continuation if we have one.
514 finish_retransmission (struct GNUNET_DHT_Handle *handle, int code)
516 struct PendingMessage *pos = handle->current;
517 struct PendingMessageList *pending_list;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': Finish (retransmission) called!\n", "DHT API");
521 GNUNET_assert (pos == handle->retransmissions->message);
522 pending_list = handle->retransmissions;
523 handle->retransmissions = handle->retransmissions->next;
524 GNUNET_free (pending_list);
526 if (handle->retransmissions == NULL)
528 handle->retransmit_stage = DHT_NOT_RETRANSMITTING;
531 if (handle->retransmissions != NULL)
533 handle->current = handle->retransmissions->message;
534 process_pending_retransmissions(handle);
536 else if (handle->retransmission_buffer != NULL)
538 handle->current = handle->retransmission_buffer;
539 process_pending_message(handle);
544 * Handler for messages received from the DHT service
545 * a demultiplexer which handles numerous message types
549 service_message_handler (void *cls,
550 const struct GNUNET_MessageHeader *msg)
552 struct GNUNET_DHT_Handle *handle = cls;
553 struct GNUNET_DHT_RouteResultMessage *dht_msg;
554 struct GNUNET_MessageHeader *enc_msg;
555 struct GNUNET_DHT_RouteHandle *route_handle;
557 GNUNET_HashCode uid_hash;
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "`%s': Received NULL from server, connection down!\n",
567 GNUNET_CLIENT_disconnect (handle->client, GNUNET_YES);
568 handle->client = GNUNET_CLIENT_connect (handle->sched,
571 if (handle->current != NULL)
573 finish(handle, GNUNET_SYSERR); /* If there was a current message, kill it! */
576 if ((handle->retransmit_stage != DHT_RETRANSMITTING) && (GNUNET_CONTAINER_multihashmap_iterate(handle->outstanding_requests, &retransmit_iterator, handle) > 0))
578 handle->retransmit_stage = DHT_RETRANSMITTING;
579 handle->current = handle->retransmissions->message;
580 process_pending_retransmissions(handle);
586 switch (ntohs (msg->type))
588 case GNUNET_MESSAGE_TYPE_DHT_ROUTE_RESULT:
590 dht_msg = (struct GNUNET_DHT_RouteResultMessage *) msg;
591 uid = GNUNET_ntohll (dht_msg->unique_id);
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594 "`%s': Received response to message (uid %llu)\n",
598 hash_from_uid (uid, &uid_hash);
600 GNUNET_CONTAINER_multihashmap_get (handle->outstanding_requests,
602 if (route_handle == NULL) /* We have no recollection of this request */
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "`%s': Received response to message (uid %llu), but have no recollection of it!\n",
613 ntohs (dht_msg->header.size) -
614 sizeof (struct GNUNET_DHT_RouteResultMessage);
615 GNUNET_assert (enc_size > 0);
616 enc_msg = (struct GNUNET_MessageHeader *) &dht_msg[1];
617 route_handle->iter (route_handle->iter_cls, enc_msg);
624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
625 "`%s': Received unknown message type %d\n", "DHT API",
629 GNUNET_CLIENT_receive (handle->client,
630 &service_message_handler,
631 handle, GNUNET_TIME_UNIT_FOREVER_REL);
637 * Initialize the connection with the DHT service.
639 * @param sched scheduler to use
640 * @param cfg configuration to use
641 * @param ht_len size of the internal hash table to use for
642 * processing multiple GET/FIND requests in parallel
644 * @return handle to the DHT service, or NULL on error
646 struct GNUNET_DHT_Handle *
647 GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
648 const struct GNUNET_CONFIGURATION_Handle *cfg,
651 struct GNUNET_DHT_Handle *handle;
653 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
655 handle->sched = sched;
656 handle->client = GNUNET_CLIENT_connect (sched, "dht", cfg);
657 handle->uid_gen = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, -1);
658 if (handle->client == NULL)
660 GNUNET_free (handle);
663 handle->outstanding_requests =
664 GNUNET_CONTAINER_multihashmap_create (ht_len);
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "`%s': Connection to service in progress\n", "DHT API");
669 GNUNET_CLIENT_receive (handle->client,
670 &service_message_handler,
671 handle, GNUNET_TIME_UNIT_FOREVER_REL);
677 * Shutdown connection with the DHT service.
679 * @param handle handle of the DHT connection to stop
682 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "`%s': Called GNUNET_DHT_disconnect\n", "DHT API");
688 GNUNET_assert (handle != NULL);
689 if (handle->th != NULL) /* We have a live transmit request in the Aether */
691 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
694 if (handle->current != NULL) /* We are trying to send something now, clean it up */
695 GNUNET_free (handle->current);
697 if (handle->client != NULL) /* Finally, disconnect from the service */
699 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
700 handle->client = NULL;
703 GNUNET_assert(GNUNET_CONTAINER_multihashmap_size(handle->outstanding_requests) == 0);
704 GNUNET_CONTAINER_multihashmap_destroy(handle->outstanding_requests);
705 GNUNET_free (handle);
710 * Transmit the next pending message, called by notify_transmit_ready
713 transmit_pending_retransmission (void *cls, size_t size, void *buf)
715 struct GNUNET_DHT_Handle *handle = cls;
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "`%s': In transmit_pending\n", "DHT API");
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "`%s': In transmit_pending buf is NULL\n", "DHT API");
728 finish_retransmission (handle, GNUNET_SYSERR);
734 if (handle->current != NULL)
736 tsize = ntohs (handle->current->msg->size);
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "`%s': Sending message size %d\n", "DHT API", tsize);
743 memcpy (buf, handle->current->msg, tsize);
744 finish_retransmission (handle, GNUNET_OK);
752 /* Have no pending request */
758 * Iterator called on each result obtained from a generic route
762 get_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply)
764 struct GNUNET_DHT_GetHandle *get_handle = cls;
765 struct GNUNET_DHT_GetResultMessage *result;
769 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
772 GNUNET_assert (ntohs (reply->size) >=
773 sizeof (struct GNUNET_DHT_GetResultMessage));
774 result = (struct GNUNET_DHT_GetResultMessage *) reply;
775 data_size = ntohs (reply->size) - sizeof(struct GNUNET_DHT_GetResultMessage);
777 result_data = (char *) &result[1]; /* Set data pointer to end of message */
779 get_handle->get_context.iter (get_handle->get_context.iter_cls,
780 result->expiration, &result->key,
781 ntohs (result->type), data_size, result_data);
786 * Iterator called on each result obtained from a generic route
790 find_peer_reply_iterator (void *cls, const struct GNUNET_MessageHeader *reply)
792 struct GNUNET_DHT_FindPeerHandle *find_peer_handle = cls;
793 struct GNUNET_MessageHeader *hello;
795 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Received wrong type of response to a find peer request...\n");
803 GNUNET_assert (ntohs (reply->size) >=
804 sizeof (struct GNUNET_MessageHeader));
805 hello = (struct GNUNET_MessageHeader *)&reply[1];
807 if (ntohs(hello->type) != GNUNET_MESSAGE_TYPE_HELLO)
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Encapsulated message of type %d, is not a `%s' message!\n", ntohs(hello->type), "HELLO");
813 find_peer_handle->find_peer_context.proc (find_peer_handle->
814 find_peer_context.proc_cls,
815 (struct GNUNET_HELLO_Message *)hello);
819 * Perform an asynchronous FIND_PEER operation on the DHT.
821 * @param handle handle to the DHT service
822 * @param key the key to look up
823 * @param desired_replication_level how many peers should ultimately receive
824 * this message (advisory only, target may be too high for the
825 * given DHT or not hit exactly).
826 * @param options options for routing
827 * @param enc send the encapsulated message to a peer close to the key
828 * @param iter function to call on each result, NULL if no replies are expected
829 * @param iter_cls closure for iter
830 * @param timeout when to abort with an error if we fail to get
831 * a confirmation for the request (when necessary) or how long
832 * to wait for tramission to the service
833 * @param cont continuation to call when done;
834 * reason will be TIMEOUT on error,
835 * reason will be PREREQ_DONE on success
836 * @param cont_cls closure for cont
838 * @return handle to stop the request, NULL if the request is "fire and forget"
840 struct GNUNET_DHT_RouteHandle *
841 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
842 const GNUNET_HashCode * key,
843 unsigned int desired_replication_level,
844 enum GNUNET_DHT_RouteOption options,
845 const struct GNUNET_MessageHeader *enc,
846 struct GNUNET_TIME_Relative timeout,
847 GNUNET_DHT_ReplyProcessor iter,
849 GNUNET_SCHEDULER_Task cont, void *cont_cls)
851 struct GNUNET_DHT_RouteHandle *route_handle;
852 struct PendingMessage *pending;
853 struct GNUNET_DHT_RouteMessage *message;
855 GNUNET_HashCode uid_key;
857 if ((handle->current != NULL) && (handle->retransmit_stage != DHT_RETRANSMITTING))
860 if (sizeof (struct GNUNET_DHT_RouteMessage) + ntohs (enc->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
866 route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
867 memcpy (&route_handle->key, key, sizeof (GNUNET_HashCode));
868 route_handle->iter = iter;
869 route_handle->iter_cls = iter_cls;
870 route_handle->dht_handle = handle;
873 route_handle->uid = handle->uid_gen++;
874 hash_from_uid (route_handle->uid, &uid_key);
875 GNUNET_CONTAINER_multihashmap_put (handle->outstanding_requests,
876 &uid_key, route_handle,
877 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "`%s': Unique ID is %llu\n", "DHT API", route_handle->uid);
885 msize = sizeof (struct GNUNET_DHT_RouteMessage) + ntohs (enc->size);
886 message = GNUNET_malloc (msize);
887 message->header.size = htons (msize);
888 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_ROUTE);
889 memcpy (&message->key, key, sizeof (GNUNET_HashCode));
890 message->options = htonl (options);
891 message->desired_replication_level = htonl (options);
892 message->unique_id = GNUNET_htonll (route_handle->uid);
893 memcpy (&message[1], enc, ntohs (enc->size));
894 pending = GNUNET_malloc (sizeof (struct PendingMessage));
895 pending->msg = &message->header;
896 pending->timeout = timeout;
897 pending->cont = cont;
898 pending->cont_cls = cont_cls;
899 pending->unique_id = route_handle->uid;
900 if (handle->current == NULL)
902 handle->current = pending;
903 process_pending_message (handle);
905 else if (handle->retransmit_stage == DHT_RETRANSMITTING)
907 handle->retransmit_stage = DHT_RETRANSMITTING_MESSAGE_QUEUED;
908 handle->retransmission_buffer = pending;
911 route_handle->message = message;
917 * Perform an asynchronous GET operation on the DHT identified.
919 * @param handle handle to the DHT service
920 * @param timeout how long to wait for transmission of this request to the service
921 * @param type expected type of the response object
922 * @param key the key to look up
923 * @param iter function to call on each result
924 * @param iter_cls closure for iter
925 * @param cont continuation to call once message sent
926 * @param cont_cls closure for continuation
928 * @return handle to stop the async get
930 struct GNUNET_DHT_GetHandle *
931 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
932 struct GNUNET_TIME_Relative timeout,
934 const GNUNET_HashCode * key,
935 GNUNET_DHT_GetIterator iter,
937 GNUNET_SCHEDULER_Task cont, void *cont_cls)
939 struct GNUNET_DHT_GetHandle *get_handle;
940 struct GNUNET_DHT_GetMessage get_msg;
942 if ((handle->current != NULL) && (handle->retransmit_stage != DHT_RETRANSMITTING)) /* Can't send right now, we have a pending message... */
945 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
946 get_handle->get_context.iter = iter;
947 get_handle->get_context.iter_cls = iter_cls;
950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 "`%s': Inserting pending get request with key %s\n", "DHT API",
955 get_msg.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
956 get_msg.header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
957 get_msg.type = htons (type);
959 get_handle->route_handle =
960 GNUNET_DHT_route_start (handle, key, 0, 0, &get_msg.header, timeout,
961 &get_reply_iterator, get_handle, cont, cont_cls);
968 * Stop a previously issued routing request
970 * @param route_handle handle to the request to stop
971 * @param cont continuation to call once this message is sent to the service or times out
972 * @param cont_cls closure for the continuation
975 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle,
976 GNUNET_SCHEDULER_Task cont, void *cont_cls)
978 struct PendingMessage *pending;
979 struct GNUNET_DHT_StopMessage *message;
981 GNUNET_HashCode uid_key;
983 msize = sizeof (struct GNUNET_DHT_StopMessage);
984 message = GNUNET_malloc (msize);
985 message->header.size = htons (msize);
986 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_STOP);
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 "`%s': Remove outstanding request for uid %llu\n", "DHT API",
992 message->unique_id = GNUNET_htonll (route_handle->uid);
993 pending = GNUNET_malloc (sizeof (struct PendingMessage));
994 pending->msg = (struct GNUNET_MessageHeader *) message;
995 pending->timeout = GNUNET_TIME_relative_get_forever();
996 pending->cont = cont;
997 pending->cont_cls = cont_cls;
998 pending->unique_id = 0; /* When finished is called, free pending->msg */
1000 if (route_handle->dht_handle->current == NULL)
1002 route_handle->dht_handle->current = pending;
1003 process_pending_message (route_handle->dht_handle);
1005 else if (route_handle->dht_handle->retransmit_stage == DHT_RETRANSMITTING)
1007 route_handle->dht_handle->retransmit_stage = DHT_RETRANSMITTING_MESSAGE_QUEUED;
1008 route_handle->dht_handle->retransmission_buffer = pending;
1015 hash_from_uid (route_handle->uid, &uid_key);
1016 GNUNET_assert (GNUNET_CONTAINER_multihashmap_remove
1017 (route_handle->dht_handle->outstanding_requests, &uid_key,
1018 route_handle) == GNUNET_YES);
1020 GNUNET_free(route_handle->message);
1021 GNUNET_free(route_handle);
1026 * Stop async DHT-get.
1028 * @param get_handle handle to the GET operation to stop
1029 * @param cont continuation to call once this message is sent to the service or times out
1030 * @param cont_cls closure for the continuation
1033 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle,
1034 GNUNET_SCHEDULER_Task cont, void *cont_cls)
1036 if ((get_handle->route_handle->dht_handle->current != NULL) &&
1037 (get_handle->route_handle->dht_handle->retransmit_stage != DHT_RETRANSMITTING))
1041 GNUNET_SCHEDULER_add_continuation (get_handle->route_handle->dht_handle->sched, cont, cont_cls,
1042 GNUNET_SCHEDULER_REASON_TIMEOUT);
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 "`%s': Removing pending get request with key %s, uid %llu\n",
1050 "DHT API", GNUNET_h2s (&get_handle->route_handle->key),
1051 get_handle->route_handle->uid);
1053 GNUNET_DHT_route_stop (get_handle->route_handle, cont, cont_cls);
1054 GNUNET_free (get_handle);
1059 * Perform an asynchronous FIND PEER operation on the DHT.
1061 * @param handle handle to the DHT service
1062 * @param timeout timeout for this request to be sent to the
1064 * @param options routing options for this message
1065 * @param key the key to look up
1066 * @param proc function to call on each result
1067 * @param proc_cls closure for proc
1068 * @param cont continuation to call once message sent
1069 * @param cont_cls closure for continuation
1071 * @return handle to stop the async get, NULL on error
1073 struct GNUNET_DHT_FindPeerHandle *
1074 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
1075 struct GNUNET_TIME_Relative timeout,
1076 enum GNUNET_DHT_RouteOption options,
1077 const GNUNET_HashCode * key,
1078 GNUNET_DHT_FindPeerProcessor proc,
1080 GNUNET_SCHEDULER_Task cont,
1083 struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
1084 struct GNUNET_MessageHeader find_peer_msg;
1086 if ((handle->current != NULL) && (handle->retransmit_stage != DHT_RETRANSMITTING)) /* Can't send right now, we have a pending message... */
1090 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerHandle));
1091 find_peer_handle->find_peer_context.proc = proc;
1092 find_peer_handle->find_peer_context.proc_cls = proc_cls;
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096 "`%s': Inserting pending `%s' request with key %s\n", "DHT API",
1097 "FIND PEER", GNUNET_h2s (key));
1100 find_peer_msg.size = htons(sizeof(struct GNUNET_MessageHeader));
1101 find_peer_msg.type = htons(GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
1102 find_peer_handle->route_handle =
1103 GNUNET_DHT_route_start (handle, key, 0, options, &find_peer_msg,
1104 timeout, &find_peer_reply_iterator,
1105 find_peer_handle, cont, cont_cls);
1106 return find_peer_handle;
1110 * Stop async find peer. Frees associated resources.
1112 * @param find_peer_handle GET operation to stop.
1113 * @param cont continuation to call once this message is sent to the service or times out
1114 * @param cont_cls closure for the continuation
1117 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle,
1118 GNUNET_SCHEDULER_Task cont, void *cont_cls)
1120 if ((find_peer_handle->route_handle->dht_handle->current != NULL) &&
1121 (find_peer_handle->route_handle->dht_handle->retransmit_stage != DHT_RETRANSMITTING))
1125 GNUNET_SCHEDULER_add_continuation (find_peer_handle->route_handle->dht_handle->sched, cont, cont_cls,
1126 GNUNET_SCHEDULER_REASON_TIMEOUT);
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1133 "`%s': Removing pending `%s' request with key %s, uid %llu\n",
1134 "DHT API", "FIND PEER",
1135 GNUNET_h2s (&find_peer_handle->route_handle->key),
1136 find_peer_handle->route_handle->uid);
1138 GNUNET_DHT_route_stop (find_peer_handle->route_handle, cont, cont_cls);
1139 GNUNET_free (find_peer_handle);
1145 * Perform a PUT operation storing data in the DHT.
1147 * @param handle handle to DHT service
1148 * @param key the key to store under
1149 * @param type type of the value
1150 * @param size number of bytes in data; must be less than 64k
1151 * @param data the data to store
1152 * @param exp desired expiration time for the value
1153 * @param timeout how long to wait for transmission of this request
1154 * @param cont continuation to call when done;
1155 * reason will be TIMEOUT on error,
1156 * reason will be PREREQ_DONE on success
1157 * @param cont_cls closure for cont
1159 * @return GNUNET_YES if put message is queued for transmission
1162 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
1163 const GNUNET_HashCode * key,
1167 struct GNUNET_TIME_Absolute exp,
1168 struct GNUNET_TIME_Relative timeout,
1169 GNUNET_SCHEDULER_Task cont, void *cont_cls)
1171 struct GNUNET_DHT_PutMessage *put_msg;
1172 struct GNUNET_DHT_RouteHandle *put_route;
1175 if ((handle->current != NULL) && (handle->retransmit_stage != DHT_RETRANSMITTING))
1179 GNUNET_SCHEDULER_add_continuation (handle->sched, cont, cont_cls,
1180 GNUNET_SCHEDULER_REASON_TIMEOUT);
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 "`%s': Inserting pending put request with key %s\n", "DHT API",
1191 msize = sizeof (struct GNUNET_DHT_PutMessage) + size;
1192 put_msg = GNUNET_malloc (msize);
1193 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
1194 put_msg->header.size = htons (msize);
1195 put_msg->type = htons (type);
1196 put_msg->data_size = htons (size);
1197 put_msg->expiration = GNUNET_TIME_absolute_hton(exp);
1198 memcpy (&put_msg[1], data, size);
1200 put_route = GNUNET_DHT_route_start (handle, key, 0, 0, &put_msg->header, timeout, NULL,
1201 NULL, cont, cont_cls);
1203 if (put_route == NULL) /* Route start failed! */
1207 GNUNET_SCHEDULER_add_continuation (handle->sched, cont, cont_cls,
1208 GNUNET_SCHEDULER_REASON_TIMEOUT);
1212 GNUNET_free(put_route);
1214 GNUNET_free (put_msg);