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-dht_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-dht_datacache.h"
29 #include "gnunet-service-dht_routing.h"
30 #include "gnunet-service-dht.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dhtcache",__VA_ARGS__)
36 * Handle to the datacache service (for inserting/retrieving data)
38 static struct GNUNET_DATACACHE_Handle *datacache;
42 * Handle a datum we've received from another peer. Cache if
45 * @param expiration when will the reply expire
46 * @param key the query this reply is for
47 * @param put_path_length number of peers in @a put_path
48 * @param put_path path the reply took on put
49 * @param type type of the reply
50 * @param data_size number of bytes in @a data
51 * @param data application payload data
54 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
55 const struct GNUNET_HashCode *key,
56 unsigned int put_path_length,
57 const struct GNUNET_PeerIdentity *put_path,
58 enum GNUNET_BLOCK_Type type,
64 if (NULL == datacache)
66 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
67 _("%s request received, but have no datacache!\n"), "PUT");
70 if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
75 /* Put size is actual data size plus struct overhead plus path length (if any) */
76 GNUNET_STATISTICS_update (GDS_stats,
77 gettext_noop ("# ITEMS stored in datacache"), 1,
79 r = GNUNET_DATACACHE_put (datacache,
87 LOG (GNUNET_ERROR_TYPE_DEBUG,
88 "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
97 * Context containing information about a GET request.
99 struct GetRequestContext
102 * extended query (see gnunet_block_lib.h).
107 * Bloomfilter to filter out duplicate replies (updated)
109 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
112 * The key this request was about
114 struct GNUNET_HashCode key;
117 * Number of bytes in xquery.
122 * Mutator value for the @e reply_bf, see gnunet_block_lib.h
124 uint32_t reply_bf_mutator;
127 * Return value to give back.
129 enum GNUNET_BLOCK_EvaluationResult eval;
134 * Iterator for local get request results,
136 * @param cls closure for iterator, a `struct GetRequestContext`
137 * @param exp when does this value expire?
138 * @param key the key this data is stored under
139 * @param size the size of the data identified by key
140 * @param data the actual data
141 * @param type the type of the @a data
142 * @param put_path_length number of peers in @a put_path
143 * @param put_path path the reply took on put
144 * @return #GNUNET_OK to continue iteration, anything else
148 datacache_get_iterator (void *cls,
149 const struct GNUNET_HashCode *key,
152 enum GNUNET_BLOCK_Type type,
153 struct GNUNET_TIME_Absolute exp,
154 unsigned int put_path_length,
155 const struct GNUNET_PeerIdentity *put_path)
157 struct GetRequestContext *ctx = cls;
158 enum GNUNET_BLOCK_EvaluationResult eval;
161 GNUNET_BLOCK_evaluate (GDS_block_context,
163 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
166 ctx->reply_bf_mutator,
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 "Found reply for query %s in datacache, evaluation result is %d\n",
178 case GNUNET_BLOCK_EVALUATION_OK_MORE:
179 case GNUNET_BLOCK_EVALUATION_OK_LAST:
180 /* forward to local clients */
181 GNUNET_STATISTICS_update (GDS_stats,
183 ("# Good RESULTS found in datacache"), 1,
185 GDS_CLIENTS_handle_reply (exp, key,
187 put_path_length, put_path,
190 /* forward to other peers */
191 GDS_ROUTING_process (type,
194 put_path_length, put_path,
198 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
199 GNUNET_STATISTICS_update (GDS_stats,
200 gettext_noop ("# Duplicate RESULTS found in datacache"),
204 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
205 GNUNET_STATISTICS_update (GDS_stats,
206 gettext_noop ("# Invalid RESULTS found in datacache"),
210 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
211 GNUNET_STATISTICS_update (GDS_stats,
212 gettext_noop ("# Irrelevant RESULTS found in datacache"),
216 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
219 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
221 return GNUNET_SYSERR;
222 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
223 GNUNET_STATISTICS_update (GDS_stats,
224 gettext_noop ("# Unsupported RESULTS found in datacache"),
227 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
228 _("Unsupported block type (%u) in local response!\n"),
232 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
237 * Handle a GET request we've received from another peer.
239 * @param key the query
240 * @param type requested data type
241 * @param xquery extended query
242 * @param xquery_size number of bytes in @a xquery
243 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
244 * @param reply_bf_mutator mutation value for @a reply_bf
245 * @return evaluation result for the local replies
247 enum GNUNET_BLOCK_EvaluationResult
248 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
249 enum GNUNET_BLOCK_Type type,
252 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
253 uint32_t reply_bf_mutator)
255 struct GetRequestContext ctx;
258 if (NULL == datacache)
259 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
260 GNUNET_STATISTICS_update (GDS_stats,
261 gettext_noop ("# GET requests given to datacache"),
264 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
267 ctx.xquery_size = xquery_size;
268 ctx.reply_bf = reply_bf;
269 ctx.reply_bf_mutator = reply_bf_mutator;
270 r = GNUNET_DATACACHE_get (datacache,
273 &datacache_get_iterator,
275 LOG (GNUNET_ERROR_TYPE_DEBUG,
276 "DATACACHE GET for key %s completed (%d). %u results found.\n",
285 * Initialize datacache subsystem.
288 GDS_DATACACHE_init ()
290 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
295 * Shutdown datacache subsystem.
298 GDS_DATACACHE_done ()
300 if (NULL != datacache)
302 GNUNET_DATACACHE_destroy (datacache);
308 /* end of gnunet-service-dht_datacache.c */