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