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
27 #include "gnunet-service-dht_datacache.h"
31 * Handle to the datacache service (for inserting/retrieving data)
33 static struct GNUNET_DATACACHE_Handle *datacache;
37 * Entry for inserting data into datacache from the DHT.
47 * Length of recorded path.
59 * Handle a datum we've received from another peer. Cache if
62 * @param expiration when will the reply expire
63 * @param key the query this reply is for
64 * @param put_path_length number of peers in 'put_path'
65 * @param put_path path the reply took on put
66 * @param type type of the reply
67 * @param data_size number of bytes in 'data'
68 * @param data application payload data
71 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
72 const GNUNET_HashCode *key,
73 unsigned int put_path_length,
74 const struct GNUNET_PeerIdentity *put_path,
79 size_t plen = data_size + put_path_length * sizeof(struct GNUNET_PeerIdentity) + sizeof(struct DHTPutEntry);
81 struct DHTPutEntry *pe;
82 struct GNUNET_PeerIdentity *pp;
85 if (datacache == NULL)
87 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
88 "%s request received, but have no datacache!\n",
92 if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
97 /* Put size is actual data size plus struct overhead plus path length (if any) */
98 pe = (struct DHTPutEntry *) buf;
99 pe->data_size = htons (data_size);
100 pe->path_length = htons ((uint16_t) put_path_length);
101 pp = (struct GNUNET_PeerIdentity *) &pe[1];
102 memcpy (pp, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity));
103 memcpy (&pp[put_path_length],
105 (void) GNUNET_DATACACHE_put (datacache, key,
106 plen, (const char *) pe, type,
112 * Context containing information about a GET request.
114 struct GetRequestContext
117 * extended query (see gnunet_block_lib.h).
122 * Bloomfilter to filter out duplicate replies (updated)
124 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
127 * The key this request was about
132 * Number of bytes in xquery.
137 * Mutator value for the reply_bf, see gnunet_block_lib.h
139 uint32_t reply_bf_mutator;
145 * Iterator for local get request results,
147 * @param cls closure for iterator, a DatacacheGetContext
148 * @param exp when does this value expire?
149 * @param key the key this data is stored under
150 * @param size the size of the data identified by key
151 * @param data the actual data
152 * @param type the type of the data
154 * @return GNUNET_OK to continue iteration, anything else
158 datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
159 const GNUNET_HashCode * key, size_t size,
160 const char *data, enum GNUNET_BLOCK_Type type)
162 struct GetRequestContext *ctx = cls;
163 const struct DHTPutEntry *pe;
164 const struct GNUNET_PeerIdentity *pp;
167 uint16_t put_path_length;
168 enum GNUNET_BLOCK_EvaluationResult eval;
170 pe = (const struct DHTPutEntry *) data;
171 put_path_length = ntohs (pe->path_length);
172 data_size = ntohs (pe->data_size);
175 sizeof (struct DHTPutEntry) + data_size +
176 (put_path_length * sizeof (struct GNUNET_PeerIdentity)))
181 pp = (const struct GNUNET_PeerIdentity *) &pe[1];
182 data = (const char*) &pp[put_path_length];
184 GNUNET_BLOCK_evaluate (block_context, type, key,
186 ctx->reply_bf_mutator,
193 case GNUNET_BLOCK_EVALUATION_OK_LAST:
194 case GNUNET_BLOCK_EVALUATION_OK_MORE:
195 /* forward to local clients */
196 GDS_CLIENT_handle_reply (exp,
200 type, data_size, data);
201 /* forward to other peers */
202 GDS_NEIGHBOURS_handle_reply (type, exp,
203 key, put_path_length, pp,
204 0, NULL, data, data_size);
206 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
208 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
210 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
213 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
215 return GNUNET_SYSERR;
216 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
218 "Unsupported block type (%u) in local response!\n",
227 * Context containing information about a GET request.
229 struct GetRequestContext
232 * extended query (see gnunet_block_lib.h).
237 * Bloomfilter to filter out duplicate replies (updated)
239 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
242 * The key this request was about
247 * Number of bytes in xquery.
252 * Mutator value for the reply_bf, see gnunet_block_lib.h
254 uint32_t reply_bf_mutator;
260 * Handle a GET request we've received from another peer.
262 * @param key the query
263 * @param type requested data type
264 * @param xquery extended query
265 * @param xquery_size number of bytes in xquery
266 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
267 * @param reply_bf_mutator mutation value for reply_bf
270 GDS_DATACACHE_handle_get (const GNUNET_HashCode *key,
274 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
275 uint32_t reply_bf_mutator)
277 struct GetRequestContext ctx;
279 if (datacache == NULL)
283 ctx.xquery_size = xquery_size;
284 ctx.reply_bf = reply_bf;
285 ctx.reply_bf_mutator = reply_bf_mutator;
286 (void) GNUNET_DATACACHE_get (datacache, &msg_ctx->key, type,
287 &datacache_get_iterator, &ctx);
292 * Initialize datacache subsystem.
295 GDS_DATACACHE_init ()
297 datacache = GNUNET_DATACACHE_create (cfg, "dhtcache");
302 * Shutdown datacache subsystem.
305 GDS_DATACACHE_done ()
307 if (datacache != NULL)
309 GNUNET_DATACACHE_destroy (datacache);
315 /* end of gnunet-service-dht_datacache.c */