-extend block API to allow specifying flags, use this to allow reducing checks for...
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_datacache.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011 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-xdht_clients.h"
30 #include "gnunet-service-xdht_datacache.h"
31 #include "gnunet-service-xdht_routing.h"
32 #include "gnunet-service-xdht_neighbours.h"
33 #include "gnunet-service-dht.h"
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__VA_ARGS__)
36
37 #define DEBUG(...)                                           \
38   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
39
40 /**
41  * Handle to the datacache service (for inserting/retrieving data)
42  */
43 static struct GNUNET_DATACACHE_Handle *datacache;
44
45
46 /**
47  * Handle a datum we've received from another peer.  Cache if
48  * possible.
49  *
50  * @param expiration when will the reply expire
51  * @param key the query this reply is for
52  * @param put_path_length number of peers in 'put_path'
53  * @param put_path path the reply took on put
54  * @param type type of the reply
55  * @param data_size number of bytes in 'data'
56  * @param data application payload data
57  */
58 void
59 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
60                           const struct GNUNET_HashCode * key,
61                           unsigned int put_path_length,
62                           const struct GNUNET_PeerIdentity *put_path,
63                           enum GNUNET_BLOCK_Type type, size_t data_size,
64                           const void *data)
65 {
66   int r;
67
68   if (NULL == datacache)
69   {
70     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
71                 _("%s request received, but have no datacache!\n"), "PUT");
72     return;
73   }
74   if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
75   {
76     GNUNET_break (0);
77     return;
78   }
79
80   /* Put size is actual data size plus struct overhead plus path length (if any) */
81   GNUNET_STATISTICS_update (GDS_stats,
82                             gettext_noop ("# ITEMS stored in datacache"), 1,
83                             GNUNET_NO);
84
85   struct GNUNET_PeerIdentity peer = GDS_NEIGHBOURS_get_my_id();
86   DEBUG("DATACACHE_PUT KEY = %s, peer = %s\n",GNUNET_h2s(key),GNUNET_i2s(&peer));
87   r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration,
88                             put_path_length, put_path);
89   LOG (GNUNET_ERROR_TYPE_DEBUG,
90        "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
91        GNUNET_h2s (key), data_size, r, put_path_length);
92 }
93
94 /**
95  * List of peers in the get path.
96  */
97 struct GetPath
98 {
99   /**
100    * Pointer to next item in the list
101    */
102   struct GetPath *next;
103
104   /**
105    * Pointer to previous item in the list
106    */
107   struct GetPath *prev;
108
109   /**
110    *  An element in the get path.
111    */
112   struct GNUNET_PeerIdentity peer;
113 };
114
115
116 /**
117  * Context containing information about a GET request.
118  */
119 struct GetRequestContext
120 {
121   /**
122    * extended query (see gnunet_block_lib.h).
123    */
124   const void *xquery;
125
126   /**
127    * Bloomfilter to filter out duplicate replies (updated)
128    */
129   struct GNUNET_CONTAINER_BloomFilter **reply_bf;
130
131   /**
132    * The key this request was about
133    */
134   struct GNUNET_HashCode key;
135
136   /**
137    * Number of bytes in xquery.
138    */
139   size_t xquery_size;
140
141   /**
142    * Mutator value for the reply_bf, see gnunet_block_lib.h
143    */
144   uint32_t reply_bf_mutator;
145
146   /**
147    * Total number of peers in get path.
148    */
149   unsigned int get_path_length;
150
151   /**
152    * Return value to give back.
153    */
154   enum GNUNET_BLOCK_EvaluationResult eval;
155
156   /**
157    * Peeer which has the data for the key.
158    */
159   struct GNUNET_PeerIdentity source_peer;
160
161   /**
162    * Next hop to forward the get result to.
163    */
164   struct GNUNET_PeerIdentity next_hop;
165
166   /**
167    * Head of get path.
168    */
169   struct GetPath *head;
170
171   /**
172    * Tail of get path.
173    */
174   struct GetPath *tail;
175
176   /* get_path */
177 };
178
179
180 /**
181  * Iterator for local get request results,
182  *
183  * @param cls closure for iterator, a `struct GetRequestContext`
184  * @param key the key this data is stored under
185  * @param size the size of the data identified by key
186  * @param data the actual data
187  * @param type the type of the data
188  * @param exp when does this value expire?
189  * @param put_path_length number of peers in @a put_path
190  * @param put_path path the reply took on put
191  * @return #GNUNET_OK to continue iteration, anything else
192  * to stop iteration.
193  */
194 static int
195 datacache_get_iterator (void *cls,
196                         const struct GNUNET_HashCode *key,
197                         size_t size,
198                         const char *data,
199                         enum GNUNET_BLOCK_Type type,
200                         struct GNUNET_TIME_Absolute exp,
201                         unsigned int put_path_length,
202                         const struct GNUNET_PeerIdentity *put_path)
203 {
204   struct GetRequestContext *ctx = cls;
205   enum GNUNET_BLOCK_EvaluationResult eval;
206
207   eval =
208       GNUNET_BLOCK_evaluate (GDS_block_context,
209                              type,
210                              GNUNET_BLOCK_EO_NONE,
211                              key,
212                              ctx->reply_bf,
213                              ctx->reply_bf_mutator,
214                              ctx->xquery,
215                              ctx->xquery_size,
216                              data,
217                              size);
218   LOG (GNUNET_ERROR_TYPE_DEBUG,
219        "Found reply for query %s in datacache, evaluation result is %d\n",
220        GNUNET_h2s (key), (int) eval);
221   ctx->eval = eval;
222
223   switch (eval)
224   {
225   case GNUNET_BLOCK_EVALUATION_OK_MORE:
226   case GNUNET_BLOCK_EVALUATION_OK_LAST:
227     /* forward to local clients */
228     GNUNET_STATISTICS_update (GDS_stats,
229                               gettext_noop
230                               ("# Good RESULTS found in datacache"), 1,
231                               GNUNET_NO);
232     struct GNUNET_PeerIdentity *get_path;
233     get_path = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity) *
234                               ctx->get_path_length);
235     struct GetPath *iterator;
236     iterator = ctx->head;
237     int i = 0;
238     while (i < ctx->get_path_length)
239     {
240       get_path[i] = iterator->peer;
241       i++;
242       iterator = iterator->next;
243     }
244     GDS_NEIGHBOURS_send_get_result (key,type, &(ctx->next_hop),&(ctx->source_peer),
245                                     put_path_length, put_path, ctx->get_path_length,
246                                     get_path, exp, data, size );
247     GNUNET_free_non_null (get_path);
248
249     break;
250   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
251     GNUNET_STATISTICS_update (GDS_stats,
252                               gettext_noop
253                               ("# Duplicate RESULTS found in datacache"), 1,
254                               GNUNET_NO);
255     break;
256   case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
257     GNUNET_STATISTICS_update (GDS_stats,
258                               gettext_noop
259                               ("# Invalid RESULTS found in datacache"), 1,
260                               GNUNET_NO);
261     break;
262   case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
263     GNUNET_STATISTICS_update (GDS_stats,
264                               gettext_noop
265                               ("# Irrelevant RESULTS found in datacache"), 1,
266                               GNUNET_NO);
267     break;
268   case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
269     GNUNET_break (0);
270     break;
271   case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
272     GNUNET_break_op (0);
273     return GNUNET_SYSERR;
274   case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
275     GNUNET_STATISTICS_update (GDS_stats,
276                               gettext_noop
277                               ("# Unsupported RESULTS found in datacache"), 1,
278                               GNUNET_NO);
279     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
280                 _("Unsupported block type (%u) in local response!\n"), type);
281     break;
282   }
283
284   return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
285 }
286
287
288 /**
289  * Handle a GET request we've received from another peer.
290  *
291  * @param key the query
292  * @param type requested data type
293  * @param xquery extended query
294  * @param xquery_size number of bytes in xquery
295  * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
296  * @param reply_bf_mutator mutation value for reply_bf
297  * @return evaluation result for the local replies
298  * @get_path_length Total number of peers in get path
299  * @get_path Peers in get path.
300  */
301 enum GNUNET_BLOCK_EvaluationResult
302 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
303                           enum GNUNET_BLOCK_Type type, const void *xquery,
304                           size_t xquery_size,
305                           struct GNUNET_CONTAINER_BloomFilter **reply_bf,
306                           uint32_t reply_bf_mutator,
307                           uint32_t get_path_length,
308                           struct GNUNET_PeerIdentity *get_path,
309                           struct GNUNET_PeerIdentity *next_hop,
310                           struct GNUNET_PeerIdentity *source_peer)
311 {
312   struct GetRequestContext ctx;
313   unsigned int r;
314
315   if (datacache == NULL)
316     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
317   GNUNET_STATISTICS_update (GDS_stats,
318                             gettext_noop ("# GET requests given to datacache"),
319                             1, GNUNET_NO);
320   ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
321   ctx.key = *key;
322   ctx.xquery = xquery;
323   ctx.xquery_size = xquery_size;
324   ctx.reply_bf = reply_bf;
325   ctx.reply_bf_mutator = reply_bf_mutator;
326   ctx.get_path_length = get_path_length;
327
328   if (next_hop != NULL)
329   {
330     memcpy (&(ctx.next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
331   }
332   unsigned int i = 0;
333
334   ctx.head = NULL;
335   ctx.tail = NULL;
336   if (get_path != NULL)
337   {
338     while (i < get_path_length)
339     {
340       struct GetPath *element;
341       element = GNUNET_new (struct GetPath);
342       element->next = NULL;
343       element->prev = NULL;
344       element->peer = get_path[i];
345       GNUNET_CONTAINER_DLL_insert_tail (ctx.head, ctx.tail, element);
346       i++;
347     }
348   }
349
350   r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
351                             &ctx);
352   DEBUG ("DATACACHE_GET for key %s completed (%d). %u results found.\n",GNUNET_h2s (key), ctx.eval, r);
353   return ctx.eval;
354 }
355
356
357 /**
358  * Initialize datacache subsystem.
359  */
360 void
361 GDS_DATACACHE_init ()
362 {
363   datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
364 }
365
366
367 /**
368  * Shutdown datacache subsystem.
369  */
370 void
371 GDS_DATACACHE_done ()
372 {
373   if (datacache != NULL)
374   {
375     GNUNET_DATACACHE_destroy (datacache);
376     datacache = NULL;
377   }
378 }
379
380
381 /* end of gnunet-service-dht_datacache.c */