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