2 This file is part of GNUnet.
3 (C) 2009, 2010 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_bandwidth_lib.h"
30 #include "gnunet_client_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_container_lib.h"
33 #include "gnunet_arm_service.h"
34 #include "gnunet_hello_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_time_lib.h"
38 #include "gnunet_dht_service.h"
41 #define DEBUG_DHT_API GNUNET_NO
44 * Entry in our list of messages to be (re-)transmitted.
49 * This is a doubly-linked list.
51 struct PendingMessage *prev;
54 * This is a doubly-linked list.
56 struct PendingMessage *next;
59 * Message that is pending, allocated at the end
62 const struct GNUNET_MessageHeader *msg;
65 * Handle to the DHT API context.
67 struct GNUNET_DHT_Handle *handle;
70 * Continuation to call when the request has been
71 * transmitted (for the first time) to the service; can be NULL.
73 GNUNET_SCHEDULER_Task cont;
81 * Timeout task for this message
83 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
86 * Unique ID for this request
91 * Free the saved message once sent, set to GNUNET_YES for messages
92 * that do not receive responses; GNUNET_NO if this pending message
93 * is aliased from a 'struct GNUNET_DHT_RouteHandle' and will be freed
99 * GNUNET_YES if this message is in our pending queue right now.
101 int in_pending_queue;
107 * Handle to a route request
109 struct GNUNET_DHT_RouteHandle
113 * Iterator to call on data receipt
115 GNUNET_DHT_ReplyProcessor iter;
118 * Closure for the iterator callback
123 * Main handle to this DHT api
125 struct GNUNET_DHT_Handle *dht_handle;
128 * The actual message sent for this request,
129 * used for retransmitting requests on service
130 * failure/reconnect. Freed on route_stop.
132 struct PendingMessage *message;
135 * Key that this get request is for
140 * Unique identifier for this request (for key collisions). FIXME: redundant!?
148 * Connection to the DHT service.
150 struct GNUNET_DHT_Handle
154 * Configuration to use.
156 const struct GNUNET_CONFIGURATION_Handle *cfg;
159 * Socket (if available).
161 struct GNUNET_CLIENT_Connection *client;
164 * Currently pending transmission request (or NULL).
166 struct GNUNET_CLIENT_TransmitHandle *th;
169 * Head of linked list of messages we would like to transmit.
171 struct PendingMessage *pending_head;
174 * Tail of linked list of messages we would like to transmit.
176 struct PendingMessage *pending_tail;
179 * Hash map containing the current outstanding unique requests
180 * (values are of type 'struct GNUNET_DHT_RouteHandle').
182 struct GNUNET_CONTAINER_MultiHashMap *active_requests;
185 * Generator for unique ids.
193 * Transmit the next pending message, called by notify_transmit_ready
196 transmit_pending (void *cls,
202 * Handler for messages received from the DHT service
203 * a demultiplexer which handles numerous message types
207 service_message_handler (void *cls,
208 const struct GNUNET_MessageHeader *msg);
214 * Try to (re)connect to the DHT service.
216 * @return GNUNET_YES on success, GNUNET_NO on failure.
219 try_connect (struct GNUNET_DHT_Handle *handle)
221 if (handle->client != NULL)
223 handle->client = GNUNET_CLIENT_connect ("dht", handle->cfg);
224 if (handle->client == NULL)
226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
227 _("Failed to connect to the DHT service!\n"));
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "Starting to process replies from DHT\n");
232 GNUNET_CLIENT_receive (handle->client,
233 &service_message_handler,
235 GNUNET_TIME_UNIT_FOREVER_REL);
241 * Add the request corresponding to the given route handle
242 * to the pending queue (if it is not already in there).
244 * @param cls the 'struct GNUNET_DHT_Handle*'
245 * @param key key for the request (not used)
246 * @param value the 'struct GNUNET_DHT_RouteHandle*'
247 * @return GNUNET_YES (always)
250 add_request_to_pending (void *cls,
251 const GNUNET_HashCode *key,
254 struct GNUNET_DHT_Handle *handle = cls;
255 struct GNUNET_DHT_RouteHandle *rh = value;
257 if (GNUNET_NO == rh->message->in_pending_queue)
259 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
260 handle->pending_tail,
262 rh->message->in_pending_queue = GNUNET_YES;
269 * Re-connect to the DHT, re-issue all pending requests if needed.
272 reconnect (struct GNUNET_DHT_Handle *handle)
274 if (handle->client != NULL)
276 GNUNET_CLIENT_disconnect (handle->client,
278 handle->client = NULL;
280 if (GNUNET_YES != try_connect (handle))
282 GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
283 &add_request_to_pending,
285 if (handle->pending_head == NULL)
287 GNUNET_CLIENT_notify_transmit_ready (handle->client,
288 ntohs(handle->pending_head->msg->size),
289 GNUNET_TIME_UNIT_FOREVER_REL,
298 * Try to send messages from list of messages to send
301 process_pending_messages (struct GNUNET_DHT_Handle *handle)
303 struct PendingMessage *head;
305 if (GNUNET_YES != try_connect (handle))
307 if (handle->th != NULL)
309 if (NULL == (head = handle->pending_head))
311 handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
312 ntohs (head->msg->size),
313 GNUNET_TIME_UNIT_FOREVER_REL,
317 if (NULL == handle->th)
326 * Transmit the next pending message, called by notify_transmit_ready
329 transmit_pending (void *cls,
333 struct GNUNET_DHT_Handle *handle = cls;
334 struct PendingMessage *head;
343 if (NULL == (head = handle->pending_head))
346 tsize = ntohs (head->msg->size);
349 process_pending_messages (handle);
352 memcpy (buf, head->msg, tsize);
353 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
354 handle->pending_tail,
356 if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
358 GNUNET_SCHEDULER_cancel (head->timeout_task);
359 head->timeout_task = GNUNET_SCHEDULER_NO_TASK;
361 if (NULL != head->cont)
363 GNUNET_SCHEDULER_add_continuation (head->cont,
365 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
367 head->cont_cls = NULL;
369 head->in_pending_queue = GNUNET_NO;
370 if (GNUNET_YES == head->free_on_send)
372 process_pending_messages (handle);
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "Forwarded request of %u bytes to DHT service\n",
375 (unsigned int) tsize);
383 * Process a given reply that might match the given
387 process_reply (void *cls,
388 const GNUNET_HashCode *key,
391 const struct GNUNET_DHT_RouteResultMessage *dht_msg = cls;
392 struct GNUNET_DHT_RouteHandle *rh = value;
393 const struct GNUNET_MessageHeader *enc_msg;
396 const struct GNUNET_PeerIdentity **get_path;
397 const struct GNUNET_PeerIdentity **put_path;
398 const struct GNUNET_PeerIdentity *pos;
403 uid = GNUNET_ntohll (dht_msg->unique_id);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Reply UID did not match request UID\n");
410 enc_size = ntohs (dht_msg->header.size) - sizeof (struct GNUNET_DHT_RouteResultMessage);
411 if (enc_size < sizeof (struct GNUNET_MessageHeader))
416 pos = (const struct GNUNET_PeerIdentity *) &dht_msg[1];
417 ppl = ntohs (dht_msg->put_path_length);
418 gpl = ntohs (dht_msg->get_path_length);
419 if ( (ppl + gpl) * sizeof (struct GNUNET_PeerIdentity) > enc_size)
426 put_path = GNUNET_malloc ((ppl+1) * sizeof (struct GNUNET_PeerIdentity*));
432 put_path[ppl] = NULL;
438 get_path = GNUNET_malloc ((gpl+1) * sizeof (struct GNUNET_PeerIdentity*));
444 get_path[gpl] = NULL;
448 enc_size -= (ppl + gpl) * sizeof (struct GNUNET_PeerIdentity);
449 enc_msg = (const struct GNUNET_MessageHeader *) pos;
450 if (enc_size != ntohs (enc_msg->size))
453 GNUNET_free_non_null (get_path);
454 GNUNET_free_non_null (put_path);
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Processing reply.\n");
459 rh->iter (rh->iter_cls,
464 GNUNET_free_non_null (get_path);
465 GNUNET_free_non_null (put_path);
471 * Handler for messages received from the DHT service
472 * a demultiplexer which handles numerous message types
474 * @param cls the 'struct GNUNET_DHT_Handle'
475 * @param msg the incoming message
478 service_message_handler (void *cls,
479 const struct GNUNET_MessageHeader *msg)
481 struct GNUNET_DHT_Handle *handle = cls;
482 const struct GNUNET_DHT_RouteResultMessage *dht_msg;
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Error receiving data from DHT service, reconnecting\n");
491 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT)
497 if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_RouteResultMessage))
503 dht_msg = (const struct GNUNET_DHT_RouteResultMessage *) msg;
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Comparing reply `%s' against %u pending requests.\n",
506 GNUNET_h2s (&dht_msg->key),
507 GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
508 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Continuing to process replies from DHT\n");
514 GNUNET_CLIENT_receive (handle->client,
515 &service_message_handler,
516 handle, GNUNET_TIME_UNIT_FOREVER_REL);
522 * Initialize the connection with the DHT service.
524 * @param cfg configuration to use
525 * @param ht_len size of the internal hash table to use for
526 * processing multiple GET/FIND requests in parallel
528 * @return handle to the DHT service, or NULL on error
530 struct GNUNET_DHT_Handle *
531 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
534 struct GNUNET_DHT_Handle *handle;
536 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
538 handle->uid_gen = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
539 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
540 if (GNUNET_NO == try_connect (handle))
542 GNUNET_DHT_disconnect (handle);
550 * Shutdown connection with the DHT service.
552 * @param handle handle of the DHT connection to stop
555 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
557 struct PendingMessage *pm;
558 GNUNET_assert(handle != NULL);
559 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size(handle->active_requests));
560 if (handle->th != NULL)
562 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
565 while (NULL != (pm = handle->pending_head))
567 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
568 handle->pending_tail,
570 GNUNET_assert (GNUNET_YES == pm->free_on_send);
571 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
572 GNUNET_SCHEDULER_cancel (pm->timeout_task);
573 if (NULL != pm->cont)
574 GNUNET_SCHEDULER_add_continuation (pm->cont,
576 GNUNET_SCHEDULER_REASON_TIMEOUT);
577 pm->in_pending_queue = GNUNET_NO;
580 if (handle->client != NULL)
582 GNUNET_CLIENT_disconnect (handle->client, GNUNET_YES);
583 handle->client = NULL;
585 GNUNET_CONTAINER_multihashmap_destroy(handle->active_requests);
586 GNUNET_free (handle);
592 /* ***** Special low-level API providing generic routing abstraction ***** */
596 * Timeout for the transmission of a fire&forget-request. Clean it up.
598 * @param cls the 'struct PendingMessage'
599 * @param tc scheduler context
602 timeout_route_request (void *cls,
603 const struct GNUNET_SCHEDULER_TaskContext *tc)
605 struct PendingMessage *pending = cls;
606 struct GNUNET_DHT_Handle *handle;
608 if (pending->free_on_send != GNUNET_YES)
610 /* timeouts should only apply to fire & forget requests! */
614 handle = pending->handle;
615 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
616 handle->pending_tail,
618 if (pending->cont != NULL)
619 pending->cont (pending->cont_cls,
621 GNUNET_free (pending);
626 * Initiate a generic DHT route operation.
628 * @param handle handle to the DHT service
629 * @param key the key to look up
630 * @param desired_replication_level how many peers should ultimately receive
631 * this message (advisory only, target may be too high for the
632 * given DHT or not hit exactly).
633 * @param options options for routing
634 * @param enc send the encapsulated message to a peer close to the key
635 * @param iter function to call on each result, NULL if no replies are expected
636 * @param iter_cls closure for iter
637 * @param timeout when to abort with an error if we fail to get
638 * a confirmation for the request (when necessary) or how long
639 * to wait for tramission to the service; only applies
641 * @param cont continuation to call when the request has been transmitted
642 * the first time to the service
643 * @param cont_cls closure for cont
644 * @return handle to stop the request, NULL if the request is "fire and forget"
646 struct GNUNET_DHT_RouteHandle *
647 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
648 const GNUNET_HashCode *key,
649 uint32_t desired_replication_level,
650 enum GNUNET_DHT_RouteOption options,
651 const struct GNUNET_MessageHeader *enc,
652 struct GNUNET_TIME_Relative timeout,
653 GNUNET_DHT_ReplyProcessor iter,
655 GNUNET_SCHEDULER_Task cont,
658 struct PendingMessage *pending;
659 struct GNUNET_DHT_RouteMessage *message;
660 struct GNUNET_DHT_RouteHandle *route_handle;
664 esize = ntohs (enc->size);
665 if (sizeof (struct GNUNET_DHT_RouteMessage) + esize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
670 msize = sizeof (struct GNUNET_DHT_RouteMessage) + esize;
671 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
672 message = (struct GNUNET_DHT_RouteMessage*) &pending[1];
673 pending->msg = &message->header;
674 pending->handle = handle;
675 pending->cont = cont;
676 pending->cont_cls = cont_cls;
678 message->header.size = htons (msize);
679 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE);
681 message->options = htonl ((uint32_t) options);
682 message->desired_replication_level = htonl (desired_replication_level);
684 message->unique_id = GNUNET_htonll (handle->uid_gen);
685 memcpy (&message[1], enc, esize);
689 route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
690 route_handle->key = *key;
691 route_handle->iter = iter;
692 route_handle->iter_cls = iter_cls;
693 route_handle->dht_handle = handle;
694 route_handle->uid = handle->uid_gen;
695 route_handle->message = pending;
696 GNUNET_CONTAINER_multihashmap_put (handle->active_requests,
699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
704 pending->free_on_send = GNUNET_YES;
705 pending->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
706 &timeout_route_request,
709 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
710 handle->pending_tail,
712 pending->in_pending_queue = GNUNET_YES;
713 process_pending_messages (handle);
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 "DHT route start request processed, returning %p\n",
722 * Stop a previously issued routing request
724 * @param route_handle handle to the request to stop
727 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle)
729 struct GNUNET_DHT_Handle *handle;
730 struct PendingMessage *pending;
731 struct GNUNET_DHT_StopMessage *message;
734 handle = route_handle->dht_handle;
735 if (GNUNET_NO == route_handle->message->in_pending_queue)
737 /* need to send stop message */
738 msize = sizeof (struct GNUNET_DHT_StopMessage);
739 pending = GNUNET_malloc (sizeof (struct PendingMessage) +
741 message = (struct GNUNET_DHT_StopMessage*) &pending[1];
742 pending->msg = &message->header;
743 message->header.size = htons (msize);
744 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP);
745 message->unique_id = GNUNET_htonll (route_handle->uid);
746 message->key = route_handle->key;
747 pending->handle = handle;
748 pending->free_on_send = GNUNET_YES;
749 pending->in_pending_queue = GNUNET_YES;
750 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
751 handle->pending_tail,
753 process_pending_messages (handle);
757 /* simply remove pending request from message queue before
758 transmission, no need to transmit STOP request! */
759 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
760 handle->pending_tail,
761 route_handle->message);
763 GNUNET_assert (GNUNET_YES ==
764 GNUNET_CONTAINER_multihashmap_remove (route_handle->dht_handle->active_requests,
767 GNUNET_free(route_handle->message);
768 GNUNET_free(route_handle);
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770 "DHT route stop request processed\n");
775 /* ***** Special API for controlling DHT routing maintenance ******* */
779 * Send a control message to the DHT.
781 * @param handle handle to the DHT service
782 * @param command command
783 * @param variable variable to the command
784 * @param cont continuation to call when done (transmitting request to service)
785 * @param cont_cls closure for cont
788 send_control_message (struct GNUNET_DHT_Handle *handle,
791 GNUNET_SCHEDULER_Task cont,
794 struct GNUNET_DHT_ControlMessage *msg;
795 struct PendingMessage *pending;
797 pending = GNUNET_malloc (sizeof (struct PendingMessage) +
798 sizeof(struct GNUNET_DHT_ControlMessage));
799 msg = (struct GNUNET_DHT_ControlMessage*) &pending[1];
800 pending->msg = &msg->header;
801 msg->header.size = htons (sizeof(struct GNUNET_DHT_ControlMessage));
802 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CONTROL);
803 msg->command = htons (command);
804 msg->variable = htons (variable);
805 pending->free_on_send = GNUNET_YES;
806 pending->cont = cont;
807 pending->cont_cls = cont_cls;
808 pending->in_pending_queue = GNUNET_YES;
809 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
810 handle->pending_tail,
812 process_pending_messages (handle);
817 * Send a message to the DHT telling it to issue a single find
818 * peer request using the peers unique identifier as key. This
819 * is used to fill the routing table, and is normally controlled
820 * by the DHT itself. However, for testing and perhaps more
821 * close control over the DHT, this can be explicitly managed.
823 * @param handle handle to the DHT service
824 * @param cont continuation to call when done (transmitting request to service)
825 * @param cont_cls closure for cont
828 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
829 GNUNET_SCHEDULER_Task cont,
832 send_control_message (handle,
833 GNUNET_MESSAGE_TYPE_DHT_FIND_PEER, 0,
842 * Send a message to the DHT telling it to start issuing random GET
843 * requests every 'frequency' milliseconds.
845 * @param handle handle to the DHT service
846 * @param frequency delay between sending malicious messages
849 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
850 struct GNUNET_TIME_Relative frequency)
852 if (frequency.rel_value > UINT16_MAX)
857 send_control_message (handle,
858 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET, frequency.rel_value,
863 * Send a message to the DHT telling it to start issuing random PUT
864 * requests every 'frequency' milliseconds.
866 * @param handle handle to the DHT service
867 * @param frequency delay between sending malicious messages
870 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
871 struct GNUNET_TIME_Relative frequency)
873 if (frequency.rel_value > UINT16_MAX)
879 send_control_message (handle,
880 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT, frequency.rel_value,
886 * Send a message to the DHT telling it to start dropping
887 * all requests received.
889 * @param handle handle to the DHT service
892 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle)
894 send_control_message (handle,
895 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP, 0,
901 /* end of dht_api.c */