-fix
[oweals/gnunet.git] / src / dht / gnunet-service-dht_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-dht_clients.h"
30 #include "gnunet-service-dht_datacache.h"
31 #include "gnunet-service-dht_routing.h"
32 #include "gnunet-service-dht.h"
33
34
35 /**
36  * Handle to the datacache service (for inserting/retrieving data)
37  */
38 static struct GNUNET_DATACACHE_Handle *datacache;
39
40
41 /**
42  * Entry for inserting data into datacache from the DHT.
43  */
44 struct DHTPutEntry
45 {
46   /**
47    * Size of data.
48    */
49   uint16_t data_size;
50
51   /**
52    * Length of recorded path.
53    */
54   uint16_t path_length;
55
56   /* PATH ENTRIES */
57
58   /* PUT DATA */
59
60 };
61
62
63 /**
64  * Handle a datum we've received from another peer.  Cache if
65  * possible.
66  *
67  * @param expiration when will the reply expire
68  * @param key the query this reply is for
69  * @param put_path_length number of peers in 'put_path'
70  * @param put_path path the reply took on put
71  * @param type type of the reply
72  * @param data_size number of bytes in 'data'
73  * @param data application payload data
74  */
75 void
76 GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration,
77                           const GNUNET_HashCode * key,
78                           unsigned int put_path_length,
79                           const struct GNUNET_PeerIdentity *put_path,
80                           enum GNUNET_BLOCK_Type type, size_t data_size,
81                           const void *data)
82 {
83   size_t plen =
84       data_size + put_path_length * sizeof (struct GNUNET_PeerIdentity) +
85       sizeof (struct DHTPutEntry);
86   char buf[plen];
87   struct DHTPutEntry *pe;
88   struct GNUNET_PeerIdentity *pp;
89
90   if (datacache == NULL)
91   {
92     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
93                 _("%s request received, but have no datacache!\n"), "PUT");
94     return;
95   }
96   if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
97   {
98     GNUNET_break (0);
99     return;
100   }
101   /* Put size is actual data size plus struct overhead plus path length (if any) */
102   GNUNET_STATISTICS_update (GDS_stats,
103                             gettext_noop ("# ITEMS stored in datacache"), 1,
104                             GNUNET_NO);
105   pe = (struct DHTPutEntry *) buf;
106   pe->data_size = htons (data_size);
107   pe->path_length = htons ((uint16_t) put_path_length);
108   pp = (struct GNUNET_PeerIdentity *) &pe[1];
109   memcpy (pp, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity));
110   memcpy (&pp[put_path_length], data, data_size);
111   (void) GNUNET_DATACACHE_put (datacache, key, plen, (const char *) pe, type,
112                                expiration);
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   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    * Return value to give back.
148    */
149   enum GNUNET_BLOCK_EvaluationResult eval;
150 };
151
152
153 /**
154  * Iterator for local get request results,
155  *
156  * @param cls closure for iterator, a DatacacheGetContext
157  * @param exp when does this value expire?
158  * @param key the key this data is stored under
159  * @param size the size of the data identified by key
160  * @param data the actual data
161  * @param type the type of the data
162  *
163  * @return GNUNET_OK to continue iteration, anything else
164  * to stop iteration.
165  */
166 static int
167 datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
168                         const GNUNET_HashCode * key, size_t size,
169                         const char *data, enum GNUNET_BLOCK_Type type)
170 {
171   struct GetRequestContext *ctx = cls;
172   const struct DHTPutEntry *pe;
173   const struct GNUNET_PeerIdentity *pp;
174   const char *rdata;
175   size_t rdata_size;
176   uint16_t put_path_length;
177   enum GNUNET_BLOCK_EvaluationResult eval;
178
179   pe = (const struct DHTPutEntry *) data;
180   put_path_length = ntohs (pe->path_length);
181   rdata_size = ntohs (pe->data_size);
182
183   if (size !=
184       sizeof (struct DHTPutEntry) + rdata_size +
185       (put_path_length * sizeof (struct GNUNET_PeerIdentity)))
186   {
187     GNUNET_break (0);
188     return GNUNET_OK;
189   }
190   pp = (const struct GNUNET_PeerIdentity *) &pe[1];
191   rdata = (const char *) &pp[put_path_length];
192   eval =
193       GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf,
194                              ctx->reply_bf_mutator, ctx->xquery,
195                              ctx->xquery_size, rdata, rdata_size);
196 #if DEBUG_DHT
197   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198               "Found reply for query %s in datacache, evaluation result is %d\n",
199               GNUNET_h2s (key), (int) eval);
200 #endif
201   ctx->eval = eval;
202   switch (eval)
203   {
204   case GNUNET_BLOCK_EVALUATION_OK_LAST:
205   case GNUNET_BLOCK_EVALUATION_OK_MORE:
206     /* forward to local clients */
207     GNUNET_STATISTICS_update (GDS_stats,
208                               gettext_noop
209                               ("# Good RESULTS found in datacache"), 1,
210                               GNUNET_NO);
211     GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, pp, type,
212                               rdata_size, rdata);
213     /* forward to other peers */
214     GDS_ROUTING_process (type, exp, key, put_path_length, pp, 0, NULL, rdata,
215                          rdata_size);
216     break;
217   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
218     GNUNET_STATISTICS_update (GDS_stats,
219                               gettext_noop
220                               ("# Duplicate RESULTS found in datacache"), 1,
221                               GNUNET_NO);
222     break;
223   case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
224     GNUNET_STATISTICS_update (GDS_stats,
225                               gettext_noop
226                               ("# Invalid RESULTS found in datacache"), 1,
227                               GNUNET_NO);
228     break;
229   case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
230     GNUNET_break (0);
231     break;
232   case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
233     GNUNET_break_op (0);
234     return GNUNET_SYSERR;
235   case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
236     GNUNET_STATISTICS_update (GDS_stats,
237                               gettext_noop
238                               ("# Unsupported RESULTS found in datacache"), 1,
239                               GNUNET_NO);
240     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
241                 _("Unsupported block type (%u) in local response!\n"), type);
242     break;
243   }
244   return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
245 }
246
247
248 /**
249  * Handle a GET request we've received from another peer.
250  *
251  * @param key the query
252  * @param type requested data type
253  * @param xquery extended query
254  * @param xquery_size number of bytes in xquery
255  * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
256  * @param reply_bf_mutator mutation value for reply_bf
257  * @return evaluation result for the local replies
258  */
259 enum GNUNET_BLOCK_EvaluationResult
260 GDS_DATACACHE_handle_get (const GNUNET_HashCode * key,
261                           enum GNUNET_BLOCK_Type type, const void *xquery,
262                           size_t xquery_size,
263                           struct GNUNET_CONTAINER_BloomFilter **reply_bf,
264                           uint32_t reply_bf_mutator)
265 {
266   struct GetRequestContext ctx;
267
268   if (datacache == NULL)
269     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
270   GNUNET_STATISTICS_update (GDS_stats,
271                             gettext_noop ("# GET requests given to datacache"),
272                             1, GNUNET_NO);
273   ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
274   ctx.key = *key;
275   ctx.xquery = xquery;
276   ctx.xquery_size = xquery_size;
277   ctx.reply_bf = reply_bf;
278   ctx.reply_bf_mutator = reply_bf_mutator;
279   (void) GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
280                                &ctx);
281   return ctx.eval;
282 }
283
284
285 /**
286  * Initialize datacache subsystem.
287  */
288 void
289 GDS_DATACACHE_init ()
290 {
291   datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
292 }
293
294
295 /**
296  * Shutdown datacache subsystem.
297  */
298 void
299 GDS_DATACACHE_done ()
300 {
301   if (datacache != NULL)
302   {
303     GNUNET_DATACACHE_destroy (datacache);
304     datacache = NULL;
305   }
306 }
307
308
309 /* end of gnunet-service-dht_datacache.c */