2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011, 2012 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
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_dht_service.h"
37 #define LOG(kind,...) GNUNET_log_from (kind, "dht-api",__VA_ARGS__)
40 * Entry in our list of messages to be (re-)transmitted.
45 * This is a doubly-linked list.
47 struct PendingMessage *prev;
50 * This is a doubly-linked list.
52 struct PendingMessage *next;
55 * Message that is pending, allocated at the end
58 const struct GNUNET_MessageHeader *msg;
61 * Handle to the DHT API context.
63 struct GNUNET_DHT_Handle *handle;
66 * Continuation to call when the request has been
67 * transmitted (for the first time) to the service; can be NULL.
69 GNUNET_SCHEDULER_Task cont;
77 * Unique ID for this request
82 * Free the saved message once sent, set to GNUNET_YES for messages
83 * that do not receive responses; GNUNET_NO if this pending message
84 * is aliased from a 'struct GNUNET_DHT_RouteHandle' and will be freed
90 * GNUNET_YES if this message is in our pending queue right now.
98 * Handle to a PUT request.
100 struct GNUNET_DHT_PutHandle
105 struct GNUNET_DHT_PutHandle *next;
110 struct GNUNET_DHT_PutHandle *prev;
113 * Continuation to call when done.
115 GNUNET_DHT_PutContinuation cont;
118 * Pending message associated with this PUT operation,
119 * NULL after the message has been transmitted to the service.
121 struct PendingMessage *pending;
124 * Main handle to this DHT api
126 struct GNUNET_DHT_Handle *dht_handle;
129 * Closure for 'cont'.
134 * Timeout task for this operation.
136 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
139 * Unique ID for the PUT operation.
148 * Handle to a GET request
150 struct GNUNET_DHT_GetHandle
154 * Iterator to call on data receipt
156 GNUNET_DHT_GetIterator iter;
159 * Closure for the iterator callback
164 * Main handle to this DHT api
166 struct GNUNET_DHT_Handle *dht_handle;
169 * The actual message sent for this request,
170 * used for retransmitting requests on service
171 * failure/reconnect. Freed on route_stop.
173 struct PendingMessage *message;
176 * Key that this get request is for
178 struct GNUNET_HashCode key;
181 * Unique identifier for this request (for key collisions).
189 * Handle to a monitoring request.
191 struct GNUNET_DHT_MonitorHandle
196 struct GNUNET_DHT_MonitorHandle *next;
201 struct GNUNET_DHT_MonitorHandle *prev;
204 * Main handle to this DHT api.
206 struct GNUNET_DHT_Handle *dht_handle;
209 * Type of block looked for.
211 enum GNUNET_BLOCK_Type type;
214 * Key being looked for, NULL == all.
216 struct GNUNET_HashCode *key;
219 * Callback for each received message of type get.
221 GNUNET_DHT_MonitorGetCB get_cb;
224 * Callback for each received message of type get response.
226 GNUNET_DHT_MonitorGetRespCB get_resp_cb;
229 * Callback for each received message of type put.
231 GNUNET_DHT_MonitorPutCB put_cb;
242 * Connection to the DHT service.
244 struct GNUNET_DHT_Handle
248 * Configuration to use.
250 const struct GNUNET_CONFIGURATION_Handle *cfg;
253 * Socket (if available).
255 struct GNUNET_CLIENT_Connection *client;
258 * Currently pending transmission request (or NULL).
260 struct GNUNET_CLIENT_TransmitHandle *th;
263 * Head of linked list of messages we would like to transmit.
265 struct PendingMessage *pending_head;
268 * Tail of linked list of messages we would like to transmit.
270 struct PendingMessage *pending_tail;
273 * Head of linked list of messages we would like to monitor.
275 struct GNUNET_DHT_MonitorHandle *monitor_head;
278 * Tail of linked list of messages we would like to monitor.
280 struct GNUNET_DHT_MonitorHandle *monitor_tail;
283 * Head of active PUT requests.
285 struct GNUNET_DHT_PutHandle *put_head;
288 * Tail of active PUT requests.
290 struct GNUNET_DHT_PutHandle *put_tail;
293 * Hash map containing the current outstanding unique GET requests
294 * (values are of type 'struct GNUNET_DHT_GetHandle').
296 struct GNUNET_CONTAINER_MultiHashMap *active_requests;
299 * Task for trying to reconnect.
301 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
304 * How quickly should we retry? Used for exponential back-off on
307 struct GNUNET_TIME_Relative retry_time;
310 * Generator for unique ids.
315 * Did we start our receive loop yet?
322 * Handler for messages received from the DHT service
323 * a demultiplexer which handles numerous message types
325 * @param cls the 'struct GNUNET_DHT_Handle'
326 * @param msg the incoming message
329 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg);
333 * Try to (re)connect to the DHT service.
335 * @param handle DHT handle to reconnect
336 * @return GNUNET_YES on success, GNUNET_NO on failure.
339 try_connect (struct GNUNET_DHT_Handle *handle)
341 if (NULL != handle->client)
343 handle->in_receive = GNUNET_NO;
344 handle->client = GNUNET_CLIENT_connect ("dht", handle->cfg);
345 if (NULL == handle->client)
347 LOG (GNUNET_ERROR_TYPE_WARNING,
348 _("Failed to connect to the DHT service!\n"));
356 * Add the request corresponding to the given route handle
357 * to the pending queue (if it is not already in there).
359 * @param cls the 'struct GNUNET_DHT_Handle*'
360 * @param key key for the request (not used)
361 * @param value the 'struct GNUNET_DHT_GetHandle*'
362 * @return GNUNET_YES (always)
365 add_request_to_pending (void *cls, const struct GNUNET_HashCode * key, void *value)
367 struct GNUNET_DHT_Handle *handle = cls;
368 struct GNUNET_DHT_GetHandle *rh = value;
370 if (GNUNET_NO == rh->message->in_pending_queue)
372 LOG (GNUNET_ERROR_TYPE_DEBUG,
373 "Retransmitting request related to %s to DHT %p\n", GNUNET_h2s (key),
375 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
377 rh->message->in_pending_queue = GNUNET_YES;
384 * Try to send messages from list of messages to send
386 * @param handle DHT_Handle
389 process_pending_messages (struct GNUNET_DHT_Handle *handle);
393 * Try reconnecting to the dht service.
395 * @param cls GNUNET_DHT_Handle
396 * @param tc scheduler context
399 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
401 struct GNUNET_DHT_Handle *handle = cls;
403 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting with DHT %p\n", handle);
404 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
405 if (handle->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
406 handle->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
408 handle->retry_time = GNUNET_TIME_relative_multiply (handle->retry_time, 2);
409 if (handle->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
410 handle->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
411 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
412 if (GNUNET_YES != try_connect (handle))
414 LOG (GNUNET_ERROR_TYPE_DEBUG, "dht reconnect failed(!)\n");
417 GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
418 &add_request_to_pending, handle);
419 process_pending_messages (handle);
424 * Try reconnecting to the DHT service.
426 * @param handle handle to dht to (possibly) disconnect and reconnect
429 do_disconnect (struct GNUNET_DHT_Handle *handle)
431 struct GNUNET_DHT_PutHandle *ph;
432 struct GNUNET_DHT_PutHandle *next;
434 if (NULL == handle->client)
436 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == handle->reconnect_task);
437 if (NULL != handle->th)
438 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Disconnecting from DHT service, will try to reconnect in %llu ms\n",
442 (unsigned long long) handle->retry_time.rel_value);
443 GNUNET_CLIENT_disconnect (handle->client);
444 handle->client = NULL;
446 /* signal disconnect to all PUT requests that were transmitted but waiting
447 for the put confirmation */
448 next = handle->put_head;
449 while (NULL != (ph = next))
452 if (NULL == ph->pending)
454 if (NULL != ph->cont)
455 ph->cont (ph->cont_cls, GNUNET_SYSERR);
456 GNUNET_DHT_put_cancel (ph);
459 handle->reconnect_task =
460 GNUNET_SCHEDULER_add_delayed (handle->retry_time, &try_reconnect, handle);
465 * Transmit the next pending message, called by notify_transmit_ready
467 * @param cls the DHT handle
468 * @param size number of bytes available in 'buf' for transmission
469 * @param buf where to copy messages for the service
470 * @return number of bytes written to 'buf'
473 transmit_pending (void *cls, size_t size, void *buf);
477 * Try to send messages from list of messages to send
479 * @param handle handle to DHT
482 process_pending_messages (struct GNUNET_DHT_Handle *handle)
484 struct PendingMessage *head;
486 if (NULL == handle->client)
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "process_pending_messages called, but client is NULL, reconnecting\n");
490 do_disconnect (handle);
493 if (NULL != handle->th)
495 if (NULL == (head = handle->pending_head))
498 GNUNET_CLIENT_notify_transmit_ready (handle->client,
499 ntohs (head->msg->size),
500 GNUNET_TIME_UNIT_FOREVER_REL,
501 GNUNET_YES, &transmit_pending,
503 if (NULL != handle->th)
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
506 "notify_transmit_ready returned NULL, reconnecting\n");
507 do_disconnect (handle);
512 * Transmit the next pending message, called by notify_transmit_ready
514 * @param cls the DHT handle
515 * @param size number of bytes available in 'buf' for transmission
516 * @param buf where to copy messages for the service
517 * @return number of bytes written to 'buf'
520 transmit_pending (void *cls, size_t size, void *buf)
522 struct GNUNET_DHT_Handle *handle = cls;
523 struct PendingMessage *head;
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Transmission to DHT service failed! Reconnecting!\n");
531 do_disconnect (handle);
534 if (NULL == (head = handle->pending_head))
537 tsize = ntohs (head->msg->size);
540 process_pending_messages (handle);
543 memcpy (buf, head->msg, tsize);
544 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
546 head->in_pending_queue = GNUNET_NO;
547 if (NULL != head->cont)
549 head->cont (head->cont_cls, NULL);
551 head->cont_cls = NULL;
553 if (GNUNET_YES == head->free_on_send)
555 process_pending_messages (handle);
556 LOG (GNUNET_ERROR_TYPE_DEBUG,
557 "Forwarded request of %u bytes to DHT service\n", (unsigned int) tsize);
558 if (GNUNET_NO == handle->in_receive)
560 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting to process replies from DHT\n");
561 handle->in_receive = GNUNET_YES;
562 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
563 GNUNET_TIME_UNIT_FOREVER_REL);
570 * Process a given reply that might match the given
573 * @param cls the 'struct GNUNET_DHT_ClientResultMessage'
574 * @param key query of the request
575 * @param value the 'struct GNUNET_DHT_RouteHandle' of a request matching the same key
576 * @return GNUNET_YES to continue to iterate over all results,
577 * GNUNET_NO if the reply is malformed
580 process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
582 const struct GNUNET_DHT_ClientResultMessage *dht_msg = cls;
583 struct GNUNET_DHT_GetHandle *get_handle = value;
584 const struct GNUNET_PeerIdentity *put_path;
585 const struct GNUNET_PeerIdentity *get_path;
586 uint32_t put_path_length;
587 uint32_t get_path_length;
593 if (dht_msg->unique_id != get_handle->unique_id)
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
597 "Ignoring reply for %s: UID mismatch: %llu/%llu\n", GNUNET_h2s (key),
598 dht_msg->unique_id, get_handle->unique_id);
601 msize = ntohs (dht_msg->header.size);
602 put_path_length = ntohl (dht_msg->put_path_length);
603 get_path_length = ntohl (dht_msg->get_path_length);
605 sizeof (struct GNUNET_DHT_ClientResultMessage) +
606 sizeof (struct GNUNET_PeerIdentity) * (get_path_length + put_path_length);
607 if ((msize < meta_length) ||
609 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
611 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
616 data_length = msize - meta_length;
617 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving %u byte reply for %s to application\n",
618 (unsigned int) data_length, GNUNET_h2s (key));
619 put_path = (const struct GNUNET_PeerIdentity *) &dht_msg[1];
620 get_path = &put_path[put_path_length];
621 data = &get_path[get_path_length];
622 get_handle->iter (get_handle->iter_cls,
623 GNUNET_TIME_absolute_ntoh (dht_msg->expiration), key,
624 get_path, get_path_length, put_path, put_path_length,
625 ntohl (dht_msg->type), data_length, data);
630 * Process a get monitor message from the service.
632 * @param handle The DHT handle.
633 * @param msg Monitor get message from the service.
635 * @return GNUNET_OK if everything went fine,
636 * GNUNET_SYSERR if the message is malformed.
639 process_monitor_get_message (struct GNUNET_DHT_Handle *handle,
640 const struct GNUNET_DHT_MonitorGetMessage *msg)
642 struct GNUNET_DHT_MonitorHandle *h;
644 for (h = handle->monitor_head; NULL != h; h = h->next)
649 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
650 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
651 sizeof (struct GNUNET_HashCode)));
652 if (type_ok && key_ok && (NULL != h->get_cb))
653 h->get_cb (h->cb_cls,
654 ntohl (msg->options),
655 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
656 ntohl (msg->hop_count),
657 ntohl (msg->desired_replication_level),
658 ntohl (msg->get_path_length),
659 (struct GNUNET_PeerIdentity *) &msg[1],
667 * Process a get response monitor message from the service.
669 * @param handle The DHT handle.
670 * @param msg monitor get response message from the service
671 * @return GNUNET_OK if everything went fine,
672 * GNUNET_SYSERR if the message is malformed.
675 process_monitor_get_resp_message (struct GNUNET_DHT_Handle *handle,
676 const struct GNUNET_DHT_MonitorGetRespMessage
679 struct GNUNET_DHT_MonitorHandle *h;
680 struct GNUNET_PeerIdentity *path;
685 msize = ntohs (msg->header.size);
686 path = (struct GNUNET_PeerIdentity *) &msg[1];
687 getl = ntohl (msg->get_path_length);
688 putl = ntohl (msg->put_path_length);
689 if ( (getl + putl < getl) ||
690 ( ((msize - sizeof (struct GNUNET_DHT_MonitorGetRespMessage)) / sizeof (struct GNUNET_PeerIdentity)) < getl + putl) )
693 return GNUNET_SYSERR;
695 for (h = handle->monitor_head; NULL != h; h = h->next)
700 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
701 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
702 sizeof (struct GNUNET_HashCode)));
703 if (type_ok && key_ok && (NULL != h->get_resp_cb))
704 h->get_resp_cb (h->cb_cls,
705 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
708 GNUNET_TIME_absolute_ntoh(msg->expiration_time),
710 (void *) &path[getl + putl],
712 sizeof (struct GNUNET_DHT_MonitorGetRespMessage) -
713 sizeof (struct GNUNET_PeerIdentity) * (putl + getl));
720 * Process a put monitor message from the service.
722 * @param handle The DHT handle.
723 * @param msg Monitor put message from the service.
725 * @return GNUNET_OK if everything went fine,
726 * GNUNET_SYSERR if the message is malformed.
729 process_monitor_put_message (struct GNUNET_DHT_Handle *handle,
730 const struct GNUNET_DHT_MonitorPutMessage *msg)
732 struct GNUNET_DHT_MonitorHandle *h;
734 struct GNUNET_PeerIdentity *path;
737 msize = ntohs (msg->header.size);
738 path = (struct GNUNET_PeerIdentity *) &msg[1];
739 putl = ntohl (msg->put_path_length);
740 if (((msize - sizeof (struct GNUNET_DHT_MonitorGetRespMessage)) / sizeof (struct GNUNET_PeerIdentity)) < putl)
743 return GNUNET_SYSERR;
745 for (h = handle->monitor_head; NULL != h; h = h->next)
750 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
751 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
752 sizeof (struct GNUNET_HashCode)));
753 if (type_ok && key_ok && (NULL != h->put_cb))
754 h->put_cb (h->cb_cls,
755 ntohl (msg->options),
756 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
757 ntohl (msg->hop_count),
758 ntohl (msg->desired_replication_level),
760 GNUNET_TIME_absolute_ntoh(msg->expiration_time),
762 (void *) &path[putl],
764 sizeof (struct GNUNET_DHT_MonitorPutMessage) -
765 sizeof (struct GNUNET_PeerIdentity) * putl);
772 * Process a put confirmation message from the service.
774 * @param handle The DHT handle.
775 * @param msg confirmation message from the service.
776 * @return GNUNET_OK if everything went fine,
777 * GNUNET_SYSERR if the message is malformed.
780 process_put_confirmation_message (struct GNUNET_DHT_Handle *handle,
781 const struct GNUNET_DHT_ClientPutConfirmationMessage *msg)
783 struct GNUNET_DHT_PutHandle *ph;
784 GNUNET_DHT_PutContinuation cont;
787 for (ph = handle->put_head; NULL != ph; ph = ph->next)
788 if (ph->unique_id == msg->unique_id)
793 cont_cls = ph->cont_cls;
794 GNUNET_DHT_put_cancel (ph);
796 cont (cont_cls, GNUNET_OK);
802 * Handler for messages received from the DHT service
803 * a demultiplexer which handles numerous message types
805 * @param cls the 'struct GNUNET_DHT_Handle'
806 * @param msg the incoming message
809 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
811 struct GNUNET_DHT_Handle *handle = cls;
812 const struct GNUNET_DHT_ClientResultMessage *dht_msg;
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 "Error receiving data from DHT service, reconnecting\n");
820 do_disconnect (handle);
824 msize = ntohs (msg->size);
825 switch (ntohs (msg->type))
827 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET:
828 if (msize < sizeof (struct GNUNET_DHT_MonitorGetMessage))
833 ret = process_monitor_get_message(handle,
834 (const struct GNUNET_DHT_MonitorGetMessage *) msg);
836 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP:
837 if (msize < sizeof (struct GNUNET_DHT_MonitorGetRespMessage))
842 ret = process_monitor_get_resp_message(handle,
843 (const struct GNUNET_DHT_MonitorGetRespMessage *) msg);
845 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT:
846 if (msize < sizeof (struct GNUNET_DHT_MonitorPutMessage))
851 ret = process_monitor_put_message(handle,
852 (const struct GNUNET_DHT_MonitorPutMessage *) msg);
854 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT_RESP:
855 /* Not implemented yet */
858 case GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT:
859 if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_ClientResultMessage))
865 dht_msg = (const struct GNUNET_DHT_ClientResultMessage *) msg;
866 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply for `%s' from DHT service %p\n",
867 GNUNET_h2s (&dht_msg->key), handle);
868 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
869 &dht_msg->key, &process_reply,
872 case GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK:
873 if (ntohs (msg->size) != sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage))
878 ret = process_put_confirmation_message (handle,
879 (const struct GNUNET_DHT_ClientPutConfirmationMessage*) msg);
883 LOG (GNUNET_ERROR_TYPE_WARNING,
884 "Unknown DHT message type: %hu (%hu) size: %hu\n",
885 ntohs (msg->type), msg->type, msize);
888 if (GNUNET_OK != ret)
891 do_disconnect (handle);
894 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
895 GNUNET_TIME_UNIT_FOREVER_REL);
900 * Initialize the connection with the DHT service.
902 * @param cfg configuration to use
903 * @param ht_len size of the internal hash table to use for
904 * processing multiple GET/FIND requests in parallel
906 * @return handle to the DHT service, or NULL on error
908 struct GNUNET_DHT_Handle *
909 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
912 struct GNUNET_DHT_Handle *handle;
914 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
917 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
918 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
919 if (GNUNET_NO == try_connect (handle))
921 GNUNET_DHT_disconnect (handle);
929 * Shutdown connection with the DHT service.
931 * @param handle handle of the DHT connection to stop
934 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
936 struct PendingMessage *pm;
937 struct GNUNET_DHT_PutHandle *ph;
939 GNUNET_assert (NULL != handle);
941 GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
942 if (NULL != handle->th)
944 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
947 while (NULL != (pm = handle->pending_head))
949 GNUNET_assert (GNUNET_YES == pm->in_pending_queue);
950 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
952 pm->in_pending_queue = GNUNET_NO;
953 GNUNET_assert (GNUNET_YES == pm->free_on_send);
954 if (NULL != pm->cont)
955 pm->cont (pm->cont_cls, NULL);
958 while (NULL != (ph = handle->put_head))
960 GNUNET_break (NULL == ph->pending);
961 if (NULL != ph->cont)
962 ph->cont (ph->cont_cls, GNUNET_SYSERR);
963 GNUNET_DHT_put_cancel (ph);
966 if (NULL != handle->client)
968 GNUNET_CLIENT_disconnect (handle->client);
969 handle->client = NULL;
971 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
972 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
973 GNUNET_CONTAINER_multihashmap_destroy (handle->active_requests);
974 GNUNET_free (handle);
979 * Timeout for the transmission of a fire&forget-request. Clean it up.
981 * @param cls the 'struct PendingMessage'
982 * @param tc scheduler context
985 timeout_put_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
987 struct GNUNET_DHT_PutHandle *ph = cls;
988 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
990 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
991 if (NULL != ph->pending)
993 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
995 ph->pending->in_pending_queue = GNUNET_NO;
996 GNUNET_free (ph->pending);
998 if (NULL != ph->cont)
999 ph->cont (ph->cont_cls, GNUNET_NO);
1000 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1008 * Function called whenever the PUT message leaves the queue. Sets
1009 * the message pointer in the put handle to NULL.
1011 * @param cls the 'struct GNUNET_DHT_PutHandle'
1015 mark_put_message_gone (void *cls,
1016 const struct GNUNET_SCHEDULER_TaskContext *tc)
1018 struct GNUNET_DHT_PutHandle *ph = cls;
1025 * Perform a PUT operation storing data in the DHT. FIXME: we should
1026 * change the protocol to get a confirmation for the PUT from the DHT
1027 * and call 'cont' only after getting the confirmation; otherwise, the
1028 * client has no good way of telling if the 'PUT' message actually got
1029 * to the DHT service!
1031 * @param handle handle to DHT service
1032 * @param key the key to store under
1033 * @param desired_replication_level estimate of how many
1034 * nearest peers this request should reach
1035 * @param options routing options for this message
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 (transmitting request to service)
1042 * You must not call GNUNET_DHT_DISCONNECT in this continuation
1043 * @param cont_cls closure for cont
1045 struct GNUNET_DHT_PutHandle *
1046 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const struct GNUNET_HashCode * key,
1047 uint32_t desired_replication_level,
1048 enum GNUNET_DHT_RouteOption options,
1049 enum GNUNET_BLOCK_Type type, size_t size, const char *data,
1050 struct GNUNET_TIME_Absolute exp,
1051 struct GNUNET_TIME_Relative timeout, GNUNET_DHT_PutContinuation cont,
1054 struct GNUNET_DHT_ClientPutMessage *put_msg;
1056 struct PendingMessage *pending;
1057 struct GNUNET_DHT_PutHandle *ph;
1059 msize = sizeof (struct GNUNET_DHT_ClientPutMessage) + size;
1060 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1061 (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1066 ph = GNUNET_malloc (sizeof (struct GNUNET_DHT_PutHandle));
1067 ph->dht_handle = handle;
1068 ph->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_put_request, ph);
1070 ph->cont_cls = cont_cls;
1071 ph->unique_id = ++handle->uid_gen;
1072 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1073 ph->pending = pending;
1074 put_msg = (struct GNUNET_DHT_ClientPutMessage *) &pending[1];
1075 pending->msg = &put_msg->header;
1076 pending->handle = handle;
1077 pending->cont = &mark_put_message_gone;
1078 pending->cont_cls = ph;
1079 pending->free_on_send = GNUNET_YES;
1080 put_msg->header.size = htons (msize);
1081 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT);
1082 put_msg->type = htonl (type);
1083 put_msg->options = htonl ((uint32_t) options);
1084 put_msg->desired_replication_level = htonl (desired_replication_level);
1085 put_msg->unique_id = ph->unique_id;
1086 put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
1087 put_msg->key = *key;
1088 memcpy (&put_msg[1], data, size);
1089 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1091 pending->in_pending_queue = GNUNET_YES;
1092 GNUNET_CONTAINER_DLL_insert_tail (handle->put_head,
1095 process_pending_messages (handle);
1101 * Cancels a DHT PUT operation. Note that the PUT request may still
1102 * go out over the network (we can't stop that); However, if the PUT
1103 * has not yet been sent to the service, cancelling the PUT will stop
1104 * this from happening (but there is no way for the user of this API
1105 * to tell if that is the case). The only use for this API is to
1106 * prevent a later call to 'cont' from "GNUNET_DHT_put" (i.e. because
1107 * the system is shutting down).
1109 * @param ph put operation to cancel ('cont' will no longer be called)
1112 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph)
1114 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
1116 if (NULL != ph->pending)
1118 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
1119 handle->pending_tail,
1121 GNUNET_free (ph->pending);
1124 if (ph->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1126 GNUNET_SCHEDULER_cancel (ph->timeout_task);
1127 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1129 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1137 * Perform an asynchronous GET operation on the DHT identified. See
1138 * also "GNUNET_BLOCK_evaluate".
1140 * @param handle handle to the DHT service
1141 * @param type expected type of the response object
1142 * @param key the key to look up
1143 * @param desired_replication_level estimate of how many
1144 nearest peers this request should reach
1145 * @param options routing options for this message
1146 * @param xquery extended query data (can be NULL, depending on type)
1147 * @param xquery_size number of bytes in xquery
1148 * @param iter function to call on each result
1149 * @param iter_cls closure for iter
1150 * @return handle to stop the async get
1152 struct GNUNET_DHT_GetHandle *
1153 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
1154 enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode * key,
1155 uint32_t desired_replication_level,
1156 enum GNUNET_DHT_RouteOption options, const void *xquery,
1157 size_t xquery_size, GNUNET_DHT_GetIterator iter,
1160 struct GNUNET_DHT_ClientGetMessage *get_msg;
1161 struct GNUNET_DHT_GetHandle *get_handle;
1163 struct PendingMessage *pending;
1165 msize = sizeof (struct GNUNET_DHT_ClientGetMessage) + xquery_size;
1166 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1167 (xquery_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending query for %s to DHT %p\n",
1173 GNUNET_h2s (key), handle);
1174 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1175 get_msg = (struct GNUNET_DHT_ClientGetMessage *) &pending[1];
1176 pending->msg = &get_msg->header;
1177 pending->handle = handle;
1178 pending->free_on_send = GNUNET_NO;
1179 get_msg->header.size = htons (msize);
1180 get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET);
1181 get_msg->options = htonl ((uint32_t) options);
1182 get_msg->desired_replication_level = htonl (desired_replication_level);
1183 get_msg->type = htonl (type);
1184 get_msg->key = *key;
1185 get_msg->unique_id = ++handle->uid_gen;
1186 memcpy (&get_msg[1], xquery, xquery_size);
1187 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1189 pending->in_pending_queue = GNUNET_YES;
1190 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
1191 get_handle->iter = iter;
1192 get_handle->iter_cls = iter_cls;
1193 get_handle->message = pending;
1194 get_handle->unique_id = get_msg->unique_id;
1195 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, key, get_handle,
1196 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1197 process_pending_messages (handle);
1203 * Stop async DHT-get.
1205 * @param get_handle handle to the GET operation to stop
1208 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
1210 struct GNUNET_DHT_Handle *handle;
1211 const struct GNUNET_DHT_ClientGetMessage *get_msg;
1212 struct GNUNET_DHT_ClientGetStopMessage *stop_msg;
1213 struct PendingMessage *pending;
1215 handle = get_handle->message->handle;
1217 (const struct GNUNET_DHT_ClientGetMessage *) get_handle->message->msg;
1218 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending STOP for %s to DHT via %p\n",
1219 GNUNET_h2s (&get_msg->key), handle);
1222 GNUNET_malloc (sizeof (struct PendingMessage) +
1223 sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1224 stop_msg = (struct GNUNET_DHT_ClientGetStopMessage *) &pending[1];
1225 pending->msg = &stop_msg->header;
1226 pending->handle = handle;
1227 pending->free_on_send = GNUNET_YES;
1228 stop_msg->header.size =
1229 htons (sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1230 stop_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP);
1231 stop_msg->reserved = htonl (0);
1232 stop_msg->unique_id = get_msg->unique_id;
1233 stop_msg->key = get_msg->key;
1234 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1236 pending->in_pending_queue = GNUNET_YES;
1238 /* remove 'GET' from active status */
1239 GNUNET_assert (GNUNET_YES ==
1240 GNUNET_CONTAINER_multihashmap_remove (handle->active_requests,
1243 if (GNUNET_YES == get_handle->message->in_pending_queue)
1245 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1246 get_handle->message);
1247 get_handle->message->in_pending_queue = GNUNET_NO;
1249 GNUNET_free (get_handle->message);
1250 GNUNET_free (get_handle);
1252 process_pending_messages (handle);
1257 * Start monitoring the local DHT service.
1259 * @param handle Handle to the DHT service.
1260 * @param type Type of blocks that are of interest.
1261 * @param key Key of data of interest, NULL for all.
1262 * @param get_cb Callback to process monitored get messages.
1263 * @param get_resp_cb Callback to process monitored get response messages.
1264 * @param put_cb Callback to process monitored put messages.
1265 * @param cb_cls Closure for cb.
1267 * @return Handle to stop monitoring.
1269 struct GNUNET_DHT_MonitorHandle *
1270 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
1271 enum GNUNET_BLOCK_Type type,
1272 const struct GNUNET_HashCode *key,
1273 GNUNET_DHT_MonitorGetCB get_cb,
1274 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
1275 GNUNET_DHT_MonitorPutCB put_cb,
1278 struct GNUNET_DHT_MonitorHandle *h;
1279 struct GNUNET_DHT_MonitorStartStopMessage *m;
1280 struct PendingMessage *pending;
1282 h = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorHandle));
1283 GNUNET_CONTAINER_DLL_insert(handle->monitor_head, handle->monitor_tail, h);
1286 h->get_resp_cb = get_resp_cb;
1290 h->dht_handle = handle;
1293 h->key = GNUNET_malloc (sizeof(struct GNUNET_HashCode));
1294 memcpy (h->key, key, sizeof(struct GNUNET_HashCode));
1297 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1298 sizeof (struct PendingMessage));
1299 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1300 pending->msg = &m->header;
1301 pending->handle = handle;
1302 pending->free_on_send = GNUNET_YES;
1303 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_START);
1304 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1305 m->type = htonl(type);
1306 m->get = htons(NULL != get_cb);
1307 m->get_resp = htons(NULL != get_resp_cb);
1308 m->put = htons(NULL != put_cb);
1310 m->filter_key = htons(1);
1311 memcpy (&m->key, key, sizeof(struct GNUNET_HashCode));
1313 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1315 pending->in_pending_queue = GNUNET_YES;
1316 process_pending_messages (handle);
1325 * @param handle The handle to the monitor request returned by monitor_start.
1327 * On return get_handle will no longer be valid, caller must not use again!!!
1330 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle)
1332 struct GNUNET_DHT_MonitorStartStopMessage *m;
1333 struct PendingMessage *pending;
1335 GNUNET_CONTAINER_DLL_remove (handle->dht_handle->monitor_head,
1336 handle->dht_handle->monitor_tail,
1339 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1340 sizeof (struct PendingMessage));
1341 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1342 pending->msg = &m->header;
1343 pending->handle = handle->dht_handle;
1344 pending->free_on_send = GNUNET_YES;
1345 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP);
1346 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1347 m->type = htonl(handle->type);
1348 m->get = htons(NULL != handle->get_cb);
1349 m->get_resp = htons(NULL != handle->get_resp_cb);
1350 m->put = htons(NULL != handle->put_cb);
1351 if (NULL != handle->key) {
1352 m->filter_key = htons(1);
1353 memcpy (&m->key, handle->key, sizeof(struct GNUNET_HashCode));
1355 GNUNET_CONTAINER_DLL_insert (handle->dht_handle->pending_head,
1356 handle->dht_handle->pending_tail,
1358 pending->in_pending_queue = GNUNET_YES;
1359 process_pending_messages (handle->dht_handle);
1361 GNUNET_free_non_null (handle->key);
1362 GNUNET_free (handle);
1367 /* end of dht_api.c */