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-dht_clients.h"
30 #include "gnunet-service-dht_datacache.h"
31 #include "gnunet-service-dht_routing.h"
32 #include "gnunet-service-dht.h"
36 * Handle to the datacache service (for inserting/retrieving data)
38 static struct GNUNET_DATACACHE_Handle *datacache;
42 * Entry for inserting data into datacache from the DHT.
52 * Length of recorded path.
64 * Handle a datum we've received from another peer. Cache if
67 * @param expiration when will the reply expire
68 * @param key the query this reply is for
69 * @param put_path_length number of peers in 'put_path'
70 * @param put_path path the reply took on put
71 * @param type type of the reply
72 * @param data_size number of bytes in 'data'
73 * @param data application payload data
76 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
77 const GNUNET_HashCode *key,
78 unsigned int put_path_length,
79 const struct GNUNET_PeerIdentity *put_path,
80 enum GNUNET_BLOCK_Type type,
84 size_t plen = data_size + put_path_length * sizeof(struct GNUNET_PeerIdentity) + sizeof(struct DHTPutEntry);
86 struct DHTPutEntry *pe;
87 struct GNUNET_PeerIdentity *pp;
89 if (datacache == NULL)
91 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
92 "%s request received, but have no datacache!\n",
96 if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
101 /* Put size is actual data size plus struct overhead plus path length (if any) */
102 GNUNET_STATISTICS_update (GDS_stats,
103 gettext_noop ("# ITEMS stored in datacache"), 1,
105 pe = (struct DHTPutEntry *) buf;
106 pe->data_size = htons (data_size);
107 pe->path_length = htons ((uint16_t) put_path_length);
108 pp = (struct GNUNET_PeerIdentity *) &pe[1];
109 memcpy (pp, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity));
110 memcpy (&pp[put_path_length],
112 (void) GNUNET_DATACACHE_put (datacache, key,
113 plen, (const char *) pe, type,
119 * Context containing information about a GET request.
121 struct GetRequestContext
124 * extended query (see gnunet_block_lib.h).
129 * Bloomfilter to filter out duplicate replies (updated)
131 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
134 * The key this request was about
139 * Number of bytes in xquery.
144 * Mutator value for the reply_bf, see gnunet_block_lib.h
146 uint32_t reply_bf_mutator;
149 * Return value to give back.
151 enum GNUNET_BLOCK_EvaluationResult eval;
156 * Iterator for local get request results,
158 * @param cls closure for iterator, a DatacacheGetContext
159 * @param exp when does this value expire?
160 * @param key the key this data is stored under
161 * @param size the size of the data identified by key
162 * @param data the actual data
163 * @param type the type of the data
165 * @return GNUNET_OK to continue iteration, anything else
169 datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
170 const GNUNET_HashCode * key, size_t size,
171 const char *data, enum GNUNET_BLOCK_Type type)
173 struct GetRequestContext *ctx = cls;
174 const struct DHTPutEntry *pe;
175 const struct GNUNET_PeerIdentity *pp;
178 uint16_t put_path_length;
179 enum GNUNET_BLOCK_EvaluationResult eval;
181 pe = (const struct DHTPutEntry *) data;
182 put_path_length = ntohs (pe->path_length);
183 rdata_size = ntohs (pe->data_size);
186 sizeof (struct DHTPutEntry) + rdata_size +
187 (put_path_length * sizeof (struct GNUNET_PeerIdentity)))
192 pp = (const struct GNUNET_PeerIdentity *) &pe[1];
193 rdata = (const char*) &pp[put_path_length];
195 GNUNET_BLOCK_evaluate (GDS_block_context, type, key,
197 ctx->reply_bf_mutator,
205 case GNUNET_BLOCK_EVALUATION_OK_LAST:
206 case GNUNET_BLOCK_EVALUATION_OK_MORE:
207 /* forward to local clients */
208 GNUNET_STATISTICS_update (GDS_stats,
209 gettext_noop ("# Good RESULTS found in datacache"), 1,
211 GDS_CLIENTS_handle_reply (exp,
215 type, rdata_size, rdata);
216 /* forward to other peers */
217 GDS_ROUTING_process (type, exp,
218 key, put_path_length, pp,
219 0, NULL, rdata, rdata_size);
221 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
222 GNUNET_STATISTICS_update (GDS_stats,
223 gettext_noop ("# Duplicate RESULTS found in datacache"), 1,
226 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
227 GNUNET_STATISTICS_update (GDS_stats,
228 gettext_noop ("# Invalid RESULTS found in datacache"), 1,
231 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
234 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
236 return GNUNET_SYSERR;
237 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
238 GNUNET_STATISTICS_update (GDS_stats,
239 gettext_noop ("# Unsupported RESULTS found in datacache"), 1,
241 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
242 "Unsupported block type (%u) in local response!\n",
246 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
251 * Handle a GET request we've received from another peer.
253 * @param key the query
254 * @param type requested data type
255 * @param xquery extended query
256 * @param xquery_size number of bytes in xquery
257 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
258 * @param reply_bf_mutator mutation value for reply_bf
259 * @return evaluation result for the local replies
261 enum GNUNET_BLOCK_EvaluationResult
262 GDS_DATACACHE_handle_get (const GNUNET_HashCode *key,
263 enum GNUNET_BLOCK_Type type,
266 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
267 uint32_t reply_bf_mutator)
269 struct GetRequestContext ctx;
271 if (datacache == NULL)
272 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
273 GNUNET_STATISTICS_update (GDS_stats,
274 gettext_noop ("# GET requests given to datacache"), 1,
276 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
279 ctx.xquery_size = xquery_size;
280 ctx.reply_bf = reply_bf;
281 ctx.reply_bf_mutator = reply_bf_mutator;
282 (void) GNUNET_DATACACHE_get (datacache, key, type,
283 &datacache_get_iterator, &ctx);
289 * Initialize datacache subsystem.
292 GDS_DATACACHE_init ()
294 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
299 * Shutdown datacache subsystem.
302 GDS_DATACACHE_done ()
304 if (datacache != NULL)
306 GNUNET_DATACACHE_destroy (datacache);
312 /* end of gnunet-service-dht_datacache.c */