-fixing #2397
[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 struct 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   struct 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 struct 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   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "Found reply for query %s in datacache, evaluation result is %d\n",
198               GNUNET_h2s (key), (int) eval);
199   ctx->eval = eval;
200   switch (eval)
201   {
202   case GNUNET_BLOCK_EVALUATION_OK_LAST:
203   case GNUNET_BLOCK_EVALUATION_OK_MORE:
204     /* forward to local clients */
205     GNUNET_STATISTICS_update (GDS_stats,
206                               gettext_noop
207                               ("# Good RESULTS found in datacache"), 1,
208                               GNUNET_NO);
209     GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, pp, type,
210                               rdata_size, rdata);
211     /* forward to other peers */
212     GDS_ROUTING_process (type, exp, key, put_path_length, pp, 0, NULL, rdata,
213                          rdata_size);
214     break;
215   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
216     GNUNET_STATISTICS_update (GDS_stats,
217                               gettext_noop
218                               ("# Duplicate RESULTS found in datacache"), 1,
219                               GNUNET_NO);
220     break;
221   case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
222     GNUNET_STATISTICS_update (GDS_stats,
223                               gettext_noop
224                               ("# Invalid RESULTS found in datacache"), 1,
225                               GNUNET_NO);
226     break;
227   case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
228     GNUNET_break (0);
229     break;
230   case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
231     GNUNET_break_op (0);
232     return GNUNET_SYSERR;
233   case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
234     GNUNET_STATISTICS_update (GDS_stats,
235                               gettext_noop
236                               ("# Unsupported RESULTS found in datacache"), 1,
237                               GNUNET_NO);
238     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
239                 _("Unsupported block type (%u) in local response!\n"), type);
240     break;
241   }
242   return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
243 }
244
245
246 /**
247  * Handle a GET request we've received from another peer.
248  *
249  * @param key the query
250  * @param type requested data type
251  * @param xquery extended query
252  * @param xquery_size number of bytes in xquery
253  * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
254  * @param reply_bf_mutator mutation value for reply_bf
255  * @return evaluation result for the local replies
256  */
257 enum GNUNET_BLOCK_EvaluationResult
258 GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key,
259                           enum GNUNET_BLOCK_Type type, const void *xquery,
260                           size_t xquery_size,
261                           struct GNUNET_CONTAINER_BloomFilter **reply_bf,
262                           uint32_t reply_bf_mutator)
263 {
264   struct GetRequestContext ctx;
265
266   if (datacache == NULL)
267     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
268   GNUNET_STATISTICS_update (GDS_stats,
269                             gettext_noop ("# GET requests given to datacache"),
270                             1, GNUNET_NO);
271   ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
272   ctx.key = *key;
273   ctx.xquery = xquery;
274   ctx.xquery_size = xquery_size;
275   ctx.reply_bf = reply_bf;
276   ctx.reply_bf_mutator = reply_bf_mutator;
277   (void) GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator,
278                                &ctx);
279   return ctx.eval;
280 }
281
282
283 /**
284  * Initialize datacache subsystem.
285  */
286 void
287 GDS_DATACACHE_init ()
288 {
289   datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
290 }
291
292
293 /**
294  * Shutdown datacache subsystem.
295  */
296 void
297 GDS_DATACACHE_done ()
298 {
299   if (datacache != NULL)
300   {
301     GNUNET_DATACACHE_destroy (datacache);
302     datacache = NULL;
303   }
304 }
305
306
307 /* end of gnunet-service-dht_datacache.c */