aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-06-26 11:50:16 +0000
committerChristian Grothoff <christian@grothoff.org>2013-06-26 11:50:16 +0000
commit5f38f9933e6f19fcef0ded8aad3d6db4e6cbb666 (patch)
tree7c17cf8ad811c21520f0e73fd7124d5a33c8935b /src
parentae4ab9e6ce8d7529bec34bef3d7db62c393dd918 (diff)
downloadgnunet-5f38f9933e6f19fcef0ded8aad3d6db4e6cbb666.tar.gz
gnunet-5f38f9933e6f19fcef0ded8aad3d6db4e6cbb666.zip
-cleaning up block code, moving all of the block logic into the block library
Diffstat (limited to 'src')
-rw-r--r--src/regex/plugin_block_regex.c15
-rw-r--r--src/regex/regex_block_lib.c115
-rw-r--r--src/regex/regex_block_lib.h45
-rw-r--r--src/regex/regex_internal_dht.c62
-rw-r--r--src/regex/regex_internal_lib.h18
5 files changed, 141 insertions, 114 deletions
diff --git a/src/regex/plugin_block_regex.c b/src/regex/plugin_block_regex.c
index 82341813e..565356786 100644
--- a/src/regex/plugin_block_regex.c
+++ b/src/regex/plugin_block_regex.c
@@ -48,11 +48,12 @@
48 */ 48 */
49static int 49static int
50rdebug (void *cls, 50rdebug (void *cls,
51 const char *token, 51 const char *token,
52 size_t len, 52 size_t len,
53 const struct GNUNET_HashCode *key) 53 const struct GNUNET_HashCode *key)
54{ 54{
55 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s: %.*s\n", 55 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
56 "%s: %.*s\n",
56 GNUNET_h2s (key), len, token); 57 GNUNET_h2s (key), len, token);
57 return GNUNET_YES; 58 return GNUNET_YES;
58} 59}
@@ -92,7 +93,7 @@ evaluate_block_regex (void *cls, enum GNUNET_BLOCK_Type type,
92 const char *query; 93 const char *query;
93 94
94 query = (const char *) xquery; 95 query = (const char *) xquery;
95 if ('\0' != query[xquery_size - 1]) /* must be valid string */ 96 if ('\0' != query[xquery_size - 1]) /* must be valid 0-terminated string */
96 { 97 {
97 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 98 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98 "Block xquery not a valid string\n"); 99 "Block xquery not a valid string\n");
@@ -111,8 +112,8 @@ evaluate_block_regex (void *cls, enum GNUNET_BLOCK_Type type,
111 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 112 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
112 } 113 }
113 switch (REGEX_INTERNAL_block_check (reply_block, 114 switch (REGEX_INTERNAL_block_check (reply_block,
114 reply_block_size, 115 reply_block_size,
115 xquery)) 116 xquery))
116 { 117 {
117 case GNUNET_SYSERR: 118 case GNUNET_SYSERR:
118 GNUNET_break_op(0); 119 GNUNET_break_op(0);
diff --git a/src/regex/regex_block_lib.c b/src/regex/regex_block_lib.c
index 63b673fbe..052c712c1 100644
--- a/src/regex/regex_block_lib.c
+++ b/src/regex/regex_block_lib.c
@@ -20,6 +20,8 @@
20/** 20/**
21 * @author Bartlomiej Polot 21 * @author Bartlomiej Polot
22 * @file regex/regex_block_lib.c 22 * @file regex/regex_block_lib.c
23 * @brief functions for manipulating non-accept blocks stored for
24 * regex in the DHT
23 */ 25 */
24#include "platform.h" 26#include "platform.h"
25#include "regex_block_lib.h" 27#include "regex_block_lib.h"
@@ -66,45 +68,32 @@ check_edge (void *cls,
66{ 68{
67 struct regex_block_xquery_ctx *ctx = cls; 69 struct regex_block_xquery_ctx *ctx = cls;
68 70
69 71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " edge %.*s [%u]: %s->%s\n", 72 "edge %.*s [%u]: %s->%s\n",
71 (int) len, token, len, ctx->key, GNUNET_h2s(key)); 73 (int) len, token, len, ctx->key, GNUNET_h2s(key));
72
73 if (NULL == ctx->xquery) 74 if (NULL == ctx->xquery)
74 return GNUNET_YES; 75 return GNUNET_YES;
75 if (strlen (ctx->xquery) < len) 76 if (strlen (ctx->xquery) < len)
76 { 77 return GNUNET_YES; /* too long */
77 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " too long!\n");
78 return GNUNET_YES;
79 }
80 if (0 == strncmp (ctx->xquery, token, len)) 78 if (0 == strncmp (ctx->xquery, token, len))
81 {
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " OK!\n");
83 ctx->found = GNUNET_OK; 79 ctx->found = GNUNET_OK;
84 }
85 else
86 {
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " KO!\n");
88 }
89
90 return GNUNET_YES; /* keep checking for malformed data! */ 80 return GNUNET_YES; /* keep checking for malformed data! */
91} 81}
92 82
93 83
94int 84int
95REGEX_INTERNAL_block_check (const struct RegexBlock *block, 85REGEX_INTERNAL_block_check (const struct RegexBlock *block,
96 size_t size, 86 size_t size,
97 const char *xquery) 87 const char *xquery)
98{ 88{
99 int res; 89 int res;
100 struct regex_block_xquery_ctx ctx; 90 struct regex_block_xquery_ctx ctx;
101 91
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
103 "* Checking block with xquery \"%s\"\n", 93 "Checking block with xquery `%s'\n",
104 NULL != xquery ? xquery : "NULL"); 94 NULL != xquery ? xquery : "NULL");
105 if ( (GNUNET_YES == ntohl (block->accepting)) && 95 if ( (GNUNET_YES == ntohl (block->accepting)) &&
106 ( (NULL == xquery) || ('\0' == xquery[0]) ) 96 ( (NULL == xquery) || ('\0' == xquery[0]) ) )
107 )
108 return GNUNET_OK; 97 return GNUNET_OK;
109 ctx.xquery = xquery; 98 ctx.xquery = xquery;
110 ctx.found = GNUNET_NO; 99 ctx.found = GNUNET_NO;
@@ -133,31 +122,24 @@ REGEX_INTERNAL_block_iterate (const struct RegexBlock *block,
133 char *aux; 122 char *aux;
134 123
135 offset = sizeof (struct RegexBlock); 124 offset = sizeof (struct RegexBlock);
136 LOG (GNUNET_ERROR_TYPE_DEBUG,
137 "* Start iterating block of size %u, off %u\n",
138 size, offset);
139 if (offset >= size) /* Is it safe to access the regex block? */ 125 if (offset >= size) /* Is it safe to access the regex block? */
140 { 126 {
141 LOG (GNUNET_ERROR_TYPE_WARNING,
142 "* Block is smaller than struct RegexBlock, END\n");
143 GNUNET_break_op (0); 127 GNUNET_break_op (0);
144 return GNUNET_SYSERR; 128 return GNUNET_SYSERR;
145 } 129 }
146 n = ntohl (block->n_proof); 130 n = ntohl (block->n_proof);
147 offset += n; 131 offset += n;
148 LOG (GNUNET_ERROR_TYPE_DEBUG,
149 "* Proof length: %u, off %u\n", n, offset);
150 if (offset >= size) /* Is it safe to access the regex proof? */ 132 if (offset >= size) /* Is it safe to access the regex proof? */
151 { 133 {
152 LOG (GNUNET_ERROR_TYPE_WARNING,
153 "* Block is smaller than Block + proof, END\n");
154 GNUNET_break_op (0); 134 GNUNET_break_op (0);
155 return GNUNET_SYSERR; 135 return GNUNET_SYSERR;
156 } 136 }
157 aux = (char *) &block[1]; /* Skip regex block */ 137 aux = (char *) &block[1]; /* Skip regex block */
158 aux = &aux[n]; /* Skip regex proof */ 138 aux = &aux[n]; /* Skip regex proof */
159 n = ntohl (block->n_edges); 139 n = ntohl (block->n_edges);
160 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Edges: %u\n", n); 140 LOG (GNUNET_ERROR_TYPE_DEBUG,
141 "Start iterating block of size %u, proof %u, off %u edges %u\n",
142 size, ntohl (block->n_proof), offset, n);
161 /* aux always points at the end of the previous block */ 143 /* aux always points at the end of the previous block */
162 for (i = 0; i < n; i++) 144 for (i = 0; i < n; i++)
163 { 145 {
@@ -191,15 +173,74 @@ REGEX_INTERNAL_block_iterate (const struct RegexBlock *block,
191 /* The total size should be exactly the size of (regex + all edges) blocks 173 /* The total size should be exactly the size of (regex + all edges) blocks
192 * If size == -1, block is from cache and therefore previously checked and 174 * If size == -1, block is from cache and therefore previously checked and
193 * assumed correct. */ 175 * assumed correct. */
194 if (offset == size || SIZE_MAX == size) 176 if ( (offset != size) && (SIZE_MAX != size) )
195 { 177 {
196 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Block processed, END OK\n"); 178 GNUNET_break_op (0);
197 return GNUNET_OK; 179 return GNUNET_SYSERR;
198 } 180 }
199 LOG (GNUNET_ERROR_TYPE_WARNING, 181 return GNUNET_OK;
200 "* Size %u (%d), read %u END KO\n", size, size, offset);
201 GNUNET_break_op (0);
202 return GNUNET_SYSERR;
203} 182}
204 183
184
185/**
186 * Construct a regex block to be stored in the DHT.
187 *
188 * @param proof proof string for the block
189 * @param num_edges number of edges in the block
190 * @param edges the edges of the block
191 * @return the regex block
192 */
193struct RegexBlock *
194REGEX_INTERNAL_block_create (const struct GNUNET_HashCode *key,
195 const char *proof,
196 unsigned int num_edges,
197 const struct REGEX_INTERNAL_Edge *edges,
198 int accepting,
199 size_t *rsize)
200{
201 struct RegexBlock *block;
202 struct RegexEdge *block_edge;
203 size_t size;
204 size_t len;
205 unsigned int i;
206 unsigned int offset;
207 char *aux;
208
209 len = strlen(proof);
210 size = sizeof (struct RegexBlock) + len;
211 block = GNUNET_malloc (size);
212 block->key = *key;
213 block->n_proof = htonl (len);
214 block->n_edges = htonl (num_edges);
215 block->accepting = htonl (accepting);
216
217 /* Store the proof at the end of the block. */
218 aux = (char *) &block[1];
219 memcpy (aux, proof, len);
220 aux = &aux[len];
221
222 /* Store each edge in a variable length MeshEdge struct at the
223 * very end of the MeshRegexBlock structure.
224 */
225 for (i = 0; i < num_edges; i++)
226 {
227 /* aux points at the end of the last block */
228 len = strlen (edges[i].label);
229 size += sizeof (struct RegexEdge) + len;
230 // Calculate offset FIXME is this ok? use size instead?
231 offset = aux - (char *) block;
232 block = GNUNET_realloc (block, size);
233 aux = &((char *) block)[offset];
234 block_edge = (struct RegexEdge *) aux;
235 block_edge->key = edges[i].destination;
236 block_edge->n_token = htonl (len);
237 aux = (char *) &block_edge[1];
238 memcpy (aux, edges[i].label, len);
239 aux = &aux[len];
240 }
241 *rsize = size;
242 return block;
243}
244
245
205/* end of regex_block_lib.c */ 246/* end of regex_block_lib.c */
diff --git a/src/regex/regex_block_lib.h b/src/regex/regex_block_lib.h
index a6c539229..ad4884f4d 100644
--- a/src/regex/regex_block_lib.h
+++ b/src/regex/regex_block_lib.h
@@ -39,6 +39,24 @@ extern "C"
39#include "platform.h" 39#include "platform.h"
40#include "block_regex.h" 40#include "block_regex.h"
41 41
42
43/**
44 * Edge representation.
45 */
46struct REGEX_INTERNAL_Edge
47{
48 /**
49 * Label of the edge. FIXME: might want to not consume exactly multiples of 8 bits, need length!
50 */
51 const char *label;
52
53 /**
54 * Destionation of the edge.
55 */
56 struct GNUNET_HashCode destination;
57};
58
59
42/** 60/**
43 * Check if the regex block is well formed, including all edges. 61 * Check if the regex block is well formed, including all edges.
44 * 62 *
@@ -53,8 +71,11 @@ extern "C"
53 */ 71 */
54int 72int
55REGEX_INTERNAL_block_check (const struct RegexBlock *block, 73REGEX_INTERNAL_block_check (const struct RegexBlock *block,
56 size_t size, 74 size_t size,
57 const char *xquery); 75 const char *xquery);
76
77
78/* FIXME: might want to use 'struct REGEX_INTERNAL_Edge' here instead of 3 arguments! */
58 79
59/** 80/**
60 * Iterator over edges in a block. 81 * Iterator over edges in a block.
@@ -62,7 +83,7 @@ REGEX_INTERNAL_block_check (const struct RegexBlock *block,
62 * @param cls Closure. 83 * @param cls Closure.
63 * @param token Token that follows to next state. 84 * @param token Token that follows to next state.
64 * @param len Length of token. 85 * @param len Length of token.
65 * @param key Hash of next state. 86 * @param key Hash of next state.
66 * 87 *
67 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise. 88 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
68 */ 89 */
@@ -94,6 +115,24 @@ REGEX_INTERNAL_block_iterate (const struct RegexBlock *block,
94 REGEX_INTERNAL_EgdeIterator iterator, 115 REGEX_INTERNAL_EgdeIterator iterator,
95 void *iter_cls); 116 void *iter_cls);
96 117
118
119/**
120 * Construct a regex block to be stored in the DHT.
121 *
122 * @param proof proof string for the block
123 * @param num_edges number of edges in the block
124 * @param edges the edges of the block
125 * @return the regex block
126 */
127struct RegexBlock *
128REGEX_INTERNAL_block_create (const struct GNUNET_HashCode *key,
129 const char *proof,
130 unsigned int num_edges,
131 const struct REGEX_INTERNAL_Edge *edges,
132 int accepting,
133 size_t *rsize);
134
135
97#if 0 /* keep Emacsens' auto-indent happy */ 136#if 0 /* keep Emacsens' auto-indent happy */
98{ 137{
99#endif 138#endif
diff --git a/src/regex/regex_internal_dht.c b/src/regex/regex_internal_dht.c
index a11d59752..3ddee06ff 100644
--- a/src/regex/regex_internal_dht.c
+++ b/src/regex/regex_internal_dht.c
@@ -91,25 +91,19 @@ regex_iterator (void *cls,
91{ 91{
92 struct REGEX_INTERNAL_Announcement *h = cls; 92 struct REGEX_INTERNAL_Announcement *h = cls;
93 struct RegexBlock *block; 93 struct RegexBlock *block;
94 struct RegexEdge *block_edge;
95 size_t size; 94 size_t size;
96 size_t len;
97 unsigned int i;
98 unsigned int offset;
99 char *aux;
100 95
101 LOG (GNUNET_ERROR_TYPE_DEBUG, 96 LOG (GNUNET_ERROR_TYPE_DEBUG,
102 " regex dht put for state %s\n", 97 "DHT PUT for state %s with proof `%s' and %u edges\n",
103 GNUNET_h2s (key)); 98 GNUNET_h2s (key),
104 LOG (GNUNET_ERROR_TYPE_DEBUG, " proof: %s\n", proof); 99 proof,
105 LOG (GNUNET_ERROR_TYPE_DEBUG, " num edges: %u\n", num_edges); 100 num_edges);
106
107 if (GNUNET_YES == accepting) 101 if (GNUNET_YES == accepting)
108 { 102 {
109 struct RegexAccept block; 103 struct RegexAccept block;
110 104
111 LOG (GNUNET_ERROR_TYPE_DEBUG, 105 LOG (GNUNET_ERROR_TYPE_DEBUG,
112 " state %s is accepting, putting own id\n", 106 "State %s is accepting, putting own id\n",
113 GNUNET_h2s(key)); 107 GNUNET_h2s(key));
114 size = sizeof (block); 108 size = sizeof (block);
115 block.key = *key; 109 block.key = *key;
@@ -124,53 +118,21 @@ regex_iterator (void *cls,
124 DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE, 118 DHT_OPT | GNUNET_DHT_RO_RECORD_ROUTE,
125 GNUNET_BLOCK_TYPE_REGEX_ACCEPT, 119 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
126 size, 120 size,
127 (char *) &block, 121 &block,
128 GNUNET_TIME_relative_to_absolute (DHT_TTL), 122 GNUNET_TIME_relative_to_absolute (DHT_TTL),
129 DHT_TTL, 123 DHT_TTL,
130 NULL, NULL); 124 NULL, NULL);
131 } 125 }
132 len = strlen(proof); 126 block = REGEX_INTERNAL_block_create (key, proof,
133 size = sizeof (struct RegexBlock) + len; 127 num_edges, edges,
134 block = GNUNET_malloc (size); 128 accepting,
135 129 &size);
136 block->key = *key;
137 block->n_proof = htonl (len);
138 block->n_edges = htonl (num_edges);
139 block->accepting = htonl (accepting);
140
141 /* Store the proof at the end of the block. */
142 aux = (char *) &block[1];
143 memcpy (aux, proof, len);
144 aux = &aux[len];
145
146 /* Store each edge in a variable length MeshEdge struct at the
147 * very end of the MeshRegexBlock structure.
148 */
149 for (i = 0; i < num_edges; i++)
150 {
151 LOG (GNUNET_ERROR_TYPE_DEBUG, " edge %s towards %s\n",
152 edges[i].label, GNUNET_h2s(&edges[i].destination));
153
154 /* aux points at the end of the last block */
155 len = strlen (edges[i].label);
156 size += sizeof (struct RegexEdge) + len;
157 // Calculate offset FIXME is this ok? use size instead?
158 offset = aux - (char *) block;
159 block = GNUNET_realloc (block, size);
160 aux = &((char *) block)[offset];
161 block_edge = (struct RegexEdge *) aux;
162 block_edge->key = edges[i].destination;
163 block_edge->n_token = htonl (len);
164 aux = (char *) &block_edge[1];
165 memcpy (aux, edges[i].label, len);
166 aux = &aux[len];
167 }
168 (void) 130 (void)
169 GNUNET_DHT_put (h->dht, key, 131 GNUNET_DHT_put (h->dht, key,
170 DHT_REPLICATION, 132 DHT_REPLICATION,
171 DHT_OPT, 133 DHT_OPT,
172 GNUNET_BLOCK_TYPE_REGEX, size, 134 GNUNET_BLOCK_TYPE_REGEX,
173 (char *) block, 135 size, block,
174 GNUNET_TIME_relative_to_absolute (DHT_TTL), 136 GNUNET_TIME_relative_to_absolute (DHT_TTL),
175 DHT_TTL, 137 DHT_TTL,
176 NULL, NULL); 138 NULL, NULL);
diff --git a/src/regex/regex_internal_lib.h b/src/regex/regex_internal_lib.h
index 7da08154d..f8bf36344 100644
--- a/src/regex/regex_internal_lib.h
+++ b/src/regex/regex_internal_lib.h
@@ -29,6 +29,7 @@
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30#include "gnunet_dht_service.h" 30#include "gnunet_dht_service.h"
31#include "gnunet_statistics_service.h" 31#include "gnunet_statistics_service.h"
32#include "regex_block_lib.h"
32 33
33#ifdef __cplusplus 34#ifdef __cplusplus
34extern "C" 35extern "C"
@@ -46,23 +47,6 @@ struct REGEX_INTERNAL_Automaton;
46 47
47 48
48/** 49/**
49 * Edge representation.
50 */
51struct REGEX_INTERNAL_Edge
52{
53 /**
54 * Label of the edge. FIXME: might want to not consume exactly multiples of 8 bits, need length?
55 */
56 const char *label;
57
58 /**
59 * Destionation of the edge.
60 */
61 struct GNUNET_HashCode destination;
62};
63
64
65/**
66 * Construct DFA for the given 'regex' of length 'len'. 50 * Construct DFA for the given 'regex' of length 'len'.
67 * 51 *
68 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be 52 * Path compression means, that for example a DFA o -> a -> b -> c -> o will be