2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2015 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.
21 * @file dht/gnunet-service-xdht_datacache.c
22 * @brief GNUnet DHT service's datacache integration
23 * @author Christian Grothoff
24 * @author Nathan Evans
27 #include "gnunet_datacache_lib.h"
28 #include "gnunet-service-xdht_clients.h"
29 #include "gnunet-service-xdht_datacache.h"
30 #include "gnunet-service-xdht_routing.h"
31 #include "gnunet-service-xdht_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 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 get path.
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 @a 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 @a 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,
257 ctx->get_path_length,
262 GNUNET_free_non_null (get_path);
264 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
265 GNUNET_STATISTICS_update (GDS_stats,
267 ("# Duplicate RESULTS found in datacache"), 1,
270 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
271 GNUNET_STATISTICS_update (GDS_stats,
273 ("# Invalid RESULTS found in datacache"), 1,
276 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
277 GNUNET_STATISTICS_update (GDS_stats,
279 ("# Irrelevant RESULTS found in datacache"), 1,
282 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
285 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
287 return GNUNET_SYSERR;
288 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
289 GNUNET_STATISTICS_update (GDS_stats,
291 ("# Unsupported RESULTS found in datacache"), 1,
293 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
294 _("Unsupported block type (%u) in local response!\n"),
299 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
304 * Handle a GET request we've received from another peer.
306 * @param key the query
307 * @param type requested data type
308 * @param xquery extended query
309 * @param xquery_size number of bytes in @a xquery
310 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
311 * @param reply_bf_mutator mutation value for @a reply_bf
312 * @return evaluation result for the local replies
313 * @get_path_length Total number of peers in get path
314 * @get_path Peers in get path.
316 enum GNUNET_BLOCK_EvaluationResult
317 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
318 enum GNUNET_BLOCK_Type type,
321 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
322 uint32_t reply_bf_mutator,
323 uint32_t get_path_length,
324 struct GNUNET_PeerIdentity *get_path,
325 struct GNUNET_PeerIdentity *next_hop,
326 struct GNUNET_PeerIdentity *source_peer)
328 struct GetRequestContext ctx;
331 if (datacache == NULL)
332 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
333 GNUNET_STATISTICS_update (GDS_stats,
334 gettext_noop ("# GET requests given to datacache"),
336 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
339 ctx.xquery_size = xquery_size;
340 ctx.reply_bf = reply_bf;
341 ctx.reply_bf_mutator = reply_bf_mutator;
342 ctx.get_path_length = get_path_length;
344 if (NULL != next_hop)
345 ctx.next_hop = *next_hop;
350 if (NULL != get_path)
352 while (i < get_path_length)
354 struct GetPath *element;
355 element = GNUNET_new (struct GetPath);
356 element->next = NULL;
357 element->prev = NULL;
358 element->peer = get_path[i];
359 GNUNET_CONTAINER_DLL_insert_tail (ctx.head, ctx.tail, element);
364 r = GNUNET_DATACACHE_get (datacache,
367 &datacache_get_iterator,
369 DEBUG ("DATACACHE_GET for key %s completed (%d). %u results found.\n",
378 * Initialize datacache subsystem.
381 GDS_DATACACHE_init ()
383 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
388 * Shutdown datacache subsystem.
391 GDS_DATACACHE_done ()
393 if (NULL != datacache)
395 GNUNET_DATACACHE_destroy (datacache);
401 /* end of gnunet-service-xdht_datacache.c */