2 This file is part of GNUnet
3 Copyright (C) 2010, 2013 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file fs/plugin_block_fs.c
23 * @brief blocks used for file-sharing
24 * @author Christian Grothoff
28 #include "gnunet_block_plugin.h"
29 #include "gnunet_fs_service.h"
31 #include "gnunet_signatures.h"
35 * Number of bits we set per entry in the bloomfilter.
38 #define BLOOMFILTER_K 16
41 * Function called to validate a reply or a request. For
42 * request evaluation, simply pass "NULL" for the reply_block.
43 * Note that it is assumed that the reply has already been
44 * matched to the key (and signatures checked) as it would
45 * be done with the #GNUNET_BLOCK_get_key() function.
48 * @param type block type
49 * @param eo control flags
50 * @param query original query (hash)
51 * @param bf pointer to bloom filter associated with query; possibly updated (!)
52 * @param bf_mutator mutation value for @a bf
53 * @param xquery extrended query data (can be NULL, depending on type)
54 * @param xquery_size number of bytes in @a xquery
55 * @param reply_block response to validate
56 * @param reply_block_size number of bytes in @a reply_block
57 * @return characterization of result
59 static enum GNUNET_BLOCK_EvaluationResult
60 block_plugin_fs_evaluate (void *cls,
61 enum GNUNET_BLOCK_Type type,
62 enum GNUNET_BLOCK_EvaluationOptions eo,
63 const struct GNUNET_HashCode *query,
64 struct GNUNET_CONTAINER_BloomFilter **bf,
68 const void *reply_block,
69 size_t reply_block_size)
71 const struct UBlock *ub;
72 struct GNUNET_HashCode hc;
73 struct GNUNET_HashCode chash;
74 struct GNUNET_HashCode mhash;
78 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
79 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
83 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
85 if (NULL == reply_block)
86 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
87 return GNUNET_BLOCK_EVALUATION_OK_LAST;
88 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
92 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
94 if (NULL == reply_block)
95 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
97 if (reply_block_size < sizeof (struct UBlock))
100 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
103 GNUNET_CRYPTO_hash (&ub->verification_key,
104 sizeof (ub->verification_key),
106 if (0 != memcmp (&hc,
108 sizeof (struct GNUNET_HashCode)))
111 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
113 if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
116 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
118 if ( (0 == (eo & GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO)) &&
120 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
123 &ub->verification_key)) )
126 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
130 GNUNET_CRYPTO_hash (reply_block,
133 GNUNET_BLOCK_mingle_hash (&chash,
139 GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
140 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
144 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
146 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
148 return GNUNET_BLOCK_EVALUATION_OK_MORE;
150 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
156 * Function called to obtain the key for a block.
159 * @param type block type
160 * @param block block to get the key for
161 * @param block_size number of bytes in @a block
162 * @param key set to the key (query) for the given block
163 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
164 * (or if extracting a key from a block of this type does not work)
167 block_plugin_fs_get_key (void *cls,
168 enum GNUNET_BLOCK_Type type,
171 struct GNUNET_HashCode *key)
173 const struct UBlock *ub;
177 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
178 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
179 GNUNET_CRYPTO_hash (block, block_size, key);
181 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
182 if (block_size < sizeof (struct UBlock))
185 return GNUNET_SYSERR;
188 GNUNET_CRYPTO_hash (&ub->verification_key,
189 sizeof (ub->verification_key),
194 return GNUNET_SYSERR;
200 * Entry point for the plugin.
203 libgnunet_plugin_block_fs_init (void *cls)
205 static enum GNUNET_BLOCK_Type types[] =
207 GNUNET_BLOCK_TYPE_FS_DBLOCK,
208 GNUNET_BLOCK_TYPE_FS_IBLOCK,
209 GNUNET_BLOCK_TYPE_FS_UBLOCK,
210 GNUNET_BLOCK_TYPE_ANY /* end of list */
212 struct GNUNET_BLOCK_PluginFunctions *api;
214 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
215 api->evaluate = &block_plugin_fs_evaluate;
216 api->get_key = &block_plugin_fs_get_key;
223 * Exit point from the plugin.
226 libgnunet_plugin_block_fs_done (void *cls)
228 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
234 /* end of plugin_block_fs.c */