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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
59 * Length of the proof regex string.
61 uint16_t proof_len GNUNET_PACKED;
64 * Is this state an accepting state?
66 int16_t is_accepting GNUNET_PACKED;
69 * Number of edges parting from this state.
71 uint16_t num_edges GNUNET_PACKED;
74 * Nubmer of unique destinations reachable from this state.
76 uint16_t num_destinations GNUNET_PACKED;
78 /* followed by 'struct GNUNET_HashCode[num_destinations]' */
80 /* followed by 'struct EdgeInfo[edge_destination_indices]' */
82 /* followed by 'char proof[n_proof]', NOT 0-terminated */
84 /* followed by 'char tokens[num_edges][edge_info[k].token_length]';
85 essentially all of the tokens one after the other in the
86 order of the edges; tokens are NOT 0-terminated */
90 GNUNET_NETWORK_STRUCT_END
94 * Test if this block is marked as being an accept state.
96 * @param block block to test
97 * @param size number of bytes in block
98 * @return #GNUNET_YES if the block is accepting, #GNUNET_NO if not
101 GNUNET_BLOCK_is_accepting (const struct RegexBlock *block,
104 if (size < sizeof(struct RegexBlock))
107 return GNUNET_SYSERR;
109 return ntohs (block->is_accepting);
114 * Check if the given 'proof' matches the given 'key'.
116 * @param proof partial regex of a state
117 * @param proof_len number of bytes in 'proof'
118 * @param key hash of a state.
119 * @return #GNUNET_OK if the proof is valid for the given key.
122 REGEX_BLOCK_check_proof (const char *proof,
124 const struct GNUNET_HashCode *key)
126 struct GNUNET_HashCode key_check;
128 if ((NULL == proof) || (NULL == key))
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
133 GNUNET_CRYPTO_hash (proof, proof_len, &key_check);
135 GNUNET_CRYPTO_hash_cmp (key, &key_check)) ? GNUNET_OK : GNUNET_NO;
140 * Struct to keep track of the xquery while iterating all the edges in a block.
142 struct CheckEdgeContext
145 * Xquery: string we are looking for.
150 * Has any edge matched the xquery so far? (GNUNET_OK / GNUNET_NO)
157 * Iterator over all edges in a block, checking for a presence of a given query.
159 * @param cls Closure, (xquery context).
160 * @param token Token that follows to next state.
161 * @param len Lenght of token.
162 * @param key Hash of next state.
164 * @return #GNUNET_YES, to keep iterating
167 check_edge (void *cls,
170 const struct GNUNET_HashCode *key)
172 struct CheckEdgeContext *ctx = cls;
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "edge %.*s [%u]: %s\n",
180 if (NULL == ctx->xquery)
182 if (strlen (ctx->xquery) < len)
183 return GNUNET_YES; /* too long */
184 if (0 == strncmp (ctx->xquery, token, len))
185 ctx->found = GNUNET_OK;
186 return GNUNET_YES; /* keep checking for malformed data! */
191 * Check if the regex block is well formed, including all edges.
193 * @param block The start of the block.
194 * @param size The size of the block.
195 * @param query the query for the block
196 * @param xquery String describing the edge we are looking for.
197 * Can be NULL in case this is a put block.
198 * @return #GNUNET_OK in case it's fine.
199 * #GNUNET_NO in case the xquery exists and is not found (IRRELEVANT).
200 * #GNUNET_SYSERR if the block is invalid.
203 REGEX_BLOCK_check (const struct RegexBlock *block,
205 const struct GNUNET_HashCode *query,
208 struct GNUNET_HashCode key;
209 struct CheckEdgeContext ctx;
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
215 REGEX_BLOCK_get_key (block, size,
219 return GNUNET_SYSERR;
221 if ((NULL != query) &&
222 (0 != GNUNET_memcmp (&key,
226 return GNUNET_SYSERR;
228 if ((GNUNET_YES == ntohs (block->is_accepting)) &&
229 ((NULL == xquery) || ('\0' == xquery[0])))
231 LOG (GNUNET_ERROR_TYPE_DEBUG,
232 " out! Is accepting: %u, xquery %p\n",
233 ntohs (block->is_accepting),
238 ctx.found = GNUNET_NO;
239 res = REGEX_BLOCK_iterate (block, size, &check_edge, &ctx);
240 if (GNUNET_SYSERR == res)
241 return GNUNET_SYSERR;
244 LOG (GNUNET_ERROR_TYPE_DEBUG, "Result %d\n", ctx.found);
250 * Obtain the key that a particular block is to be stored under.
252 * @param block block to get the key from
253 * @param block_len number of bytes in block
254 * @param key where to store the key
255 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block is malformed
258 REGEX_BLOCK_get_key (const struct RegexBlock *block,
260 struct GNUNET_HashCode *key)
263 const struct GNUNET_HashCode *destinations;
264 const struct EdgeInfo *edges;
265 uint16_t num_destinations;
269 if (block_len < sizeof(struct RegexBlock))
272 return GNUNET_SYSERR;
274 num_destinations = ntohs (block->num_destinations);
275 num_edges = ntohs (block->num_edges);
276 len = ntohs (block->proof_len);
277 destinations = (const struct GNUNET_HashCode *) &block[1];
278 edges = (const struct EdgeInfo *) &destinations[num_destinations];
279 total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct
281 + num_edges * sizeof(struct EdgeInfo) + len;
282 if (block_len < total)
285 return GNUNET_SYSERR;
287 GNUNET_CRYPTO_hash (&edges[num_edges], len, key);
293 * Iterate over all edges of a block of a regex state.
295 * @param block Block to iterate over.
296 * @param size Size of @a block.
297 * @param iterator Function to call on each edge in the block.
298 * @param iter_cls Closure for the @a iterator.
299 * @return #GNUNET_SYSERR if an error has been encountered.
300 * #GNUNET_OK if no error has been encountered.
301 * Note that if the iterator stops the iteration by returning
302 * #GNUNET_NO, the block will no longer be checked for further errors.
303 * The return value will be GNUNET_OK meaning that no errors were
304 * found until the edge last notified to the iterator, but there might
305 * be errors in further edges.
308 REGEX_BLOCK_iterate (const struct RegexBlock *block,
310 REGEX_INTERNAL_EgdeIterator iterator,
314 const struct GNUNET_HashCode *destinations;
315 const struct EdgeInfo *edges;
317 uint16_t num_destinations;
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Block iterate\n");
324 if (size < sizeof(struct RegexBlock))
327 return GNUNET_SYSERR;
329 num_destinations = ntohs (block->num_destinations);
330 num_edges = ntohs (block->num_edges);
331 len = ntohs (block->proof_len);
332 destinations = (const struct GNUNET_HashCode *) &block[1];
333 edges = (const struct EdgeInfo *) &destinations[num_destinations];
334 aux = (const char *) &edges[num_edges];
335 total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct
337 + 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",
362 n + 1, num_edges, off,
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 (
370 edges[n].destination_index)]))
372 off += ntohs (edges[n].token_length);
379 * Construct a regex block to be stored in the DHT.
381 * @param proof proof string for the block
382 * @param num_edges number of edges in the block
383 * @param edges the edges of the block
384 * @param accepting is this an accepting state
385 * @param rsize set to the size of the returned block (OUT-only)
386 * @return the regex block, NULL on error
389 REGEX_BLOCK_create (const char *proof,
390 unsigned int num_edges,
391 const struct REGEX_BLOCK_Edge *edges,
395 struct RegexBlock *block;
396 struct GNUNET_HashCode destinations[1024]; /* 1024 = 64k/64 bytes/key == absolute MAX */
397 uint16_t destination_indices[num_edges];
398 struct GNUNET_HashCode *dests;
399 struct EdgeInfo *edgeinfos;
404 unsigned int unique_destinations;
409 len = strlen (proof);
410 if (len > UINT16_MAX)
415 unique_destinations = 0;
416 total = sizeof(struct RegexBlock) + len;
417 for (i = 0; i < num_edges; i++)
419 slen = strlen (edges[i].label);
420 if (slen > UINT16_MAX)
426 for (j = 0; j < unique_destinations; j++)
427 if (0 == memcmp (&destinations[j],
428 &edges[i].destination,
429 sizeof(struct GNUNET_HashCode)))
436 destination_indices[i] = j;
437 if (j == unique_destinations)
438 destinations[unique_destinations++] = edges[i].destination;
440 total += num_edges * sizeof(struct EdgeInfo) + unique_destinations
441 * sizeof(struct GNUNET_HashCode);
442 if (total >= GNUNET_CONSTANTS_MAX_BLOCK_SIZE)
447 block = GNUNET_malloc (total);
448 block->proof_len = htons (len);
449 block->is_accepting = htons (accepting);
450 block->num_edges = htons (num_edges);
451 block->num_destinations = htons (unique_destinations);
452 dests = (struct GNUNET_HashCode *) &block[1];
453 GNUNET_memcpy (dests, destinations, sizeof(struct GNUNET_HashCode)
454 * unique_destinations);
455 edgeinfos = (struct EdgeInfo *) &dests[unique_destinations];
456 aux = (char *) &edgeinfos[num_edges];
458 GNUNET_memcpy (aux, proof, len);
459 for (i = 0; i < num_edges; i++)
461 slen = strlen (edges[i].label);
462 edgeinfos[i].token_length = htons ((uint16_t) slen);
463 edgeinfos[i].destination_index = htons (destination_indices[i]);
464 GNUNET_memcpy (&aux[off],
474 /* end of regex_block_lib.c */