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_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 DEBUG_DHT_API GNUNET_EXTRA_LOGGING
39 #define LOG(kind,...) GNUNET_log_from (kind, "dht-api",__VA_ARGS__)
42 * Entry in our list of messages to be (re-)transmitted.
47 * This is a doubly-linked list.
49 struct PendingMessage *prev;
52 * This is a doubly-linked list.
54 struct PendingMessage *next;
57 * Message that is pending, allocated at the end
60 const struct GNUNET_MessageHeader *msg;
63 * Handle to the DHT API context.
65 struct GNUNET_DHT_Handle *handle;
68 * Continuation to call when the request has been
69 * transmitted (for the first time) to the service; can be NULL.
71 GNUNET_SCHEDULER_Task cont;
79 * Timeout task for this message
81 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
84 * Unique ID for this request
89 * Free the saved message once sent, set to GNUNET_YES for messages
90 * that do not receive responses; GNUNET_NO if this pending message
91 * is aliased from a 'struct GNUNET_DHT_RouteHandle' and will be freed
97 * GNUNET_YES if this message is in our pending queue right now.
105 * Handle to a GET request
107 struct GNUNET_DHT_GetHandle
111 * Iterator to call on data receipt
113 GNUNET_DHT_GetIterator iter;
116 * Closure for the iterator callback
121 * Main handle to this DHT api
123 struct GNUNET_DHT_Handle *dht_handle;
126 * The actual message sent for this request,
127 * used for retransmitting requests on service
128 * failure/reconnect. Freed on route_stop.
130 struct PendingMessage *message;
133 * Key that this get request is for
138 * Unique identifier for this request (for key collisions).
146 * Handle to a monitoring request.
148 struct GNUNET_DHT_MonitorHandle
153 struct GNUNET_DHT_MonitorHandle *next;
158 struct GNUNET_DHT_MonitorHandle *prev;
161 * Main handle to this DHT api.
163 struct GNUNET_DHT_Handle *dht_handle;
166 * Type of block looked for.
168 enum GNUNET_BLOCK_Type type;
171 * Key being looked for, NULL == all.
173 GNUNET_HashCode *key;
176 * Callback for each received message of interest.
178 GNUNET_DHT_MonitorCB cb;
189 * Connection to the DHT service.
191 struct GNUNET_DHT_Handle
195 * Configuration to use.
197 const struct GNUNET_CONFIGURATION_Handle *cfg;
200 * Socket (if available).
202 struct GNUNET_CLIENT_Connection *client;
205 * Currently pending transmission request (or NULL).
207 struct GNUNET_CLIENT_TransmitHandle *th;
210 * Head of linked list of messages we would like to transmit.
212 struct PendingMessage *pending_head;
215 * Tail of linked list of messages we would like to transmit.
217 struct PendingMessage *pending_tail;
220 * Head of linked list of messages we would like to monitor.
222 struct GNUNET_DHT_MonitorHandle *monitor_head;
225 * Tail of linked list of messages we would like to monitor.
227 struct GNUNET_DHT_MonitorHandle *monitor_tail;
230 * Hash map containing the current outstanding unique requests
231 * (values are of type 'struct GNUNET_DHT_RouteHandle').
233 struct GNUNET_CONTAINER_MultiHashMap *active_requests;
236 * Task for trying to reconnect.
238 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
241 * How quickly should we retry? Used for exponential back-off on
244 struct GNUNET_TIME_Relative retry_time;
247 * Generator for unique ids.
252 * Did we start our receive loop yet?
259 * Handler for messages received from the DHT service
260 * a demultiplexer which handles numerous message types
264 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg);
268 * Try to (re)connect to the DHT service.
270 * @return GNUNET_YES on success, GNUNET_NO on failure.
273 try_connect (struct GNUNET_DHT_Handle *handle)
275 if (handle->client != NULL)
277 handle->in_receive = GNUNET_NO;
278 handle->client = GNUNET_CLIENT_connect ("dht", handle->cfg);
279 if (handle->client == NULL)
281 LOG (GNUNET_ERROR_TYPE_WARNING,
282 _("Failed to connect to the DHT service!\n"));
290 * Add the request corresponding to the given route handle
291 * to the pending queue (if it is not already in there).
293 * @param cls the 'struct GNUNET_DHT_Handle*'
294 * @param key key for the request (not used)
295 * @param value the 'struct GNUNET_DHT_GetHandle*'
296 * @return GNUNET_YES (always)
299 add_request_to_pending (void *cls, const GNUNET_HashCode * key, void *value)
301 struct GNUNET_DHT_Handle *handle = cls;
302 struct GNUNET_DHT_GetHandle *rh = value;
304 if (GNUNET_NO == rh->message->in_pending_queue)
307 LOG (GNUNET_ERROR_TYPE_DEBUG,
308 "Retransmitting request related to %s to DHT %p\n", GNUNET_h2s (key),
311 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
313 rh->message->in_pending_queue = GNUNET_YES;
320 * Try to send messages from list of messages to send
321 * @param handle DHT_Handle
324 process_pending_messages (struct GNUNET_DHT_Handle *handle);
328 * Try reconnecting to the dht service.
330 * @param cls GNUNET_DHT_Handle
331 * @param tc scheduler context
334 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
336 struct GNUNET_DHT_Handle *handle = cls;
339 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting with DHT %p\n", handle);
341 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
342 if (handle->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
343 handle->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
345 handle->retry_time = GNUNET_TIME_relative_multiply (handle->retry_time, 2);
346 if (handle->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
347 handle->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
348 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
349 if (GNUNET_YES != try_connect (handle))
352 LOG (GNUNET_ERROR_TYPE_DEBUG, "dht reconnect failed(!)\n");
356 GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
357 &add_request_to_pending, handle);
358 process_pending_messages (handle);
363 * Try reconnecting to the DHT service.
365 * @param handle handle to dht to (possibly) disconnect and reconnect
368 do_disconnect (struct GNUNET_DHT_Handle *handle)
370 if (handle->client == NULL)
372 GNUNET_assert (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
373 if (NULL != handle->th)
374 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Disconnecting from DHT service, will try to reconnect in %llu ms\n",
378 (unsigned long long) handle->retry_time.rel_value);
379 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
380 handle->client = NULL;
381 handle->reconnect_task =
382 GNUNET_SCHEDULER_add_delayed (handle->retry_time, &try_reconnect, handle);
387 * Transmit the next pending message, called by notify_transmit_ready
390 transmit_pending (void *cls, size_t size, void *buf);
394 * Try to send messages from list of messages to send
397 process_pending_messages (struct GNUNET_DHT_Handle *handle)
399 struct PendingMessage *head;
401 if (handle->client == NULL)
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "process_pending_messages called, but client is null, reconnecting\n");
405 do_disconnect (handle);
408 if (handle->th != NULL)
410 if (NULL == (head = handle->pending_head))
413 GNUNET_CLIENT_notify_transmit_ready (handle->client,
414 ntohs (head->msg->size),
415 GNUNET_TIME_UNIT_FOREVER_REL,
416 GNUNET_YES, &transmit_pending,
418 if (NULL != handle->th)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "notify_transmit_ready returned NULL, reconnecting\n");
422 do_disconnect (handle);
427 * Transmit the next pending message, called by notify_transmit_ready
430 transmit_pending (void *cls, size_t size, void *buf)
432 struct GNUNET_DHT_Handle *handle = cls;
433 struct PendingMessage *head;
440 LOG (GNUNET_ERROR_TYPE_DEBUG,
441 "Transmission to DHT service failed! Reconnecting!\n");
443 do_disconnect (handle);
446 if (NULL == (head = handle->pending_head))
449 tsize = ntohs (head->msg->size);
452 process_pending_messages (handle);
455 memcpy (buf, head->msg, tsize);
456 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
458 head->in_pending_queue = GNUNET_NO;
459 if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
461 GNUNET_SCHEDULER_cancel (head->timeout_task);
462 head->timeout_task = GNUNET_SCHEDULER_NO_TASK;
464 if (NULL != head->cont)
466 GNUNET_SCHEDULER_add_continuation (head->cont, head->cont_cls,
467 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
469 head->cont_cls = NULL;
471 if (GNUNET_YES == head->free_on_send)
473 process_pending_messages (handle);
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "Forwarded request of %u bytes to DHT service\n", (unsigned int) tsize);
478 if (GNUNET_NO == handle->in_receive)
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting to process replies from DHT\n");
483 handle->in_receive = GNUNET_YES;
484 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
485 GNUNET_TIME_UNIT_FOREVER_REL);
492 * Process a given reply that might match the given
495 * @param cls the 'struct GNUNET_DHT_ClientResultMessage'
496 * @param key query of the request
497 * @param value the 'struct GNUNET_DHT_RouteHandle' of a request matching the same key
498 * @return GNUNET_YES to continue to iterate over all results,
499 * GNUNET_NO if the reply is malformed
502 process_reply (void *cls, const GNUNET_HashCode * key, void *value)
504 const struct GNUNET_DHT_ClientResultMessage *dht_msg = cls;
505 struct GNUNET_DHT_GetHandle *get_handle = value;
506 const struct GNUNET_PeerIdentity *put_path;
507 const struct GNUNET_PeerIdentity *get_path;
508 uint32_t put_path_length;
509 uint32_t get_path_length;
515 if (dht_msg->unique_id != get_handle->unique_id)
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 "Ignoring reply for %s: UID mismatch: %llu/%llu\n", GNUNET_h2s (key),
521 dht_msg->unique_id, get_handle->unique_id);
525 msize = ntohs (dht_msg->header.size);
526 put_path_length = ntohl (dht_msg->put_path_length);
527 get_path_length = ntohl (dht_msg->get_path_length);
529 sizeof (struct GNUNET_DHT_ClientResultMessage) +
530 sizeof (struct GNUNET_PeerIdentity) * (get_path_length + put_path_length);
531 if ((msize < meta_length) ||
533 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) ||
535 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)))
540 data_length = msize - meta_length;
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving %u byte reply for %s to application\n",
543 (unsigned int) data_length, GNUNET_h2s (key));
545 put_path = (const struct GNUNET_PeerIdentity *) &dht_msg[1];
546 get_path = &put_path[put_path_length];
547 data = &get_path[get_path_length];
548 get_handle->iter (get_handle->iter_cls,
549 GNUNET_TIME_absolute_ntoh (dht_msg->expiration), key,
550 get_path, get_path_length, put_path, put_path_length,
551 ntohl (dht_msg->type), data_length, data);
557 * Process a monitoring message from the service.
559 * @param handle The DHT handle.
560 * @param msg Message from the service.
562 * @return GNUNET_OK if everything went fine,
563 * GNUNET_SYSERR if the message is malformed.
566 process_monitor_message (struct GNUNET_DHT_Handle *handle,
567 const struct GNUNET_MessageHeader *msg)
569 struct GNUNET_DHT_MonitorMessage *m;
570 struct GNUNET_DHT_MonitorHandle *h;
573 if (ntohs (msg->type) < GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET ||
574 ntohs (msg->type) > GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT)
575 return GNUNET_SYSERR;
576 msize = ntohs (msg->size);
577 if (msize < sizeof (struct GNUNET_DHT_MonitorMessage))
578 return GNUNET_SYSERR;
580 m = (struct GNUNET_DHT_MonitorMessage *) msg;
581 h = handle->monitor_head;
584 if (h->type == ntohl(m->type) &&
586 memcmp (h->key, &m->key, sizeof (GNUNET_HashCode)) == 0))
588 struct GNUNET_PeerIdentity *path;
592 path = (struct GNUNET_PeerIdentity *) &m[1];
593 getl = ntohl (m->get_path_length);
594 putl = ntohl (m->put_path_length);
595 h->cb (h->cb_cls, ntohs(msg->type),
596 GNUNET_TIME_absolute_ntoh(m->expiration),
598 &path[getl], putl, path, getl,
599 ntohl (m->desired_replication_level),
600 ntohl (m->options), ntohl (m->type),
601 (void *) &path[getl + putl],
603 sizeof (struct GNUNET_DHT_MonitorMessage) -
604 sizeof (struct GNUNET_PeerIdentity) * (putl + getl));
613 * Handler for messages received from the DHT service
614 * a demultiplexer which handles numerous message types
616 * @param cls the 'struct GNUNET_DHT_Handle'
617 * @param msg the incoming message
620 service_message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
622 struct GNUNET_DHT_Handle *handle = cls;
623 const struct GNUNET_DHT_ClientResultMessage *dht_msg;
628 LOG (GNUNET_ERROR_TYPE_DEBUG,
629 "Error receiving data from DHT service, reconnecting\n");
631 do_disconnect (handle);
634 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT)
636 if (process_monitor_message (handle, msg) == GNUNET_OK)
638 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
639 GNUNET_TIME_UNIT_FOREVER_REL);
643 do_disconnect (handle);
646 if (ntohs (msg->size) < sizeof (struct GNUNET_DHT_ClientResultMessage))
649 do_disconnect (handle);
652 dht_msg = (const struct GNUNET_DHT_ClientResultMessage *) msg;
654 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply for `%s' from DHT service %p\n",
655 GNUNET_h2s (&dht_msg->key), handle);
657 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
658 &dht_msg->key, &process_reply,
660 GNUNET_CLIENT_receive (handle->client, &service_message_handler, handle,
661 GNUNET_TIME_UNIT_FOREVER_REL);
666 * Initialize the connection with the DHT service.
668 * @param cfg configuration to use
669 * @param ht_len size of the internal hash table to use for
670 * processing multiple GET/FIND requests in parallel
672 * @return handle to the DHT service, or NULL on error
674 struct GNUNET_DHT_Handle *
675 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
678 struct GNUNET_DHT_Handle *handle;
680 handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_Handle));
683 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
684 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
685 if (GNUNET_NO == try_connect (handle))
687 GNUNET_DHT_disconnect (handle);
695 * Shutdown connection with the DHT service.
697 * @param handle handle of the DHT connection to stop
700 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
702 struct PendingMessage *pm;
704 GNUNET_assert (handle != NULL);
706 GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
707 if (handle->th != NULL)
709 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
712 while (NULL != (pm = handle->pending_head))
714 GNUNET_assert (GNUNET_YES == pm->in_pending_queue);
715 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
717 pm->in_pending_queue = GNUNET_NO;
718 GNUNET_assert (GNUNET_YES == pm->free_on_send);
719 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
720 GNUNET_SCHEDULER_cancel (pm->timeout_task);
721 if (NULL != pm->cont)
722 GNUNET_SCHEDULER_add_continuation (pm->cont, pm->cont_cls,
723 GNUNET_SCHEDULER_REASON_TIMEOUT);
726 if (handle->client != NULL)
728 GNUNET_CLIENT_disconnect (handle->client, GNUNET_YES);
729 handle->client = NULL;
731 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
732 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
733 GNUNET_CONTAINER_multihashmap_destroy (handle->active_requests);
734 GNUNET_free (handle);
739 * Timeout for the transmission of a fire&forget-request. Clean it up.
741 * @param cls the 'struct PendingMessage'
742 * @param tc scheduler context
745 timeout_put_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
747 struct PendingMessage *pending = cls;
748 struct GNUNET_DHT_Handle *handle;
750 handle = pending->handle;
751 GNUNET_assert (GNUNET_YES == pending->in_pending_queue);
752 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
754 pending->in_pending_queue = GNUNET_NO;
755 if (pending->cont != NULL)
756 pending->cont (pending->cont_cls, tc);
757 GNUNET_free (pending);
762 * Perform a PUT operation storing data in the DHT.
764 * @param handle handle to DHT service
765 * @param key the key to store under
766 * @param desired_replication_level estimate of how many
767 * nearest peers this request should reach
768 * @param options routing options for this message
769 * @param type type of the value
770 * @param size number of bytes in data; must be less than 64k
771 * @param data the data to store
772 * @param exp desired expiration time for the value
773 * @param timeout how long to wait for transmission of this request
774 * @param cont continuation to call when done (transmitting request to service)
775 * @param cont_cls closure for cont
778 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const GNUNET_HashCode * key,
779 uint32_t desired_replication_level,
780 enum GNUNET_DHT_RouteOption options,
781 enum GNUNET_BLOCK_Type type, size_t size, const char *data,
782 struct GNUNET_TIME_Absolute exp,
783 struct GNUNET_TIME_Relative timeout, GNUNET_SCHEDULER_Task cont,
786 struct GNUNET_DHT_ClientPutMessage *put_msg;
788 struct PendingMessage *pending;
790 msize = sizeof (struct GNUNET_DHT_ClientPutMessage) + size;
791 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
792 (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
796 cont (cont_cls, NULL);
799 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
800 put_msg = (struct GNUNET_DHT_ClientPutMessage *) &pending[1];
801 pending->msg = &put_msg->header;
802 pending->handle = handle;
803 pending->cont = cont;
804 pending->cont_cls = cont_cls;
805 pending->free_on_send = GNUNET_YES;
806 pending->timeout_task =
807 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_put_request, pending);
808 put_msg->header.size = htons (msize);
809 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT);
810 put_msg->type = htonl (type);
811 put_msg->options = htonl ((uint32_t) options);
812 put_msg->desired_replication_level = htonl (desired_replication_level);
813 put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
815 memcpy (&put_msg[1], data, size);
816 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
818 pending->in_pending_queue = GNUNET_YES;
819 process_pending_messages (handle);
824 * Perform an asynchronous GET operation on the DHT identified. See
825 * also "GNUNET_BLOCK_evaluate".
827 * @param handle handle to the DHT service
828 * @param timeout how long to wait for transmission of this request to the service
829 * @param type expected type of the response object
830 * @param key the key to look up
831 * @param desired_replication_level estimate of how many
832 nearest peers this request should reach
833 * @param options routing options for this message
834 * @param xquery extended query data (can be NULL, depending on type)
835 * @param xquery_size number of bytes in xquery
836 * @param iter function to call on each result
837 * @param iter_cls closure for iter
838 * @return handle to stop the async get
840 struct GNUNET_DHT_GetHandle *
841 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
842 struct GNUNET_TIME_Relative timeout,
843 enum GNUNET_BLOCK_Type type, const GNUNET_HashCode * key,
844 uint32_t desired_replication_level,
845 enum GNUNET_DHT_RouteOption options, const void *xquery,
846 size_t xquery_size, GNUNET_DHT_GetIterator iter,
849 struct GNUNET_DHT_ClientGetMessage *get_msg;
850 struct GNUNET_DHT_GetHandle *get_handle;
852 struct PendingMessage *pending;
854 msize = sizeof (struct GNUNET_DHT_ClientGetMessage) + xquery_size;
855 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
856 (xquery_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
862 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending query for %s to DHT %p\n",
863 GNUNET_h2s (key), handle);
865 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
866 get_msg = (struct GNUNET_DHT_ClientGetMessage *) &pending[1];
867 pending->msg = &get_msg->header;
868 pending->handle = handle;
869 pending->free_on_send = GNUNET_NO;
870 get_msg->header.size = htons (msize);
871 get_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET);
872 get_msg->options = htonl ((uint32_t) options);
873 get_msg->desired_replication_level = htonl (desired_replication_level);
874 get_msg->type = htonl (type);
877 get_msg->unique_id = handle->uid_gen;
878 memcpy (&get_msg[1], xquery, xquery_size);
879 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
881 pending->in_pending_queue = GNUNET_YES;
882 get_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_GetHandle));
883 get_handle->iter = iter;
884 get_handle->iter_cls = iter_cls;
885 get_handle->message = pending;
886 get_handle->unique_id = get_msg->unique_id;
887 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, key, get_handle,
888 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
889 process_pending_messages (handle);
895 * Stop async DHT-get.
897 * @param get_handle handle to the GET operation to stop
900 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle)
902 struct GNUNET_DHT_Handle *handle;
903 const struct GNUNET_DHT_ClientGetMessage *get_msg;
904 struct GNUNET_DHT_ClientGetStopMessage *stop_msg;
905 struct PendingMessage *pending;
907 handle = get_handle->message->handle;
909 (const struct GNUNET_DHT_ClientGetMessage *) get_handle->message->msg;
911 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending STOP for %s to DHT via %p\n",
912 GNUNET_h2s (&get_msg->key), handle);
916 GNUNET_malloc (sizeof (struct PendingMessage) +
917 sizeof (struct GNUNET_DHT_ClientGetStopMessage));
918 stop_msg = (struct GNUNET_DHT_ClientGetStopMessage *) &pending[1];
919 pending->msg = &stop_msg->header;
920 pending->handle = handle;
921 pending->free_on_send = GNUNET_YES;
922 stop_msg->header.size =
923 htons (sizeof (struct GNUNET_DHT_ClientGetStopMessage));
924 stop_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP);
925 stop_msg->reserved = htonl (0);
926 stop_msg->unique_id = get_msg->unique_id;
927 stop_msg->key = get_msg->key;
928 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
930 pending->in_pending_queue = GNUNET_YES;
932 /* remove 'GET' from active status */
933 GNUNET_assert (GNUNET_YES ==
934 GNUNET_CONTAINER_multihashmap_remove (handle->active_requests,
937 if (GNUNET_YES == get_handle->message->in_pending_queue)
939 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
940 get_handle->message);
941 get_handle->message->in_pending_queue = GNUNET_NO;
943 GNUNET_free (get_handle->message);
944 GNUNET_free (get_handle);
946 process_pending_messages (handle);
951 * Start monitoring the local DHT service.
953 * @param handle Handle to the DHT service.
954 * @param type Type of blocks that are of interest.
955 * @param key Key of data of interest, NULL for all.
956 * @param cb Callback to process all monitored data.
957 * @param cb_cls Closure for cb.
959 * @return Handle to stop monitoring.
961 struct GNUNET_DHT_MonitorHandle *
962 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
963 enum GNUNET_BLOCK_Type type,
964 const GNUNET_HashCode *key,
965 GNUNET_DHT_MonitorCB cb,
968 struct GNUNET_DHT_MonitorHandle *h;
969 struct GNUNET_DHT_MonitorMessage *m;
970 struct PendingMessage *pending;
972 h = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorHandle));
973 GNUNET_CONTAINER_DLL_insert(handle->monitor_head, handle->monitor_tail, h);
975 GNUNET_assert (NULL != cb);
979 h->dht_handle = handle;
982 h->key = GNUNET_malloc (sizeof(GNUNET_HashCode));
983 memcpy (h->key, key, sizeof(GNUNET_HashCode));
986 pending = GNUNET_malloc (sizeof (struct GNUNET_DHT_MonitorMessage) +
987 sizeof (struct PendingMessage));
988 m = (struct GNUNET_DHT_MonitorMessage *) &pending[1];
989 pending->msg = &m->header;
990 pending->handle = handle;
991 pending->free_on_send = GNUNET_YES;
992 m->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET);
993 m->header.size = htons (sizeof (struct GNUNET_DHT_MonitorMessage));
994 m->type = htonl(type);
996 memcpy (&m->key, key, sizeof(GNUNET_HashCode));
997 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
999 pending->in_pending_queue = GNUNET_YES;
1000 process_pending_messages (handle);
1009 * @param handle The handle to the monitor request returned by monitor_start.
1011 * On return get_handle will no longer be valid, caller must not use again!!!
1014 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle)
1016 GNUNET_free_non_null (handle->key);
1017 GNUNET_CONTAINER_DLL_remove (handle->dht_handle->monitor_head,
1018 handle->dht_handle->monitor_tail,
1020 GNUNET_free (handle);
1025 /* end of dht_api.c */