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 struct GNUNET_HashCode * key,
78 unsigned int put_path_length,
79 const struct GNUNET_PeerIdentity *put_path,
80 enum GNUNET_BLOCK_Type type, size_t data_size,
84 data_size + put_path_length * sizeof (struct GNUNET_PeerIdentity) +
85 sizeof (struct DHTPutEntry);
87 struct DHTPutEntry *pe;
88 struct GNUNET_PeerIdentity *pp;
90 if (datacache == NULL)
92 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
93 _("%s request received, but have no datacache!\n"), "PUT");
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], data, data_size);
111 (void) GNUNET_DATACACHE_put (datacache, key, plen, (const char *) pe, type,
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 * Return value to give back.
149 enum GNUNET_BLOCK_EvaluationResult eval;
154 * Iterator for local get request results,
156 * @param cls closure for iterator, a DatacacheGetContext
157 * @param exp when does this value expire?
158 * @param key the key this data is stored under
159 * @param size the size of the data identified by key
160 * @param data the actual data
161 * @param type the type of the data
163 * @return GNUNET_OK to continue iteration, anything else
167 datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
168 const struct GNUNET_HashCode * key, size_t size,
169 const char *data, enum GNUNET_BLOCK_Type type)
171 struct GetRequestContext *ctx = cls;
172 const struct DHTPutEntry *pe;
173 const struct GNUNET_PeerIdentity *pp;
176 uint16_t put_path_length;
177 enum GNUNET_BLOCK_EvaluationResult eval;
179 pe = (const struct DHTPutEntry *) data;
180 put_path_length = ntohs (pe->path_length);
181 rdata_size = ntohs (pe->data_size);
184 sizeof (struct DHTPutEntry) + rdata_size +
185 (put_path_length * sizeof (struct GNUNET_PeerIdentity)))
190 pp = (const struct GNUNET_PeerIdentity *) &pe[1];
191 rdata = (const char *) &pp[put_path_length];
193 GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
194 ctx->reply_bf_mutator, ctx->xquery,
195 ctx->xquery_size, rdata, rdata_size);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Found reply for query %s in datacache, evaluation result is %d\n",
198 GNUNET_h2s (key), (int) eval);
202 case GNUNET_BLOCK_EVALUATION_OK_LAST:
203 case GNUNET_BLOCK_EVALUATION_OK_MORE:
204 /* forward to local clients */
205 GNUNET_STATISTICS_update (GDS_stats,
207 ("# Good RESULTS found in datacache"), 1,
209 GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, pp, type,
211 /* forward to other peers */
212 GDS_ROUTING_process (type, exp, key, put_path_length, pp, 0, NULL, rdata,
215 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
216 GNUNET_STATISTICS_update (GDS_stats,
218 ("# Duplicate RESULTS found in datacache"), 1,
221 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
222 GNUNET_STATISTICS_update (GDS_stats,
224 ("# Invalid RESULTS found in datacache"), 1,
227 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
230 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
232 return GNUNET_SYSERR;
233 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
234 GNUNET_STATISTICS_update (GDS_stats,
236 ("# Unsupported RESULTS found in datacache"), 1,
238 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
239 _("Unsupported block type (%u) in local response!\n"), type);
242 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
247 * Handle a GET request we've received from another peer.
249 * @param key the query
250 * @param type requested data type
251 * @param xquery extended query
252 * @param xquery_size number of bytes in xquery
253 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
254 * @param reply_bf_mutator mutation value for reply_bf
255 * @return evaluation result for the local replies
257 enum GNUNET_BLOCK_EvaluationResult
258 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
259 enum GNUNET_BLOCK_Type type, const void *xquery,
261 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
262 uint32_t reply_bf_mutator)
264 struct GetRequestContext ctx;
266 if (datacache == NULL)
267 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
268 GNUNET_STATISTICS_update (GDS_stats,
269 gettext_noop ("# GET requests given to datacache"),
271 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
274 ctx.xquery_size = xquery_size;
275 ctx.reply_bf = reply_bf;
276 ctx.reply_bf_mutator = reply_bf_mutator;
277 (void) GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
284 * Initialize datacache subsystem.
287 GDS_DATACACHE_init ()
289 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
294 * Shutdown datacache subsystem.
297 GDS_DATACACHE_done ()
299 if (datacache != NULL)
301 GNUNET_DATACACHE_destroy (datacache);
307 /* end of gnunet-service-dht_datacache.c */