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.
21 * @author Bartlomiej Polot
22 * @file mesh/mesh_block_lib.c
25 #include "mesh_block_lib.h"
28 * Noop iterator over all edges in a block.
30 * @param cls Closure, not used.
31 * @param token Token that follows to next state.
32 * @param len Lenght of token.
33 * @param key Hash of next state.
35 * @return GNUNET_YES, to keep iterating
38 check_edge (void *cls,
41 const struct GNUNET_HashCode *key)
48 * Check if the regex block is well formed, including all edges
50 * @param block The start of the block.
51 * @param size The size of the block.
53 * @return GNUNET_OK in case it's fine, GNUNET_SYSERR otherwise.
56 GNUNET_MESH_regex_block_check (const struct MeshRegexBlock *block,
59 return GNUNET_MESH_regex_block_iterate(block, size, &check_edge, NULL);
64 * Iterate over all edges of a block of a regex state.
66 * @param block Block to iterate over.
67 * @param size Size of block.
68 * @param iterator Function to call on each edge in the block.
69 * @param iter_cls Closure for the iterator.
71 * @return How many bytes of block have been processed
74 GNUNET_MESH_regex_block_iterate (const struct MeshRegexBlock *block,
76 GNUNET_MESH_EgdeIterator iterator,
79 struct MeshRegexEdge *edge;
86 offset = sizeof (struct MeshRegexBlock);
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 "* Start iterating block of size %u, off %u\n",
90 if (offset > size) // Is it safe to access the regex block?
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93 "* Block is smaller than struct MeshRegexBlock, END\n");
96 n = ntohl (block->n_proof);
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 "* Proof length: %u, off %u\n", n, offset);
100 if (offset > size) // Is it safe to access the regex proof?
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
103 "* Block is smaller than Block + proof, END\n");
104 return GNUNET_SYSERR;
106 aux = (char *) &block[1]; // Skip regex block
107 aux = &aux[n]; // Skip regex proof
108 n = ntohl (block->n_edges);
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Edges: %u\n", n);
110 for (i = 0; i < n; i++) // aux always points at the end of the previous block
112 offset += sizeof (struct MeshRegexEdge);
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Edge %u, off %u\n", i, offset);
114 if (offset > size) // Is it safe to access the next edge block?
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "* Size not enough for MeshRegexEdge, END\n");
118 return GNUNET_SYSERR;
120 edge = (struct MeshRegexEdge *) aux;
121 n_token = ntohl (edge->n_token);
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124 "* Token lenght %u, off %u\n", n_token, offset);
125 if (offset > size) // Is it safe to access the edge token?
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "* Size not enough for edge token, END\n");
129 return GNUNET_SYSERR;
131 aux = (char *) &edge[1]; // Skip edge block
132 if (NULL != iterator)
133 if (GNUNET_NO == iterator (iter_cls, aux, n_token, &edge->key))
135 aux = &aux[n_token]; // Skip edge token
137 // The total size should be exactly the size of (regex + all edges) blocks
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141 "* Block processed, END OK\n");
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "* Size %u, read %u END KO\n", size, offset);
146 return GNUNET_SYSERR;
149 /* end of mesh_block_lib.c */