- ttl is deprecated, don't warn
[oweals/gnunet.git] / src / dht / gnunet-service-dht_datacache.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011, 2015 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file dht/gnunet-service-dht_datacache.c
23  * @brief GNUnet DHT service's datacache integration
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27 #include "platform.h"
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"
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dhtcache",__VA_ARGS__)
35
36
37 /**
38  * Handle to the datacache service (for inserting/retrieving data)
39  */
40 static struct GNUNET_DATACACHE_Handle *datacache;
41
42
43 /**
44  * Handle a datum we've received from another peer.  Cache if
45  * possible.
46  *
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 @a 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 @a data
53  * @param data application payload data
54  */
55 void
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,
61                           size_t data_size,
62                           const void *data)
63 {
64   int r;
65
66   if (NULL == datacache)
67   {
68     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
69                 _("%s request received, but have no datacache!\n"), "PUT");
70     return;
71   }
72   if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
73   {
74     GNUNET_break (0);
75     return;
76   }
77   /* Put size is actual data size plus struct overhead plus path length (if any) */
78   GNUNET_STATISTICS_update (GDS_stats,
79                             gettext_noop ("# ITEMS stored in datacache"), 1,
80                             GNUNET_NO);
81   r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
82                             put_path_length, put_path);
83   LOG (GNUNET_ERROR_TYPE_DEBUG,
84        "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
85        GNUNET_h2s (key), data_size, r, put_path_length);
86 }
87
88
89 /**
90  * Context containing information about a GET request.
91  */
92 struct GetRequestContext
93 {
94   /**
95    * extended query (see gnunet_block_lib.h).
96    */
97   const void *xquery;
98
99   /**
100    * Bloomfilter to filter out duplicate replies (updated)
101    */
102   struct GNUNET_CONTAINER_BloomFilter **reply_bf;
103
104   /**
105    * The key this request was about
106    */
107   struct GNUNET_HashCode key;
108
109   /**
110    * Number of bytes in xquery.
111    */
112   size_t xquery_size;
113
114   /**
115    * Mutator value for the reply_bf, see gnunet_block_lib.h
116    */
117   uint32_t reply_bf_mutator;
118
119   /**
120    * Return value to give back.
121    */
122   enum GNUNET_BLOCK_EvaluationResult eval;
123 };
124
125
126 /**
127  * Iterator for local get request results,
128  *
129  * @param cls closure for iterator, a DatacacheGetContext
130  * @param exp when does this value expire?
131  * @param key the key this data is stored under
132  * @param size the size of the data identified by key
133  * @param data the actual data
134  * @param type the type of the @a data
135  * @param put_path_length number of peers in @a put_path
136  * @param put_path path the reply took on put
137  * @return #GNUNET_OK to continue iteration, anything else
138  * to stop iteration.
139  */
140 static int
141 datacache_get_iterator (void *cls,
142                         const struct GNUNET_HashCode *key,
143                         size_t size,
144                         const char *data,
145                         enum GNUNET_BLOCK_Type type,
146                         struct GNUNET_TIME_Absolute exp,
147                         unsigned int put_path_length,
148                         const struct GNUNET_PeerIdentity *put_path)
149 {
150   struct GetRequestContext *ctx = cls;
151   enum GNUNET_BLOCK_EvaluationResult eval;
152
153   eval =
154       GNUNET_BLOCK_evaluate (GDS_block_context,
155                              type,
156                              GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
157                              key,
158                              ctx->reply_bf,
159                              ctx->reply_bf_mutator,
160                              ctx->xquery,
161                              ctx->xquery_size,
162                              data,
163                              size);
164   LOG (GNUNET_ERROR_TYPE_DEBUG,
165        "Found reply for query %s in datacache, evaluation result is %d\n",
166        GNUNET_h2s (key),
167        (int) eval);
168   ctx->eval = eval;
169   switch (eval)
170   {
171   case GNUNET_BLOCK_EVALUATION_OK_MORE:
172   case GNUNET_BLOCK_EVALUATION_OK_LAST:
173     /* forward to local clients */
174     GNUNET_STATISTICS_update (GDS_stats,
175                               gettext_noop
176                               ("# Good RESULTS found in datacache"), 1,
177                               GNUNET_NO);
178     GDS_CLIENTS_handle_reply (exp, key,
179                               0, NULL,
180                               put_path_length, put_path,
181                               type,
182                               size, data);
183     /* forward to other peers */
184     GDS_ROUTING_process (type,
185                          exp,
186                          key,
187                          put_path_length, put_path,
188                          0, NULL,
189                          data, size);
190     break;
191   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
192     GNUNET_STATISTICS_update (GDS_stats,
193                               gettext_noop ("# Duplicate RESULTS found in datacache"),
194                               1,
195                               GNUNET_NO);
196     break;
197   case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
198     GNUNET_STATISTICS_update (GDS_stats,
199                               gettext_noop ("# Invalid RESULTS found in datacache"),
200                               1,
201                               GNUNET_NO);
202     break;
203   case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
204     GNUNET_STATISTICS_update (GDS_stats,
205                               gettext_noop ("# Irrelevant RESULTS found in datacache"),
206                               1,
207                               GNUNET_NO);
208     break;
209   case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
210     GNUNET_break (0);
211     break;
212   case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
213     GNUNET_break_op (0);
214     return GNUNET_SYSERR;
215   case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
216     GNUNET_STATISTICS_update (GDS_stats,
217                               gettext_noop ("# Unsupported RESULTS found in datacache"),
218                               1,
219                               GNUNET_NO);
220     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
221                 _("Unsupported block type (%u) in local response!\n"),
222                 type);
223     break;
224   }
225   return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
226 }
227
228
229 /**
230  * Handle a GET request we've received from another peer.
231  *
232  * @param key the query
233  * @param type requested data type
234  * @param xquery extended query
235  * @param xquery_size number of bytes in @a xquery
236  * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
237  * @param reply_bf_mutator mutation value for @a reply_bf
238  * @return evaluation result for the local replies
239  */
240 enum GNUNET_BLOCK_EvaluationResult
241 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
242                           enum GNUNET_BLOCK_Type type,
243                           const void *xquery,
244                           size_t xquery_size,
245                           struct GNUNET_CONTAINER_BloomFilter **reply_bf,
246                           uint32_t reply_bf_mutator)
247 {
248   struct GetRequestContext ctx;
249   unsigned int r;
250
251   if (NULL == datacache)
252     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
253   GNUNET_STATISTICS_update (GDS_stats,
254                             gettext_noop ("# GET requests given to datacache"),
255                             1,
256                             GNUNET_NO);
257   ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
258   ctx.key = *key;
259   ctx.xquery = xquery;
260   ctx.xquery_size = xquery_size;
261   ctx.reply_bf = reply_bf;
262   ctx.reply_bf_mutator = reply_bf_mutator;
263   r = GNUNET_DATACACHE_get (datacache,
264                             key,
265                             type,
266                             &datacache_get_iterator,
267                             &ctx);
268   LOG (GNUNET_ERROR_TYPE_DEBUG,
269        "DATACACHE GET for key %s completed (%d). %u results found.\n",
270        GNUNET_h2s (key),
271        ctx.eval,
272        r);
273   return ctx.eval;
274 }
275
276
277 /**
278  * Initialize datacache subsystem.
279  */
280 void
281 GDS_DATACACHE_init ()
282 {
283   datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
284 }
285
286
287 /**
288  * Shutdown datacache subsystem.
289  */
290 void
291 GDS_DATACACHE_done ()
292 {
293   if (NULL != datacache)
294   {
295     GNUNET_DATACACHE_destroy (datacache);
296     datacache = NULL;
297   }
298 }
299
300
301 /* end of gnunet-service-dht_datacache.c */