abort on error -- missing return
[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,
81                           size_t data_size,
82                           const void *data)
83 {
84   size_t plen = data_size + put_path_length * sizeof(struct GNUNET_PeerIdentity) + sizeof(struct DHTPutEntry);
85   char buf[plen];
86   struct DHTPutEntry *pe;
87   struct GNUNET_PeerIdentity *pp;
88
89   if (datacache == NULL)
90     {
91       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
92                   "%s request received, but have no datacache!\n",
93                   "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],
111           data, data_size);
112   (void) GNUNET_DATACACHE_put (datacache, key, 
113                                plen, (const char *) pe, type,
114                                expiration);
115 }
116
117
118 /**
119  * Context containing information about a GET request.
120  */
121 struct GetRequestContext
122 {
123   /**
124    * extended query (see gnunet_block_lib.h).
125    */
126   const void *xquery;
127
128   /**
129    * Bloomfilter to filter out duplicate replies (updated)
130    */
131   struct GNUNET_CONTAINER_BloomFilter **reply_bf;
132
133   /**
134    * The key this request was about
135    */
136   GNUNET_HashCode key;
137
138   /**
139    * Number of bytes in xquery.
140    */
141   size_t xquery_size;
142
143   /**
144    * Mutator value for the reply_bf, see gnunet_block_lib.h
145    */
146   uint32_t reply_bf_mutator;
147
148   /**
149    * Return value to give back.
150    */
151   enum GNUNET_BLOCK_EvaluationResult eval;
152 };
153
154
155 /**
156  * Iterator for local get request results,
157  *
158  * @param cls closure for iterator, a DatacacheGetContext
159  * @param exp when does this value expire?
160  * @param key the key this data is stored under
161  * @param size the size of the data identified by key
162  * @param data the actual data
163  * @param type the type of the data
164  *
165  * @return GNUNET_OK to continue iteration, anything else
166  * to stop iteration.
167  */
168 static int
169 datacache_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
170                         const GNUNET_HashCode * key, size_t size,
171                         const char *data, enum GNUNET_BLOCK_Type type)
172 {
173   struct GetRequestContext *ctx = cls;
174   const struct DHTPutEntry *pe;
175   const struct GNUNET_PeerIdentity *pp;
176   const char *rdata;
177   size_t rdata_size;
178   uint16_t put_path_length;
179   enum GNUNET_BLOCK_EvaluationResult eval;
180
181   pe = (const struct DHTPutEntry *) data;
182   put_path_length = ntohs (pe->path_length);
183   rdata_size = ntohs (pe->data_size);
184
185   if (size !=
186       sizeof (struct DHTPutEntry) + rdata_size +
187       (put_path_length * sizeof (struct GNUNET_PeerIdentity)))
188   {
189     GNUNET_break (0);
190     return GNUNET_OK;
191   }
192   pp = (const struct GNUNET_PeerIdentity *) &pe[1];
193   rdata = (const char*) &pp[put_path_length];
194   eval =
195       GNUNET_BLOCK_evaluate (GDS_block_context, type, key, 
196                              ctx->reply_bf,
197                              ctx->reply_bf_mutator,
198                              ctx->xquery,
199                              ctx->xquery_size, 
200                              rdata,
201                              rdata_size);
202   ctx->eval = eval;      
203   switch (eval)
204   {
205   case GNUNET_BLOCK_EVALUATION_OK_LAST:
206   case GNUNET_BLOCK_EVALUATION_OK_MORE:
207     /* forward to local clients */
208     GNUNET_STATISTICS_update (GDS_stats,
209                               gettext_noop ("# Good RESULTS found in datacache"), 1,
210                               GNUNET_NO);
211     GDS_CLIENTS_handle_reply (exp,
212                              key,
213                              0, NULL,
214                              put_path_length, pp,
215                              type, rdata_size, rdata);
216     /* forward to other peers */
217     GDS_ROUTING_process (type, exp,
218                          key, put_path_length, pp, 
219                          0, NULL, rdata, rdata_size);
220     break;
221   case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
222     GNUNET_STATISTICS_update (GDS_stats,
223                               gettext_noop ("# Duplicate RESULTS found in datacache"), 1,
224                               GNUNET_NO);
225     break;
226   case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
227     GNUNET_STATISTICS_update (GDS_stats,
228                               gettext_noop ("# Invalid RESULTS found in datacache"), 1,
229                               GNUNET_NO);
230     break;
231   case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
232     GNUNET_break (0);
233     break;
234   case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
235     GNUNET_break_op (0);
236     return GNUNET_SYSERR;
237   case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
238     GNUNET_STATISTICS_update (GDS_stats,
239                               gettext_noop ("# Unsupported RESULTS found in datacache"), 1,
240                               GNUNET_NO);
241     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
242                 "Unsupported block type (%u) in local response!\n",
243                 type);
244     break;
245   }
246   return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK;
247 }
248
249
250 /**
251  * Handle a GET request we've received from another peer.
252  *
253  * @param key the query 
254  * @param type requested data type
255  * @param xquery extended query
256  * @param xquery_size number of bytes in xquery
257  * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
258  * @param reply_bf_mutator mutation value for reply_bf
259  * @return evaluation result for the local replies
260  */
261 enum GNUNET_BLOCK_EvaluationResult
262 GDS_DATACACHE_handle_get (const GNUNET_HashCode *key,
263                           enum GNUNET_BLOCK_Type type,
264                           const void *xquery,
265                           size_t xquery_size,
266                           struct GNUNET_CONTAINER_BloomFilter **reply_bf,
267                           uint32_t reply_bf_mutator)
268 {
269   struct GetRequestContext ctx;
270
271   if (datacache == NULL)
272     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
273   GNUNET_STATISTICS_update (GDS_stats,
274                             gettext_noop ("# GET requests given to datacache"), 1,
275                             GNUNET_NO);
276   ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
277   ctx.key = *key;
278   ctx.xquery = xquery;
279   ctx.xquery_size = xquery_size;
280   ctx.reply_bf = reply_bf;
281   ctx.reply_bf_mutator = reply_bf_mutator;
282   (void) GNUNET_DATACACHE_get (datacache, key, type,
283                                &datacache_get_iterator, &ctx);
284   return ctx.eval;
285 }
286
287
288 /**
289  * Initialize datacache subsystem.
290  */
291 void 
292 GDS_DATACACHE_init ()
293 {
294   datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache");
295 }
296
297
298 /**
299  * Shutdown datacache subsystem.
300  */
301 void
302 GDS_DATACACHE_done ()
303 {
304   if (datacache != NULL)
305   {
306     GNUNET_DATACACHE_destroy (datacache);
307     datacache = NULL;
308   }
309 }
310
311
312 /* end of gnunet-service-dht_datacache.c */