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