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-xdht_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.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__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 '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 '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, size_t data_size,
65 if (NULL == datacache)
67 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
68 _("%s request received, but have no datacache!\n"), "PUT");
71 if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
76 /* Put size is actual data size plus struct overhead plus path length (if any) */
77 GNUNET_STATISTICS_update (GDS_stats,
78 gettext_noop ("# ITEMS stored in datacache"), 1,
80 r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
81 put_path_length, put_path);
82 LOG (GNUNET_ERROR_TYPE_DEBUG,
83 "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
84 GNUNET_h2s (key), data_size, r, put_path_length);
89 * Context containing information about a GET request.
91 struct GetRequestContext
94 * extended query (see gnunet_block_lib.h).
99 * Bloomfilter to filter out duplicate replies (updated)
101 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
104 * The key this request was about
106 struct GNUNET_HashCode key;
109 * Number of bytes in xquery.
114 * Mutator value for the reply_bf, see gnunet_block_lib.h
116 uint32_t reply_bf_mutator;
119 * Return value to give back.
121 enum GNUNET_BLOCK_EvaluationResult eval;
126 * Iterator for local get request results,
128 * @param cls closure for iterator, a DatacacheGetContext
129 * @param exp when does this value expire?
130 * @param key the key this data is stored under
131 * @param size the size of the data identified by key
132 * @param data the actual data
133 * @param type the type of the data
134 * @param put_path_length number of peers in 'put_path'
135 * @param put_path path the reply took on put
136 * @return GNUNET_OK to continue iteration, anything else
140 datacache_get_iterator (void *cls,
141 const struct GNUNET_HashCode * key, size_t size,
142 const char *data, enum GNUNET_BLOCK_Type type,
143 struct GNUNET_TIME_Absolute exp,
144 unsigned int put_path_length,
145 const struct GNUNET_PeerIdentity *put_path)
147 struct GetRequestContext *ctx = cls;
148 enum GNUNET_BLOCK_EvaluationResult eval;
151 GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
152 ctx->reply_bf_mutator, ctx->xquery,
153 ctx->xquery_size, data, size);
154 LOG (GNUNET_ERROR_TYPE_DEBUG,
155 "Found reply for query %s in datacache, evaluation result is %d\n",
156 GNUNET_h2s (key), (int) eval);
160 case GNUNET_BLOCK_EVALUATION_OK_MORE:
161 case GNUNET_BLOCK_EVALUATION_OK_LAST:
162 /* forward to local clients */
163 GNUNET_STATISTICS_update (GDS_stats,
165 ("# Good RESULTS found in datacache"), 1,
167 GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, put_path,
169 /* forward to other peers */
170 GDS_ROUTING_process (type, exp, key, put_path_length, put_path, 0, NULL,
173 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
174 GNUNET_STATISTICS_update (GDS_stats,
176 ("# Duplicate RESULTS found in datacache"), 1,
179 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
180 GNUNET_STATISTICS_update (GDS_stats,
182 ("# Invalid RESULTS found in datacache"), 1,
185 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
186 GNUNET_STATISTICS_update (GDS_stats,
188 ("# Irrelevant RESULTS found in datacache"), 1,
191 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
194 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
196 return GNUNET_SYSERR;
197 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
198 GNUNET_STATISTICS_update (GDS_stats,
200 ("# Unsupported RESULTS found in datacache"), 1,
202 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
203 _("Unsupported block type (%u) in local response!\n"), type);
206 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
211 * Handle a GET request we've received from another peer.
213 * @param key the query
214 * @param type requested data type
215 * @param xquery extended query
216 * @param xquery_size number of bytes in xquery
217 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
218 * @param reply_bf_mutator mutation value for reply_bf
219 * @return evaluation result for the local replies
221 enum GNUNET_BLOCK_EvaluationResult
222 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
223 enum GNUNET_BLOCK_Type type, const void *xquery,
225 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
226 uint32_t reply_bf_mutator)
228 struct GetRequestContext ctx;
231 if (datacache == NULL)
232 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
233 GNUNET_STATISTICS_update (GDS_stats,
234 gettext_noop ("# GET requests given to datacache"),
236 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
239 ctx.xquery_size = xquery_size;
240 ctx.reply_bf = reply_bf;
241 ctx.reply_bf_mutator = reply_bf_mutator;
242 r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
244 LOG (GNUNET_ERROR_TYPE_DEBUG,
245 "DATACACHE GET for key %s completed (%d). %u results found.\n",
246 GNUNET_h2s (key), ctx.eval, r);
252 * Initialize datacache subsystem.
255 GDS_DATACACHE_init ()
257 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
262 * Shutdown datacache subsystem.
265 GDS_DATACACHE_done ()
267 if (datacache != NULL)
269 GNUNET_DATACACHE_destroy (datacache);
275 /* end of gnunet-service-dht_datacache.c */