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)
148 * Connection to the DHT service.
150 struct GNUNET_DHT_Handle
155 struct GNUNET_SCHEDULER_Handle *sched;
158 * Configuration to use.
160 const struct GNUNET_CONFIGURATION_Handle *cfg;
163 * Socket (if available).
165 struct GNUNET_CLIENT_Connection *client;
168 * Currently pending transmission request (or NULL).
170 struct GNUNET_CLIENT_TransmitHandle *th;
173 * Head of linked list of messages we would like to transmit.
175 struct PendingMessage *pending_head;
178 * Tail of linked list of messages we would like to transmit.
180 struct PendingMessage *pending_tail;
183 * Hash map containing the current outstanding unique requests
184 * (values are of type 'struct GNUNET_DHT_RouteHandle').
186 struct GNUNET_CONTAINER_MultiHashMap *active_requests;
189 * Generator for unique ids.
197 * Transmit the next pending message, called by notify_transmit_ready
200 transmit_pending (void *cls,
206 * Handler for messages received from the DHT service
207 * a demultiplexer which handles numerous message types
211 service_message_handler (void *cls,
212 const struct GNUNET_MessageHeader *msg);
218 * Try to (re)connect to the DHT service.
220 * @return GNUNET_YES on success, GNUNET_NO on failure.
223 try_connect (struct GNUNET_DHT_Handle *handle)
225 if (handle->client != NULL)
227 handle->client = GNUNET_CLIENT_connect (handle->sched, "dht", handle->cfg);
228 if (handle->client == NULL)
230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
231 _("Failed to connect to the DHT service!\n"));
234 GNUNET_CLIENT_receive (handle->client,
235 &service_message_handler,
237 GNUNET_TIME_UNIT_FOREVER_REL);
243 * Add the request corresponding to the given route handle
244 * to the pending queue (if it is not already in there).
246 * @param cls the 'struct GNUNET_DHT_Handle*'
247 * @param key key for the request (not used)
248 * @param value the 'struct GNUNET_DHT_RouteHandle*'
249 * @return GNUNET_YES (always)
252 add_request_to_pending (void *cls,
253 const GNUNET_HashCode *key,
256 struct GNUNET_DHT_Handle *handle = cls;
257 struct GNUNET_DHT_RouteHandle *rh = value;
259 if (GNUNET_NO == rh->message->in_pending_queue)
261 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
262 handle->pending_tail,
264 rh->message->in_pending_queue = GNUNET_YES;
271 * Re-connect to the DHT, re-issue all pending requests if needed.
274 reconnect (struct GNUNET_DHT_Handle *handle)
276 if (handle->client != NULL)
278 GNUNET_CLIENT_disconnect (handle->client,
280 handle->client = NULL;
282 if (GNUNET_YES != try_connect (handle))
284 GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
285 &add_request_to_pending,
287 if (handle->pending_head == NULL)
289 GNUNET_CLIENT_notify_transmit_ready (handle->client,
290 ntohs(handle->pending_head->msg->size),
291 GNUNET_TIME_UNIT_FOREVER_REL,
300 * Try to send messages from list of messages to send
303 process_pending_messages (struct GNUNET_DHT_Handle *handle)
305 struct PendingMessage *head;
307 if (GNUNET_YES != try_connect (handle))
309 if (handle->th != NULL)
311 if (NULL == (head = handle->pending_head))
313 handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
314 ntohs (head->msg->size),
315 GNUNET_TIME_UNIT_FOREVER_REL,
319 if (NULL == handle->th)
328 * Transmit the next pending message, called by notify_transmit_ready
331 transmit_pending (void *cls,
335 struct GNUNET_DHT_Handle *handle = cls;
336 struct PendingMessage *head;
345 if (NULL == (head = handle->pending_head))
348 tsize = ntohs (head->msg->size);
351 process_pending_messages (handle);
354 memcpy (buf, head->msg, tsize);
355 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
356 handle->pending_tail,
358 if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
360 GNUNET_SCHEDULER_cancel (handle->sched,
362 head->timeout_task = GNUNET_SCHEDULER_NO_TASK;
364 if (NULL != head->cont)
366 GNUNET_SCHEDULER_add_now (handle->sched,
370 head->cont_cls = NULL;
372 head->in_pending_queue = GNUNET_NO;
373 if (GNUNET_YES == head->free_on_send)
375 process_pending_messages (handle);
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;
397 uid = GNUNET_ntohll (dht_msg->unique_id);
400 enc_size = ntohs (dht_msg->header.size) - sizeof (struct GNUNET_DHT_RouteResultMessage);
401 if (enc_size < sizeof (struct GNUNET_MessageHeader))
406 enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
407 if (enc_size != ntohs (enc_msg->size))
412 rh->iter (rh->iter_cls,
420 * Handler for messages received from the DHT service
421 * a demultiplexer which handles numerous message types
423 * @param cls the 'struct GNUNET_DHT_Handle'
424 * @param msg the incoming message
427 service_message_handler (void *cls,
428 const struct GNUNET_MessageHeader *msg)
430 struct GNUNET_DHT_Handle *handle = cls;
431 const struct GNUNET_DHT_RouteResultMessage *dht_msg;
438 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT)
444 if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_RouteResultMessage))
450 dht_msg = (const struct GNUNET_DHT_RouteResultMessage *) msg;
451 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
455 GNUNET_CLIENT_receive (handle->client,
456 &service_message_handler,
457 handle, GNUNET_TIME_UNIT_FOREVER_REL);
463 * Initialize the connection with the DHT service.
465 * @param sched scheduler to use
466 * @param cfg configuration to use
467 * @param ht_len size of the internal hash table to use for
468 * processing multiple GET/FIND requests in parallel
470 * @return handle to the DHT service, or NULL on error
472 struct GNUNET_DHT_Handle *
473 GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
474 const struct GNUNET_CONFIGURATION_Handle *cfg,
477 struct GNUNET_DHT_Handle *handle;
479 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
481 handle->sched = sched;
482 handle->uid_gen = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
483 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
484 if (GNUNET_NO == try_connect (handle))
486 GNUNET_DHT_disconnect (handle);
494 * Shutdown connection with the DHT service.
496 * @param handle handle of the DHT connection to stop
499 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
501 struct PendingMessage *pm;
503 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size(handle->active_requests));
504 if (handle->th != NULL)
506 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
509 while (NULL != (pm = handle->pending_head))
511 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
512 handle->pending_tail,
514 GNUNET_assert (GNUNET_YES == pm->free_on_send);
515 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
516 GNUNET_SCHEDULER_cancel (handle->sched,
518 if (NULL != pm->cont)
519 GNUNET_SCHEDULER_add_continuation (handle->sched,
522 GNUNET_SCHEDULER_REASON_TIMEOUT);
523 pm->in_pending_queue = GNUNET_NO;
526 if (handle->client != NULL)
528 GNUNET_CLIENT_disconnect (handle->client, GNUNET_YES);
529 handle->client = NULL;
531 GNUNET_CONTAINER_multihashmap_destroy(handle->active_requests);
532 GNUNET_free (handle);
538 /* ***** Special low-level API providing generic routeing abstraction ***** */
542 * Timeout for the transmission of a fire&forget-request. Clean it up.
544 * @param cls the 'struct PendingMessage'
545 * @param tc scheduler context
548 timeout_route_request (void *cls,
549 const struct GNUNET_SCHEDULER_TaskContext *tc)
551 struct PendingMessage *pending = cls;
552 struct GNUNET_DHT_Handle *handle;
554 if (pending->free_on_send != GNUNET_YES)
556 /* timeouts should only apply to fire & forget requests! */
560 handle = pending->handle;
561 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
562 handle->pending_tail,
564 if (pending->cont != NULL)
565 pending->cont (pending->cont_cls,
567 GNUNET_free (pending);
572 * Initiate a generic DHT route operation.
574 * @param handle handle to the DHT service
575 * @param key the key to look up
576 * @param desired_replication_level how many peers should ultimately receive
577 * this message (advisory only, target may be too high for the
578 * given DHT or not hit exactly).
579 * @param options options for routing
580 * @param enc send the encapsulated message to a peer close to the key
581 * @param iter function to call on each result, NULL if no replies are expected
582 * @param iter_cls closure for iter
583 * @param timeout when to abort with an error if we fail to get
584 * a confirmation for the request (when necessary) or how long
585 * to wait for tramission to the service; only applies
587 * @param cont continuation to call when the request has been transmitted
588 * the first time to the service
589 * @param cont_cls closure for cont
590 * @return handle to stop the request, NULL if the request is "fire and forget"
592 struct GNUNET_DHT_RouteHandle *
593 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
594 const GNUNET_HashCode * key,
595 uint32_t desired_replication_level,
596 enum GNUNET_DHT_RouteOption options,
597 const struct GNUNET_MessageHeader *enc,
598 struct GNUNET_TIME_Relative timeout,
599 GNUNET_DHT_ReplyProcessor iter,
601 GNUNET_SCHEDULER_Task cont,
604 struct PendingMessage *pending;
605 struct GNUNET_DHT_RouteMessage *message;
606 struct GNUNET_DHT_RouteHandle *route_handle;
610 esize = ntohs (enc->size);
611 if (sizeof (struct GNUNET_DHT_RouteMessage) + esize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
616 msize = sizeof (struct GNUNET_DHT_RouteMessage) + esize;
617 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
618 message = (struct GNUNET_DHT_RouteMessage*) &pending[1];
619 pending->msg = &message->header;
620 pending->handle = handle;
621 pending->cont = cont;
622 pending->cont_cls = cont_cls;
624 message->header.size = htons (msize);
625 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE);
627 message->options = htonl ((uint32_t) options);
628 message->desired_replication_level = htonl (desired_replication_level);
629 memcpy (&message[1], enc, esize);
632 route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
633 route_handle->key = *key;
634 route_handle->iter = iter;
635 route_handle->iter_cls = iter_cls;
636 route_handle->dht_handle = handle;
637 route_handle->uid = handle->uid_gen++;
638 route_handle->message = pending;
639 message->unique_id = GNUNET_htonll (route_handle->uid);
640 GNUNET_CONTAINER_multihashmap_put (handle->active_requests,
643 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
648 pending->free_on_send = GNUNET_YES;
649 pending->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched,
651 &timeout_route_request,
654 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
655 handle->pending_tail,
657 pending->in_pending_queue = GNUNET_YES;
658 process_pending_messages (handle);
664 * Stop a previously issued routing request
666 * @param route_handle handle to the request to stop
669 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle)
671 struct GNUNET_DHT_Handle *handle;
672 struct PendingMessage *pending;
673 struct GNUNET_DHT_StopMessage *message;
676 handle = route_handle->dht_handle;
677 if (GNUNET_NO == route_handle->message->in_pending_queue)
679 /* need to send stop message */
680 msize = sizeof (struct GNUNET_DHT_StopMessage);
681 pending = GNUNET_malloc (sizeof (struct PendingMessage) +
683 message = (struct GNUNET_DHT_StopMessage*) &pending[1];
684 pending->msg = &message->header;
685 message->header.size = htons (msize);
686 message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP);
687 message->unique_id = GNUNET_htonll (route_handle->uid);
688 message->key = route_handle->key;
689 pending->handle = handle;
690 pending->free_on_send = GNUNET_YES;
691 pending->in_pending_queue = GNUNET_YES;
692 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
693 handle->pending_tail,
695 process_pending_messages (handle);
699 /* simply remove pending request from message queue before
700 transmission, no need to transmit STOP request! */
701 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
702 handle->pending_tail,
703 route_handle->message);
705 GNUNET_assert (GNUNET_YES ==
706 GNUNET_CONTAINER_multihashmap_remove (route_handle->dht_handle->active_requests,
709 GNUNET_free(route_handle->message);
710 GNUNET_free(route_handle);
715 /* ***** Special API for controlling DHT routing maintenance ******* */
719 * Send a control message to the DHT.
721 * @param handle handle to the DHT service
722 * @param command command
723 * @param variable variable to the command
724 * @param cont continuation to call when done (transmitting request to service)
725 * @param cont_cls closure for cont
728 send_control_message (struct GNUNET_DHT_Handle *handle,
731 GNUNET_SCHEDULER_Task cont,
734 struct GNUNET_DHT_ControlMessage *msg;
735 struct PendingMessage *pending;
737 pending = GNUNET_malloc (sizeof (struct PendingMessage) +
738 sizeof(struct GNUNET_DHT_ControlMessage));
739 msg = (struct GNUNET_DHT_ControlMessage*) &pending[1];
740 pending->msg = &msg->header;
741 msg->header.size = htons (sizeof(struct GNUNET_DHT_ControlMessage));
742 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CONTROL);
743 msg->command = htons (command);
744 msg->variable = htons (variable);
745 pending->free_on_send = GNUNET_YES;
746 pending->cont = cont;
747 pending->cont_cls = cont_cls;
748 pending->in_pending_queue = GNUNET_YES;
749 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
750 handle->pending_tail,
752 process_pending_messages (handle);
757 * Send a message to the DHT telling it to issue a single find
758 * peer request using the peers unique identifier as key. This
759 * is used to fill the routing table, and is normally controlled
760 * by the DHT itself. However, for testing and perhaps more
761 * close control over the DHT, this can be explicitly managed.
763 * @param handle handle to the DHT service
764 * @param cont continuation to call when done (transmitting request to service)
765 * @param cont_cls closure for cont
768 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
769 GNUNET_SCHEDULER_Task cont,
772 send_control_message (handle,
773 GNUNET_MESSAGE_TYPE_DHT_FIND_PEER, 0,
782 * Send a message to the DHT telling it to start issuing random GET
783 * requests every 'frequency' milliseconds.
785 * @param handle handle to the DHT service
786 * @param frequency delay between sending malicious messages
789 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
790 struct GNUNET_TIME_Relative frequency)
792 if (frequency.value > UINT16_MAX)
797 send_control_message (handle,
798 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET, frequency.value,
803 * Send a message to the DHT telling it to start issuing random PUT
804 * requests every 'frequency' milliseconds.
806 * @param handle handle to the DHT service
807 * @param frequency delay between sending malicious messages
810 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
811 struct GNUNET_TIME_Relative frequency)
813 if (frequency.value > UINT16_MAX)
818 send_control_message (handle,
819 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT, frequency.value,
825 * Send a message to the DHT telling it to start dropping
826 * all requests received.
828 * @param handle handle to the DHT service
831 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle)
833 send_control_message (handle,
834 GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP, 0,
840 /* end of dht_api.c */