2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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-xdht_clients.h"
30 #include "gnunet-service-xdht_datacache.h"
31 #include "gnunet-service-xdht_routing.h"
32 #include "gnunet-service-xdht_neighbours.h"
33 #include "gnunet-service-dht.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__VA_ARGS__)
39 * Handle to the datacache service (for inserting/retrieving data)
41 static struct GNUNET_DATACACHE_Handle *datacache;
45 * Handle a datum we've received from another peer. Cache if
48 * @param expiration when will the reply expire
49 * @param key the query this reply is for
50 * @param put_path_length number of peers in 'put_path'
51 * @param put_path path the reply took on put
52 * @param type type of the reply
53 * @param data_size number of bytes in 'data'
54 * @param data application payload data
57 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
58 const struct GNUNET_HashCode * key,
59 unsigned int put_path_length,
60 const struct GNUNET_PeerIdentity *put_path,
61 enum GNUNET_BLOCK_Type type, size_t data_size,
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);
89 * List of peers in the get path.
94 * Pointer to next item in the list
99 * Pointer to previous item in the list
101 struct GetPath *prev;
104 * An element in the get path.
106 struct GNUNET_PeerIdentity peer;
111 * Context containing information about a GET request.
113 struct GetRequestContext
116 * extended query (see gnunet_block_lib.h).
121 * Bloomfilter to filter out duplicate replies (updated)
123 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
126 * The key this request was about
128 struct GNUNET_HashCode key;
131 * Number of bytes in xquery.
136 * Mutator value for the reply_bf, see gnunet_block_lib.h
138 uint32_t reply_bf_mutator;
141 * Total number of peers in get path.
143 unsigned int get_path_length;
146 * Return value to give back.
148 enum GNUNET_BLOCK_EvaluationResult eval;
151 * Peeer which has the data for the key.
153 struct GNUNET_PeerIdentity source_peer;
156 * Next hop to forward the get result to.
158 struct GNUNET_PeerIdentity next_hop;
163 struct GetPath *head;
168 struct GetPath *tail;
174 * Iterator for local get request results,
176 * @param cls closure for iterator, a DatacacheGetContext
177 * @param exp when does this value expire?
178 * @param key the key this data is stored under
179 * @param size the size of the data identified by key
180 * @param data the actual data
181 * @param type the type of the data
182 * @param put_path_length number of peers in 'put_path'
183 * @param put_path path the reply took on put
184 * @return GNUNET_OK to continue iteration, anything else
188 datacache_get_iterator (void *cls,
189 const struct GNUNET_HashCode * key, size_t size,
190 const char *data, enum GNUNET_BLOCK_Type type,
191 struct GNUNET_TIME_Absolute exp,
192 unsigned int put_path_length,
193 const struct GNUNET_PeerIdentity *put_path)
195 struct GetRequestContext *ctx = cls;
196 enum GNUNET_BLOCK_EvaluationResult eval;
199 GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
200 ctx->reply_bf_mutator, ctx->xquery,
201 ctx->xquery_size, data, size);
202 LOG (GNUNET_ERROR_TYPE_DEBUG,
203 "Found reply for query %s in datacache, evaluation result is %d\n",
204 GNUNET_h2s (key), (int) eval);
208 case GNUNET_BLOCK_EVALUATION_OK_MORE:
209 case GNUNET_BLOCK_EVALUATION_OK_LAST:
210 /* forward to local clients */
211 GNUNET_STATISTICS_update (GDS_stats,
213 ("# Good RESULTS found in datacache"), 1,
215 struct GNUNET_PeerIdentity *get_path;
216 get_path = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
217 struct GetPath *iterator;
218 iterator = ctx->head;
220 while (i < ctx->get_path_length)
222 memcpy (&get_path[i], &(iterator->peer), sizeof (struct GNUNET_PeerIdentity));
224 iterator = iterator->next;
226 GDS_NEIGHBOURS_send_get_result (exp, key, put_path_length, put_path,
227 type, size, data, get_path, ctx->get_path_length,
229 &(ctx->source_peer));
232 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
233 GNUNET_STATISTICS_update (GDS_stats,
235 ("# Duplicate RESULTS found in datacache"), 1,
238 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
239 GNUNET_STATISTICS_update (GDS_stats,
241 ("# Invalid RESULTS found in datacache"), 1,
244 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
245 GNUNET_STATISTICS_update (GDS_stats,
247 ("# Irrelevant RESULTS found in datacache"), 1,
250 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
253 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
255 return GNUNET_SYSERR;
256 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
257 GNUNET_STATISTICS_update (GDS_stats,
259 ("# Unsupported RESULTS found in datacache"), 1,
261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
262 _("Unsupported block type (%u) in local response!\n"), type);
265 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
270 * Handle a GET request we've received from another peer.
272 * @param key the query
273 * @param type requested data type
274 * @param xquery extended query
275 * @param xquery_size number of bytes in xquery
276 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
277 * @param reply_bf_mutator mutation value for reply_bf
278 * @return evaluation result for the local replies
279 * @get_path_length Total number of peers in get path
280 * @get_path Peers in get path.
282 enum GNUNET_BLOCK_EvaluationResult
283 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
284 enum GNUNET_BLOCK_Type type, const void *xquery,
286 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
287 uint32_t reply_bf_mutator,
288 uint32_t get_path_length,
289 struct GNUNET_PeerIdentity *get_path,
290 struct GNUNET_PeerIdentity *next_hop,
291 struct GNUNET_PeerIdentity *source_peer)
293 struct GetRequestContext ctx;
296 if (datacache == NULL)
297 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
298 GNUNET_STATISTICS_update (GDS_stats,
299 gettext_noop ("# GET requests given to datacache"),
301 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
304 ctx.xquery_size = xquery_size;
305 ctx.reply_bf = reply_bf;
306 ctx.reply_bf_mutator = reply_bf_mutator;
307 ctx.get_path_length = get_path_length;
308 if (next_hop != NULL)
309 memcpy (&(ctx.next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
311 /* FIXME: add the get path into ctx and then call gds_neighbours_handle_get*/
316 while (i < get_path_length)
318 struct GetPath *element;
319 element = GNUNET_malloc (sizeof (struct GetPath));
320 element->next = NULL;
321 element->prev = NULL;
323 memcpy (&(element->peer), &get_path[i], sizeof(struct GNUNET_PeerIdentity));
324 GNUNET_CONTAINER_DLL_insert_tail (ctx.head, ctx.tail, element); /* FIXME: changed from insert_tail to insert. */
329 r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
331 LOG (GNUNET_ERROR_TYPE_DEBUG,
332 "DATACACHE GET for key %s completed (%d). %u results found.\n",
333 GNUNET_h2s (key), ctx.eval, r);
339 * Initialize datacache subsystem.
342 GDS_DATACACHE_init ()
344 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
349 * Shutdown datacache subsystem.
352 GDS_DATACACHE_done ()
354 if (datacache != NULL)
356 GNUNET_DATACACHE_destroy (datacache);
362 /* end of gnunet-service-dht_datacache.c */