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-dht_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-dht_clients.h"
30 #include "gnunet-service-dht_datacache.h"
31 #include "gnunet-service-dht_routing.h"
32 #include "gnunet-service-dht.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dhtcache",__VA_ARGS__)
38 * Handle to the datacache service (for inserting/retrieving data)
40 static struct GNUNET_DATACACHE_Handle *datacache;
44 * Handle a datum we've received from another peer. Cache if
47 * @param expiration when will the reply expire
48 * @param key the query this reply is for
49 * @param put_path_length number of peers in @a put_path
50 * @param put_path path the reply took on put
51 * @param type type of the reply
52 * @param data_size number of bytes in @a data
53 * @param data application payload data
56 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
57 const struct GNUNET_HashCode *key,
58 unsigned int put_path_length,
59 const struct GNUNET_PeerIdentity *put_path,
60 enum GNUNET_BLOCK_Type type,
66 if (NULL == datacache)
68 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
69 _("%s request received, but have no datacache!\n"), "PUT");
72 if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
77 /* Put size is actual data size plus struct overhead plus path length (if any) */
78 GNUNET_STATISTICS_update (GDS_stats,
79 gettext_noop ("# ITEMS stored in datacache"), 1,
81 r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
82 put_path_length, put_path);
83 LOG (GNUNET_ERROR_TYPE_DEBUG,
84 "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
85 GNUNET_h2s (key), data_size, r, put_path_length);
90 * Context containing information about a GET request.
92 struct GetRequestContext
95 * extended query (see gnunet_block_lib.h).
100 * Bloomfilter to filter out duplicate replies (updated)
102 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
105 * The key this request was about
107 struct GNUNET_HashCode key;
110 * Number of bytes in xquery.
115 * Mutator value for the reply_bf, see gnunet_block_lib.h
117 uint32_t reply_bf_mutator;
120 * Return value to give back.
122 enum GNUNET_BLOCK_EvaluationResult eval;
127 * Iterator for local get request results,
129 * @param cls closure for iterator, a DatacacheGetContext
130 * @param exp when does this value expire?
131 * @param key the key this data is stored under
132 * @param size the size of the data identified by key
133 * @param data the actual data
134 * @param type the type of the @a data
135 * @param put_path_length number of peers in @a put_path
136 * @param put_path path the reply took on put
137 * @return #GNUNET_OK to continue iteration, anything else
141 datacache_get_iterator (void *cls,
142 const struct GNUNET_HashCode *key,
145 enum GNUNET_BLOCK_Type type,
146 struct GNUNET_TIME_Absolute exp,
147 unsigned int put_path_length,
148 const struct GNUNET_PeerIdentity *put_path)
150 struct GetRequestContext *ctx = cls;
151 enum GNUNET_BLOCK_EvaluationResult eval;
154 GNUNET_BLOCK_evaluate (GDS_block_context,
156 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
159 ctx->reply_bf_mutator,
164 LOG (GNUNET_ERROR_TYPE_DEBUG,
165 "Found reply for query %s in datacache, evaluation result is %d\n",
171 case GNUNET_BLOCK_EVALUATION_OK_MORE:
172 case GNUNET_BLOCK_EVALUATION_OK_LAST:
173 /* forward to local clients */
174 GNUNET_STATISTICS_update (GDS_stats,
176 ("# Good RESULTS found in datacache"), 1,
178 GDS_CLIENTS_handle_reply (exp, key,
180 put_path_length, put_path,
183 /* forward to other peers */
184 GDS_ROUTING_process (type,
187 put_path_length, put_path,
191 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
192 GNUNET_STATISTICS_update (GDS_stats,
193 gettext_noop ("# Duplicate RESULTS found in datacache"),
197 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
198 GNUNET_STATISTICS_update (GDS_stats,
199 gettext_noop ("# Invalid RESULTS found in datacache"),
203 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
204 GNUNET_STATISTICS_update (GDS_stats,
205 gettext_noop ("# Irrelevant RESULTS found in datacache"),
209 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
212 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
214 return GNUNET_SYSERR;
215 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
216 GNUNET_STATISTICS_update (GDS_stats,
217 gettext_noop ("# Unsupported RESULTS found in datacache"),
220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
221 _("Unsupported block type (%u) in local response!\n"),
225 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
230 * Handle a GET request we've received from another peer.
232 * @param key the query
233 * @param type requested data type
234 * @param xquery extended query
235 * @param xquery_size number of bytes in @a xquery
236 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
237 * @param reply_bf_mutator mutation value for @a reply_bf
238 * @return evaluation result for the local replies
240 enum GNUNET_BLOCK_EvaluationResult
241 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
242 enum GNUNET_BLOCK_Type type,
245 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
246 uint32_t reply_bf_mutator)
248 struct GetRequestContext ctx;
251 if (NULL == datacache)
252 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
253 GNUNET_STATISTICS_update (GDS_stats,
254 gettext_noop ("# GET requests given to datacache"),
257 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
260 ctx.xquery_size = xquery_size;
261 ctx.reply_bf = reply_bf;
262 ctx.reply_bf_mutator = reply_bf_mutator;
263 r = GNUNET_DATACACHE_get (datacache,
266 &datacache_get_iterator,
268 LOG (GNUNET_ERROR_TYPE_DEBUG,
269 "DATACACHE GET for key %s completed (%d). %u results found.\n",
278 * Initialize datacache subsystem.
281 GDS_DATACACHE_init ()
283 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
288 * Shutdown datacache subsystem.
291 GDS_DATACACHE_done ()
293 if (NULL != datacache)
295 GNUNET_DATACACHE_destroy (datacache);
301 /* end of gnunet-service-dht_datacache.c */