2 This file is part of GNUnet.
3 Copyright (C) 2012,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.
21 * @author Bartlomiej Polot
22 * @file regex/regex_block_lib.c
23 * @brief functions for manipulating non-accept blocks stored for
27 #include "regex_block_lib.h"
28 #include "gnunet_constants.h"
30 #define LOG(kind,...) GNUNET_log_from (kind,"regex-bck",__VA_ARGS__)
32 GNUNET_NETWORK_STRUCT_BEGIN
35 * Information for each edge.
40 * Index of the destination of this edge in the
41 * unique destinations array.
43 uint16_t destination_index GNUNET_PACKED;
46 * Number of bytes the token for this edge takes in the
49 uint16_t token_length GNUNET_PACKED;
54 * @brief Block to announce a regex state.
60 * Length of the proof regex string.
62 uint16_t proof_len GNUNET_PACKED;
65 * Is this state an accepting state?
67 int16_t is_accepting GNUNET_PACKED;
70 * Number of edges parting from this state.
72 uint16_t num_edges GNUNET_PACKED;
75 * Nubmer of unique destinations reachable from this state.
77 uint16_t num_destinations GNUNET_PACKED;
79 /* followed by 'struct GNUNET_HashCode[num_destinations]' */
81 /* followed by 'struct EdgeInfo[edge_destination_indices]' */
83 /* followed by 'char proof[n_proof]', NOT 0-terminated */
85 /* followed by 'char tokens[num_edges][edge_info[k].token_length]';
86 essentially all of the tokens one after the other in the
87 order of the edges; tokens are NOT 0-terminated */
92 GNUNET_NETWORK_STRUCT_END
96 * Test if this block is marked as being an accept state.
98 * @param block block to test
99 * @param size number of bytes in block
100 * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not
103 GNUNET_BLOCK_is_accepting (const struct RegexBlock *block,
106 if (size < sizeof (struct RegexBlock))
109 return GNUNET_SYSERR;
111 return ntohs (block->is_accepting);
116 * Check if the given 'proof' matches the given 'key'.
118 * @param proof partial regex of a state
119 * @param proof_len number of bytes in 'proof'
120 * @param key hash of a state.
121 * @return #GNUNET_OK if the proof is valid for the given key.
124 REGEX_BLOCK_check_proof (const char *proof,
126 const struct GNUNET_HashCode *key)
128 struct GNUNET_HashCode key_check;
130 if ( (NULL == proof) || (NULL == key))
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
135 GNUNET_CRYPTO_hash (proof, proof_len, &key_check);
137 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
142 * Struct to keep track of the xquery while iterating all the edges in a block.
144 struct CheckEdgeContext
147 * Xquery: string we are looking for.
152 * Has any edge matched the xquery so far? (GNUNET_OK / GNUNET_NO)
160 * Iterator over all edges in a block, checking for a presence of a given query.
162 * @param cls Closure, (xquery context).
163 * @param token Token that follows to next state.
164 * @param len Lenght of token.
165 * @param key Hash of next state.
167 * @return #GNUNET_YES, to keep iterating
170 check_edge (void *cls,
173 const struct GNUNET_HashCode *key)
175 struct CheckEdgeContext *ctx = cls;
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "edge %.*s [%u]: %sn",
183 if (NULL == ctx->xquery)
185 if (strlen (ctx->xquery) < len)
186 return GNUNET_YES; /* too long */
187 if (0 == strncmp (ctx->xquery, token, len))
188 ctx->found = GNUNET_OK;
189 return GNUNET_YES; /* keep checking for malformed data! */
194 * Check if the regex block is well formed, including all edges.
196 * @param block The start of the block.
197 * @param size The size of the block.
198 * @param query the query for the block
199 * @param xquery String describing the edge we are looking for.
200 * Can be NULL in case this is a put block.
201 * @return #GNUNET_OK in case it's fine.
202 * #GNUNET_NO in case the xquery exists and is not found (IRRELEVANT).
203 * #GNUNET_SYSERR if the block is invalid.
206 REGEX_BLOCK_check (const struct RegexBlock *block,
208 const struct GNUNET_HashCode *query,
211 struct GNUNET_HashCode key;
212 struct CheckEdgeContext ctx;
215 LOG (GNUNET_ERROR_TYPE_DEBUG,
218 REGEX_BLOCK_get_key (block, size,
222 return GNUNET_SYSERR;
227 sizeof (struct GNUNET_HashCode)))
230 return GNUNET_SYSERR;
232 if ( (GNUNET_YES == ntohs (block->is_accepting)) &&
233 ( (NULL == xquery) || ('\0' == xquery[0]) ) )
235 LOG (GNUNET_ERROR_TYPE_DEBUG,
236 " out! Is accepting: %u, xquery %p\n",
237 ntohs(block->is_accepting),
242 ctx.found = GNUNET_NO;
243 res = REGEX_BLOCK_iterate (block, size, &check_edge, &ctx);
244 if (GNUNET_SYSERR == res)
245 return GNUNET_SYSERR;
248 LOG (GNUNET_ERROR_TYPE_DEBUG, "Result %d\n", ctx.found);
254 * Obtain the key that a particular block is to be stored under.
256 * @param block block to get the key from
257 * @param block_len number of bytes in block
258 * @param key where to store the key
259 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed
262 REGEX_BLOCK_get_key (const struct RegexBlock *block,
264 struct GNUNET_HashCode *key)
267 const struct GNUNET_HashCode *destinations;
268 const struct EdgeInfo *edges;
269 uint16_t num_destinations;
273 if (block_len < sizeof (struct RegexBlock))
276 return GNUNET_SYSERR;
278 num_destinations = ntohs (block->num_destinations);
279 num_edges = ntohs (block->num_edges);
280 len = ntohs (block->proof_len);
281 destinations = (const struct GNUNET_HashCode *) &block[1];
282 edges = (const struct EdgeInfo *) &destinations[num_destinations];
283 total = sizeof (struct RegexBlock) + num_destinations * sizeof (struct GNUNET_HashCode) + num_edges * sizeof (struct EdgeInfo) + len;
284 if (block_len < total)
287 return GNUNET_SYSERR;
289 GNUNET_CRYPTO_hash (&edges[num_edges], len, key);
295 * Iterate over all edges of a block of a regex state.
297 * @param block Block to iterate over.
298 * @param size Size of @a block.
299 * @param iterator Function to call on each edge in the block.
300 * @param iter_cls Closure for the @a iterator.
301 * @return #GNUNET_SYSERR if an error has been encountered.
302 * #GNUNET_OK if no error has been encountered.
303 * Note that if the iterator stops the iteration by returning
304 * #GNUNET_NO, the block will no longer be checked for further errors.
305 * The return value will be GNUNET_OK meaning that no errors were
306 * found until the edge last notified to the iterator, but there might
307 * be errors in further edges.
310 REGEX_BLOCK_iterate (const struct RegexBlock *block,
312 REGEX_INTERNAL_EgdeIterator iterator,
316 const struct GNUNET_HashCode *destinations;
317 const struct EdgeInfo *edges;
319 uint16_t num_destinations;
325 LOG (GNUNET_ERROR_TYPE_DEBUG, "Block iterate\n");
326 if (size < sizeof (struct RegexBlock))
329 return GNUNET_SYSERR;
331 num_destinations = ntohs (block->num_destinations);
332 num_edges = ntohs (block->num_edges);
333 len = ntohs (block->proof_len);
334 destinations = (const struct GNUNET_HashCode *) &block[1];
335 edges = (const struct EdgeInfo *) &destinations[num_destinations];
336 aux = (const char *) &edges[num_edges];
337 total = sizeof (struct RegexBlock) + num_destinations * sizeof (struct GNUNET_HashCode) + num_edges * sizeof (struct EdgeInfo) + len;
341 return GNUNET_SYSERR;
343 for (n=0;n<num_edges;n++)
344 total += ntohs (edges[n].token_length);
347 fprintf (stderr, "Expected %u, got %u\n",
349 (unsigned int) total);
351 return GNUNET_SYSERR;
354 LOG (GNUNET_ERROR_TYPE_DEBUG,
355 "Start iterating block of size %u, proof %u, off %u edges %u\n",
357 /* &aux[off] always points to our token */
358 for (n=0;n<num_edges;n++)
360 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "Edge %u/%u, off %u tokenlen %u (%.*s)\n",
363 ntohs (edges[n].token_length), ntohs (edges[n].token_length),
365 if (NULL != iterator)
366 if (GNUNET_NO == iterator (iter_cls,
368 ntohs (edges[n].token_length),
369 &destinations[ntohs (edges[n].destination_index)]))
371 off += ntohs (edges[n].token_length);
378 * Construct a regex block to be stored in the DHT.
380 * @param proof proof string for the block
381 * @param num_edges number of edges in the block
382 * @param edges the edges of the block
383 * @param accepting is this an accepting state
384 * @param rsize set to the size of the returned block (OUT-only)
385 * @return the regex block, NULL on error
388 REGEX_BLOCK_create (const char *proof,
389 unsigned int num_edges,
390 const struct REGEX_BLOCK_Edge *edges,
394 struct RegexBlock *block;
395 struct GNUNET_HashCode destinations[1024]; /* 1024 = 64k/64 bytes/key == absolute MAX */
396 uint16_t destination_indices[num_edges];
397 struct GNUNET_HashCode *dests;
398 struct EdgeInfo *edgeinfos;
403 unsigned int unique_destinations;
408 len = strlen (proof);
409 if (len > UINT16_MAX)
414 unique_destinations = 0;
415 total = sizeof (struct RegexBlock) + len;
416 for (i=0;i<num_edges;i++)
418 slen = strlen (edges[i].label);
419 if (slen > UINT16_MAX)
425 for (j=0;j<unique_destinations;j++)
426 if (0 == memcmp (&destinations[j],
427 &edges[i].destination,
428 sizeof (struct GNUNET_HashCode)))
435 destination_indices[i] = j;
436 if (j == unique_destinations)
437 destinations[unique_destinations++] = edges[i].destination;
439 total += num_edges * sizeof (struct EdgeInfo) + unique_destinations * sizeof (struct GNUNET_HashCode);
440 if (total >= GNUNET_CONSTANTS_MAX_BLOCK_SIZE)
445 block = GNUNET_malloc (total);
446 block->proof_len = htons (len);
447 block->is_accepting = htons (accepting);
448 block->num_edges = htons (num_edges);
449 block->num_destinations = htons (unique_destinations);
450 dests = (struct GNUNET_HashCode *) &block[1];
451 memcpy (dests, destinations, sizeof (struct GNUNET_HashCode) * unique_destinations);
452 edgeinfos = (struct EdgeInfo *) &dests[unique_destinations];
453 aux = (char *) &edgeinfos[num_edges];
455 memcpy (aux, proof, len);
456 for (i=0;i<num_edges;i++)
458 slen = strlen (edges[i].label);
459 edgeinfos[i].token_length = htons ((uint16_t) slen);
460 edgeinfos[i].destination_index = htons (destination_indices[i]);
471 /* end of regex_block_lib.c */