2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2015 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.
22 * @file dht/gnunet-service-wdht_datacache.c
23 * @brief GNUnet DHT service's datacache integration
24 * @author Christian Grothoff
25 * @author Nathan Evans
28 #include "gnunet_datacache_lib.h"
29 #include "gnunet-service-wdht_clients.h"
30 #include "gnunet-service-wdht_datacache.h"
31 #include "gnunet-service-wdht_neighbours.h"
32 #include "gnunet-service-dht.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__VA_ARGS__)
37 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
40 * Handle to the datacache service (for inserting/retrieving data)
42 static struct GNUNET_DATACACHE_Handle *datacache;
46 * Handle a datum we've received from another peer. Cache if
49 * @param expiration when will the reply expire
50 * @param key the query this reply is for
51 * @param put_path_length number of peers in @a put_path
52 * @param put_path path the reply took on put
53 * @param type type of the reply
54 * @param data_size number of bytes in @a data
55 * @param data application payload data
58 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
59 const struct GNUNET_HashCode *key,
60 unsigned int put_path_length,
61 const struct GNUNET_PeerIdentity *put_path,
62 enum GNUNET_BLOCK_Type type,
68 if (NULL == datacache)
70 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
71 _("PUT request received, but have no datacache!\n"));
74 if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
80 /* Put size is actual data size plus struct overhead plus path length (if any) */
81 r = GNUNET_DATACACHE_put (datacache,
90 GNUNET_STATISTICS_update (GDS_stats,
91 gettext_noop ("# ITEMS stored in datacache"), 1,
93 LOG (GNUNET_ERROR_TYPE_DEBUG,
94 "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
103 * List of peers in the get path.
108 * Pointer to next item in the list
110 struct GetPath *next;
113 * Pointer to previous item in the list
115 struct GetPath *prev;
118 * An element in the get path.
120 struct GNUNET_PeerIdentity peer;
125 * Context containing information about a GET request.
127 struct GetRequestContext
130 * extended query (see gnunet_block_lib.h).
135 * Bloomfilter to filter out duplicate replies (updated)
137 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
140 * The key this request was about
142 struct GNUNET_HashCode key;
145 * Number of bytes in @e xquery.
150 * Mutator value for the @e reply_bf, see gnunet_block_lib.h
152 uint32_t reply_bf_mutator;
155 * Total number of peers in @e head.
157 unsigned int get_path_length;
160 * Return value to give back.
162 enum GNUNET_BLOCK_EvaluationResult eval;
165 * Peeer which has the data for the key.
167 struct GNUNET_PeerIdentity source_peer;
170 * Next hop to forward the get result to.
172 struct GNUNET_PeerIdentity next_hop;
177 struct GetPath *head;
182 struct GetPath *tail;
189 * Iterator for local get request results,
191 * @param cls closure for iterator, a `struct GetRequestContext`
192 * @param key the key this data is stored under
193 * @param size the size of the data identified by key
194 * @param data the actual data
195 * @param type the type of the data
196 * @param exp when does this value expire?
197 * @param put_path_length number of peers in @a put_path
198 * @param put_path path the reply took on put
199 * @return #GNUNET_OK to continue iteration, anything else
203 datacache_get_iterator (void *cls,
204 const struct GNUNET_HashCode *key,
207 enum GNUNET_BLOCK_Type type,
208 struct GNUNET_TIME_Absolute exp,
209 unsigned int put_path_length,
210 const struct GNUNET_PeerIdentity *put_path)
212 struct GetRequestContext *ctx = cls;
213 enum GNUNET_BLOCK_EvaluationResult eval;
216 GNUNET_BLOCK_evaluate (GDS_block_context,
218 GNUNET_BLOCK_EO_NONE,
221 ctx->reply_bf_mutator,
226 LOG (GNUNET_ERROR_TYPE_DEBUG,
227 "Found reply for query %s in datacache, evaluation result is %d\n",
228 GNUNET_h2s (key), (int) eval);
233 case GNUNET_BLOCK_EVALUATION_OK_MORE:
234 case GNUNET_BLOCK_EVALUATION_OK_LAST:
235 /* forward to local clients */
236 GNUNET_STATISTICS_update (GDS_stats,
238 ("# Good RESULTS found in datacache"), 1,
240 struct GNUNET_PeerIdentity *get_path;
241 get_path = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
242 ctx->get_path_length);
243 struct GetPath *iterator;
244 iterator = ctx->head;
246 while (i < ctx->get_path_length)
248 get_path[i] = iterator->peer;
250 iterator = iterator->next;
252 GDS_NEIGHBOURS_send_get_result (key,type, &(ctx->next_hop),&(ctx->source_peer),
253 put_path_length, put_path, ctx->get_path_length,
254 get_path, exp, data, size );
255 GNUNET_free_non_null (get_path);
258 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
259 GNUNET_STATISTICS_update (GDS_stats,
261 ("# Duplicate RESULTS found in datacache"), 1,
264 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
265 GNUNET_STATISTICS_update (GDS_stats,
267 ("# Invalid RESULTS found in datacache"), 1,
270 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
271 GNUNET_STATISTICS_update (GDS_stats,
273 ("# Irrelevant RESULTS found in datacache"), 1,
276 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
279 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
281 return GNUNET_SYSERR;
282 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
283 GNUNET_STATISTICS_update (GDS_stats,
285 ("# Unsupported RESULTS found in datacache"), 1,
287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
288 _("Unsupported block type (%u) in local response!\n"), type);
292 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
297 * Handle a GET request we've received from another peer.
299 * @param key the query
300 * @param type requested data type
301 * @param xquery extended query
302 * @param xquery_size number of bytes in @a xquery
303 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
304 * @param reply_bf_mutator mutation value for @a reply_bf
305 * @param get_path_length Total number of peers in @a get_path
306 * @param get_path Peers query has traversed during GET
307 * @return evaluation result for the local replies
309 enum GNUNET_BLOCK_EvaluationResult
310 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
311 enum GNUNET_BLOCK_Type type,
314 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
315 uint32_t reply_bf_mutator,
316 uint32_t get_path_length,
317 struct GNUNET_PeerIdentity *get_path,
318 struct GNUNET_PeerIdentity *next_hop,
319 struct GNUNET_PeerIdentity *source_peer)
321 struct GetRequestContext ctx;
324 if (datacache == NULL)
325 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
326 GNUNET_STATISTICS_update (GDS_stats,
327 gettext_noop ("# GET requests given to datacache"),
329 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
332 ctx.xquery_size = xquery_size;
333 ctx.reply_bf = reply_bf;
334 ctx.reply_bf_mutator = reply_bf_mutator;
335 ctx.get_path_length = get_path_length;
337 if (NULL != next_hop)
338 ctx.next_hop = *next_hop;
343 if (NULL != get_path)
345 while (i < get_path_length)
347 struct GetPath *element;
348 element = GNUNET_new (struct GetPath);
349 element->next = NULL;
350 element->prev = NULL;
351 element->peer = get_path[i];
352 GNUNET_CONTAINER_DLL_insert_tail (ctx.head, ctx.tail, element);
357 r = GNUNET_DATACACHE_get (datacache,
360 &datacache_get_iterator,
362 DEBUG ("DATACACHE_GET for key %s completed (%d). %u results found.\n",
371 * Function called with a random element from the datacache.
372 * Stores the key in the closure.
374 * @param cls a `struct GNUNET_HashCode *`, where to store the @a key
375 * @param key key for the content
376 * @param data_size number of bytes in @a data
377 * @param data content stored
378 * @param type type of the content
379 * @param exp when will the content expire?
380 * @param path_info_len number of entries in @a path_info
381 * @param path_info a path through the network
382 * @return #GNUNET_OK to continue iterating, #GNUNET_SYSERR to abort
385 datacache_random_iterator (void *cls,
386 const struct GNUNET_HashCode *key,
389 enum GNUNET_BLOCK_Type type,
390 struct GNUNET_TIME_Absolute exp,
391 unsigned int path_info_len,
392 const struct GNUNET_PeerIdentity *path_info)
394 struct GNUNET_HashCode *dest = cls;
397 return GNUNET_OK; /* should actually not matter which we return */
402 * Obtain a random key from the datacache.
403 * Used by Whanau for load-balancing.
405 * @param[out] key where to store the key of a random element,
406 * randomized by PRNG if datacache is empty
407 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the datacache is empty
410 GDS_DATACACHE_get_random_key (struct GNUNET_HashCode *key)
413 GNUNET_DATACACHE_get_random (datacache,
414 &datacache_random_iterator,
417 /* randomize key in this case */
418 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
420 return GNUNET_SYSERR;
427 * Initialize datacache subsystem.
430 GDS_DATACACHE_init ()
432 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
437 * Shutdown datacache subsystem.
440 GDS_DATACACHE_done ()
442 if (NULL != datacache)
444 GNUNET_DATACACHE_destroy (datacache);
450 /* end of gnunet-service-wdht_datacache.c */