2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/plugin_block_mesh.c
23 * @brief blocks used for mesh peer discovery
24 * @author Bartlomiej Polot
28 #include "gnunet_block_plugin.h"
29 #include "block_mesh.h"
32 * Number of bits we set per entry in the bloomfilter.
35 #define BLOOMFILTER_K 16
39 * Check if the regex block is well formed, including all edges
41 * @param block The start of the block.
42 * @param size The size of the block.
44 * @return GNUNET_OK in case it's fine, GNUNET_SYSERR otherwise.
47 check_mesh_regex_block (const struct MeshRegexBlock *block, size_t size)
49 struct MeshRegexEdge *edge;
56 offset = sizeof (struct MeshRegexBlock);
57 if (offset > size) // Is it safe to access the regex block?
59 n = ntohl (block->n_proof);
61 if (offset > size) // Is it safe to access the regex proof?
63 aux = (char *) &block[1]; // Skip regex block
64 aux = &aux[n]; // Skip regex proof
65 n = ntohl (block->n_edges);
66 for (i = 0; i < n; n++) // aux always points at the end of the previous block
68 offset += sizeof (struct MeshRegexEdge);
69 if (offset > size) // Is it safe to access the next edge block?
71 edge = (struct MeshRegexEdge *) aux;
72 n_token = ntohl (edge->n_token);
74 if (offset > size) // Is it safe to access the edge token?
76 aux = (char *) &edge[1]; // Skip edge block
77 aux = &aux[n_token]; // Skip edge token
79 // The total size should be exactly the size of (regex + all edges) blocks
80 return (offset == size) ? GNUNET_OK : GNUNET_SYSERR;
85 * Function called to validate a reply or a request. For
86 * request evaluation, simply pass "NULL" for the reply_block.
87 * Note that it is assumed that the reply has already been
88 * matched to the key (and signatures checked) as it would
89 * be done with the "get_key" function.
92 * @param type block type
93 * @param query original query (hash)
94 * @param bf pointer to bloom filter associated with query; possibly updated (!)
95 * @param bf_mutator mutation value for bf
96 * @param xquery extrended query data (can be NULL, depending on type)
97 * @param xquery_size number of bytes in xquery
98 * @param reply_block response to validate
99 * @param reply_block_size number of bytes in reply block
100 * @return characterization of result
102 static enum GNUNET_BLOCK_EvaluationResult
103 block_plugin_mesh_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
104 const struct GNUNET_HashCode * query,
105 struct GNUNET_CONTAINER_BloomFilter **bf,
106 int32_t bf_mutator, const void *xquery,
107 size_t xquery_size, const void *reply_block,
108 size_t reply_block_size)
110 struct GNUNET_HashCode chash;
111 struct GNUNET_HashCode mhash;
115 case GNUNET_BLOCK_TYPE_MESH_PEER:
116 if (0 != xquery_size)
119 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
121 if (NULL == reply_block)
122 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
123 if (sizeof (struct PBlock) != reply_block_size)
124 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
125 return GNUNET_BLOCK_EVALUATION_OK_LAST;
126 case GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE:
127 /* FIXME: have an xquery? not sure */
128 if (0 != xquery_size)
131 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
133 if (NULL == reply_block)
134 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
135 if (sizeof (struct PBlock) != reply_block_size)
136 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
139 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
140 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
143 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
144 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
148 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
150 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
152 return GNUNET_BLOCK_EVALUATION_OK_MORE;
153 case GNUNET_BLOCK_TYPE_MESH_REGEX:
154 if (0 != xquery_size)
157 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
159 if (NULL == reply_block)
160 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
161 if (GNUNET_OK != check_mesh_regex_block (reply_block, reply_block_size))
162 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
165 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
166 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
169 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
170 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
174 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
176 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
178 return GNUNET_BLOCK_EVALUATION_OK_MORE;
179 case GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT:
180 if (0 != xquery_size)
183 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
185 if (NULL == reply_block)
186 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
187 if (sizeof (struct MeshRegexAccept) != reply_block_size)
188 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
191 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
192 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
195 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
196 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
200 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
202 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
204 return GNUNET_BLOCK_EVALUATION_OK_MORE;
206 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
212 * Function called to obtain the key for a block.
215 * @param type block type
216 * @param block block to get the key for
217 * @param block_size number of bytes in block
218 * @param key set to the key (query) for the given block
219 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
220 * (or if extracting a key from a block of this type does not work)
223 block_plugin_mesh_get_key (void *cls, enum GNUNET_BLOCK_Type type,
224 const void *block, size_t block_size,
225 struct GNUNET_HashCode * key)
227 const struct PBlock *pb;
232 case GNUNET_BLOCK_TYPE_MESH_PEER:
233 if (sizeof (struct PBlock) != block_size)
234 return GNUNET_SYSERR;
235 *key = pb->id.hashPubKey;
237 case GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE:
238 GNUNET_CRYPTO_hash (&pb->type, sizeof(GNUNET_MESH_ApplicationType), key);
241 return GNUNET_SYSERR;
247 * Entry point for the plugin.
250 libgnunet_plugin_block_mesh_init (void *cls)
252 static enum GNUNET_BLOCK_Type types[] =
254 GNUNET_BLOCK_TYPE_MESH_PEER,
255 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
256 GNUNET_BLOCK_TYPE_ANY /* end of list */
258 struct GNUNET_BLOCK_PluginFunctions *api;
260 api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
261 api->evaluate = &block_plugin_mesh_evaluate;
262 api->get_key = &block_plugin_mesh_get_key;
269 * Exit point from the plugin.
272 libgnunet_plugin_block_mesh_done (void *cls)
274 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
280 /* end of plugin_block_mesh.c */