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__)
38 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
41 * Handle to the datacache service (for inserting/retrieving data)
43 static struct GNUNET_DATACACHE_Handle *datacache;
47 * Handle a datum we've received from another peer. Cache if
50 * @param expiration when will the reply expire
51 * @param key the query this reply is for
52 * @param put_path_length number of peers in 'put_path'
53 * @param put_path path the reply took on put
54 * @param type type of the reply
55 * @param data_size number of bytes in 'data'
56 * @param data application payload data
59 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
60 const struct GNUNET_HashCode * key,
61 unsigned int put_path_length,
62 const struct GNUNET_PeerIdentity *put_path,
63 enum GNUNET_BLOCK_Type type, size_t data_size,
68 if (NULL == datacache)
70 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
71 _("%s request received, but have no datacache!\n"), "PUT");
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 GNUNET_STATISTICS_update (GDS_stats,
82 gettext_noop ("# ITEMS stored in datacache"), 1,
85 struct GNUNET_PeerIdentity peer = GDS_NEIGHBOURS_get_my_id();
86 DEBUG("DATACACHE_PUT KEY = %s, peer = %s\n",GNUNET_h2s(key),GNUNET_i2s(&peer));
87 r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
88 put_path_length, put_path);
89 LOG (GNUNET_ERROR_TYPE_DEBUG,
90 "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
91 GNUNET_h2s (key), data_size, r, put_path_length);
95 * List of peers in the get path.
100 * Pointer to next item in the list
102 struct GetPath *next;
105 * Pointer to previous item in the list
107 struct GetPath *prev;
110 * An element in the get path.
112 struct GNUNET_PeerIdentity peer;
117 * Context containing information about a GET request.
119 struct GetRequestContext
122 * extended query (see gnunet_block_lib.h).
127 * Bloomfilter to filter out duplicate replies (updated)
129 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
132 * The key this request was about
134 struct GNUNET_HashCode key;
137 * Number of bytes in xquery.
142 * Mutator value for the reply_bf, see gnunet_block_lib.h
144 uint32_t reply_bf_mutator;
147 * Total number of peers in get path.
149 unsigned int get_path_length;
152 * Return value to give back.
154 enum GNUNET_BLOCK_EvaluationResult eval;
157 * Peeer which has the data for the key.
159 struct GNUNET_PeerIdentity source_peer;
162 * Next hop to forward the get result to.
164 struct GNUNET_PeerIdentity next_hop;
169 struct GetPath *head;
174 struct GetPath *tail;
181 * Iterator for local get request results,
183 * @param cls closure for iterator, a DatacacheGetContext
184 * @param exp when does this value expire?
185 * @param key the key this data is stored under
186 * @param size the size of the data identified by key
187 * @param data the actual data
188 * @param type the type of the data
189 * @param put_path_length number of peers in 'put_path'
190 * @param put_path path the reply took on put
191 * @return GNUNET_OK to continue iteration, anything else
195 datacache_get_iterator (void *cls,
196 const struct GNUNET_HashCode * key, size_t size,
197 const char *data, enum GNUNET_BLOCK_Type type,
198 struct GNUNET_TIME_Absolute exp,
199 unsigned int put_path_length,
200 const struct GNUNET_PeerIdentity *put_path)
202 struct GetRequestContext *ctx = cls;
203 enum GNUNET_BLOCK_EvaluationResult eval;
206 GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
207 ctx->reply_bf_mutator, ctx->xquery,
208 ctx->xquery_size, data, size);
209 LOG (GNUNET_ERROR_TYPE_DEBUG,
210 "Found reply for query %s in datacache, evaluation result is %d\n",
211 GNUNET_h2s (key), (int) eval);
216 case GNUNET_BLOCK_EVALUATION_OK_MORE:
217 case GNUNET_BLOCK_EVALUATION_OK_LAST:
218 /* forward to local clients */
219 GNUNET_STATISTICS_update (GDS_stats,
221 ("# Good RESULTS found in datacache"), 1,
223 struct GNUNET_PeerIdentity *get_path;
224 get_path = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
225 ctx->get_path_length);
226 struct GetPath *iterator;
227 iterator = ctx->head;
229 while (i < ctx->get_path_length)
231 get_path[i] = iterator->peer;
233 iterator = iterator->next;
235 GDS_NEIGHBOURS_send_get_result (key,type, &(ctx->next_hop),&(ctx->source_peer),
236 put_path_length, put_path, ctx->get_path_length,
237 get_path, exp, data, size );
238 GNUNET_free_non_null (get_path);
241 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
242 GNUNET_STATISTICS_update (GDS_stats,
244 ("# Duplicate RESULTS found in datacache"), 1,
247 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
248 GNUNET_STATISTICS_update (GDS_stats,
250 ("# Invalid RESULTS found in datacache"), 1,
253 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
254 GNUNET_STATISTICS_update (GDS_stats,
256 ("# Irrelevant RESULTS found in datacache"), 1,
259 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
262 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
264 return GNUNET_SYSERR;
265 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
266 GNUNET_STATISTICS_update (GDS_stats,
268 ("# Unsupported RESULTS found in datacache"), 1,
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 _("Unsupported block type (%u) in local response!\n"), type);
275 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
280 * Handle a GET request we've received from another peer.
282 * @param key the query
283 * @param type requested data type
284 * @param xquery extended query
285 * @param xquery_size number of bytes in xquery
286 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
287 * @param reply_bf_mutator mutation value for reply_bf
288 * @return evaluation result for the local replies
289 * @get_path_length Total number of peers in get path
290 * @get_path Peers in get path.
292 enum GNUNET_BLOCK_EvaluationResult
293 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
294 enum GNUNET_BLOCK_Type type, const void *xquery,
296 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
297 uint32_t reply_bf_mutator,
298 uint32_t get_path_length,
299 struct GNUNET_PeerIdentity *get_path,
300 struct GNUNET_PeerIdentity *next_hop,
301 struct GNUNET_PeerIdentity *source_peer)
303 struct GetRequestContext ctx;
306 if (datacache == NULL)
307 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
308 GNUNET_STATISTICS_update (GDS_stats,
309 gettext_noop ("# GET requests given to datacache"),
311 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
314 ctx.xquery_size = xquery_size;
315 ctx.reply_bf = reply_bf;
316 ctx.reply_bf_mutator = reply_bf_mutator;
317 ctx.get_path_length = get_path_length;
319 if (next_hop != NULL)
321 memcpy (&(ctx.next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
327 if (get_path != NULL)
329 while (i < get_path_length)
331 struct GetPath *element;
332 element = GNUNET_new (struct GetPath);
333 element->next = NULL;
334 element->prev = NULL;
335 element->peer = get_path[i];
336 GNUNET_CONTAINER_DLL_insert_tail (ctx.head, ctx.tail, element);
341 r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
343 DEBUG ("DATACACHE_GET for key %s completed (%d). %u results found.\n",GNUNET_h2s (key), ctx.eval, r);
349 * Initialize datacache subsystem.
352 GDS_DATACACHE_init ()
354 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
359 * Shutdown datacache subsystem.
362 GDS_DATACACHE_done ()
364 if (datacache != NULL)
366 GNUNET_DATACACHE_destroy (datacache);
372 /* end of gnunet-service-dht_datacache.c */