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 * Handle a datum we've received from another peer. Cache if
45 * @param expiration when will the reply expire
46 * @param key the query this reply is for
47 * @param put_path_length number of peers in 'put_path'
48 * @param put_path path the reply took on put
49 * @param type type of the reply
50 * @param data_size number of bytes in 'data'
51 * @param data application payload data
54 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
55 const struct GNUNET_HashCode * key,
56 unsigned int put_path_length,
57 const struct GNUNET_PeerIdentity *put_path,
58 enum GNUNET_BLOCK_Type type, size_t data_size,
61 if (NULL == datacache)
63 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
64 _("%s request received, but have no datacache!\n"), "PUT");
67 if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
72 /* Put size is actual data size plus struct overhead plus path length (if any) */
73 GNUNET_STATISTICS_update (GDS_stats,
74 gettext_noop ("# ITEMS stored in datacache"), 1,
76 (void) GNUNET_DATACACHE_put (datacache, key,
77 data_size, data, type,
78 expiration, put_path_length, put_path);
83 * Context containing information about a GET request.
85 struct GetRequestContext
88 * extended query (see gnunet_block_lib.h).
93 * Bloomfilter to filter out duplicate replies (updated)
95 struct GNUNET_CONTAINER_BloomFilter **reply_bf;
98 * The key this request was about
100 struct GNUNET_HashCode key;
103 * Number of bytes in xquery.
108 * Mutator value for the reply_bf, see gnunet_block_lib.h
110 uint32_t reply_bf_mutator;
113 * Return value to give back.
115 enum GNUNET_BLOCK_EvaluationResult eval;
120 * Iterator for local get request results,
122 * @param cls closure for iterator, a DatacacheGetContext
123 * @param exp when does this value expire?
124 * @param key the key this data is stored under
125 * @param size the size of the data identified by key
126 * @param data the actual data
127 * @param type the type of the data
128 * @param put_path_length number of peers in 'put_path'
129 * @param put_path path the reply took on put
130 * @return GNUNET_OK to continue iteration, anything else
134 datacache_get_iterator (void *cls,
135 const struct GNUNET_HashCode * key, size_t size,
136 const char *data, enum GNUNET_BLOCK_Type type,
137 struct GNUNET_TIME_Absolute exp,
138 unsigned int put_path_length,
139 const struct GNUNET_PeerIdentity *put_path)
141 struct GetRequestContext *ctx = cls;
142 enum GNUNET_BLOCK_EvaluationResult eval;
145 GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
146 ctx->reply_bf_mutator, ctx->xquery,
147 ctx->xquery_size, data, size);
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Found reply for query %s in datacache, evaluation result is %d\n",
150 GNUNET_h2s (key), (int) eval);
154 case GNUNET_BLOCK_EVALUATION_OK_LAST:
155 case GNUNET_BLOCK_EVALUATION_OK_MORE:
156 /* forward to local clients */
157 GNUNET_STATISTICS_update (GDS_stats,
159 ("# Good RESULTS found in datacache"), 1,
161 GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, put_path, type,
163 /* forward to other peers */
164 GDS_ROUTING_process (type, exp, key, put_path_length, put_path, 0, NULL, data,
167 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
168 GNUNET_STATISTICS_update (GDS_stats,
170 ("# Duplicate RESULTS found in datacache"), 1,
173 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
174 GNUNET_STATISTICS_update (GDS_stats,
176 ("# Invalid RESULTS found in datacache"), 1,
179 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
180 GNUNET_STATISTICS_update (GDS_stats,
182 ("# Irrelevant RESULTS found in datacache"), 1,
185 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
188 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
190 return GNUNET_SYSERR;
191 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
192 GNUNET_STATISTICS_update (GDS_stats,
194 ("# Unsupported RESULTS found in datacache"), 1,
196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
197 _("Unsupported block type (%u) in local response!\n"), type);
200 return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
205 * Handle a GET request we've received from another peer.
207 * @param key the query
208 * @param type requested data type
209 * @param xquery extended query
210 * @param xquery_size number of bytes in xquery
211 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
212 * @param reply_bf_mutator mutation value for reply_bf
213 * @return evaluation result for the local replies
215 enum GNUNET_BLOCK_EvaluationResult
216 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
217 enum GNUNET_BLOCK_Type type, const void *xquery,
219 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
220 uint32_t reply_bf_mutator)
222 struct GetRequestContext ctx;
224 if (datacache == NULL)
225 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
226 GNUNET_STATISTICS_update (GDS_stats,
227 gettext_noop ("# GET requests given to datacache"),
229 ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
232 ctx.xquery_size = xquery_size;
233 ctx.reply_bf = reply_bf;
234 ctx.reply_bf_mutator = reply_bf_mutator;
235 (void) GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
242 * Initialize datacache subsystem.
245 GDS_DATACACHE_init ()
247 datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
252 * Shutdown datacache subsystem.
255 GDS_DATACACHE_done ()
257 if (datacache != NULL)
259 GNUNET_DATACACHE_destroy (datacache);
265 /* end of gnunet-service-dht_datacache.c */