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->retry_time = GNUNET_TIME_STD_BACKOFF (handle->retry_time);
405 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
406 if (GNUNET_YES != try_connect (handle))
408 LOG (GNUNET_ERROR_TYPE_DEBUG, "dht reconnect failed(!)\n");
411 GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
412 &add_request_to_pending, handle);
413 process_pending_messages (handle);
418 * Try reconnecting to the DHT service.
420 * @param handle handle to dht to (possibly) disconnect and reconnect
423 do_disconnect (struct GNUNET_DHT_Handle *handle)
425 struct GNUNET_DHT_PutHandle *ph;
426 struct GNUNET_DHT_PutHandle *next;
428 if (NULL == handle->client)
430 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == handle->reconnect_task);
431 if (NULL != handle->th)
432 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Disconnecting from DHT service, will try to reconnect in %s\n",
436 GNUNET_STRINGS_relative_time_to_string (handle->retry_time,
438 GNUNET_CLIENT_disconnect (handle->client);
439 handle->client = NULL;
441 /* signal disconnect to all PUT requests that were transmitted but waiting
442 for the put confirmation */
443 next = handle->put_head;
444 while (NULL != (ph = next))
447 if (NULL == ph->pending)
449 if (NULL != ph->cont)
450 ph->cont (ph->cont_cls, GNUNET_SYSERR);
451 GNUNET_DHT_put_cancel (ph);
454 handle->reconnect_task =
455 GNUNET_SCHEDULER_add_delayed (handle->retry_time, &try_reconnect, handle);
460 * Transmit the next pending message, called by notify_transmit_ready
462 * @param cls the DHT handle
463 * @param size number of bytes available in 'buf' for transmission
464 * @param buf where to copy messages for the service
465 * @return number of bytes written to 'buf'
468 transmit_pending (void *cls, size_t size, void *buf);
472 * Try to send messages from list of messages to send
474 * @param handle handle to DHT
477 process_pending_messages (struct GNUNET_DHT_Handle *handle)
479 struct PendingMessage *head;
481 if (NULL == handle->client)
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "process_pending_messages called, but client is NULL, reconnecting\n");
485 do_disconnect (handle);
488 if (NULL != handle->th)
490 if (NULL == (head = handle->pending_head))
493 GNUNET_CLIENT_notify_transmit_ready (handle->client,
494 ntohs (head->msg->size),
495 GNUNET_TIME_UNIT_FOREVER_REL,
496 GNUNET_YES, &transmit_pending,
498 if (NULL != handle->th)
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "notify_transmit_ready returned NULL, reconnecting\n");
502 do_disconnect (handle);
507 * Transmit the next pending message, called by notify_transmit_ready
509 * @param cls the DHT handle
510 * @param size number of bytes available in 'buf' for transmission
511 * @param buf where to copy messages for the service
512 * @return number of bytes written to 'buf'
515 transmit_pending (void *cls, size_t size, void *buf)
517 struct GNUNET_DHT_Handle *handle = cls;
518 struct PendingMessage *head;
524 LOG (GNUNET_ERROR_TYPE_DEBUG,
525 "Transmission to DHT service failed! Reconnecting!\n");
526 do_disconnect (handle);
529 if (NULL == (head = handle->pending_head))
532 tsize = ntohs (head->msg->size);
535 process_pending_messages (handle);
538 memcpy (buf, head->msg, tsize);
539 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
541 head->in_pending_queue = GNUNET_NO;
542 if (NULL != head->cont)
544 head->cont (head->cont_cls, NULL);
546 head->cont_cls = NULL;
548 if (GNUNET_YES == head->free_on_send)
550 process_pending_messages (handle);
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Forwarded request of %u bytes to DHT service\n", (unsigned int) tsize);
553 if (GNUNET_NO == handle->in_receive)
555 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting to process replies from DHT\n");
556 handle->in_receive = GNUNET_YES;
557 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
558 GNUNET_TIME_UNIT_FOREVER_REL);
565 * Process a given reply that might match the given
568 * @param cls the 'struct GNUNET_DHT_ClientResultMessage'
569 * @param key query of the request
570 * @param value the 'struct GNUNET_DHT_RouteHandle' of a request matching the same key
571 * @return GNUNET_YES to continue to iterate over all results,
572 * GNUNET_NO if the reply is malformed
575 process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
577 const struct GNUNET_DHT_ClientResultMessage *dht_msg = cls;
578 struct GNUNET_DHT_GetHandle *get_handle = value;
579 const struct GNUNET_PeerIdentity *put_path;
580 const struct GNUNET_PeerIdentity *get_path;
581 uint32_t put_path_length;
582 uint32_t get_path_length;
588 if (dht_msg->unique_id != get_handle->unique_id)
591 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 "Ignoring reply for %s: UID mismatch: %llu/%llu\n", GNUNET_h2s (key),
593 dht_msg->unique_id, get_handle->unique_id);
596 msize = ntohs (dht_msg->header.size);
597 put_path_length = ntohl (dht_msg->put_path_length);
598 get_path_length = ntohl (dht_msg->get_path_length);
600 sizeof (struct GNUNET_DHT_ClientResultMessage) +
601 sizeof (struct GNUNET_PeerIdentity) * (get_path_length + put_path_length);
602 if ((msize < meta_length) ||
604 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
606 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
611 data_length = msize - meta_length;
612 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving %u byte reply for %s to application\n",
613 (unsigned int) data_length, GNUNET_h2s (key));
614 put_path = (const struct GNUNET_PeerIdentity *) &dht_msg[1];
615 get_path = &put_path[put_path_length];
616 data = &get_path[get_path_length];
617 get_handle->iter (get_handle->iter_cls,
618 GNUNET_TIME_absolute_ntoh (dht_msg->expiration), key,
619 get_path, get_path_length, put_path, put_path_length,
620 ntohl (dht_msg->type), data_length, data);
625 * Process a get monitor message from the service.
627 * @param handle The DHT handle.
628 * @param msg Monitor get message from the service.
630 * @return GNUNET_OK if everything went fine,
631 * GNUNET_SYSERR if the message is malformed.
634 process_monitor_get_message (struct GNUNET_DHT_Handle *handle,
635 const struct GNUNET_DHT_MonitorGetMessage *msg)
637 struct GNUNET_DHT_MonitorHandle *h;
639 for (h = handle->monitor_head; NULL != h; h = h->next)
644 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
645 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
646 sizeof (struct GNUNET_HashCode)));
647 if (type_ok && key_ok && (NULL != h->get_cb))
648 h->get_cb (h->cb_cls,
649 ntohl (msg->options),
650 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
651 ntohl (msg->hop_count),
652 ntohl (msg->desired_replication_level),
653 ntohl (msg->get_path_length),
654 (struct GNUNET_PeerIdentity *) &msg[1],
662 * Process a get response monitor message from the service.
664 * @param handle The DHT handle.
665 * @param msg monitor get response message from the service
666 * @return GNUNET_OK if everything went fine,
667 * GNUNET_SYSERR if the message is malformed.
670 process_monitor_get_resp_message (struct GNUNET_DHT_Handle *handle,
671 const struct GNUNET_DHT_MonitorGetRespMessage
674 struct GNUNET_DHT_MonitorHandle *h;
675 struct GNUNET_PeerIdentity *path;
680 msize = ntohs (msg->header.size);
681 path = (struct GNUNET_PeerIdentity *) &msg[1];
682 getl = ntohl (msg->get_path_length);
683 putl = ntohl (msg->put_path_length);
684 if ( (getl + putl < getl) ||
685 ( ((msize - sizeof (struct GNUNET_DHT_MonitorGetRespMessage)) / sizeof (struct GNUNET_PeerIdentity)) < getl + putl) )
688 return GNUNET_SYSERR;
690 for (h = handle->monitor_head; NULL != h; h = h->next)
695 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
696 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
697 sizeof (struct GNUNET_HashCode)));
698 if (type_ok && key_ok && (NULL != h->get_resp_cb))
699 h->get_resp_cb (h->cb_cls,
700 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
703 GNUNET_TIME_absolute_ntoh(msg->expiration_time),
705 (void *) &path[getl + putl],
707 sizeof (struct GNUNET_DHT_MonitorGetRespMessage) -
708 sizeof (struct GNUNET_PeerIdentity) * (putl + getl));
715 * Process a put monitor message from the service.
717 * @param handle The DHT handle.
718 * @param msg Monitor put message from the service.
720 * @return GNUNET_OK if everything went fine,
721 * GNUNET_SYSERR if the message is malformed.
724 process_monitor_put_message (struct GNUNET_DHT_Handle *handle,
725 const struct GNUNET_DHT_MonitorPutMessage *msg)
727 struct GNUNET_DHT_MonitorHandle *h;
729 struct GNUNET_PeerIdentity *path;
732 msize = ntohs (msg->header.size);
733 path = (struct GNUNET_PeerIdentity *) &msg[1];
734 putl = ntohl (msg->put_path_length);
735 if (((msize - sizeof (struct GNUNET_DHT_MonitorGetRespMessage)) / sizeof (struct GNUNET_PeerIdentity)) < putl)
738 return GNUNET_SYSERR;
740 for (h = handle->monitor_head; NULL != h; h = h->next)
745 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
746 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
747 sizeof (struct GNUNET_HashCode)));
748 if (type_ok && key_ok && (NULL != h->put_cb))
749 h->put_cb (h->cb_cls,
750 ntohl (msg->options),
751 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
752 ntohl (msg->hop_count),
753 ntohl (msg->desired_replication_level),
755 GNUNET_TIME_absolute_ntoh(msg->expiration_time),
757 (void *) &path[putl],
759 sizeof (struct GNUNET_DHT_MonitorPutMessage) -
760 sizeof (struct GNUNET_PeerIdentity) * putl);
767 * Process a put confirmation message from the service.
769 * @param handle The DHT handle.
770 * @param msg confirmation message from the service.
771 * @return GNUNET_OK if everything went fine,
772 * GNUNET_SYSERR if the message is malformed.
775 process_put_confirmation_message (struct GNUNET_DHT_Handle *handle,
776 const struct GNUNET_DHT_ClientPutConfirmationMessage *msg)
778 struct GNUNET_DHT_PutHandle *ph;
779 GNUNET_DHT_PutContinuation cont;
782 for (ph = handle->put_head; NULL != ph; ph = ph->next)
783 if (ph->unique_id == msg->unique_id)
788 cont_cls = ph->cont_cls;
789 GNUNET_DHT_put_cancel (ph);
791 cont (cont_cls, GNUNET_OK);
797 * Handler for messages received from the DHT service
798 * a demultiplexer which handles numerous message types
800 * @param cls the 'struct GNUNET_DHT_Handle'
801 * @param msg the incoming message
804 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
806 struct GNUNET_DHT_Handle *handle = cls;
807 const struct GNUNET_DHT_ClientResultMessage *dht_msg;
813 LOG (GNUNET_ERROR_TYPE_DEBUG,
814 "Error receiving data from DHT service, reconnecting\n");
815 do_disconnect (handle);
819 msize = ntohs (msg->size);
820 switch (ntohs (msg->type))
822 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET:
823 if (msize < sizeof (struct GNUNET_DHT_MonitorGetMessage))
828 ret = process_monitor_get_message(handle,
829 (const struct GNUNET_DHT_MonitorGetMessage *) msg);
831 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP:
832 if (msize < sizeof (struct GNUNET_DHT_MonitorGetRespMessage))
837 ret = process_monitor_get_resp_message(handle,
838 (const struct GNUNET_DHT_MonitorGetRespMessage *) msg);
840 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT:
841 if (msize < sizeof (struct GNUNET_DHT_MonitorPutMessage))
846 ret = process_monitor_put_message(handle,
847 (const struct GNUNET_DHT_MonitorPutMessage *) msg);
849 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT_RESP:
850 /* Not implemented yet */
853 case GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT:
854 if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_ClientResultMessage))
860 dht_msg = (const struct GNUNET_DHT_ClientResultMessage *) msg;
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply for `%s' from DHT service %p\n",
862 GNUNET_h2s (&dht_msg->key), handle);
863 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
864 &dht_msg->key, &process_reply,
867 case GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK:
868 if (ntohs (msg->size) != sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage))
873 ret = process_put_confirmation_message (handle,
874 (const struct GNUNET_DHT_ClientPutConfirmationMessage*) msg);
878 LOG (GNUNET_ERROR_TYPE_WARNING,
879 "Unknown DHT message type: %hu (%hu) size: %hu\n",
880 ntohs (msg->type), msg->type, msize);
883 if (GNUNET_OK != ret)
886 do_disconnect (handle);
889 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
890 GNUNET_TIME_UNIT_FOREVER_REL);
895 * Initialize the connection with the DHT service.
897 * @param cfg configuration to use
898 * @param ht_len size of the internal hash table to use for
899 * processing multiple GET/FIND requests in parallel
901 * @return handle to the DHT service, or NULL on error
903 struct GNUNET_DHT_Handle *
904 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
907 struct GNUNET_DHT_Handle *handle;
909 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
912 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
913 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len, GNUNET_NO);
914 if (GNUNET_NO == try_connect (handle))
916 GNUNET_DHT_disconnect (handle);
924 * Shutdown connection with the DHT service.
926 * @param handle handle of the DHT connection to stop
929 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
931 struct PendingMessage *pm;
932 struct GNUNET_DHT_PutHandle *ph;
934 GNUNET_assert (NULL != handle);
936 GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
937 if (NULL != handle->th)
939 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
942 while (NULL != (pm = handle->pending_head))
944 GNUNET_assert (GNUNET_YES == pm->in_pending_queue);
945 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
947 pm->in_pending_queue = GNUNET_NO;
948 GNUNET_assert (GNUNET_YES == pm->free_on_send);
949 if (NULL != pm->cont)
950 pm->cont (pm->cont_cls, NULL);
953 while (NULL != (ph = handle->put_head))
955 GNUNET_break (NULL == ph->pending);
956 if (NULL != ph->cont)
957 ph->cont (ph->cont_cls, GNUNET_SYSERR);
958 GNUNET_DHT_put_cancel (ph);
961 if (NULL != handle->client)
963 GNUNET_CLIENT_disconnect (handle->client);
964 handle->client = NULL;
966 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
967 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
968 GNUNET_CONTAINER_multihashmap_destroy (handle->active_requests);
969 GNUNET_free (handle);
974 * Timeout for the transmission of a fire&forget-request. Clean it up.
976 * @param cls the 'struct PendingMessage'
977 * @param tc scheduler context
980 timeout_put_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
982 struct GNUNET_DHT_PutHandle *ph = cls;
983 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
985 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
986 if (NULL != ph->pending)
988 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
990 ph->pending->in_pending_queue = GNUNET_NO;
991 GNUNET_free (ph->pending);
993 if (NULL != ph->cont)
994 ph->cont (ph->cont_cls, GNUNET_NO);
995 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1003 * Function called whenever the PUT message leaves the queue. Sets
1004 * the message pointer in the put handle to NULL.
1006 * @param cls the 'struct GNUNET_DHT_PutHandle'
1010 mark_put_message_gone (void *cls,
1011 const struct GNUNET_SCHEDULER_TaskContext *tc)
1013 struct GNUNET_DHT_PutHandle *ph = cls;
1020 * Perform a PUT operation storing data in the DHT. FIXME: we should
1021 * change the protocol to get a confirmation for the PUT from the DHT
1022 * and call 'cont' only after getting the confirmation; otherwise, the
1023 * client has no good way of telling if the 'PUT' message actually got
1024 * to the DHT service!
1026 * @param handle handle to DHT service
1027 * @param key the key to store under
1028 * @param desired_replication_level estimate of how many
1029 * nearest peers this request should reach
1030 * @param options routing options for this message
1031 * @param type type of the value
1032 * @param size number of bytes in data; must be less than 64k
1033 * @param data the data to store
1034 * @param exp desired expiration time for the value
1035 * @param timeout how long to wait for transmission of this request
1036 * @param cont continuation to call when done (transmitting request to service)
1037 * You must not call GNUNET_DHT_DISCONNECT in this continuation
1038 * @param cont_cls closure for cont
1040 struct GNUNET_DHT_PutHandle *
1041 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const struct GNUNET_HashCode * key,
1042 uint32_t desired_replication_level,
1043 enum GNUNET_DHT_RouteOption options,
1044 enum GNUNET_BLOCK_Type type, size_t size, const void *data,
1045 struct GNUNET_TIME_Absolute exp,
1046 struct GNUNET_TIME_Relative timeout, GNUNET_DHT_PutContinuation cont,
1049 struct GNUNET_DHT_ClientPutMessage *put_msg;
1051 struct PendingMessage *pending;
1052 struct GNUNET_DHT_PutHandle *ph;
1054 msize = sizeof (struct GNUNET_DHT_ClientPutMessage) + size;
1055 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1056 (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1061 ph = GNUNET_malloc (sizeof (struct GNUNET_DHT_PutHandle));
1062 ph->dht_handle = handle;
1063 ph->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_put_request, ph);
1065 ph->cont_cls = cont_cls;
1066 ph->unique_id = ++handle->uid_gen;
1067 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1068 ph->pending = pending;
1069 put_msg = (struct GNUNET_DHT_ClientPutMessage *) &pending[1];
1070 pending->msg = &put_msg->header;
1071 pending->handle = handle;
1072 pending->cont = &mark_put_message_gone;
1073 pending->cont_cls = ph;
1074 pending->free_on_send = GNUNET_YES;
1075 put_msg->header.size = htons (msize);
1076 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT);
1077 put_msg->type = htonl (type);
1078 put_msg->options = htonl ((uint32_t) options);
1079 put_msg->desired_replication_level = htonl (desired_replication_level);
1080 put_msg->unique_id = ph->unique_id;
1081 put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
1082 put_msg->key = *key;
1083 memcpy (&put_msg[1], data, size);
1084 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1086 pending->in_pending_queue = GNUNET_YES;
1087 GNUNET_CONTAINER_DLL_insert_tail (handle->put_head,
1090 process_pending_messages (handle);
1096 * Cancels a DHT PUT operation. Note that the PUT request may still
1097 * go out over the network (we can't stop that); However, if the PUT
1098 * has not yet been sent to the service, cancelling the PUT will stop
1099 * this from happening (but there is no way for the user of this API
1100 * to tell if that is the case). The only use for this API is to
1101 * prevent a later call to 'cont' from "GNUNET_DHT_put" (i.e. because
1102 * the system is shutting down).
1104 * @param ph put operation to cancel ('cont' will no longer be called)
1107 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph)
1109 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
1111 if (NULL != ph->pending)
1113 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
1114 handle->pending_tail,
1116 GNUNET_free (ph->pending);
1119 if (ph->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1121 GNUNET_SCHEDULER_cancel (ph->timeout_task);
1122 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1124 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1132 * Perform an asynchronous GET operation on the DHT identified. See
1133 * also "GNUNET_BLOCK_evaluate".
1135 * @param handle handle to the DHT service
1136 * @param type expected type of the response object
1137 * @param key the key to look up
1138 * @param desired_replication_level estimate of how many
1139 nearest peers this request should reach
1140 * @param options routing options for this message
1141 * @param xquery extended query data (can be NULL, depending on type)
1142 * @param xquery_size number of bytes in xquery
1143 * @param iter function to call on each result
1144 * @param iter_cls closure for iter
1145 * @return handle to stop the async get
1147 struct GNUNET_DHT_GetHandle *
1148 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
1149 enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode * key,
1150 uint32_t desired_replication_level,
1151 enum GNUNET_DHT_RouteOption options, const void *xquery,
1152 size_t xquery_size, GNUNET_DHT_GetIterator iter,
1155 struct GNUNET_DHT_ClientGetMessage *get_msg;
1156 struct GNUNET_DHT_GetHandle *get_handle;
1158 struct PendingMessage *pending;
1160 msize = sizeof (struct GNUNET_DHT_ClientGetMessage) + xquery_size;
1161 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1162 (xquery_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending query for %s to DHT %p\n",
1168 GNUNET_h2s (key), handle);
1169 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1170 get_msg = (struct GNUNET_DHT_ClientGetMessage *) &pending[1];
1171 pending->msg = &get_msg->header;
1172 pending->handle = handle;
1173 pending->free_on_send = GNUNET_NO;
1174 get_msg->header.size = htons (msize);
1175 get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET);
1176 get_msg->options = htonl ((uint32_t) options);
1177 get_msg->desired_replication_level = htonl (desired_replication_level);
1178 get_msg->type = htonl (type);
1179 get_msg->key = *key;
1180 get_msg->unique_id = ++handle->uid_gen;
1181 memcpy (&get_msg[1], xquery, xquery_size);
1182 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1184 pending->in_pending_queue = GNUNET_YES;
1185 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
1186 get_handle->iter = iter;
1187 get_handle->iter_cls = iter_cls;
1188 get_handle->message = pending;
1189 get_handle->unique_id = get_msg->unique_id;
1190 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, key, get_handle,
1191 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1192 process_pending_messages (handle);
1198 * Stop async DHT-get.
1200 * @param get_handle handle to the GET operation to stop
1203 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
1205 struct GNUNET_DHT_Handle *handle;
1206 const struct GNUNET_DHT_ClientGetMessage *get_msg;
1207 struct GNUNET_DHT_ClientGetStopMessage *stop_msg;
1208 struct PendingMessage *pending;
1210 handle = get_handle->message->handle;
1212 (const struct GNUNET_DHT_ClientGetMessage *) get_handle->message->msg;
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending STOP for %s to DHT via %p\n",
1214 GNUNET_h2s (&get_msg->key), handle);
1217 GNUNET_malloc (sizeof (struct PendingMessage) +
1218 sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1219 stop_msg = (struct GNUNET_DHT_ClientGetStopMessage *) &pending[1];
1220 pending->msg = &stop_msg->header;
1221 pending->handle = handle;
1222 pending->free_on_send = GNUNET_YES;
1223 stop_msg->header.size =
1224 htons (sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1225 stop_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP);
1226 stop_msg->reserved = htonl (0);
1227 stop_msg->unique_id = get_msg->unique_id;
1228 stop_msg->key = get_msg->key;
1229 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1231 pending->in_pending_queue = GNUNET_YES;
1233 /* remove 'GET' from active status */
1234 GNUNET_assert (GNUNET_YES ==
1235 GNUNET_CONTAINER_multihashmap_remove (handle->active_requests,
1238 if (GNUNET_YES == get_handle->message->in_pending_queue)
1240 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1241 get_handle->message);
1242 get_handle->message->in_pending_queue = GNUNET_NO;
1244 GNUNET_free (get_handle->message);
1245 GNUNET_free (get_handle);
1247 process_pending_messages (handle);
1252 * Start monitoring the local DHT service.
1254 * @param handle Handle to the DHT service.
1255 * @param type Type of blocks that are of interest.
1256 * @param key Key of data of interest, NULL for all.
1257 * @param get_cb Callback to process monitored get messages.
1258 * @param get_resp_cb Callback to process monitored get response messages.
1259 * @param put_cb Callback to process monitored put messages.
1260 * @param cb_cls Closure for cb.
1262 * @return Handle to stop monitoring.
1264 struct GNUNET_DHT_MonitorHandle *
1265 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
1266 enum GNUNET_BLOCK_Type type,
1267 const struct GNUNET_HashCode *key,
1268 GNUNET_DHT_MonitorGetCB get_cb,
1269 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
1270 GNUNET_DHT_MonitorPutCB put_cb,
1273 struct GNUNET_DHT_MonitorHandle *h;
1274 struct GNUNET_DHT_MonitorStartStopMessage *m;
1275 struct PendingMessage *pending;
1277 h = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorHandle));
1278 GNUNET_CONTAINER_DLL_insert(handle->monitor_head, handle->monitor_tail, h);
1281 h->get_resp_cb = get_resp_cb;
1285 h->dht_handle = handle;
1288 h->key = GNUNET_malloc (sizeof(struct GNUNET_HashCode));
1289 memcpy (h->key, key, sizeof(struct GNUNET_HashCode));
1292 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1293 sizeof (struct PendingMessage));
1294 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1295 pending->msg = &m->header;
1296 pending->handle = handle;
1297 pending->free_on_send = GNUNET_YES;
1298 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_START);
1299 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1300 m->type = htonl(type);
1301 m->get = htons(NULL != get_cb);
1302 m->get_resp = htons(NULL != get_resp_cb);
1303 m->put = htons(NULL != put_cb);
1305 m->filter_key = htons(1);
1306 memcpy (&m->key, key, sizeof(struct GNUNET_HashCode));
1308 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1310 pending->in_pending_queue = GNUNET_YES;
1311 process_pending_messages (handle);
1320 * @param handle The handle to the monitor request returned by monitor_start.
1322 * On return get_handle will no longer be valid, caller must not use again!!!
1325 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle)
1327 struct GNUNET_DHT_MonitorStartStopMessage *m;
1328 struct PendingMessage *pending;
1330 GNUNET_CONTAINER_DLL_remove (handle->dht_handle->monitor_head,
1331 handle->dht_handle->monitor_tail,
1334 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1335 sizeof (struct PendingMessage));
1336 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1337 pending->msg = &m->header;
1338 pending->handle = handle->dht_handle;
1339 pending->free_on_send = GNUNET_YES;
1340 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP);
1341 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1342 m->type = htonl(handle->type);
1343 m->get = htons(NULL != handle->get_cb);
1344 m->get_resp = htons(NULL != handle->get_resp_cb);
1345 m->put = htons(NULL != handle->put_cb);
1346 if (NULL != handle->key) {
1347 m->filter_key = htons(1);
1348 memcpy (&m->key, handle->key, sizeof(struct GNUNET_HashCode));
1350 GNUNET_CONTAINER_DLL_insert (handle->dht_handle->pending_head,
1351 handle->dht_handle->pending_tail,
1353 pending->in_pending_queue = GNUNET_YES;
1354 process_pending_messages (handle->dht_handle);
1356 GNUNET_free_non_null (handle->key);
1357 GNUNET_free (handle);
1362 /* end of dht_api.c */