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