- backport fix to mesh_new
[oweals/gnunet.git] / src / mesh / mesh_block_lib.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @author Bartlomiej Polot
22  * @file mesh/mesh_block_lib.c
23  */
24 #include "platform.h"
25 #include "mesh_block_lib.h"
26
27 /**
28  * Noop iterator over all edges in a block.
29  *
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.
34  * 
35  * @return GNUNET_YES, to keep iterating
36  */
37 static int
38 check_edge (void *cls,
39             const char *token,
40             size_t len,
41             const struct GNUNET_HashCode *key)
42 {
43   return GNUNET_YES;
44 }
45
46
47 /**
48  * Check if the regex block is well formed, including all edges
49  *
50  * @param block The start of the block.
51  * @param size The size of the block.
52  *
53  * @return GNUNET_OK in case it's fine, GNUNET_SYSERR otherwise.
54  */
55 int
56 GNUNET_MESH_regex_block_check (const struct MeshRegexBlock *block,
57                                size_t size)
58 {
59   return GNUNET_MESH_regex_block_iterate(block, size, &check_edge, NULL);
60 }
61
62
63 /**
64  * Iterate over all edges of a block of a regex state.
65  *
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.
70  *
71  * @return How many bytes of block have been processed
72  */
73 int
74 GNUNET_MESH_regex_block_iterate (const struct MeshRegexBlock *block,
75                                  size_t size,
76                                  GNUNET_MESH_EgdeIterator iterator,
77                                  void *iter_cls)
78 {
79   struct MeshRegexEdge *edge;
80   unsigned int n;
81   unsigned int n_token;
82   unsigned int i;
83   size_t offset;
84   char *aux;
85
86   offset = sizeof (struct MeshRegexBlock);
87   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88               "* Start iterating block of size %u, off %u\n",
89               size, offset);
90   if (offset > size) // Is it safe to access the regex block?
91   {
92     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93               "*   Block is smaller than struct MeshRegexBlock, END\n");
94     return GNUNET_SYSERR;
95   }
96   n = ntohl (block->n_proof);
97   offset += n;
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?
101   {
102     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
103                 "*   Block is smaller than Block + proof, END\n");
104     return GNUNET_SYSERR;
105   }
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
111   {
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?
115     {
116       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117                   "*   Size not enough for MeshRegexEdge, END\n");
118       return GNUNET_SYSERR;
119     }
120     edge = (struct MeshRegexEdge *) aux;
121     n_token = ntohl (edge->n_token);
122     offset += 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?
126     {
127       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128                   "*   Size not enough for edge token, END\n");
129       return GNUNET_SYSERR;
130     }
131     aux = (char *) &edge[1]; // Skip edge block
132     if (NULL != iterator)
133         if (GNUNET_NO == iterator (iter_cls, aux, n_token, &edge->key))
134             return GNUNET_OK;
135     aux = &aux[n_token];     // Skip edge token
136   }
137   // The total size should be exactly the size of (regex + all edges) blocks
138   if (offset == size)
139   {
140     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141                   "* Block processed, END OK\n");
142     return GNUNET_OK;
143   }
144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145               "*   Size %u, read %u END KO\n", size, offset);
146   return GNUNET_SYSERR;
147 }
148
149 /* end of mesh_block_lib.c */