2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2016, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file dht/gnunet-service-dht_clients.c
21 * @brief GNUnet DHT service's client management code
22 * @author Christian Grothoff
23 * @author Nathan Evans
27 #include "gnunet_constants.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet-service-dht.h"
31 #include "gnunet-service-dht_datacache.h"
32 #include "gnunet-service-dht_neighbours.h"
37 * Should routing details be logged to stderr (for debugging)?
39 #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
41 #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
45 * Struct containing information about a client,
46 * handle to connect to it, and any pending messages
47 * that need to be sent to it.
53 * Entry in the local forwarding map for a client's GET request.
55 struct ClientQueryRecord
59 * The key this request was about
61 struct GNUNET_HashCode key;
64 * Kept in a DLL with @e client.
66 struct ClientQueryRecord *next;
69 * Kept in a DLL with @e client.
71 struct ClientQueryRecord *prev;
74 * Client responsible for the request.
76 struct ClientHandle *ch;
79 * Extended query (see gnunet_block_lib.h), allocated at the end of this struct.
84 * Replies we have already seen for this request.
86 struct GNUNET_HashCode *seen_replies;
89 * Pointer to this nodes heap location in the retry-heap (for fast removal)
91 struct GNUNET_CONTAINER_HeapNode *hnode;
94 * What's the delay between re-try operations that we currently use for this
97 struct GNUNET_TIME_Relative retry_frequency;
100 * What's the next time we should re-try this request?
102 struct GNUNET_TIME_Absolute retry_time;
105 * The unique identifier of this request
110 * Number of bytes in xquery.
115 * Number of entries in 'seen_replies'.
117 unsigned int seen_replies_count;
120 * Desired replication level
122 uint32_t replication;
125 * Any message options for this request
127 uint32_t msg_options;
130 * The type for the data for the GET request.
132 enum GNUNET_BLOCK_Type type;
138 * Struct containing paremeters of monitoring requests.
140 struct ClientMonitorRecord
144 * Next element in DLL.
146 struct ClientMonitorRecord *next;
149 * Previous element in DLL.
151 struct ClientMonitorRecord *prev;
154 * Type of blocks that are of interest
156 enum GNUNET_BLOCK_Type type;
159 * Key of data of interest, NULL for all.
161 struct GNUNET_HashCode *key;
164 * Flag whether to notify about GET messages.
169 * Flag whether to notify about GET_REPONSE messages.
174 * Flag whether to notify about PUT messages.
179 * Client to notify of these requests.
181 struct ClientHandle *ch;
186 * Struct containing information about a client,
187 * handle to connect to it, and any pending messages
188 * that need to be sent to it.
193 * Linked list of active queries of this client.
195 struct ClientQueryRecord *cqr_head;
198 * Linked list of active queries of this client.
200 struct ClientQueryRecord *cqr_tail;
203 * The handle to this client
205 struct GNUNET_SERVICE_Client *client;
208 * The message queue to this client
210 struct GNUNET_MQ_Handle *mq;
215 * Our handle to the BLOCK library.
217 struct GNUNET_BLOCK_Context *GDS_block_context;
220 * Handle for the statistics service.
222 struct GNUNET_STATISTICS_Handle *GDS_stats;
225 * Handle for the service.
227 struct GNUNET_SERVICE_Handle *GDS_service;
230 * The configuration the DHT service is running with
232 const struct GNUNET_CONFIGURATION_Handle *GDS_cfg;
235 * List of active monitoring requests.
237 static struct ClientMonitorRecord *monitor_head;
240 * List of active monitoring requests.
242 static struct ClientMonitorRecord *monitor_tail;
245 * Hashmap for fast key based lookup, maps keys to `struct ClientQueryRecord` entries.
247 static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
250 * Heap with all of our client's request, sorted by retry time (earliest on top).
252 static struct GNUNET_CONTAINER_Heap *retry_heap;
255 * Task that re-transmits requests (using retry_heap).
257 static struct GNUNET_SCHEDULER_Task *retry_task;
261 * Free data structures associated with the given query.
263 * @param record record to remove
266 remove_client_record (struct ClientQueryRecord *record)
268 struct ClientHandle *ch = record->ch;
270 GNUNET_CONTAINER_DLL_remove (ch->cqr_head,
273 GNUNET_assert (GNUNET_YES ==
274 GNUNET_CONTAINER_multihashmap_remove (forward_map,
277 if (NULL != record->hnode)
278 GNUNET_CONTAINER_heap_remove_node (record->hnode);
279 GNUNET_array_grow (record->seen_replies,
280 record->seen_replies_count,
282 GNUNET_free (record);
287 * Functions with this signature are called whenever a local client is
290 * @param cls closure (NULL for dht)
291 * @param client identification of the client
292 * @param mq message queue for talking to @a client
293 * @return our `struct ClientHandle` for @a client
296 client_connect_cb (void *cls,
297 struct GNUNET_SERVICE_Client *client,
298 struct GNUNET_MQ_Handle *mq)
300 struct ClientHandle *ch;
302 ch = GNUNET_new (struct ClientHandle);
310 * Functions with this signature are called whenever a client
311 * is disconnected on the network level.
313 * @param cls closure (NULL for dht)
314 * @param client identification of the client
315 * @param app_ctx our `struct ClientHandle` for @a client
318 client_disconnect_cb (void *cls,
319 struct GNUNET_SERVICE_Client *client,
322 struct ClientHandle *ch = app_ctx;
323 struct ClientQueryRecord *cqr;
324 struct ClientMonitorRecord *monitor;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Local client %p disconnects\n",
329 monitor = monitor_head;
330 while (NULL != monitor)
332 if (monitor->ch == ch)
334 struct ClientMonitorRecord *next;
336 next = monitor->next;
337 GNUNET_free_non_null (monitor->key);
338 GNUNET_CONTAINER_DLL_remove (monitor_head,
341 GNUNET_free (monitor);
346 monitor = monitor->next;
349 while (NULL != (cqr = ch->cqr_head))
350 remove_client_record (cqr);
356 * Route the given request via the DHT. This includes updating
357 * the bloom filter and retransmission times, building the P2P
358 * message and initiating the routing operation.
361 transmit_request (struct ClientQueryRecord *cqr)
363 struct GNUNET_BLOCK_Group *bg;
364 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
366 GNUNET_STATISTICS_update (GDS_stats,
367 gettext_noop ("# GET requests from clients injected"),
370 bg = GNUNET_BLOCK_group_create (GDS_block_context,
372 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
377 cqr->seen_replies_count,
379 GNUNET_BLOCK_group_set_seen (bg,
381 cqr->seen_replies_count);
383 = GNUNET_CONTAINER_bloomfilter_init (NULL,
385 GNUNET_CONSTANTS_BLOOMFILTER_K);
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "Initiating GET for %s, replication %u, already have %u replies\n",
388 GNUNET_h2s (&cqr->key),
390 cqr->seen_replies_count);
391 GDS_NEIGHBOURS_handle_get (cqr->type,
400 GNUNET_BLOCK_group_destroy (bg);
401 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
403 /* exponential back-off for retries.
404 * max GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD (15 min) */
405 cqr->retry_frequency = GNUNET_TIME_STD_BACKOFF (cqr->retry_frequency);
406 cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
411 * Task that looks at the #retry_heap and transmits all of the requests
412 * on the heap that are ready for transmission. Then re-schedules
413 * itself (unless the heap is empty).
418 transmit_next_request_task (void *cls)
420 struct ClientQueryRecord *cqr;
421 struct GNUNET_TIME_Relative delay;
424 while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
427 delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
428 if (delay.rel_value_us > 0)
431 = GNUNET_CONTAINER_heap_insert (retry_heap,
433 cqr->retry_time.abs_value_us);
435 = GNUNET_SCHEDULER_add_at (cqr->retry_time,
436 &transmit_next_request_task,
440 transmit_request (cqr);
442 = GNUNET_CONTAINER_heap_insert (retry_heap,
444 cqr->retry_time.abs_value_us);
450 * Check DHT PUT messages from the client.
452 * @param cls the client we received this message from
453 * @param dht_msg the actual message received
454 * @return #GNUNET_OK (always)
457 check_dht_local_put (void *cls,
458 const struct GNUNET_DHT_ClientPutMessage *dht_msg)
460 /* always well-formed */
466 * Handler for PUT messages.
468 * @param cls the client we received this message from
469 * @param dht_msg the actual message received
472 handle_dht_local_put (void *cls,
473 const struct GNUNET_DHT_ClientPutMessage *dht_msg)
475 struct ClientHandle *ch = cls;
476 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
479 size = ntohs (dht_msg->header.size);
480 GNUNET_STATISTICS_update (GDS_stats,
481 gettext_noop ("# PUT requests received from clients"),
484 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
486 GNUNET_h2s_full (&dht_msg->key));
487 /* give to local clients */
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "Handling local PUT of %u-bytes for query %s\n",
490 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
491 GNUNET_h2s (&dht_msg->key));
492 GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
498 ntohl (dht_msg->type),
499 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
502 GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
506 ntohl (dht_msg->type),
507 size - sizeof (struct GNUNET_DHT_ClientPutMessage),
509 /* route to other peers */
511 = GNUNET_CONTAINER_bloomfilter_init (NULL,
513 GNUNET_CONSTANTS_BLOOMFILTER_K);
514 GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type),
515 ntohl (dht_msg->options),
516 ntohl (dht_msg->desired_replication_level),
517 GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
524 size - sizeof (struct GNUNET_DHT_ClientPutMessage));
525 GDS_CLIENTS_process_put (ntohl (dht_msg->options),
526 ntohl (dht_msg->type),
528 ntohl (dht_msg->desired_replication_level),
530 GDS_NEIGHBOURS_get_id(),
531 GNUNET_TIME_absolute_ntoh (dht_msg->expiration),
534 size - sizeof (struct GNUNET_DHT_ClientPutMessage));
535 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
536 GNUNET_SERVICE_client_continue (ch->client);
541 * Check DHT GET messages from the client.
543 * @param cls the client we received this message from
544 * @param message the actual message received
545 * @return #GNUNET_OK (always)
548 check_dht_local_get (void *cls,
549 const struct GNUNET_DHT_ClientGetMessage *get)
551 /* always well-formed */
557 * Handle a result from local datacache for a GET operation.
559 * @param cls the `struct ClientHandle` of the client doing the query
560 * @param type type of the block
561 * @param expiration_time when does the content expire
562 * @param key key for the content
563 * @param put_path_length number of entries in @a put_path
564 * @param put_path peers the original PUT traversed (if tracked)
565 * @param get_path_length number of entries in @a get_path
566 * @param get_path peers this reply has traversed so far (if tracked)
567 * @param data payload of the reply
568 * @param data_size number of bytes in @a data
571 handle_local_result (void *cls,
572 enum GNUNET_BLOCK_Type type,
573 struct GNUNET_TIME_Absolute expiration_time,
574 const struct GNUNET_HashCode *key,
575 unsigned int put_path_length,
576 const struct GNUNET_PeerIdentity *put_path,
577 unsigned int get_path_length,
578 const struct GNUNET_PeerIdentity *get_path,
582 // FIXME: this needs some clean up: inline the function,
583 // possibly avoid even looking up the client!
584 GDS_CLIENTS_handle_reply (expiration_time,
587 put_path_length, put_path,
594 * Handler for DHT GET messages from the client.
596 * @param cls the client we received this message from
597 * @param message the actual message received
600 handle_dht_local_get (void *cls,
601 const struct GNUNET_DHT_ClientGetMessage *get)
603 struct ClientHandle *ch = cls;
604 struct ClientQueryRecord *cqr;
609 size = ntohs (get->header.size);
610 xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
611 xquery = (const char *) &get[1];
612 GNUNET_STATISTICS_update (GDS_stats,
614 ("# GET requests received from clients"), 1,
616 LOG (GNUNET_ERROR_TYPE_DEBUG,
617 "Received GET request for %s from local client %p, xq: %.*s\n",
618 GNUNET_h2s (&get->key),
622 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
624 GNUNET_h2s_full (&get->key));
626 cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
629 cqr->xquery = (void *) &cqr[1];
630 GNUNET_memcpy (&cqr[1], xquery, xquery_size);
631 cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
632 cqr->retry_frequency = GNUNET_TIME_UNIT_SECONDS;
633 cqr->retry_time = GNUNET_TIME_absolute_get ();
634 cqr->unique_id = get->unique_id;
635 cqr->xquery_size = xquery_size;
636 cqr->replication = ntohl (get->desired_replication_level);
637 cqr->msg_options = ntohl (get->options);
638 cqr->type = ntohl (get->type);
639 GNUNET_CONTAINER_DLL_insert (ch->cqr_head,
642 GNUNET_CONTAINER_multihashmap_put (forward_map,
645 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
646 GDS_CLIENTS_process_get (ntohl (get->options),
649 ntohl (get->desired_replication_level),
651 GDS_NEIGHBOURS_get_id(),
653 /* start remote requests */
654 if (NULL != retry_task)
655 GNUNET_SCHEDULER_cancel (retry_task);
656 retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task,
658 /* perform local lookup */
659 GDS_DATACACHE_handle_get (&get->key,
664 &handle_local_result,
666 GNUNET_SERVICE_client_continue (ch->client);
671 * Closure for #find_by_unique_id().
673 struct FindByUniqueIdContext
676 * Where to store the result, if found.
678 struct ClientQueryRecord *cqr;
685 * Function called for each existing DHT record for the given
686 * query. Checks if it matches the UID given in the closure
687 * and if so returns the entry as a result.
689 * @param cls the search context
690 * @param key query for the lookup (not used)
691 * @param value the `struct ClientQueryRecord`
692 * @return #GNUNET_YES to continue iteration (result not yet found)
695 find_by_unique_id (void *cls,
696 const struct GNUNET_HashCode *key,
699 struct FindByUniqueIdContext *fui_ctx = cls;
700 struct ClientQueryRecord *cqr = value;
702 if (cqr->unique_id != fui_ctx->unique_id)
710 * Check "GET result seen" messages from the client.
712 * @param cls the client we received this message from
713 * @param message the actual message received
714 * @return #GNUNET_OK if @a seen is well-formed
717 check_dht_local_get_result_seen (void *cls,
718 const struct GNUNET_DHT_ClientGetResultSeenMessage *seen)
721 unsigned int hash_count;
723 size = ntohs (seen->header.size);
724 hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
725 if (size != sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage) + hash_count * sizeof (struct GNUNET_HashCode))
728 return GNUNET_SYSERR;
735 * Handler for "GET result seen" messages from the client.
737 * @param cls the client we received this message from
738 * @param message the actual message received
741 handle_dht_local_get_result_seen (void *cls,
742 const struct GNUNET_DHT_ClientGetResultSeenMessage *seen)
744 struct ClientHandle *ch = cls;
746 unsigned int hash_count;
747 unsigned int old_count;
748 const struct GNUNET_HashCode *hc;
749 struct FindByUniqueIdContext fui_ctx;
750 struct ClientQueryRecord *cqr;
752 size = ntohs (seen->header.size);
753 hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
754 hc = (const struct GNUNET_HashCode*) &seen[1];
755 fui_ctx.unique_id = seen->unique_id;
757 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
761 if (NULL == (cqr = fui_ctx.cqr))
764 GNUNET_SERVICE_client_drop (ch->client);
767 /* finally, update 'seen' list */
768 old_count = cqr->seen_replies_count;
769 GNUNET_array_grow (cqr->seen_replies,
770 cqr->seen_replies_count,
771 cqr->seen_replies_count + hash_count);
772 GNUNET_memcpy (&cqr->seen_replies[old_count],
774 sizeof (struct GNUNET_HashCode) * hash_count);
779 * Closure for #remove_by_unique_id().
781 struct RemoveByUniqueIdContext
784 * Client that issued the removal request.
786 struct ClientHandle *ch;
789 * Unique ID of the request.
796 * Iterator over hash map entries that frees all entries
797 * that match the given client and unique ID.
799 * @param cls unique ID and client to search for in source routes
800 * @param key current key code
801 * @param value value in the hash map, a ClientQueryRecord
802 * @return #GNUNET_YES (we should continue to iterate)
805 remove_by_unique_id (void *cls,
806 const struct GNUNET_HashCode *key,
809 const struct RemoveByUniqueIdContext *ctx = cls;
810 struct ClientQueryRecord *cqr = value;
812 if (cqr->unique_id != ctx->unique_id)
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Removing client %p's record for key %s (by unique id)\n",
818 remove_client_record (cqr);
824 * Handler for any generic DHT stop messages, calls the appropriate handler
825 * depending on message type (if processed locally)
827 * @param cls client we received this message from
828 * @param message the actual message received
832 handle_dht_local_get_stop (void *cls,
833 const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg)
835 struct ClientHandle *ch = cls;
836 struct RemoveByUniqueIdContext ctx;
838 GNUNET_STATISTICS_update (GDS_stats,
840 ("# GET STOP requests received from clients"), 1,
842 LOG (GNUNET_ERROR_TYPE_DEBUG,
843 "Received GET STOP request for %s from local client %p\n",
844 GNUNET_h2s (&dht_stop_msg->key),
847 ctx.unique_id = dht_stop_msg->unique_id;
848 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
850 &remove_by_unique_id,
852 GNUNET_SERVICE_client_continue (ch->client);
857 * Handler for monitor start messages
859 * @param cls the client we received this message from
860 * @param msg the actual message received
864 handle_dht_local_monitor (void *cls,
865 const struct GNUNET_DHT_MonitorStartStopMessage *msg)
867 struct ClientHandle *ch = cls;
868 struct ClientMonitorRecord *r;
870 r = GNUNET_new (struct ClientMonitorRecord);
872 r->type = ntohl (msg->type);
873 r->get = ntohs (msg->get);
874 r->get_resp = ntohs (msg->get_resp);
875 r->put = ntohs (msg->put);
876 if (0 == ntohs (msg->filter_key))
882 r->key = GNUNET_new (struct GNUNET_HashCode);
883 GNUNET_memcpy (r->key,
885 sizeof (struct GNUNET_HashCode));
887 GNUNET_CONTAINER_DLL_insert (monitor_head,
890 GNUNET_SERVICE_client_continue (ch->client);
895 * Handler for monitor stop messages
897 * @param cls the client we received this message from
898 * @param msg the actual message received
901 handle_dht_local_monitor_stop (void *cls,
902 const struct GNUNET_DHT_MonitorStartStopMessage *msg)
904 struct ClientHandle *ch = cls;
905 struct ClientMonitorRecord *r;
908 GNUNET_SERVICE_client_continue (ch->client);
909 for (r = monitor_head; NULL != r; r = r->next)
913 keys_match = (0 == ntohs(msg->filter_key));
917 keys_match = ( (0 != ntohs(msg->filter_key)) &&
920 sizeof(struct GNUNET_HashCode))) );
922 if ( (ch == r->ch) &&
923 (ntohl(msg->type) == r->type) &&
924 (r->get == msg->get) &&
925 (r->get_resp == msg->get_resp) &&
926 (r->put == msg->put) &&
929 GNUNET_CONTAINER_DLL_remove (monitor_head,
932 GNUNET_free_non_null (r->key);
934 return; /* Delete only ONE entry */
941 * Closure for #forward_reply()
943 struct ForwardReplyContext
947 * Expiration time of the reply.
949 struct GNUNET_TIME_Absolute expiration;
954 const struct GNUNET_PeerIdentity *get_path;
959 const struct GNUNET_PeerIdentity *put_path;
967 * Number of bytes in data.
972 * Number of entries in @e get_path.
974 unsigned int get_path_length;
977 * Number of entries in @e put_path.
979 unsigned int put_path_length;
984 enum GNUNET_BLOCK_Type type;
990 * Iterator over hash map entries that send a given reply to
991 * each of the matching clients. With some tricky recycling
994 * @param cls the 'struct ForwardReplyContext'
995 * @param key current key
996 * @param value value in the hash map, a ClientQueryRecord
997 * @return #GNUNET_YES (we should continue to iterate),
998 * if the result is mal-formed, #GNUNET_NO
1001 forward_reply (void *cls,
1002 const struct GNUNET_HashCode *key,
1005 struct ForwardReplyContext *frc = cls;
1006 struct ClientQueryRecord *record = value;
1007 struct GNUNET_MQ_Envelope *env;
1008 struct GNUNET_DHT_ClientResultMessage *reply;
1009 enum GNUNET_BLOCK_EvaluationResult eval;
1011 struct GNUNET_HashCode ch;
1012 struct GNUNET_PeerIdentity *paths;
1014 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
1015 "CLIENT-RESULT %s\n",
1016 GNUNET_h2s_full (key));
1017 if ( (record->type != GNUNET_BLOCK_TYPE_ANY) &&
1018 (record->type != frc->type))
1020 LOG (GNUNET_ERROR_TYPE_DEBUG,
1021 "Record type mismatch, not passing request for key %s to local client\n",
1023 GNUNET_STATISTICS_update (GDS_stats,
1025 ("# Key match, type mismatches in REPLY to CLIENT"),
1027 return GNUNET_YES; /* type mismatch */
1029 GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch);
1030 for (unsigned int i = 0; i < record->seen_replies_count; i++)
1031 if (0 == memcmp (&record->seen_replies[i],
1033 sizeof (struct GNUNET_HashCode)))
1035 LOG (GNUNET_ERROR_TYPE_DEBUG,
1036 "Duplicate reply, not passing request for key %s to local client\n",
1038 GNUNET_STATISTICS_update (GDS_stats,
1040 ("# Duplicate REPLIES to CLIENT request dropped"),
1042 return GNUNET_YES; /* duplicate */
1045 = GNUNET_BLOCK_evaluate (GDS_block_context,
1048 GNUNET_BLOCK_EO_NONE,
1051 record->xquery_size,
1054 LOG (GNUNET_ERROR_TYPE_DEBUG,
1055 "Evaluation result is %d for key %s for local client's query\n",
1060 case GNUNET_BLOCK_EVALUATION_OK_LAST:
1061 do_free = GNUNET_YES;
1063 case GNUNET_BLOCK_EVALUATION_OK_MORE:
1064 GNUNET_array_append (record->seen_replies,
1065 record->seen_replies_count,
1067 do_free = GNUNET_NO;
1069 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
1070 /* should be impossible to encounter here */
1073 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
1074 GNUNET_break_op (0);
1076 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
1079 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
1082 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
1084 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
1085 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1086 _("Unsupported block type (%u) in request!\n"), record->type);
1092 GNUNET_STATISTICS_update (GDS_stats,
1093 gettext_noop ("# RESULTS queued for clients"),
1096 env = GNUNET_MQ_msg_extra (reply,
1098 (frc->get_path_length + frc->put_path_length) * sizeof (struct GNUNET_PeerIdentity),
1099 GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
1100 reply->type = htonl (frc->type);
1101 reply->get_path_length = htonl (frc->get_path_length);
1102 reply->put_path_length = htonl (frc->put_path_length);
1103 reply->unique_id = record->unique_id;
1104 reply->expiration = GNUNET_TIME_absolute_hton (frc->expiration);
1106 paths = (struct GNUNET_PeerIdentity *) &reply[1];
1107 GNUNET_memcpy (paths,
1109 sizeof (struct GNUNET_PeerIdentity) * frc->put_path_length);
1110 GNUNET_memcpy (&paths[frc->put_path_length],
1112 sizeof (struct GNUNET_PeerIdentity) * frc->get_path_length);
1113 GNUNET_memcpy (&paths[frc->get_path_length + frc->put_path_length],
1116 LOG (GNUNET_ERROR_TYPE_DEBUG,
1117 "Sending reply to query %s for client %p\n",
1119 record->ch->client);
1120 GNUNET_MQ_send (record->ch->mq,
1122 if (GNUNET_YES == do_free)
1123 remove_client_record (record);
1129 * Handle a reply we've received from another peer. If the reply
1130 * matches any of our pending queries, forward it to the respective
1133 * @param expiration when will the reply expire
1134 * @param key the query this reply is for
1135 * @param get_path_length number of peers in @a get_path
1136 * @param get_path path the reply took on get
1137 * @param put_path_length number of peers in @a put_path
1138 * @param put_path path the reply took on put
1139 * @param type type of the reply
1140 * @param data_size number of bytes in @a data
1141 * @param data application payload data
1144 GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
1145 const struct GNUNET_HashCode *key,
1146 unsigned int get_path_length,
1147 const struct GNUNET_PeerIdentity *get_path,
1148 unsigned int put_path_length,
1149 const struct GNUNET_PeerIdentity *put_path,
1150 enum GNUNET_BLOCK_Type type,
1154 struct ForwardReplyContext frc;
1157 msize = sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size +
1158 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
1159 if (msize >= GNUNET_MAX_MESSAGE_SIZE)
1164 if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map,
1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1168 "No matching client for reply for key %s\n",
1170 GNUNET_STATISTICS_update (GDS_stats,
1171 gettext_noop ("# REPLIES ignored for CLIENTS (no match)"),
1174 return; /* no matching request, fast exit! */
1176 frc.expiration = expiration;
1177 frc.get_path = get_path;
1178 frc.put_path = put_path;
1180 frc.data_size = data_size;
1181 frc.get_path_length = get_path_length;
1182 frc.put_path_length = put_path_length;
1184 LOG (GNUNET_ERROR_TYPE_DEBUG,
1185 "Forwarding reply for key %s to client\n",
1187 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
1196 * Check if some client is monitoring GET messages and notify
1197 * them in that case.
1199 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
1200 * @param type The type of data in the request.
1201 * @param hop_count Hop count so far.
1202 * @param path_length number of entries in path (or 0 if not recorded).
1203 * @param path peers on the GET path (or NULL if not recorded).
1204 * @param desired_replication_level Desired replication level.
1205 * @param key Key of the requested data.
1208 GDS_CLIENTS_process_get (uint32_t options,
1209 enum GNUNET_BLOCK_Type type,
1211 uint32_t desired_replication_level,
1212 unsigned int path_length,
1213 const struct GNUNET_PeerIdentity *path,
1214 const struct GNUNET_HashCode * key)
1216 struct ClientMonitorRecord *m;
1217 struct ClientHandle **cl;
1218 unsigned int cl_size;
1222 for (m = monitor_head; NULL != m; m = m->next)
1224 if ( ( (GNUNET_BLOCK_TYPE_ANY == m->type) ||
1225 (m->type == type) ) &&
1226 ( (NULL == m->key) ||
1229 sizeof(struct GNUNET_HashCode))) ) )
1231 struct GNUNET_MQ_Envelope *env;
1232 struct GNUNET_DHT_MonitorGetMessage *mmsg;
1233 struct GNUNET_PeerIdentity *msg_path;
1237 /* Don't send duplicates */
1238 for (i = 0; i < cl_size; i++)
1243 GNUNET_array_append (cl,
1247 msize = path_length * sizeof (struct GNUNET_PeerIdentity);
1248 env = GNUNET_MQ_msg_extra (mmsg,
1250 GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET);
1251 mmsg->options = htonl(options);
1252 mmsg->type = htonl(type);
1253 mmsg->hop_count = htonl(hop_count);
1254 mmsg->desired_replication_level = htonl(desired_replication_level);
1255 mmsg->get_path_length = htonl(path_length);
1257 msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1258 GNUNET_memcpy (msg_path,
1260 path_length * sizeof (struct GNUNET_PeerIdentity));
1261 GNUNET_MQ_send (m->ch->mq,
1265 GNUNET_free_non_null (cl);
1270 * Check if some client is monitoring GET RESP messages and notify
1271 * them in that case.
1273 * @param type The type of data in the result.
1274 * @param get_path Peers on GET path (or NULL if not recorded).
1275 * @param get_path_length number of entries in get_path.
1276 * @param put_path peers on the PUT path (or NULL if not recorded).
1277 * @param put_path_length number of entries in get_path.
1278 * @param exp Expiration time of the data.
1279 * @param key Key of the data.
1280 * @param data Pointer to the result data.
1281 * @param size Number of bytes in @a data.
1284 GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type,
1285 const struct GNUNET_PeerIdentity *get_path,
1286 unsigned int get_path_length,
1287 const struct GNUNET_PeerIdentity *put_path,
1288 unsigned int put_path_length,
1289 struct GNUNET_TIME_Absolute exp,
1290 const struct GNUNET_HashCode * key,
1294 struct ClientMonitorRecord *m;
1295 struct ClientHandle **cl;
1296 unsigned int cl_size;
1300 for (m = monitor_head; NULL != m; m = m->next)
1302 if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
1304 memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
1306 struct GNUNET_MQ_Envelope *env;
1307 struct GNUNET_DHT_MonitorGetRespMessage *mmsg;
1308 struct GNUNET_PeerIdentity *path;
1312 /* Don't send duplicates */
1313 for (i = 0; i < cl_size; i++)
1318 GNUNET_array_append (cl,
1323 msize += (get_path_length + put_path_length)
1324 * sizeof (struct GNUNET_PeerIdentity);
1325 env = GNUNET_MQ_msg_extra (mmsg,
1327 GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP);
1328 mmsg->type = htonl(type);
1329 mmsg->put_path_length = htonl(put_path_length);
1330 mmsg->get_path_length = htonl(get_path_length);
1331 mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
1333 path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1334 GNUNET_memcpy (path,
1336 put_path_length * sizeof (struct GNUNET_PeerIdentity));
1337 GNUNET_memcpy (path,
1339 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1340 GNUNET_memcpy (&path[get_path_length],
1343 GNUNET_MQ_send (m->ch->mq,
1347 GNUNET_free_non_null (cl);
1352 * Check if some client is monitoring PUT messages and notify
1353 * them in that case.
1355 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
1356 * @param type The type of data in the request.
1357 * @param hop_count Hop count so far.
1358 * @param path_length number of entries in path (or 0 if not recorded).
1359 * @param path peers on the PUT path (or NULL if not recorded).
1360 * @param desired_replication_level Desired replication level.
1361 * @param exp Expiration time of the data.
1362 * @param key Key under which data is to be stored.
1363 * @param data Pointer to the data carried.
1364 * @param size Number of bytes in data.
1367 GDS_CLIENTS_process_put (uint32_t options,
1368 enum GNUNET_BLOCK_Type type,
1370 uint32_t desired_replication_level,
1371 unsigned int path_length,
1372 const struct GNUNET_PeerIdentity *path,
1373 struct GNUNET_TIME_Absolute exp,
1374 const struct GNUNET_HashCode *key,
1378 struct ClientMonitorRecord *m;
1379 struct ClientHandle **cl;
1380 unsigned int cl_size;
1384 for (m = monitor_head; NULL != m; m = m->next)
1386 if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
1388 memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
1390 struct GNUNET_MQ_Envelope *env;
1391 struct GNUNET_DHT_MonitorPutMessage *mmsg;
1392 struct GNUNET_PeerIdentity *msg_path;
1396 /* Don't send duplicates */
1397 for (i = 0; i < cl_size; i++)
1402 GNUNET_array_append (cl,
1407 msize += path_length * sizeof (struct GNUNET_PeerIdentity);
1408 env = GNUNET_MQ_msg_extra (mmsg,
1410 GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT);
1411 mmsg->options = htonl(options);
1412 mmsg->type = htonl(type);
1413 mmsg->hop_count = htonl(hop_count);
1414 mmsg->desired_replication_level = htonl (desired_replication_level);
1415 mmsg->put_path_length = htonl (path_length);
1417 mmsg->expiration_time = GNUNET_TIME_absolute_hton (exp);
1418 msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1419 GNUNET_memcpy (msg_path,
1421 path_length * sizeof (struct GNUNET_PeerIdentity));
1422 GNUNET_memcpy (&msg_path[path_length],
1425 GNUNET_MQ_send (m->ch->mq,
1429 GNUNET_free_non_null (cl);
1434 * Initialize client subsystem.
1436 * @param server the initialized server
1442 = GNUNET_CONTAINER_multihashmap_create (1024,
1445 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1450 * Shutdown client subsystem.
1455 if (NULL != retry_task)
1457 GNUNET_SCHEDULER_cancel (retry_task);
1464 * Define "main" method using service macro.
1466 * @param name name of the service, i.e. "dht" or "xdht"
1467 * @param run name of the initializaton method for the service
1469 #define GDS_DHT_SERVICE_INIT(name,run) \
1470 GNUNET_SERVICE_MAIN \
1472 GNUNET_SERVICE_OPTION_NONE, \
1474 &client_connect_cb, \
1475 &client_disconnect_cb, \
1477 GNUNET_MQ_hd_var_size (dht_local_put, \
1478 GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, \
1479 struct GNUNET_DHT_ClientPutMessage, \
1481 GNUNET_MQ_hd_var_size (dht_local_get, \
1482 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, \
1483 struct GNUNET_DHT_ClientGetMessage, \
1485 GNUNET_MQ_hd_fixed_size (dht_local_get_stop, \
1486 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP, \
1487 struct GNUNET_DHT_ClientGetStopMessage, \
1489 GNUNET_MQ_hd_fixed_size (dht_local_monitor, \
1490 GNUNET_MESSAGE_TYPE_DHT_MONITOR_START, \
1491 struct GNUNET_DHT_MonitorStartStopMessage, \
1493 GNUNET_MQ_hd_fixed_size (dht_local_monitor_stop, \
1494 GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP, \
1495 struct GNUNET_DHT_MonitorStartStopMessage, \
1497 GNUNET_MQ_hd_var_size (dht_local_get_result_seen, \
1498 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN, \
1499 struct GNUNET_DHT_ClientGetResultSeenMessage , \
1501 GNUNET_MQ_handler_end ())
1505 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1507 void __attribute__ ((destructor))
1510 if (NULL != retry_heap)
1512 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
1513 GNUNET_CONTAINER_heap_destroy (retry_heap);
1516 if (NULL != forward_map)
1518 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
1519 GNUNET_CONTAINER_multihashmap_destroy (forward_map);
1524 /* end of gnunet-service-dht_clients.c */