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 * Array of hash codes over the results that we have already
179 struct GNUNET_HashCode *seen_results;
182 * Key that this get request is for
184 struct GNUNET_HashCode key;
187 * Unique identifier for this request (for key collisions).
192 * Size of the 'seen_results' array. Note that not
193 * all positions might be used (as we over-allocate).
195 unsigned int seen_results_size;
198 * Offset into the 'seen_results' array marking the
199 * end of the positions that are actually used.
201 unsigned int seen_results_end;
204 * Offset into the 'seen_results' array marking the
205 * position up to where we've send the hash codes to
206 * the DHT for blocking (needed as we might not be
207 * able to send all hash codes at once).
209 unsigned int seen_results_transmission_offset;
216 * Handle to a monitoring request.
218 struct GNUNET_DHT_MonitorHandle
223 struct GNUNET_DHT_MonitorHandle *next;
228 struct GNUNET_DHT_MonitorHandle *prev;
231 * Main handle to this DHT api.
233 struct GNUNET_DHT_Handle *dht_handle;
236 * Type of block looked for.
238 enum GNUNET_BLOCK_Type type;
241 * Key being looked for, NULL == all.
243 struct GNUNET_HashCode *key;
246 * Callback for each received message of type get.
248 GNUNET_DHT_MonitorGetCB get_cb;
251 * Callback for each received message of type get response.
253 GNUNET_DHT_MonitorGetRespCB get_resp_cb;
256 * Callback for each received message of type put.
258 GNUNET_DHT_MonitorPutCB put_cb;
269 * Connection to the DHT service.
271 struct GNUNET_DHT_Handle
275 * Configuration to use.
277 const struct GNUNET_CONFIGURATION_Handle *cfg;
280 * Socket (if available).
282 struct GNUNET_CLIENT_Connection *client;
285 * Currently pending transmission request (or NULL).
287 struct GNUNET_CLIENT_TransmitHandle *th;
290 * Head of linked list of messages we would like to transmit.
292 struct PendingMessage *pending_head;
295 * Tail of linked list of messages we would like to transmit.
297 struct PendingMessage *pending_tail;
300 * Head of linked list of messages we would like to monitor.
302 struct GNUNET_DHT_MonitorHandle *monitor_head;
305 * Tail of linked list of messages we would like to monitor.
307 struct GNUNET_DHT_MonitorHandle *monitor_tail;
310 * Head of active PUT requests.
312 struct GNUNET_DHT_PutHandle *put_head;
315 * Tail of active PUT requests.
317 struct GNUNET_DHT_PutHandle *put_tail;
320 * Hash map containing the current outstanding unique GET requests
321 * (values are of type 'struct GNUNET_DHT_GetHandle').
323 struct GNUNET_CONTAINER_MultiHashMap *active_requests;
326 * Task for trying to reconnect.
328 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
331 * How quickly should we retry? Used for exponential back-off on
334 struct GNUNET_TIME_Relative retry_time;
337 * Generator for unique ids.
342 * Did we start our receive loop yet?
349 * Handler for messages received from the DHT service
350 * a demultiplexer which handles numerous message types
352 * @param cls the 'struct GNUNET_DHT_Handle'
353 * @param msg the incoming message
356 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg);
360 * Try to (re)connect to the DHT service.
362 * @param handle DHT handle to reconnect
363 * @return GNUNET_YES on success, GNUNET_NO on failure.
366 try_connect (struct GNUNET_DHT_Handle *handle)
368 if (NULL != handle->client)
370 handle->in_receive = GNUNET_NO;
371 handle->client = GNUNET_CLIENT_connect ("dht", handle->cfg);
372 if (NULL == handle->client)
374 LOG (GNUNET_ERROR_TYPE_WARNING,
375 _("Failed to connect to the DHT service!\n"));
383 * Queue messages to DHT to block certain results from the result set.
385 * @param get_handle GET to generate messages for.
388 queue_filter_messages (struct GNUNET_DHT_GetHandle *get_handle)
390 struct PendingMessage *pm;
391 struct GNUNET_DHT_ClientGetResultSeenMessage *msg;
396 while (get_handle->seen_results_transmission_offset < get_handle->seen_results_end)
398 delta = get_handle->seen_results_end - get_handle->seen_results_transmission_offset;
399 max = (GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
402 msize = sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage) + delta * sizeof (struct GNUNET_HashCode);
404 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
405 msg = (struct GNUNET_DHT_ClientGetResultSeenMessage *) &pm[1];
406 pm->msg = &msg->header;
407 pm->handle = get_handle->dht_handle;
408 pm->unique_id = get_handle->unique_id;
409 pm->free_on_send = GNUNET_YES;
410 pm->in_pending_queue = GNUNET_YES;
411 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN);
412 msg->header.size = htons (msize);
413 msg->key = get_handle->key;
414 msg->unique_id = get_handle->unique_id;
416 &get_handle->seen_results[get_handle->seen_results_transmission_offset],
417 sizeof (struct GNUNET_HashCode) * delta);
418 get_handle->seen_results_transmission_offset += delta;
419 GNUNET_CONTAINER_DLL_insert_tail (get_handle->dht_handle->pending_head,
420 get_handle->dht_handle->pending_tail,
427 * Add the request corresponding to the given route handle
428 * to the pending queue (if it is not already in there).
430 * @param cls the 'struct GNUNET_DHT_Handle*'
431 * @param key key for the request (not used)
432 * @param value the 'struct GNUNET_DHT_GetHandle*'
433 * @return GNUNET_YES (always)
436 add_request_to_pending (void *cls, const struct GNUNET_HashCode * key, void *value)
438 struct GNUNET_DHT_Handle *handle = cls;
439 struct GNUNET_DHT_GetHandle *get_handle = value;
441 if (GNUNET_NO == get_handle->message->in_pending_queue)
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "Retransmitting request related to %s to DHT %p\n", GNUNET_h2s (key),
446 get_handle->seen_results_transmission_offset = 0;
447 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
448 get_handle->message);
449 queue_filter_messages (get_handle);
450 get_handle->message->in_pending_queue = GNUNET_YES;
457 * Try to send messages from list of messages to send
459 * @param handle DHT_Handle
462 process_pending_messages (struct GNUNET_DHT_Handle *handle);
466 * Try reconnecting to the dht service.
468 * @param cls GNUNET_DHT_Handle
469 * @param tc scheduler context
472 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
474 struct GNUNET_DHT_Handle *handle = cls;
476 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting with DHT %p\n", handle);
477 handle->retry_time = GNUNET_TIME_STD_BACKOFF (handle->retry_time);
478 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
479 if (GNUNET_YES != try_connect (handle))
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "dht reconnect failed(!)\n");
484 GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
485 &add_request_to_pending, handle);
486 process_pending_messages (handle);
491 * Try reconnecting to the DHT service.
493 * @param handle handle to dht to (possibly) disconnect and reconnect
496 do_disconnect (struct GNUNET_DHT_Handle *handle)
498 struct GNUNET_DHT_PutHandle *ph;
499 struct GNUNET_DHT_PutHandle *next;
501 if (NULL == handle->client)
503 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == handle->reconnect_task);
504 if (NULL != handle->th)
505 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Disconnecting from DHT service, will try to reconnect in %s\n",
509 GNUNET_STRINGS_relative_time_to_string (handle->retry_time,
511 GNUNET_CLIENT_disconnect (handle->client);
512 handle->client = NULL;
514 /* signal disconnect to all PUT requests that were transmitted but waiting
515 for the put confirmation */
516 next = handle->put_head;
517 while (NULL != (ph = next))
520 if (NULL == ph->pending)
522 if (NULL != ph->cont)
523 ph->cont (ph->cont_cls, GNUNET_SYSERR);
524 GNUNET_DHT_put_cancel (ph);
527 handle->reconnect_task =
528 GNUNET_SCHEDULER_add_delayed (handle->retry_time, &try_reconnect, handle);
533 * Transmit the next pending message, called by notify_transmit_ready
535 * @param cls the DHT handle
536 * @param size number of bytes available in 'buf' for transmission
537 * @param buf where to copy messages for the service
538 * @return number of bytes written to 'buf'
541 transmit_pending (void *cls, size_t size, void *buf);
545 * Try to send messages from list of messages to send
547 * @param handle handle to DHT
550 process_pending_messages (struct GNUNET_DHT_Handle *handle)
552 struct PendingMessage *head;
554 if (NULL == handle->client)
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "process_pending_messages called, but client is NULL, reconnecting\n");
558 do_disconnect (handle);
561 if (NULL != handle->th)
563 if (NULL == (head = handle->pending_head))
566 GNUNET_CLIENT_notify_transmit_ready (handle->client,
567 ntohs (head->msg->size),
568 GNUNET_TIME_UNIT_FOREVER_REL,
569 GNUNET_YES, &transmit_pending,
571 if (NULL != handle->th)
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "notify_transmit_ready returned NULL, reconnecting\n");
575 do_disconnect (handle);
580 * Transmit the next pending message, called by notify_transmit_ready
582 * @param cls the DHT handle
583 * @param size number of bytes available in 'buf' for transmission
584 * @param buf where to copy messages for the service
585 * @return number of bytes written to 'buf'
588 transmit_pending (void *cls, size_t size, void *buf)
590 struct GNUNET_DHT_Handle *handle = cls;
591 struct PendingMessage *head;
597 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 "Transmission to DHT service failed! Reconnecting!\n");
599 do_disconnect (handle);
602 if (NULL == (head = handle->pending_head))
605 tsize = ntohs (head->msg->size);
608 process_pending_messages (handle);
611 memcpy (buf, head->msg, tsize);
612 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
614 head->in_pending_queue = GNUNET_NO;
615 if (NULL != head->cont)
617 head->cont (head->cont_cls, NULL);
619 head->cont_cls = NULL;
621 if (GNUNET_YES == head->free_on_send)
623 process_pending_messages (handle);
624 LOG (GNUNET_ERROR_TYPE_DEBUG,
625 "Forwarded request of %u bytes to DHT service\n", (unsigned int) tsize);
626 if (GNUNET_NO == handle->in_receive)
628 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting to process replies from DHT\n");
629 handle->in_receive = GNUNET_YES;
630 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
631 GNUNET_TIME_UNIT_FOREVER_REL);
638 * Process a given reply that might match the given
641 * @param cls the 'struct GNUNET_DHT_ClientResultMessage'
642 * @param key query of the request
643 * @param value the 'struct GNUNET_DHT_RouteHandle' of a request matching the same key
644 * @return GNUNET_YES to continue to iterate over all results,
645 * GNUNET_NO if the reply is malformed
648 process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
650 const struct GNUNET_DHT_ClientResultMessage *dht_msg = cls;
651 struct GNUNET_DHT_GetHandle *get_handle = value;
652 const struct GNUNET_PeerIdentity *put_path;
653 const struct GNUNET_PeerIdentity *get_path;
654 struct GNUNET_HashCode hc;
655 uint32_t put_path_length;
656 uint32_t get_path_length;
662 if (dht_msg->unique_id != get_handle->unique_id)
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "Ignoring reply for %s: UID mismatch: %llu/%llu\n", GNUNET_h2s (key),
667 dht_msg->unique_id, get_handle->unique_id);
670 msize = ntohs (dht_msg->header.size);
671 put_path_length = ntohl (dht_msg->put_path_length);
672 get_path_length = ntohl (dht_msg->get_path_length);
674 sizeof (struct GNUNET_DHT_ClientResultMessage) +
675 sizeof (struct GNUNET_PeerIdentity) * (get_path_length + put_path_length);
676 if ((msize < meta_length) ||
678 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
680 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
685 data_length = msize - meta_length;
686 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving %u byte reply for %s to application\n",
687 (unsigned int) data_length, GNUNET_h2s (key));
688 put_path = (const struct GNUNET_PeerIdentity *) &dht_msg[1];
689 get_path = &put_path[put_path_length];
690 data = &get_path[get_path_length];
691 /* remember that we've seen this result */
692 GNUNET_CRYPTO_hash (data, data_length, &hc);
693 if (get_handle->seen_results_size == get_handle->seen_results_end)
694 GNUNET_array_grow (get_handle->seen_results,
695 get_handle->seen_results_size,
696 get_handle->seen_results_size * 2 + 1);
697 GNUNET_assert (get_handle->seen_results_end == get_handle->seen_results_transmission_offset);
698 get_handle->seen_results[get_handle->seen_results_end++] = hc;
699 /* no need to block it explicitly, service already knows about it! */
700 get_handle->seen_results_transmission_offset++;
702 get_handle->iter (get_handle->iter_cls,
703 GNUNET_TIME_absolute_ntoh (dht_msg->expiration), key,
704 get_path, get_path_length, put_path, put_path_length,
705 ntohl (dht_msg->type), data_length, data);
710 * Process a get monitor message from the service.
712 * @param handle The DHT handle.
713 * @param msg Monitor get message from the service.
715 * @return GNUNET_OK if everything went fine,
716 * GNUNET_SYSERR if the message is malformed.
719 process_monitor_get_message (struct GNUNET_DHT_Handle *handle,
720 const struct GNUNET_DHT_MonitorGetMessage *msg)
722 struct GNUNET_DHT_MonitorHandle *h;
724 for (h = handle->monitor_head; NULL != h; h = h->next)
729 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
730 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
731 sizeof (struct GNUNET_HashCode)));
732 if (type_ok && key_ok && (NULL != h->get_cb))
733 h->get_cb (h->cb_cls,
734 ntohl (msg->options),
735 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
736 ntohl (msg->hop_count),
737 ntohl (msg->desired_replication_level),
738 ntohl (msg->get_path_length),
739 (struct GNUNET_PeerIdentity *) &msg[1],
747 * Process a get response monitor message from the service.
749 * @param handle The DHT handle.
750 * @param msg monitor get response message from the service
751 * @return GNUNET_OK if everything went fine,
752 * GNUNET_SYSERR if the message is malformed.
755 process_monitor_get_resp_message (struct GNUNET_DHT_Handle *handle,
756 const struct GNUNET_DHT_MonitorGetRespMessage
759 struct GNUNET_DHT_MonitorHandle *h;
760 struct GNUNET_PeerIdentity *path;
765 msize = ntohs (msg->header.size);
766 path = (struct GNUNET_PeerIdentity *) &msg[1];
767 getl = ntohl (msg->get_path_length);
768 putl = ntohl (msg->put_path_length);
769 if ( (getl + putl < getl) ||
770 ( ((msize - sizeof (struct GNUNET_DHT_MonitorGetRespMessage)) / sizeof (struct GNUNET_PeerIdentity)) < getl + putl) )
773 return GNUNET_SYSERR;
775 for (h = handle->monitor_head; NULL != h; h = h->next)
780 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
781 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
782 sizeof (struct GNUNET_HashCode)));
783 if (type_ok && key_ok && (NULL != h->get_resp_cb))
784 h->get_resp_cb (h->cb_cls,
785 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
788 GNUNET_TIME_absolute_ntoh(msg->expiration_time),
790 (void *) &path[getl + putl],
792 sizeof (struct GNUNET_DHT_MonitorGetRespMessage) -
793 sizeof (struct GNUNET_PeerIdentity) * (putl + getl));
800 * Process a put monitor message from the service.
802 * @param handle The DHT handle.
803 * @param msg Monitor put message from the service.
805 * @return GNUNET_OK if everything went fine,
806 * GNUNET_SYSERR if the message is malformed.
809 process_monitor_put_message (struct GNUNET_DHT_Handle *handle,
810 const struct GNUNET_DHT_MonitorPutMessage *msg)
812 struct GNUNET_DHT_MonitorHandle *h;
814 struct GNUNET_PeerIdentity *path;
817 msize = ntohs (msg->header.size);
818 path = (struct GNUNET_PeerIdentity *) &msg[1];
819 putl = ntohl (msg->put_path_length);
820 if (((msize - sizeof (struct GNUNET_DHT_MonitorGetRespMessage)) / sizeof (struct GNUNET_PeerIdentity)) < putl)
823 return GNUNET_SYSERR;
825 for (h = handle->monitor_head; NULL != h; h = h->next)
830 type_ok = (GNUNET_BLOCK_TYPE_ANY == h->type) || (h->type == ntohl(msg->type));
831 key_ok = (NULL == h->key) || (0 == memcmp (h->key, &msg->key,
832 sizeof (struct GNUNET_HashCode)));
833 if (type_ok && key_ok && (NULL != h->put_cb))
834 h->put_cb (h->cb_cls,
835 ntohl (msg->options),
836 (enum GNUNET_BLOCK_Type) ntohl(msg->type),
837 ntohl (msg->hop_count),
838 ntohl (msg->desired_replication_level),
840 GNUNET_TIME_absolute_ntoh(msg->expiration_time),
842 (void *) &path[putl],
844 sizeof (struct GNUNET_DHT_MonitorPutMessage) -
845 sizeof (struct GNUNET_PeerIdentity) * putl);
852 * Process a put confirmation message from the service.
854 * @param handle The DHT handle.
855 * @param msg confirmation message from the service.
856 * @return GNUNET_OK if everything went fine,
857 * GNUNET_SYSERR if the message is malformed.
860 process_put_confirmation_message (struct GNUNET_DHT_Handle *handle,
861 const struct GNUNET_DHT_ClientPutConfirmationMessage *msg)
863 struct GNUNET_DHT_PutHandle *ph;
864 GNUNET_DHT_PutContinuation cont;
867 for (ph = handle->put_head; NULL != ph; ph = ph->next)
868 if (ph->unique_id == msg->unique_id)
873 cont_cls = ph->cont_cls;
874 GNUNET_DHT_put_cancel (ph);
876 cont (cont_cls, GNUNET_OK);
882 * Handler for messages received from the DHT service
883 * a demultiplexer which handles numerous message types
885 * @param cls the 'struct GNUNET_DHT_Handle'
886 * @param msg the incoming message
889 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
891 struct GNUNET_DHT_Handle *handle = cls;
892 const struct GNUNET_DHT_ClientResultMessage *dht_msg;
898 LOG (GNUNET_ERROR_TYPE_DEBUG,
899 "Error receiving data from DHT service, reconnecting\n");
900 do_disconnect (handle);
904 msize = ntohs (msg->size);
905 switch (ntohs (msg->type))
907 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET:
908 if (msize < sizeof (struct GNUNET_DHT_MonitorGetMessage))
913 ret = process_monitor_get_message(handle,
914 (const struct GNUNET_DHT_MonitorGetMessage *) msg);
916 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP:
917 if (msize < sizeof (struct GNUNET_DHT_MonitorGetRespMessage))
922 ret = process_monitor_get_resp_message(handle,
923 (const struct GNUNET_DHT_MonitorGetRespMessage *) msg);
925 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT:
926 if (msize < sizeof (struct GNUNET_DHT_MonitorPutMessage))
931 ret = process_monitor_put_message(handle,
932 (const struct GNUNET_DHT_MonitorPutMessage *) msg);
934 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT_RESP:
935 /* Not implemented yet */
938 case GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT:
939 if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_ClientResultMessage))
945 dht_msg = (const struct GNUNET_DHT_ClientResultMessage *) msg;
946 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply for `%s' from DHT service %p\n",
947 GNUNET_h2s (&dht_msg->key), handle);
948 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
949 &dht_msg->key, &process_reply,
952 case GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK:
953 if (ntohs (msg->size) != sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage))
958 ret = process_put_confirmation_message (handle,
959 (const struct GNUNET_DHT_ClientPutConfirmationMessage*) msg);
963 LOG (GNUNET_ERROR_TYPE_WARNING,
964 "Unknown DHT message type: %hu (%hu) size: %hu\n",
965 ntohs (msg->type), msg->type, msize);
968 if (GNUNET_OK != ret)
971 do_disconnect (handle);
974 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
975 GNUNET_TIME_UNIT_FOREVER_REL);
980 * Initialize the connection with the DHT service.
982 * @param cfg configuration to use
983 * @param ht_len size of the internal hash table to use for
984 * processing multiple GET/FIND requests in parallel
986 * @return handle to the DHT service, or NULL on error
988 struct GNUNET_DHT_Handle *
989 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
992 struct GNUNET_DHT_Handle *handle;
994 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
997 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
998 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len, GNUNET_NO);
999 if (GNUNET_NO == try_connect (handle))
1001 GNUNET_DHT_disconnect (handle);
1009 * Shutdown connection with the DHT service.
1011 * @param handle handle of the DHT connection to stop
1014 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
1016 struct PendingMessage *pm;
1017 struct GNUNET_DHT_PutHandle *ph;
1019 GNUNET_assert (NULL != handle);
1021 GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
1022 if (NULL != handle->th)
1024 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1027 while (NULL != (pm = handle->pending_head))
1029 GNUNET_assert (GNUNET_YES == pm->in_pending_queue);
1030 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1032 pm->in_pending_queue = GNUNET_NO;
1033 GNUNET_assert (GNUNET_YES == pm->free_on_send);
1034 if (NULL != pm->cont)
1035 pm->cont (pm->cont_cls, NULL);
1038 while (NULL != (ph = handle->put_head))
1040 GNUNET_break (NULL == ph->pending);
1041 if (NULL != ph->cont)
1042 ph->cont (ph->cont_cls, GNUNET_SYSERR);
1043 GNUNET_DHT_put_cancel (ph);
1046 if (NULL != handle->client)
1048 GNUNET_CLIENT_disconnect (handle->client);
1049 handle->client = NULL;
1051 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1052 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1053 GNUNET_CONTAINER_multihashmap_destroy (handle->active_requests);
1054 GNUNET_free (handle);
1059 * Timeout for the transmission of a fire&forget-request. Clean it up.
1061 * @param cls the 'struct PendingMessage'
1062 * @param tc scheduler context
1065 timeout_put_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1067 struct GNUNET_DHT_PutHandle *ph = cls;
1068 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
1070 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1071 if (NULL != ph->pending)
1073 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1075 ph->pending->in_pending_queue = GNUNET_NO;
1076 GNUNET_free (ph->pending);
1078 if (NULL != ph->cont)
1079 ph->cont (ph->cont_cls, GNUNET_NO);
1080 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1088 * Function called whenever the PUT message leaves the queue. Sets
1089 * the message pointer in the put handle to NULL.
1091 * @param cls the 'struct GNUNET_DHT_PutHandle'
1095 mark_put_message_gone (void *cls,
1096 const struct GNUNET_SCHEDULER_TaskContext *tc)
1098 struct GNUNET_DHT_PutHandle *ph = cls;
1105 * Perform a PUT operation storing data in the DHT. FIXME: we should
1106 * change the protocol to get a confirmation for the PUT from the DHT
1107 * and call 'cont' only after getting the confirmation; otherwise, the
1108 * client has no good way of telling if the 'PUT' message actually got
1109 * to the DHT service!
1111 * @param handle handle to DHT service
1112 * @param key the key to store under
1113 * @param desired_replication_level estimate of how many
1114 * nearest peers this request should reach
1115 * @param options routing options for this message
1116 * @param type type of the value
1117 * @param size number of bytes in data; must be less than 64k
1118 * @param data the data to store
1119 * @param exp desired expiration time for the value
1120 * @param timeout how long to wait for transmission of this request
1121 * @param cont continuation to call when done (transmitting request to service)
1122 * You must not call GNUNET_DHT_DISCONNECT in this continuation
1123 * @param cont_cls closure for cont
1125 struct GNUNET_DHT_PutHandle *
1126 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const struct GNUNET_HashCode * key,
1127 uint32_t desired_replication_level,
1128 enum GNUNET_DHT_RouteOption options,
1129 enum GNUNET_BLOCK_Type type, size_t size, const void *data,
1130 struct GNUNET_TIME_Absolute exp,
1131 struct GNUNET_TIME_Relative timeout, GNUNET_DHT_PutContinuation cont,
1134 struct GNUNET_DHT_ClientPutMessage *put_msg;
1136 struct PendingMessage *pending;
1137 struct GNUNET_DHT_PutHandle *ph;
1139 msize = sizeof (struct GNUNET_DHT_ClientPutMessage) + size;
1140 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1141 (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1146 ph = GNUNET_malloc (sizeof (struct GNUNET_DHT_PutHandle));
1147 ph->dht_handle = handle;
1148 ph->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_put_request, ph);
1150 ph->cont_cls = cont_cls;
1151 ph->unique_id = ++handle->uid_gen;
1152 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1153 ph->pending = pending;
1154 put_msg = (struct GNUNET_DHT_ClientPutMessage *) &pending[1];
1155 pending->msg = &put_msg->header;
1156 pending->handle = handle;
1157 pending->cont = &mark_put_message_gone;
1158 pending->cont_cls = ph;
1159 pending->free_on_send = GNUNET_YES;
1160 put_msg->header.size = htons (msize);
1161 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT);
1162 put_msg->type = htonl (type);
1163 put_msg->options = htonl ((uint32_t) options);
1164 put_msg->desired_replication_level = htonl (desired_replication_level);
1165 put_msg->unique_id = ph->unique_id;
1166 put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
1167 put_msg->key = *key;
1168 memcpy (&put_msg[1], data, size);
1169 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1171 pending->in_pending_queue = GNUNET_YES;
1172 GNUNET_CONTAINER_DLL_insert_tail (handle->put_head,
1175 process_pending_messages (handle);
1181 * Cancels a DHT PUT operation. Note that the PUT request may still
1182 * go out over the network (we can't stop that); However, if the PUT
1183 * has not yet been sent to the service, cancelling the PUT will stop
1184 * this from happening (but there is no way for the user of this API
1185 * to tell if that is the case). The only use for this API is to
1186 * prevent a later call to 'cont' from "GNUNET_DHT_put" (i.e. because
1187 * the system is shutting down).
1189 * @param ph put operation to cancel ('cont' will no longer be called)
1192 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph)
1194 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
1196 if (NULL != ph->pending)
1198 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
1199 handle->pending_tail,
1201 GNUNET_free (ph->pending);
1204 if (ph->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1206 GNUNET_SCHEDULER_cancel (ph->timeout_task);
1207 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1209 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1217 * Perform an asynchronous GET operation on the DHT identified. See
1218 * also "GNUNET_BLOCK_evaluate".
1220 * @param handle handle to the DHT service
1221 * @param type expected type of the response object
1222 * @param key the key to look up
1223 * @param desired_replication_level estimate of how many
1224 nearest peers this request should reach
1225 * @param options routing options for this message
1226 * @param xquery extended query data (can be NULL, depending on type)
1227 * @param xquery_size number of bytes in xquery
1228 * @param iter function to call on each result
1229 * @param iter_cls closure for iter
1230 * @return handle to stop the async get
1232 struct GNUNET_DHT_GetHandle *
1233 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
1234 enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode * key,
1235 uint32_t desired_replication_level,
1236 enum GNUNET_DHT_RouteOption options, const void *xquery,
1237 size_t xquery_size, GNUNET_DHT_GetIterator iter,
1240 struct GNUNET_DHT_ClientGetMessage *get_msg;
1241 struct GNUNET_DHT_GetHandle *get_handle;
1243 struct PendingMessage *pending;
1245 msize = sizeof (struct GNUNET_DHT_ClientGetMessage) + xquery_size;
1246 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1247 (xquery_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1252 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending query for %s to DHT %p\n",
1253 GNUNET_h2s (key), handle);
1254 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1255 get_msg = (struct GNUNET_DHT_ClientGetMessage *) &pending[1];
1256 pending->msg = &get_msg->header;
1257 pending->handle = handle;
1258 pending->free_on_send = GNUNET_NO;
1259 get_msg->header.size = htons (msize);
1260 get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET);
1261 get_msg->options = htonl ((uint32_t) options);
1262 get_msg->desired_replication_level = htonl (desired_replication_level);
1263 get_msg->type = htonl (type);
1264 get_msg->key = *key;
1265 get_msg->unique_id = ++handle->uid_gen;
1266 memcpy (&get_msg[1], xquery, xquery_size);
1267 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1269 pending->in_pending_queue = GNUNET_YES;
1270 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
1271 get_handle->dht_handle = handle;
1272 get_handle->iter = iter;
1273 get_handle->iter_cls = iter_cls;
1274 get_handle->message = pending;
1275 get_handle->unique_id = get_msg->unique_id;
1276 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, key, get_handle,
1277 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1278 process_pending_messages (handle);
1285 * Tell the DHT not to return any of the following known results
1288 * @param get_handle get operation for which results should be filtered
1289 * @param num_results number of results to be blocked that are
1290 * provided in this call (size of the 'results' array)
1291 * @param results array of hash codes over the 'data' of the results
1295 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
1296 unsigned int num_results,
1297 const struct GNUNET_HashCode *results)
1299 unsigned int needed;
1301 needed = get_handle->seen_results_end + num_results;
1302 if (needed > get_handle->seen_results_size)
1303 GNUNET_array_grow (get_handle->seen_results,
1304 get_handle->seen_results_size,
1306 memcpy (&get_handle->seen_results[get_handle->seen_results_end],
1308 num_results * sizeof (struct GNUNET_HashCode));
1309 get_handle->seen_results_end += num_results;
1310 queue_filter_messages (get_handle);
1311 process_pending_messages (get_handle->dht_handle);
1316 * Stop async DHT-get.
1318 * @param get_handle handle to the GET operation to stop
1321 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
1323 struct GNUNET_DHT_Handle *handle;
1324 const struct GNUNET_DHT_ClientGetMessage *get_msg;
1325 struct GNUNET_DHT_ClientGetStopMessage *stop_msg;
1326 struct PendingMessage *pending;
1328 handle = get_handle->message->handle;
1330 (const struct GNUNET_DHT_ClientGetMessage *) get_handle->message->msg;
1331 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending STOP for %s to DHT via %p\n",
1332 GNUNET_h2s (&get_msg->key), handle);
1335 GNUNET_malloc (sizeof (struct PendingMessage) +
1336 sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1337 stop_msg = (struct GNUNET_DHT_ClientGetStopMessage *) &pending[1];
1338 pending->msg = &stop_msg->header;
1339 pending->handle = handle;
1340 pending->free_on_send = GNUNET_YES;
1341 stop_msg->header.size =
1342 htons (sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1343 stop_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP);
1344 stop_msg->reserved = htonl (0);
1345 stop_msg->unique_id = get_msg->unique_id;
1346 stop_msg->key = get_msg->key;
1347 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1349 pending->in_pending_queue = GNUNET_YES;
1351 /* remove 'GET' from active status */
1352 GNUNET_assert (GNUNET_YES ==
1353 GNUNET_CONTAINER_multihashmap_remove (handle->active_requests,
1356 if (GNUNET_YES == get_handle->message->in_pending_queue)
1358 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1359 get_handle->message);
1360 get_handle->message->in_pending_queue = GNUNET_NO;
1362 GNUNET_free (get_handle->message);
1363 GNUNET_array_grow (get_handle->seen_results,
1364 get_handle->seen_results_end,
1366 GNUNET_free (get_handle);
1367 process_pending_messages (handle);
1372 * Start monitoring the local DHT service.
1374 * @param handle Handle to the DHT service.
1375 * @param type Type of blocks that are of interest.
1376 * @param key Key of data of interest, NULL for all.
1377 * @param get_cb Callback to process monitored get messages.
1378 * @param get_resp_cb Callback to process monitored get response messages.
1379 * @param put_cb Callback to process monitored put messages.
1380 * @param cb_cls Closure for cb.
1382 * @return Handle to stop monitoring.
1384 struct GNUNET_DHT_MonitorHandle *
1385 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
1386 enum GNUNET_BLOCK_Type type,
1387 const struct GNUNET_HashCode *key,
1388 GNUNET_DHT_MonitorGetCB get_cb,
1389 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
1390 GNUNET_DHT_MonitorPutCB put_cb,
1393 struct GNUNET_DHT_MonitorHandle *h;
1394 struct GNUNET_DHT_MonitorStartStopMessage *m;
1395 struct PendingMessage *pending;
1397 h = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorHandle));
1398 GNUNET_CONTAINER_DLL_insert(handle->monitor_head, handle->monitor_tail, h);
1401 h->get_resp_cb = get_resp_cb;
1405 h->dht_handle = handle;
1408 h->key = GNUNET_malloc (sizeof(struct GNUNET_HashCode));
1409 memcpy (h->key, key, sizeof(struct GNUNET_HashCode));
1412 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1413 sizeof (struct PendingMessage));
1414 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1415 pending->msg = &m->header;
1416 pending->handle = handle;
1417 pending->free_on_send = GNUNET_YES;
1418 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_START);
1419 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1420 m->type = htonl(type);
1421 m->get = htons(NULL != get_cb);
1422 m->get_resp = htons(NULL != get_resp_cb);
1423 m->put = htons(NULL != put_cb);
1425 m->filter_key = htons(1);
1426 memcpy (&m->key, key, sizeof(struct GNUNET_HashCode));
1428 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1430 pending->in_pending_queue = GNUNET_YES;
1431 process_pending_messages (handle);
1440 * @param handle The handle to the monitor request returned by monitor_start.
1442 * On return get_handle will no longer be valid, caller must not use again!!!
1445 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle)
1447 struct GNUNET_DHT_MonitorStartStopMessage *m;
1448 struct PendingMessage *pending;
1450 GNUNET_CONTAINER_DLL_remove (handle->dht_handle->monitor_head,
1451 handle->dht_handle->monitor_tail,
1454 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1455 sizeof (struct PendingMessage));
1456 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1457 pending->msg = &m->header;
1458 pending->handle = handle->dht_handle;
1459 pending->free_on_send = GNUNET_YES;
1460 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP);
1461 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1462 m->type = htonl(handle->type);
1463 m->get = htons(NULL != handle->get_cb);
1464 m->get_resp = htons(NULL != handle->get_resp_cb);
1465 m->put = htons(NULL != handle->put_cb);
1466 if (NULL != handle->key) {
1467 m->filter_key = htons(1);
1468 memcpy (&m->key, handle->key, sizeof(struct GNUNET_HashCode));
1470 GNUNET_CONTAINER_DLL_insert (handle->dht_handle->pending_head,
1471 handle->dht_handle->pending_tail,
1473 pending->in_pending_queue = GNUNET_YES;
1474 process_pending_messages (handle->dht_handle);
1476 GNUNET_free_non_null (handle->key);
1477 GNUNET_free (handle);
1482 /* end of dht_api.c */