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 or we found a matching request
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);
903 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
904 GNUNET_TIME_UNIT_FOREVER_REL);
906 msize = ntohs (msg->size);
907 switch (ntohs (msg->type))
909 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET:
910 if (msize < sizeof (struct GNUNET_DHT_MonitorGetMessage))
915 ret = process_monitor_get_message(handle,
916 (const struct GNUNET_DHT_MonitorGetMessage *) msg);
918 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP:
919 if (msize < sizeof (struct GNUNET_DHT_MonitorGetRespMessage))
924 ret = process_monitor_get_resp_message(handle,
925 (const struct GNUNET_DHT_MonitorGetRespMessage *) msg);
927 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT:
928 if (msize < sizeof (struct GNUNET_DHT_MonitorPutMessage))
933 ret = process_monitor_put_message(handle,
934 (const struct GNUNET_DHT_MonitorPutMessage *) msg);
936 case GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT_RESP:
937 /* Not implemented yet */
940 case GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT:
941 if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_ClientResultMessage))
946 dht_msg = (const struct GNUNET_DHT_ClientResultMessage *) msg;
947 LOG (GNUNET_ERROR_TYPE_DEBUG,
948 "Received reply for `%s' from DHT service %p\n",
949 GNUNET_h2s (&dht_msg->key), handle);
950 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
956 case GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK:
957 if (ntohs (msg->size) != sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage))
962 ret = process_put_confirmation_message (handle,
963 (const struct GNUNET_DHT_ClientPutConfirmationMessage*) msg);
967 LOG (GNUNET_ERROR_TYPE_WARNING,
968 "Unknown DHT message type: %hu (%hu) size: %hu\n",
969 ntohs (msg->type), msg->type, msize);
972 if (GNUNET_OK != ret)
975 do_disconnect (handle);
982 * Initialize the connection with the DHT service.
984 * @param cfg configuration to use
985 * @param ht_len size of the internal hash table to use for
986 * processing multiple GET/FIND requests in parallel
988 * @return handle to the DHT service, or NULL on error
990 struct GNUNET_DHT_Handle *
991 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
994 struct GNUNET_DHT_Handle *handle;
996 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
999 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
1000 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len, GNUNET_NO);
1001 if (GNUNET_NO == try_connect (handle))
1003 GNUNET_DHT_disconnect (handle);
1011 * Shutdown connection with the DHT service.
1013 * @param handle handle of the DHT connection to stop
1016 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
1018 struct PendingMessage *pm;
1019 struct GNUNET_DHT_PutHandle *ph;
1021 GNUNET_assert (NULL != handle);
1023 GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
1024 if (NULL != handle->th)
1026 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1029 while (NULL != (pm = handle->pending_head))
1031 GNUNET_assert (GNUNET_YES == pm->in_pending_queue);
1032 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1034 pm->in_pending_queue = GNUNET_NO;
1035 GNUNET_assert (GNUNET_YES == pm->free_on_send);
1036 if (NULL != pm->cont)
1037 pm->cont (pm->cont_cls, NULL);
1040 while (NULL != (ph = handle->put_head))
1042 GNUNET_break (NULL == ph->pending);
1043 if (NULL != ph->cont)
1044 ph->cont (ph->cont_cls, GNUNET_SYSERR);
1045 GNUNET_DHT_put_cancel (ph);
1048 if (NULL != handle->client)
1050 GNUNET_CLIENT_disconnect (handle->client);
1051 handle->client = NULL;
1053 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1054 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1055 GNUNET_CONTAINER_multihashmap_destroy (handle->active_requests);
1056 GNUNET_free (handle);
1061 * Timeout for the transmission of a fire&forget-request. Clean it up.
1063 * @param cls the 'struct PendingMessage'
1064 * @param tc scheduler context
1067 timeout_put_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1069 struct GNUNET_DHT_PutHandle *ph = cls;
1070 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
1072 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1073 if (NULL != ph->pending)
1075 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1077 ph->pending->in_pending_queue = GNUNET_NO;
1078 GNUNET_free (ph->pending);
1080 if (NULL != ph->cont)
1081 ph->cont (ph->cont_cls, GNUNET_NO);
1082 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1090 * Function called whenever the PUT message leaves the queue. Sets
1091 * the message pointer in the put handle to NULL.
1093 * @param cls the 'struct GNUNET_DHT_PutHandle'
1097 mark_put_message_gone (void *cls,
1098 const struct GNUNET_SCHEDULER_TaskContext *tc)
1100 struct GNUNET_DHT_PutHandle *ph = cls;
1107 * Perform a PUT operation storing data in the DHT. FIXME: we should
1108 * change the protocol to get a confirmation for the PUT from the DHT
1109 * and call 'cont' only after getting the confirmation; otherwise, the
1110 * client has no good way of telling if the 'PUT' message actually got
1111 * to the DHT service!
1113 * @param handle handle to DHT service
1114 * @param key the key to store under
1115 * @param desired_replication_level estimate of how many
1116 * nearest peers this request should reach
1117 * @param options routing options for this message
1118 * @param type type of the value
1119 * @param size number of bytes in data; must be less than 64k
1120 * @param data the data to store
1121 * @param exp desired expiration time for the value
1122 * @param timeout how long to wait for transmission of this request
1123 * @param cont continuation to call when done (transmitting request to service)
1124 * You must not call GNUNET_DHT_DISCONNECT in this continuation
1125 * @param cont_cls closure for cont
1127 struct GNUNET_DHT_PutHandle *
1128 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const struct GNUNET_HashCode * key,
1129 uint32_t desired_replication_level,
1130 enum GNUNET_DHT_RouteOption options,
1131 enum GNUNET_BLOCK_Type type, size_t size, const void *data,
1132 struct GNUNET_TIME_Absolute exp,
1133 struct GNUNET_TIME_Relative timeout, GNUNET_DHT_PutContinuation cont,
1136 struct GNUNET_DHT_ClientPutMessage *put_msg;
1138 struct PendingMessage *pending;
1139 struct GNUNET_DHT_PutHandle *ph;
1141 msize = sizeof (struct GNUNET_DHT_ClientPutMessage) + size;
1142 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1143 (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1148 ph = GNUNET_malloc (sizeof (struct GNUNET_DHT_PutHandle));
1149 ph->dht_handle = handle;
1150 ph->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_put_request, ph);
1152 ph->cont_cls = cont_cls;
1153 ph->unique_id = ++handle->uid_gen;
1154 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1155 ph->pending = pending;
1156 put_msg = (struct GNUNET_DHT_ClientPutMessage *) &pending[1];
1157 pending->msg = &put_msg->header;
1158 pending->handle = handle;
1159 pending->cont = &mark_put_message_gone;
1160 pending->cont_cls = ph;
1161 pending->free_on_send = GNUNET_YES;
1162 put_msg->header.size = htons (msize);
1163 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT);
1164 put_msg->type = htonl (type);
1165 put_msg->options = htonl ((uint32_t) options);
1166 put_msg->desired_replication_level = htonl (desired_replication_level);
1167 put_msg->unique_id = ph->unique_id;
1168 put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
1169 put_msg->key = *key;
1170 memcpy (&put_msg[1], data, size);
1171 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1173 pending->in_pending_queue = GNUNET_YES;
1174 GNUNET_CONTAINER_DLL_insert_tail (handle->put_head,
1177 process_pending_messages (handle);
1183 * Cancels a DHT PUT operation. Note that the PUT request may still
1184 * go out over the network (we can't stop that); However, if the PUT
1185 * has not yet been sent to the service, cancelling the PUT will stop
1186 * this from happening (but there is no way for the user of this API
1187 * to tell if that is the case). The only use for this API is to
1188 * prevent a later call to 'cont' from "GNUNET_DHT_put" (i.e. because
1189 * the system is shutting down).
1191 * @param ph put operation to cancel ('cont' will no longer be called)
1194 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph)
1196 struct GNUNET_DHT_Handle *handle = ph->dht_handle;
1198 if (NULL != ph->pending)
1200 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
1201 handle->pending_tail,
1203 GNUNET_free (ph->pending);
1206 if (ph->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1208 GNUNET_SCHEDULER_cancel (ph->timeout_task);
1209 ph->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1211 GNUNET_CONTAINER_DLL_remove (handle->put_head,
1219 * Perform an asynchronous GET operation on the DHT identified. See
1220 * also "GNUNET_BLOCK_evaluate".
1222 * @param handle handle to the DHT service
1223 * @param type expected type of the response object
1224 * @param key the key to look up
1225 * @param desired_replication_level estimate of how many
1226 nearest peers this request should reach
1227 * @param options routing options for this message
1228 * @param xquery extended query data (can be NULL, depending on type)
1229 * @param xquery_size number of bytes in xquery
1230 * @param iter function to call on each result
1231 * @param iter_cls closure for iter
1232 * @return handle to stop the async get
1234 struct GNUNET_DHT_GetHandle *
1235 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
1236 enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode * key,
1237 uint32_t desired_replication_level,
1238 enum GNUNET_DHT_RouteOption options, const void *xquery,
1239 size_t xquery_size, GNUNET_DHT_GetIterator iter,
1242 struct GNUNET_DHT_ClientGetMessage *get_msg;
1243 struct GNUNET_DHT_GetHandle *get_handle;
1245 struct PendingMessage *pending;
1247 msize = sizeof (struct GNUNET_DHT_ClientGetMessage) + xquery_size;
1248 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1249 (xquery_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1254 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending query for %s to DHT %p\n",
1255 GNUNET_h2s (key), handle);
1256 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1257 get_msg = (struct GNUNET_DHT_ClientGetMessage *) &pending[1];
1258 pending->msg = &get_msg->header;
1259 pending->handle = handle;
1260 pending->free_on_send = GNUNET_NO;
1261 get_msg->header.size = htons (msize);
1262 get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET);
1263 get_msg->options = htonl ((uint32_t) options);
1264 get_msg->desired_replication_level = htonl (desired_replication_level);
1265 get_msg->type = htonl (type);
1266 get_msg->key = *key;
1267 get_msg->unique_id = ++handle->uid_gen;
1268 memcpy (&get_msg[1], xquery, xquery_size);
1269 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1271 pending->in_pending_queue = GNUNET_YES;
1272 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
1273 get_handle->dht_handle = handle;
1274 get_handle->iter = iter;
1275 get_handle->iter_cls = iter_cls;
1276 get_handle->message = pending;
1277 get_handle->unique_id = get_msg->unique_id;
1278 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, key, get_handle,
1279 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1280 process_pending_messages (handle);
1287 * Tell the DHT not to return any of the following known results
1290 * @param get_handle get operation for which results should be filtered
1291 * @param num_results number of results to be blocked that are
1292 * provided in this call (size of the 'results' array)
1293 * @param results array of hash codes over the 'data' of the results
1297 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
1298 unsigned int num_results,
1299 const struct GNUNET_HashCode *results)
1301 unsigned int needed;
1303 needed = get_handle->seen_results_end + num_results;
1304 if (needed > get_handle->seen_results_size)
1305 GNUNET_array_grow (get_handle->seen_results,
1306 get_handle->seen_results_size,
1308 memcpy (&get_handle->seen_results[get_handle->seen_results_end],
1310 num_results * sizeof (struct GNUNET_HashCode));
1311 get_handle->seen_results_end += num_results;
1312 queue_filter_messages (get_handle);
1313 process_pending_messages (get_handle->dht_handle);
1318 * Stop async DHT-get.
1320 * @param get_handle handle to the GET operation to stop
1323 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
1325 struct GNUNET_DHT_Handle *handle;
1326 const struct GNUNET_DHT_ClientGetMessage *get_msg;
1327 struct GNUNET_DHT_ClientGetStopMessage *stop_msg;
1328 struct PendingMessage *pending;
1330 handle = get_handle->message->handle;
1332 (const struct GNUNET_DHT_ClientGetMessage *) get_handle->message->msg;
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending STOP for %s to DHT via %p\n",
1334 GNUNET_h2s (&get_msg->key), handle);
1337 GNUNET_malloc (sizeof (struct PendingMessage) +
1338 sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1339 stop_msg = (struct GNUNET_DHT_ClientGetStopMessage *) &pending[1];
1340 pending->msg = &stop_msg->header;
1341 pending->handle = handle;
1342 pending->free_on_send = GNUNET_YES;
1343 stop_msg->header.size =
1344 htons (sizeof (struct GNUNET_DHT_ClientGetStopMessage));
1345 stop_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP);
1346 stop_msg->reserved = htonl (0);
1347 stop_msg->unique_id = get_msg->unique_id;
1348 stop_msg->key = get_msg->key;
1349 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1351 pending->in_pending_queue = GNUNET_YES;
1353 /* remove 'GET' from active status */
1354 GNUNET_assert (GNUNET_YES ==
1355 GNUNET_CONTAINER_multihashmap_remove (handle->active_requests,
1358 if (GNUNET_YES == get_handle->message->in_pending_queue)
1360 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
1361 get_handle->message);
1362 get_handle->message->in_pending_queue = GNUNET_NO;
1364 GNUNET_free (get_handle->message);
1365 GNUNET_array_grow (get_handle->seen_results,
1366 get_handle->seen_results_end,
1368 GNUNET_free (get_handle);
1369 process_pending_messages (handle);
1374 * Start monitoring the local DHT service.
1376 * @param handle Handle to the DHT service.
1377 * @param type Type of blocks that are of interest.
1378 * @param key Key of data of interest, NULL for all.
1379 * @param get_cb Callback to process monitored get messages.
1380 * @param get_resp_cb Callback to process monitored get response messages.
1381 * @param put_cb Callback to process monitored put messages.
1382 * @param cb_cls Closure for cb.
1384 * @return Handle to stop monitoring.
1386 struct GNUNET_DHT_MonitorHandle *
1387 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
1388 enum GNUNET_BLOCK_Type type,
1389 const struct GNUNET_HashCode *key,
1390 GNUNET_DHT_MonitorGetCB get_cb,
1391 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
1392 GNUNET_DHT_MonitorPutCB put_cb,
1395 struct GNUNET_DHT_MonitorHandle *h;
1396 struct GNUNET_DHT_MonitorStartStopMessage *m;
1397 struct PendingMessage *pending;
1399 h = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorHandle));
1400 GNUNET_CONTAINER_DLL_insert(handle->monitor_head, handle->monitor_tail, h);
1403 h->get_resp_cb = get_resp_cb;
1407 h->dht_handle = handle;
1410 h->key = GNUNET_malloc (sizeof(struct GNUNET_HashCode));
1411 memcpy (h->key, key, sizeof(struct GNUNET_HashCode));
1414 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1415 sizeof (struct PendingMessage));
1416 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1417 pending->msg = &m->header;
1418 pending->handle = handle;
1419 pending->free_on_send = GNUNET_YES;
1420 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_START);
1421 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1422 m->type = htonl(type);
1423 m->get = htons(NULL != get_cb);
1424 m->get_resp = htons(NULL != get_resp_cb);
1425 m->put = htons(NULL != put_cb);
1427 m->filter_key = htons(1);
1428 memcpy (&m->key, key, sizeof(struct GNUNET_HashCode));
1430 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
1432 pending->in_pending_queue = GNUNET_YES;
1433 process_pending_messages (handle);
1442 * @param handle The handle to the monitor request returned by monitor_start.
1444 * On return get_handle will no longer be valid, caller must not use again!!!
1447 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle)
1449 struct GNUNET_DHT_MonitorStartStopMessage *m;
1450 struct PendingMessage *pending;
1452 GNUNET_CONTAINER_DLL_remove (handle->dht_handle->monitor_head,
1453 handle->dht_handle->monitor_tail,
1456 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorStartStopMessage) +
1457 sizeof (struct PendingMessage));
1458 m = (struct GNUNET_DHT_MonitorStartStopMessage *) &pending[1];
1459 pending->msg = &m->header;
1460 pending->handle = handle->dht_handle;
1461 pending->free_on_send = GNUNET_YES;
1462 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP);
1463 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorStartStopMessage));
1464 m->type = htonl(handle->type);
1465 m->get = htons(NULL != handle->get_cb);
1466 m->get_resp = htons(NULL != handle->get_resp_cb);
1467 m->put = htons(NULL != handle->put_cb);
1468 if (NULL != handle->key) {
1469 m->filter_key = htons(1);
1470 memcpy (&m->key, handle->key, sizeof(struct GNUNET_HashCode));
1472 GNUNET_CONTAINER_DLL_insert (handle->dht_handle->pending_head,
1473 handle->dht_handle->pending_tail,
1475 pending->in_pending_queue = GNUNET_YES;
1476 process_pending_messages (handle->dht_handle);
1478 GNUNET_free_non_null (handle->key);
1479 GNUNET_free (handle);
1484 /* end of dht_api.c */