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
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 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 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 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 (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 (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 (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);
885 if (GNUNET_OK != ret)
888 do_disconnect (handle);
891 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
892 GNUNET_TIME_UNIT_FOREVER_REL);
897 * Initialize the connection with the DHT service.
899 * @param cfg configuration to use
900 * @param ht_len size of the internal hash table to use for
901 * processing multiple GET/FIND requests in parallel
903 * @return handle to the DHT service, or NULL on error
905 struct GNUNET_DHT_Handle *
906 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
909 struct GNUNET_DHT_Handle *handle;
911 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
914 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
915 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
916 if (GNUNET_NO == try_connect (handle))
918 GNUNET_DHT_disconnect (handle);
926 * Shutdown connection with the DHT service.
928 * @param handle handle of the DHT connection to stop
931 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
933 struct PendingMessage *pm;
934 struct GNUNET_DHT_PutHandle *ph;
936 GNUNET_assert (NULL != handle);
938 GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
939 if (NULL != handle->th)
941 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
944 while (NULL != (pm = handle->pending_head))
946 GNUNET_assert (GNUNET_YES == pm->in_pending_queue);
947 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
949 pm->in_pending_queue = GNUNET_NO;
950 GNUNET_assert (GNUNET_YES == pm->free_on_send);
951 if (NULL != pm->cont)
952 pm->cont (pm->cont_cls, NULL);
955 while (NULL != (ph = handle->put_head))
957 GNUNET_break (NULL == ph->pending);
958 if (NULL != ph->cont)
959 ph->cont (ph->cont_cls, GNUNET_SYSERR);
960 GNUNET_DHT_put_cancel (ph);
963 if (NULL != handle->client)
965 GNUNET_CLIENT_disconnect (handle->client);
966 handle->client = NULL;
968 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
969 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
970 GNUNET_CONTAINER_multihashmap_destroy (handle->active_requests);
971 GNUNET_free (handle);
976 * Timeout for the transmission of a fire&forget-request. Clean it up.
978 * @param cls the 'struct PendingMessage'
979 * @param tc scheduler context
982 timeout_put_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
984 struct GNUNET_DHT_PutHandle *ph = cls;
985 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
987 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
988 if (NULL != ph->pending)
990 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
992 ph->pending->in_pending_queue = GNUNET_NO;
993 GNUNET_free (ph->pending);
995 if (NULL != ph->cont)
996 ph->cont (ph->cont_cls, GNUNET_NO);
997 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1005 * Function called whenever the PUT message leaves the queue. Sets
1006 * the message pointer in the put handle to NULL.
1008 * @param cls the 'struct GNUNET_DHT_PutHandle'
1012 mark_put_message_gone (void *cls,
1013 const struct GNUNET_SCHEDULER_TaskContext *tc)
1015 struct GNUNET_DHT_PutHandle *ph = cls;
1022 * Perform a PUT operation storing data in the DHT. FIXME: we should
1023 * change the protocol to get a confirmation for the PUT from the DHT
1024 * and call 'cont' only after getting the confirmation; otherwise, the
1025 * client has no good way of telling if the 'PUT' message actually got
1026 * to the DHT service!
1028 * @param handle handle to DHT service
1029 * @param key the key to store under
1030 * @param desired_replication_level estimate of how many
1031 * nearest peers this request should reach
1032 * @param options routing options for this message
1033 * @param type type of the value
1034 * @param size number of bytes in data; must be less than 64k
1035 * @param data the data to store
1036 * @param exp desired expiration time for the value
1037 * @param timeout how long to wait for transmission of this request
1038 * @param cont continuation to call when done (transmitting request to service)
1039 * You must not call GNUNET_DHT_DISCONNECT in this continuation
1040 * @param cont_cls closure for cont
1042 struct GNUNET_DHT_PutHandle *
1043 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const GNUNET_HashCode * key,
1044 uint32_t desired_replication_level,
1045 enum GNUNET_DHT_RouteOption options,
1046 enum GNUNET_BLOCK_Type type, size_t size, const char *data,
1047 struct GNUNET_TIME_Absolute exp,
1048 struct GNUNET_TIME_Relative timeout, GNUNET_DHT_PutContinuation cont,
1051 struct GNUNET_DHT_ClientPutMessage *put_msg;
1053 struct PendingMessage *pending;
1054 struct GNUNET_DHT_PutHandle *ph;
1056 msize = sizeof (struct GNUNET_DHT_ClientPutMessage) + size;
1057 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1058 (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1063 ph = GNUNET_malloc (sizeof (struct GNUNET_DHT_PutHandle));
1064 ph->dht_handle = handle;
1065 ph->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_put_request, ph);
1067 ph->cont_cls = cont_cls;
1068 ph->unique_id = ++handle->uid_gen;
1069 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1070 ph->pending = pending;
1071 put_msg = (struct GNUNET_DHT_ClientPutMessage *) &pending[1];
1072 pending->msg = &put_msg->header;
1073 pending->handle = handle;
1074 pending->cont = &mark_put_message_gone;
1075 pending->cont_cls = ph;
1076 pending->free_on_send = GNUNET_YES;
1077 put_msg->header.size = htons (msize);
1078 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT);
1079 put_msg->type = htonl (type);
1080 put_msg->options = htonl ((uint32_t) options);
1081 put_msg->desired_replication_level = htonl (desired_replication_level);
1082 put_msg->unique_id = ph->unique_id;
1083 put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
1084 put_msg->key = *key;
1085 memcpy (&put_msg[1], data, size);
1086 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1088 pending->in_pending_queue = GNUNET_YES;
1089 GNUNET_CONTAINER_DLL_insert_tail (handle->put_head,
1092 process_pending_messages (handle);
1098 * Cancels a DHT PUT operation. Note that the PUT request may still
1099 * go out over the network (we can't stop that); However, if the PUT
1100 * has not yet been sent to the service, cancelling the PUT will stop
1101 * this from happening (but there is no way for the user of this API
1102 * to tell if that is the case). The only use for this API is to
1103 * prevent a later call to 'cont' from "GNUNET_DHT_put" (i.e. because
1104 * the system is shutting down).
1106 * @param ph put operation to cancel ('cont' will no longer be called)
1109 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph)
1111 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
1113 if (NULL != ph->pending)
1115 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
1116 handle->pending_tail,
1118 GNUNET_free (ph->pending);
1121 if (ph->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1123 GNUNET_SCHEDULER_cancel (ph->timeout_task);
1124 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1126 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1134 * Perform an asynchronous GET operation on the DHT identified. See
1135 * also "GNUNET_BLOCK_evaluate".
1137 * @param handle handle to the DHT service
1138 * @param type expected type of the response object
1139 * @param key the key to look up
1140 * @param desired_replication_level estimate of how many
1141 nearest peers this request should reach
1142 * @param options routing options for this message
1143 * @param xquery extended query data (can be NULL, depending on type)
1144 * @param xquery_size number of bytes in xquery
1145 * @param iter function to call on each result
1146 * @param iter_cls closure for iter
1147 * @return handle to stop the async get
1149 struct GNUNET_DHT_GetHandle *
1150 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
1151 enum GNUNET_BLOCK_Type type, const GNUNET_HashCode * key,
1152 uint32_t desired_replication_level,
1153 enum GNUNET_DHT_RouteOption options, const void *xquery,
1154 size_t xquery_size, GNUNET_DHT_GetIterator iter,
1157 struct GNUNET_DHT_ClientGetMessage *get_msg;
1158 struct GNUNET_DHT_GetHandle *get_handle;
1160 struct PendingMessage *pending;
1162 msize = sizeof (struct GNUNET_DHT_ClientGetMessage) + xquery_size;
1163 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1164 (xquery_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1169 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending query for %s to DHT %p\n",
1170 GNUNET_h2s (key), handle);
1171 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1172 get_msg = (struct GNUNET_DHT_ClientGetMessage *) &pending[1];
1173 pending->msg = &get_msg->header;
1174 pending->handle = handle;
1175 pending->free_on_send = GNUNET_NO;
1176 get_msg->header.size = htons (msize);
1177 get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET);
1178 get_msg->options = htonl ((uint32_t) options);
1179 get_msg->desired_replication_level = htonl (desired_replication_level);
1180 get_msg->type = htonl (type);
1181 get_msg->key = *key;
1182 get_msg->unique_id = ++handle->uid_gen;
1183 memcpy (&get_msg[1], xquery, xquery_size);
1184 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1186 pending->in_pending_queue = GNUNET_YES;
1187 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
1188 get_handle->iter = iter;
1189 get_handle->iter_cls = iter_cls;
1190 get_handle->message = pending;
1191 get_handle->unique_id = get_msg->unique_id;
1192 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, key, get_handle,
1193 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1194 process_pending_messages (handle);
1200 * Stop async DHT-get.
1202 * @param get_handle handle to the GET operation to stop
1205 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
1207 struct GNUNET_DHT_Handle *handle;
1208 const struct GNUNET_DHT_ClientGetMessage *get_msg;
1209 struct GNUNET_DHT_ClientGetStopMessage *stop_msg;
1210 struct PendingMessage *pending;
1212 handle = get_handle->message->handle;
1214 (const struct GNUNET_DHT_ClientGetMessage *) get_handle->message->msg;
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending STOP for %s to DHT via %p\n",
1216 GNUNET_h2s (&get_msg->key), handle);
1219 GNUNET_malloc (sizeof (struct PendingMessage) +
1220 sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1221 stop_msg = (struct GNUNET_DHT_ClientGetStopMessage *) &pending[1];
1222 pending->msg = &stop_msg->header;
1223 pending->handle = handle;
1224 pending->free_on_send = GNUNET_YES;
1225 stop_msg->header.size =
1226 htons (sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1227 stop_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP);
1228 stop_msg->reserved = htonl (0);
1229 stop_msg->unique_id = get_msg->unique_id;
1230 stop_msg->key = get_msg->key;
1231 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1233 pending->in_pending_queue = GNUNET_YES;
1235 /* remove 'GET' from active status */
1236 GNUNET_assert (GNUNET_YES ==
1237 GNUNET_CONTAINER_multihashmap_remove (handle->active_requests,
1240 if (GNUNET_YES == get_handle->message->in_pending_queue)
1242 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1243 get_handle->message);
1244 get_handle->message->in_pending_queue = GNUNET_NO;
1246 GNUNET_free (get_handle->message);
1247 GNUNET_free (get_handle);
1249 process_pending_messages (handle);
1254 * Start monitoring the local DHT service.
1256 * @param handle Handle to the DHT service.
1257 * @param type Type of blocks that are of interest.
1258 * @param key Key of data of interest, NULL for all.
1259 * @param get_cb Callback to process monitored get messages.
1260 * @param get_resp_cb Callback to process monitored get response messages.
1261 * @param put_cb Callback to process monitored put messages.
1262 * @param cb_cls Closure for cb.
1264 * @return Handle to stop monitoring.
1266 struct GNUNET_DHT_MonitorHandle *
1267 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
1268 enum GNUNET_BLOCK_Type type,
1269 const GNUNET_HashCode *key,
1270 GNUNET_DHT_MonitorGetCB get_cb,
1271 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
1272 GNUNET_DHT_MonitorPutCB put_cb,
1275 struct GNUNET_DHT_MonitorHandle *h;
1276 struct GNUNET_DHT_MonitorStartStopMessage *m;
1277 struct PendingMessage *pending;
1279 h = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorHandle));
1280 GNUNET_CONTAINER_DLL_insert(handle->monitor_head, handle->monitor_tail, h);
1283 h->get_resp_cb = get_resp_cb;
1287 h->dht_handle = handle;
1290 h->key = GNUNET_malloc (sizeof(GNUNET_HashCode));
1291 memcpy (h->key, key, sizeof(GNUNET_HashCode));
1294 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1295 sizeof (struct PendingMessage));
1296 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1297 pending->msg = &m->header;
1298 pending->handle = handle;
1299 pending->free_on_send = GNUNET_YES;
1300 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_START);
1301 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1302 m->type = htonl(type);
1303 m->get = htons(NULL != get_cb);
1304 m->get_resp = htons(NULL != get_resp_cb);
1305 m->put = htons(NULL != put_cb);
1307 m->filter_key = htons(1);
1308 memcpy (&m->key, key, sizeof(GNUNET_HashCode));
1310 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1312 pending->in_pending_queue = GNUNET_YES;
1313 process_pending_messages (handle);
1322 * @param handle The handle to the monitor request returned by monitor_start.
1324 * On return get_handle will no longer be valid, caller must not use again!!!
1327 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle)
1329 struct GNUNET_DHT_MonitorStartStopMessage *m;
1330 struct PendingMessage *pending;
1332 GNUNET_CONTAINER_DLL_remove (handle->dht_handle->monitor_head,
1333 handle->dht_handle->monitor_tail,
1336 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1337 sizeof (struct PendingMessage));
1338 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1339 pending->msg = &m->header;
1340 pending->handle = handle->dht_handle;
1341 pending->free_on_send = GNUNET_YES;
1342 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP);
1343 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1344 m->type = htonl(handle->type);
1345 m->get = htons(NULL != handle->get_cb);
1346 m->get_resp = htons(NULL != handle->get_resp_cb);
1347 m->put = htons(NULL != handle->put_cb);
1348 if (NULL != handle->key) {
1349 m->filter_key = htons(1);
1350 memcpy (&m->key, handle->key, sizeof(GNUNET_HashCode));
1352 GNUNET_CONTAINER_DLL_insert (handle->dht_handle->pending_head,
1353 handle->dht_handle->pending_tail,
1355 pending->in_pending_queue = GNUNET_YES;
1356 process_pending_messages (handle->dht_handle);
1358 GNUNET_free_non_null (handle->key);
1359 GNUNET_free (handle);
1364 /* end of dht_api.c */