2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file dht/gnunet-service-dht_clients.c
23 * @brief GNUnet DHT service's client management code
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_constants.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet-service-dht.h"
33 #include "gnunet-service-dht_datacache.h"
34 #include "gnunet-service-dht_neighbours.h"
39 * Should routing details be logged to stderr (for debugging)?
41 #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__)
43 #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
47 * Struct containing information about a client,
48 * handle to connect to it, and any pending messages
49 * that need to be sent to it.
55 * Entry in the local forwarding map for a client's GET request.
57 struct ClientQueryRecord
61 * The key this request was about
63 struct GNUNET_HashCode key;
66 * Kept in a DLL with @e client.
68 struct ClientQueryRecord *next;
71 * Kept in a DLL with @e client.
73 struct ClientQueryRecord *prev;
76 * Client responsible for the request.
78 struct ClientHandle *ch;
81 * Extended query (see gnunet_block_lib.h), allocated at the end of this struct.
86 * Replies we have already seen for this request.
88 struct GNUNET_HashCode *seen_replies;
91 * Pointer to this nodes heap location in the retry-heap (for fast removal)
93 struct GNUNET_CONTAINER_HeapNode *hnode;
96 * What's the delay between re-try operations that we currently use for this
99 struct GNUNET_TIME_Relative retry_frequency;
102 * What's the next time we should re-try this request?
104 struct GNUNET_TIME_Absolute retry_time;
107 * The unique identifier of this request
112 * Number of bytes in xquery.
117 * Number of entries in 'seen_replies'.
119 unsigned int seen_replies_count;
122 * Desired replication level
124 uint32_t replication;
127 * Any message options for this request
129 uint32_t msg_options;
132 * The type for the data for the GET request.
134 enum GNUNET_BLOCK_Type type;
140 * Struct containing paremeters of monitoring requests.
142 struct ClientMonitorRecord
146 * Next element in DLL.
148 struct ClientMonitorRecord *next;
151 * Previous element in DLL.
153 struct ClientMonitorRecord *prev;
156 * Type of blocks that are of interest
158 enum GNUNET_BLOCK_Type type;
161 * Key of data of interest, NULL for all.
163 struct GNUNET_HashCode *key;
166 * Flag whether to notify about GET messages.
171 * Flag whether to notify about GET_REPONSE messages.
176 * Flag whether to notify about PUT messages.
181 * Client to notify of these requests.
183 struct ClientHandle *ch;
188 * Struct containing information about a client,
189 * handle to connect to it, and any pending messages
190 * that need to be sent to it.
195 * Linked list of active queries of this client.
197 struct ClientQueryRecord *cqr_head;
200 * Linked list of active queries of this client.
202 struct ClientQueryRecord *cqr_tail;
205 * The handle to this client
207 struct GNUNET_SERVICE_Client *client;
210 * The message queue to this client
212 struct GNUNET_MQ_Handle *mq;
217 * Our handle to the BLOCK library.
219 struct GNUNET_BLOCK_Context *GDS_block_context;
222 * Handle for the statistics service.
224 struct GNUNET_STATISTICS_Handle *GDS_stats;
227 * Handle for the service.
229 struct GNUNET_SERVICE_Handle *GDS_service;
232 * The configuration the DHT service is running with
234 const struct GNUNET_CONFIGURATION_Handle *GDS_cfg;
237 * List of active monitoring requests.
239 static struct ClientMonitorRecord *monitor_head;
242 * List of active monitoring requests.
244 static struct ClientMonitorRecord *monitor_tail;
247 * Hashmap for fast key based lookup, maps keys to `struct ClientQueryRecord` entries.
249 static struct GNUNET_CONTAINER_MultiHashMap *forward_map;
252 * Heap with all of our client's request, sorted by retry time (earliest on top).
254 static struct GNUNET_CONTAINER_Heap *retry_heap;
257 * Task that re-transmits requests (using retry_heap).
259 static struct GNUNET_SCHEDULER_Task *retry_task;
263 * Free data structures associated with the given query.
265 * @param record record to remove
268 remove_client_record (struct ClientQueryRecord *record)
270 struct ClientHandle *ch = record->ch;
272 GNUNET_CONTAINER_DLL_remove (ch->cqr_head,
275 GNUNET_assert (GNUNET_YES ==
276 GNUNET_CONTAINER_multihashmap_remove (forward_map,
279 if (NULL != record->hnode)
280 GNUNET_CONTAINER_heap_remove_node (record->hnode);
281 GNUNET_array_grow (record->seen_replies,
282 record->seen_replies_count,
284 GNUNET_free (record);
289 * Functions with this signature are called whenever a local client is
292 * @param cls closure (NULL for dht)
293 * @param client identification of the client
294 * @param mq message queue for talking to @a client
295 * @return our `struct ClientHandle` for @a client
298 client_connect_cb (void *cls,
299 struct GNUNET_SERVICE_Client *client,
300 struct GNUNET_MQ_Handle *mq)
302 struct ClientHandle *ch;
304 ch = GNUNET_new (struct ClientHandle);
312 * Functions with this signature are called whenever a client
313 * is disconnected on the network level.
315 * @param cls closure (NULL for dht)
316 * @param client identification of the client
317 * @param app_ctx our `struct ClientHandle` for @a client
320 client_disconnect_cb (void *cls,
321 struct GNUNET_SERVICE_Client *client,
324 struct ClientHandle *ch = app_ctx;
325 struct ClientQueryRecord *cqr;
326 struct ClientMonitorRecord *monitor;
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "Local client %p disconnects\n",
331 monitor = monitor_head;
332 while (NULL != monitor)
334 if (monitor->ch == ch)
336 struct ClientMonitorRecord *next;
338 next = monitor->next;
339 GNUNET_free_non_null (monitor->key);
340 GNUNET_CONTAINER_DLL_remove (monitor_head,
343 GNUNET_free (monitor);
348 monitor = monitor->next;
351 while (NULL != (cqr = ch->cqr_head))
352 remove_client_record (cqr);
358 * Route the given request via the DHT. This includes updating
359 * the bloom filter and retransmission times, building the P2P
360 * message and initiating the routing operation.
363 transmit_request (struct ClientQueryRecord *cqr)
365 int32_t reply_bf_mutator;
366 struct GNUNET_CONTAINER_BloomFilter *reply_bf;
367 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
369 GNUNET_STATISTICS_update (GDS_stats,
370 gettext_noop ("# GET requests from clients injected"),
374 (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
377 = GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator,
379 cqr->seen_replies_count);
381 = GNUNET_CONTAINER_bloomfilter_init (NULL,
383 GNUNET_CONSTANTS_BLOOMFILTER_K);
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "Initiating GET for %s, replication %u, already have %u replies\n",
386 GNUNET_h2s (&cqr->key),
388 cqr->seen_replies_count);
389 GDS_NEIGHBOURS_handle_get (cqr->type,
399 GNUNET_CONTAINER_bloomfilter_free (reply_bf);
400 GNUNET_CONTAINER_bloomfilter_free (peer_bf);
402 /* exponential back-off for retries.
403 * max GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD (15 min) */
404 cqr->retry_frequency = GNUNET_TIME_STD_BACKOFF (cqr->retry_frequency);
405 cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency);
410 * Task that looks at the #retry_heap and transmits all of the requests
411 * on the heap that are ready for transmission. Then re-schedules
412 * itself (unless the heap is empty).
417 transmit_next_request_task (void *cls)
419 struct ClientQueryRecord *cqr;
420 struct GNUNET_TIME_Relative delay;
423 while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
426 delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time);
427 if (delay.rel_value_us > 0)
430 GNUNET_CONTAINER_heap_insert (retry_heap,
432 cqr->retry_time.abs_value_us);
434 GNUNET_SCHEDULER_add_delayed (delay,
435 &transmit_next_request_task,
439 transmit_request (cqr);
441 = GNUNET_CONTAINER_heap_insert (retry_heap, cqr,
442 cqr->retry_time.abs_value_us);
448 * Check DHT PUT messages from the client.
450 * @param cls the client we received this message from
451 * @param dht_msg the actual message received
452 * @return #GNUNET_OK (always)
455 check_dht_local_put (void *cls,
456 const struct GNUNET_DHT_ClientPutMessage *dht_msg)
458 /* always well-formed */
464 * Handler for PUT messages.
466 * @param cls the client we received this message from
467 * @param dht_msg the actual message received
470 handle_dht_local_put (void *cls,
471 const struct GNUNET_DHT_ClientPutMessage *dht_msg)
473 struct ClientHandle *ch = cls;
474 struct GNUNET_CONTAINER_BloomFilter *peer_bf;
476 struct GNUNET_MQ_Envelope *env;
477 struct GNUNET_DHT_ClientPutConfirmationMessage *conf;
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 env = GNUNET_MQ_msg (conf,
537 GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK);
538 conf->reserved = htonl (0);
539 conf->unique_id = dht_msg->unique_id;
540 GNUNET_MQ_send (ch->mq,
542 GNUNET_SERVICE_client_continue (ch->client);
547 * Check DHT GET messages from the client.
549 * @param cls the client we received this message from
550 * @param message the actual message received
551 * @return #GNUNET_OK (always)
554 check_dht_local_get (void *cls,
555 const struct GNUNET_DHT_ClientGetMessage *get)
557 /* always well-formed */
563 * Handle a result from local datacache for a GET operation.
565 * @param cls the `struct ClientHandle` of the client doing the query
566 * @param type type of the block
567 * @param expiration_time when does the content expire
568 * @param key key for the content
569 * @param put_path_length number of entries in @a put_path
570 * @param put_path peers the original PUT traversed (if tracked)
571 * @param get_path_length number of entries in @a get_path
572 * @param get_path peers this reply has traversed so far (if tracked)
573 * @param data payload of the reply
574 * @param data_size number of bytes in @a data
577 handle_local_result (void *cls,
578 enum GNUNET_BLOCK_Type type,
579 struct GNUNET_TIME_Absolute expiration_time,
580 const struct GNUNET_HashCode *key,
581 unsigned int put_path_length,
582 const struct GNUNET_PeerIdentity *put_path,
583 unsigned int get_path_length,
584 const struct GNUNET_PeerIdentity *get_path,
588 // FIXME: this needs some clean up: inline the function,
589 // possibly avoid even looking up the client!
590 GDS_CLIENTS_handle_reply (expiration_time,
593 put_path_length, put_path,
600 * Handler for DHT GET messages from the client.
602 * @param cls the client we received this message from
603 * @param message the actual message received
606 handle_dht_local_get (void *cls,
607 const struct GNUNET_DHT_ClientGetMessage *get)
609 struct ClientHandle *ch = cls;
610 struct ClientQueryRecord *cqr;
615 size = ntohs (get->header.size);
616 xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage);
617 xquery = (const char *) &get[1];
618 GNUNET_STATISTICS_update (GDS_stats,
620 ("# GET requests received from clients"), 1,
622 LOG (GNUNET_ERROR_TYPE_DEBUG,
623 "Received GET request for %s from local client %p, xq: %.*s\n",
624 GNUNET_h2s (&get->key),
628 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
630 GNUNET_h2s_full (&get->key));
632 cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size);
635 cqr->xquery = (void *) &cqr[1];
636 GNUNET_memcpy (&cqr[1], xquery, xquery_size);
637 cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
638 cqr->retry_frequency = GNUNET_TIME_UNIT_SECONDS;
639 cqr->retry_time = GNUNET_TIME_absolute_get ();
640 cqr->unique_id = get->unique_id;
641 cqr->xquery_size = xquery_size;
642 cqr->replication = ntohl (get->desired_replication_level);
643 cqr->msg_options = ntohl (get->options);
644 cqr->type = ntohl (get->type);
645 GNUNET_CONTAINER_DLL_insert (ch->cqr_head,
648 GNUNET_CONTAINER_multihashmap_put (forward_map,
651 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
652 GDS_CLIENTS_process_get (ntohl (get->options),
655 ntohl (get->desired_replication_level),
657 GDS_NEIGHBOURS_get_id(),
659 /* start remote requests */
660 if (NULL != retry_task)
661 GNUNET_SCHEDULER_cancel (retry_task);
662 retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task,
664 /* perform local lookup */
665 GDS_DATACACHE_handle_get (&get->key,
671 &handle_local_result,
673 GNUNET_SERVICE_client_continue (ch->client);
678 * Closure for #find_by_unique_id().
680 struct FindByUniqueIdContext
683 * Where to store the result, if found.
685 struct ClientQueryRecord *cqr;
692 * Function called for each existing DHT record for the given
693 * query. Checks if it matches the UID given in the closure
694 * and if so returns the entry as a result.
696 * @param cls the search context
697 * @param key query for the lookup (not used)
698 * @param value the `struct ClientQueryRecord`
699 * @return #GNUNET_YES to continue iteration (result not yet found)
702 find_by_unique_id (void *cls,
703 const struct GNUNET_HashCode *key,
706 struct FindByUniqueIdContext *fui_ctx = cls;
707 struct ClientQueryRecord *cqr = value;
709 if (cqr->unique_id != fui_ctx->unique_id)
717 * Check "GET result seen" messages from the client.
719 * @param cls the client we received this message from
720 * @param message the actual message received
721 * @return #GNUNET_OK if @a seen is well-formed
724 check_dht_local_get_result_seen (void *cls,
725 const struct GNUNET_DHT_ClientGetResultSeenMessage *seen)
728 unsigned int hash_count;
730 size = ntohs (seen->header.size);
731 hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
732 if (size != sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage) + hash_count * sizeof (struct GNUNET_HashCode))
735 return GNUNET_SYSERR;
742 * Handler for "GET result seen" messages from the client.
744 * @param cls the client we received this message from
745 * @param message the actual message received
748 handle_dht_local_get_result_seen (void *cls,
749 const struct GNUNET_DHT_ClientGetResultSeenMessage *seen)
751 struct ClientHandle *ch = cls;
753 unsigned int hash_count;
754 unsigned int old_count;
755 const struct GNUNET_HashCode *hc;
756 struct FindByUniqueIdContext fui_ctx;
757 struct ClientQueryRecord *cqr;
759 size = ntohs (seen->header.size);
760 hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode);
761 hc = (const struct GNUNET_HashCode*) &seen[1];
762 fui_ctx.unique_id = seen->unique_id;
764 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
768 if (NULL == (cqr = fui_ctx.cqr))
771 GNUNET_SERVICE_client_drop (ch->client);
774 /* finally, update 'seen' list */
775 old_count = cqr->seen_replies_count;
776 GNUNET_array_grow (cqr->seen_replies,
777 cqr->seen_replies_count,
778 cqr->seen_replies_count + hash_count);
779 GNUNET_memcpy (&cqr->seen_replies[old_count],
781 sizeof (struct GNUNET_HashCode) * hash_count);
786 * Closure for #remove_by_unique_id().
788 struct RemoveByUniqueIdContext
791 * Client that issued the removal request.
793 struct ClientHandle *ch;
796 * Unique ID of the request.
803 * Iterator over hash map entries that frees all entries
804 * that match the given client and unique ID.
806 * @param cls unique ID and client to search for in source routes
807 * @param key current key code
808 * @param value value in the hash map, a ClientQueryRecord
809 * @return #GNUNET_YES (we should continue to iterate)
812 remove_by_unique_id (void *cls,
813 const struct GNUNET_HashCode *key,
816 const struct RemoveByUniqueIdContext *ctx = cls;
817 struct ClientQueryRecord *cqr = value;
819 if (cqr->unique_id != ctx->unique_id)
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Removing client %p's record for key %s (by unique id)\n",
825 remove_client_record (cqr);
831 * Handler for any generic DHT stop messages, calls the appropriate handler
832 * depending on message type (if processed locally)
834 * @param cls client we received this message from
835 * @param message the actual message received
839 handle_dht_local_get_stop (void *cls,
840 const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg)
842 struct ClientHandle *ch = cls;
843 struct RemoveByUniqueIdContext ctx;
845 GNUNET_STATISTICS_update (GDS_stats,
847 ("# GET STOP requests received from clients"), 1,
849 LOG (GNUNET_ERROR_TYPE_DEBUG,
850 "Received GET STOP request for %s from local client %p\n",
851 GNUNET_h2s (&dht_stop_msg->key),
854 ctx.unique_id = dht_stop_msg->unique_id;
855 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
857 &remove_by_unique_id,
859 GNUNET_SERVICE_client_continue (ch->client);
864 * Handler for monitor start messages
866 * @param cls the client we received this message from
867 * @param msg the actual message received
871 handle_dht_local_monitor (void *cls,
872 const struct GNUNET_DHT_MonitorStartStopMessage *msg)
874 struct ClientHandle *ch = cls;
875 struct ClientMonitorRecord *r;
877 r = GNUNET_new (struct ClientMonitorRecord);
879 r->type = ntohl (msg->type);
880 r->get = ntohs (msg->get);
881 r->get_resp = ntohs (msg->get_resp);
882 r->put = ntohs (msg->put);
883 if (0 == ntohs (msg->filter_key))
889 r->key = GNUNET_new (struct GNUNET_HashCode);
890 GNUNET_memcpy (r->key,
892 sizeof (struct GNUNET_HashCode));
894 GNUNET_CONTAINER_DLL_insert (monitor_head,
897 GNUNET_SERVICE_client_continue (ch->client);
902 * Handler for monitor stop messages
904 * @param cls the client we received this message from
905 * @param msg the actual message received
908 handle_dht_local_monitor_stop (void *cls,
909 const struct GNUNET_DHT_MonitorStartStopMessage *msg)
911 struct ClientHandle *ch = cls;
912 struct ClientMonitorRecord *r;
915 GNUNET_SERVICE_client_continue (ch->client);
916 for (r = monitor_head; NULL != r; r = r->next)
920 keys_match = (0 == ntohs(msg->filter_key));
924 keys_match = ( (0 != ntohs(msg->filter_key)) &&
927 sizeof(struct GNUNET_HashCode))) );
929 if ( (ch == r->ch) &&
930 (ntohl(msg->type) == r->type) &&
931 (r->get == msg->get) &&
932 (r->get_resp == msg->get_resp) &&
933 (r->put == msg->put) &&
936 GNUNET_CONTAINER_DLL_remove (monitor_head,
939 GNUNET_free_non_null (r->key);
941 return; /* Delete only ONE entry */
948 * Closure for #forward_reply()
950 struct ForwardReplyContext
954 * Expiration time of the reply.
956 struct GNUNET_TIME_Absolute expiration;
961 const struct GNUNET_PeerIdentity *get_path;
966 const struct GNUNET_PeerIdentity *put_path;
974 * Number of bytes in data.
979 * Number of entries in @e get_path.
981 unsigned int get_path_length;
984 * Number of entries in @e put_path.
986 unsigned int put_path_length;
991 enum GNUNET_BLOCK_Type type;
997 * Iterator over hash map entries that send a given reply to
998 * each of the matching clients. With some tricky recycling
1001 * @param cls the 'struct ForwardReplyContext'
1002 * @param key current key
1003 * @param value value in the hash map, a ClientQueryRecord
1004 * @return #GNUNET_YES (we should continue to iterate),
1005 * if the result is mal-formed, #GNUNET_NO
1008 forward_reply (void *cls,
1009 const struct GNUNET_HashCode *key,
1012 struct ForwardReplyContext *frc = cls;
1013 struct ClientQueryRecord *record = value;
1014 struct GNUNET_MQ_Envelope *env;
1015 struct GNUNET_DHT_ClientResultMessage *reply;
1016 enum GNUNET_BLOCK_EvaluationResult eval;
1018 struct GNUNET_HashCode ch;
1019 struct GNUNET_PeerIdentity *paths;
1021 LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
1022 "CLIENT-RESULT %s\n",
1023 GNUNET_h2s_full (key));
1024 if ( (record->type != GNUNET_BLOCK_TYPE_ANY) &&
1025 (record->type != frc->type))
1027 LOG (GNUNET_ERROR_TYPE_DEBUG,
1028 "Record type missmatch, not passing request for key %s to local client\n",
1030 GNUNET_STATISTICS_update (GDS_stats,
1032 ("# Key match, type mismatches in REPLY to CLIENT"),
1034 return GNUNET_YES; /* type mismatch */
1036 GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch);
1037 for (unsigned int i = 0; i < record->seen_replies_count; i++)
1038 if (0 == memcmp (&record->seen_replies[i],
1040 sizeof (struct GNUNET_HashCode)))
1042 LOG (GNUNET_ERROR_TYPE_DEBUG,
1043 "Duplicate reply, not passing request for key %s to local client\n",
1045 GNUNET_STATISTICS_update (GDS_stats,
1047 ("# Duplicate REPLIES to CLIENT request dropped"),
1049 return GNUNET_YES; /* duplicate */
1052 = GNUNET_BLOCK_evaluate (GDS_block_context,
1054 GNUNET_BLOCK_EO_NONE,
1059 record->xquery_size,
1062 LOG (GNUNET_ERROR_TYPE_DEBUG,
1063 "Evaluation result is %d for key %s for local client's query\n",
1068 case GNUNET_BLOCK_EVALUATION_OK_LAST:
1069 do_free = GNUNET_YES;
1071 case GNUNET_BLOCK_EVALUATION_OK_MORE:
1072 GNUNET_array_append (record->seen_replies,
1073 record->seen_replies_count,
1075 do_free = GNUNET_NO;
1077 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
1078 /* should be impossible to encounter here */
1081 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
1082 GNUNET_break_op (0);
1084 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
1087 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
1090 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
1092 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
1093 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1094 _("Unsupported block type (%u) in request!\n"), record->type);
1100 GNUNET_STATISTICS_update (GDS_stats,
1101 gettext_noop ("# RESULTS queued for clients"),
1104 env = GNUNET_MQ_msg_extra (reply,
1106 (frc->get_path_length + frc->put_path_length) * sizeof (struct GNUNET_PeerIdentity),
1107 GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT);
1108 reply->type = htonl (frc->type);
1109 reply->get_path_length = htonl (frc->get_path_length);
1110 reply->put_path_length = htonl (frc->put_path_length);
1111 reply->unique_id = record->unique_id;
1112 reply->expiration = GNUNET_TIME_absolute_hton (frc->expiration);
1114 paths = (struct GNUNET_PeerIdentity *) &reply[1];
1115 GNUNET_memcpy (paths,
1117 sizeof (struct GNUNET_PeerIdentity) * frc->put_path_length);
1118 GNUNET_memcpy (&paths[frc->put_path_length],
1120 sizeof (struct GNUNET_PeerIdentity) * frc->get_path_length);
1121 GNUNET_memcpy (&paths[frc->get_path_length + frc->put_path_length],
1124 LOG (GNUNET_ERROR_TYPE_DEBUG,
1125 "Sending reply to query %s for client %p\n",
1127 record->ch->client);
1128 GNUNET_MQ_send (record->ch->mq,
1130 if (GNUNET_YES == do_free)
1131 remove_client_record (record);
1137 * Handle a reply we've received from another peer. If the reply
1138 * matches any of our pending queries, forward it to the respective
1141 * @param expiration when will the reply expire
1142 * @param key the query this reply is for
1143 * @param get_path_length number of peers in @a get_path
1144 * @param get_path path the reply took on get
1145 * @param put_path_length number of peers in @a put_path
1146 * @param put_path path the reply took on put
1147 * @param type type of the reply
1148 * @param data_size number of bytes in @a data
1149 * @param data application payload data
1152 GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
1153 const struct GNUNET_HashCode *key,
1154 unsigned int get_path_length,
1155 const struct GNUNET_PeerIdentity *get_path,
1156 unsigned int put_path_length,
1157 const struct GNUNET_PeerIdentity *put_path,
1158 enum GNUNET_BLOCK_Type type,
1162 struct ForwardReplyContext frc;
1165 msize = sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size +
1166 (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
1167 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1172 if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map,
1175 LOG (GNUNET_ERROR_TYPE_DEBUG,
1176 "No matching client for reply for key %s\n",
1178 GNUNET_STATISTICS_update (GDS_stats,
1179 gettext_noop ("# REPLIES ignored for CLIENTS (no match)"),
1182 return; /* no matching request, fast exit! */
1184 frc.expiration = expiration;
1185 frc.get_path = get_path;
1186 frc.put_path = put_path;
1188 frc.data_size = data_size;
1189 frc.get_path_length = get_path_length;
1190 frc.put_path_length = put_path_length;
1192 LOG (GNUNET_ERROR_TYPE_DEBUG,
1193 "Forwarding reply for key %s to client\n",
1195 GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
1204 * Check if some client is monitoring GET messages and notify
1205 * them in that case.
1207 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
1208 * @param type The type of data in the request.
1209 * @param hop_count Hop count so far.
1210 * @param path_length number of entries in path (or 0 if not recorded).
1211 * @param path peers on the GET path (or NULL if not recorded).
1212 * @param desired_replication_level Desired replication level.
1213 * @param key Key of the requested data.
1216 GDS_CLIENTS_process_get (uint32_t options,
1217 enum GNUNET_BLOCK_Type type,
1219 uint32_t desired_replication_level,
1220 unsigned int path_length,
1221 const struct GNUNET_PeerIdentity *path,
1222 const struct GNUNET_HashCode * key)
1224 struct ClientMonitorRecord *m;
1225 struct ClientHandle **cl;
1226 unsigned int cl_size;
1230 for (m = monitor_head; NULL != m; m = m->next)
1232 if ( ( (GNUNET_BLOCK_TYPE_ANY == m->type) ||
1233 (m->type == type) ) &&
1234 ( (NULL == m->key) ||
1237 sizeof(struct GNUNET_HashCode))) ) )
1239 struct GNUNET_MQ_Envelope *env;
1240 struct GNUNET_DHT_MonitorGetMessage *mmsg;
1241 struct GNUNET_PeerIdentity *msg_path;
1245 /* Don't send duplicates */
1246 for (i = 0; i < cl_size; i++)
1251 GNUNET_array_append (cl,
1255 msize = path_length * sizeof (struct GNUNET_PeerIdentity);
1256 env = GNUNET_MQ_msg_extra (mmsg,
1258 GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET);
1259 mmsg->options = htonl(options);
1260 mmsg->type = htonl(type);
1261 mmsg->hop_count = htonl(hop_count);
1262 mmsg->desired_replication_level = htonl(desired_replication_level);
1263 mmsg->get_path_length = htonl(path_length);
1265 msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1266 GNUNET_memcpy (msg_path,
1268 path_length * sizeof (struct GNUNET_PeerIdentity));
1269 GNUNET_MQ_send (m->ch->mq,
1273 GNUNET_free_non_null (cl);
1278 * Check if some client is monitoring GET RESP messages and notify
1279 * them in that case.
1281 * @param type The type of data in the result.
1282 * @param get_path Peers on GET path (or NULL if not recorded).
1283 * @param get_path_length number of entries in get_path.
1284 * @param put_path peers on the PUT path (or NULL if not recorded).
1285 * @param put_path_length number of entries in get_path.
1286 * @param exp Expiration time of the data.
1287 * @param key Key of the data.
1288 * @param data Pointer to the result data.
1289 * @param size Number of bytes in @a data.
1292 GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type,
1293 const struct GNUNET_PeerIdentity *get_path,
1294 unsigned int get_path_length,
1295 const struct GNUNET_PeerIdentity *put_path,
1296 unsigned int put_path_length,
1297 struct GNUNET_TIME_Absolute exp,
1298 const struct GNUNET_HashCode * key,
1302 struct ClientMonitorRecord *m;
1303 struct ClientHandle **cl;
1304 unsigned int cl_size;
1308 for (m = monitor_head; NULL != m; m = m->next)
1310 if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
1312 memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
1314 struct GNUNET_MQ_Envelope *env;
1315 struct GNUNET_DHT_MonitorGetRespMessage *mmsg;
1316 struct GNUNET_PeerIdentity *path;
1320 /* Don't send duplicates */
1321 for (i = 0; i < cl_size; i++)
1326 GNUNET_array_append (cl,
1331 msize += (get_path_length + put_path_length)
1332 * sizeof (struct GNUNET_PeerIdentity);
1333 env = GNUNET_MQ_msg_extra (mmsg,
1335 GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP);
1336 mmsg->type = htonl(type);
1337 mmsg->put_path_length = htonl(put_path_length);
1338 mmsg->get_path_length = htonl(get_path_length);
1339 mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
1341 path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1342 GNUNET_memcpy (path,
1344 put_path_length * sizeof (struct GNUNET_PeerIdentity));
1345 GNUNET_memcpy (path,
1347 get_path_length * sizeof (struct GNUNET_PeerIdentity));
1348 GNUNET_memcpy (&path[get_path_length],
1351 GNUNET_MQ_send (m->ch->mq,
1355 GNUNET_free_non_null (cl);
1360 * Check if some client is monitoring PUT messages and notify
1361 * them in that case.
1363 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
1364 * @param type The type of data in the request.
1365 * @param hop_count Hop count so far.
1366 * @param path_length number of entries in path (or 0 if not recorded).
1367 * @param path peers on the PUT path (or NULL if not recorded).
1368 * @param desired_replication_level Desired replication level.
1369 * @param exp Expiration time of the data.
1370 * @param key Key under which data is to be stored.
1371 * @param data Pointer to the data carried.
1372 * @param size Number of bytes in data.
1375 GDS_CLIENTS_process_put (uint32_t options,
1376 enum GNUNET_BLOCK_Type type,
1378 uint32_t desired_replication_level,
1379 unsigned int path_length,
1380 const struct GNUNET_PeerIdentity *path,
1381 struct GNUNET_TIME_Absolute exp,
1382 const struct GNUNET_HashCode *key,
1386 struct ClientMonitorRecord *m;
1387 struct ClientHandle **cl;
1388 unsigned int cl_size;
1392 for (m = monitor_head; NULL != m; m = m->next)
1394 if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) &&
1396 memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0))
1398 struct GNUNET_MQ_Envelope *env;
1399 struct GNUNET_DHT_MonitorPutMessage *mmsg;
1400 struct GNUNET_PeerIdentity *msg_path;
1404 /* Don't send duplicates */
1405 for (i = 0; i < cl_size; i++)
1410 GNUNET_array_append (cl,
1415 msize += path_length * sizeof (struct GNUNET_PeerIdentity);
1416 env = GNUNET_MQ_msg_extra (mmsg,
1418 GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT);
1419 mmsg->options = htonl(options);
1420 mmsg->type = htonl(type);
1421 mmsg->hop_count = htonl(hop_count);
1422 mmsg->desired_replication_level = htonl (desired_replication_level);
1423 mmsg->put_path_length = htonl (path_length);
1425 mmsg->expiration_time = GNUNET_TIME_absolute_hton (exp);
1426 msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
1427 GNUNET_memcpy (msg_path,
1429 path_length * sizeof (struct GNUNET_PeerIdentity));
1430 GNUNET_memcpy (&msg_path[path_length],
1433 GNUNET_MQ_send (m->ch->mq,
1437 GNUNET_free_non_null (cl);
1442 * Initialize client subsystem.
1444 * @param server the initialized server
1450 = GNUNET_CONTAINER_multihashmap_create (1024,
1453 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1458 * Shutdown client subsystem.
1463 if (NULL != retry_task)
1465 GNUNET_SCHEDULER_cancel (retry_task);
1472 * Define "main" method using service macro.
1474 * @param name name of the service, i.e. "dht" or "xdht"
1475 * @param run name of the initializaton method for the service
1477 #define GDS_DHT_SERVICE_INIT(name,run) \
1478 GNUNET_SERVICE_MAIN \
1480 GNUNET_SERVICE_OPTION_NONE, \
1482 &client_connect_cb, \
1483 &client_disconnect_cb, \
1485 GNUNET_MQ_hd_var_size (dht_local_put, \
1486 GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, \
1487 struct GNUNET_DHT_ClientPutMessage, \
1489 GNUNET_MQ_hd_var_size (dht_local_get, \
1490 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, \
1491 struct GNUNET_DHT_ClientGetMessage, \
1493 GNUNET_MQ_hd_fixed_size (dht_local_get_stop, \
1494 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP, \
1495 struct GNUNET_DHT_ClientGetStopMessage, \
1497 GNUNET_MQ_hd_fixed_size (dht_local_monitor, \
1498 GNUNET_MESSAGE_TYPE_DHT_MONITOR_START, \
1499 struct GNUNET_DHT_MonitorStartStopMessage, \
1501 GNUNET_MQ_hd_fixed_size (dht_local_monitor_stop, \
1502 GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP, \
1503 struct GNUNET_DHT_MonitorStartStopMessage, \
1505 GNUNET_MQ_hd_var_size (dht_local_get_result_seen, \
1506 GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN, \
1507 struct GNUNET_DHT_ClientGetResultSeenMessage , \
1509 GNUNET_MQ_handler_end ())
1513 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1515 void __attribute__ ((destructor))
1518 if (NULL != retry_heap)
1520 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap));
1521 GNUNET_CONTAINER_heap_destroy (retry_heap);
1524 if (NULL != forward_map)
1526 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map));
1527 GNUNET_CONTAINER_multihashmap_destroy (forward_map);
1532 /* end of gnunet-service-dht_clients.c */