diff options
Diffstat (limited to 'src/regex/plugin_block_regex.c')
-rw-r--r-- | src/regex/plugin_block_regex.c | 146 |
1 files changed, 92 insertions, 54 deletions
diff --git a/src/regex/plugin_block_regex.c b/src/regex/plugin_block_regex.c index 36926c25d..287ecf905 100644 --- a/src/regex/plugin_block_regex.c +++ b/src/regex/plugin_block_regex.c | |||
@@ -23,16 +23,75 @@ | |||
23 | * @brief blocks used for regex storage and search | 23 | * @brief blocks used for regex storage and search |
24 | * @author Bartlomiej Polot | 24 | * @author Bartlomiej Polot |
25 | */ | 25 | */ |
26 | |||
27 | #include "platform.h" | 26 | #include "platform.h" |
28 | #include "gnunet_block_plugin.h" | 27 | #include "gnunet_block_plugin.h" |
28 | #include "gnunet_block_group_lib.h" | ||
29 | #include "block_regex.h" | 29 | #include "block_regex.h" |
30 | #include "regex_block_lib.h" | 30 | #include "regex_block_lib.h" |
31 | #include "gnunet_constants.h" | ||
32 | #include "gnunet_signatures.h" | 31 | #include "gnunet_signatures.h" |
33 | 32 | ||
34 | 33 | ||
35 | /** | 34 | /** |
35 | * Number of bits we set per entry in the bloomfilter. | ||
36 | * Do not change! | ||
37 | */ | ||
38 | #define BLOOMFILTER_K 16 | ||
39 | |||
40 | |||
41 | /** | ||
42 | * How big is the BF we use for REGEX blocks? | ||
43 | */ | ||
44 | #define REGEX_BF_SIZE 8 | ||
45 | |||
46 | |||
47 | /** | ||
48 | * Create a new block group. | ||
49 | * | ||
50 | * @param ctx block context in which the block group is created | ||
51 | * @param type type of the block for which we are creating the group | ||
52 | * @param nonce random value used to seed the group creation | ||
53 | * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh | ||
54 | * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh | ||
55 | * @param va variable arguments specific to @a type | ||
56 | * @return block group handle, NULL if block groups are not supported | ||
57 | * by this @a type of block (this is not an error) | ||
58 | */ | ||
59 | static struct GNUNET_BLOCK_Group * | ||
60 | block_plugin_regex_create_group (void *cls, | ||
61 | enum GNUNET_BLOCK_Type type, | ||
62 | uint32_t nonce, | ||
63 | const void *raw_data, | ||
64 | size_t raw_data_size, | ||
65 | va_list va) | ||
66 | { | ||
67 | unsigned int bf_size; | ||
68 | const char *guard; | ||
69 | |||
70 | guard = va_arg (va, const char *); | ||
71 | if (0 == strcmp (guard, | ||
72 | "seen-set-size")) | ||
73 | bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int), | ||
74 | BLOOMFILTER_K); | ||
75 | else if (0 == strcmp (guard, | ||
76 | "filter-size")) | ||
77 | bf_size = va_arg (va, unsigned int); | ||
78 | else | ||
79 | { | ||
80 | GNUNET_break (0); | ||
81 | bf_size = REGEX_BF_SIZE; | ||
82 | } | ||
83 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
84 | return GNUNET_BLOCK_GROUP_bf_create (cls, | ||
85 | bf_size, | ||
86 | BLOOMFILTER_K, | ||
87 | type, | ||
88 | nonce, | ||
89 | raw_data, | ||
90 | raw_data_size); | ||
91 | } | ||
92 | |||
93 | |||
94 | /** | ||
36 | * Function called to validate a reply or a request of type | 95 | * Function called to validate a reply or a request of type |
37 | * #GNUNET_BLOCK_TYPE_REGEX. | 96 | * #GNUNET_BLOCK_TYPE_REGEX. |
38 | * For request evaluation, pass "NULL" for the reply_block. | 97 | * For request evaluation, pass "NULL" for the reply_block. |
@@ -42,10 +101,9 @@ | |||
42 | * | 101 | * |
43 | * @param cls closure | 102 | * @param cls closure |
44 | * @param type block type | 103 | * @param type block type |
104 | * @param bg block group to evaluate against | ||
45 | * @param eo control flags | 105 | * @param eo control flags |
46 | * @param query original query (hash) | 106 | * @param query original query (hash) |
47 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
48 | * @param bf_mutator mutation value for bf | ||
49 | * @param xquery extrended query data (can be NULL, depending on type) | 107 | * @param xquery extrended query data (can be NULL, depending on type) |
50 | * @param xquery_size number of bytes in @a xquery | 108 | * @param xquery_size number of bytes in @a xquery |
51 | * @param reply_block response to validate | 109 | * @param reply_block response to validate |
@@ -55,15 +113,16 @@ | |||
55 | static enum GNUNET_BLOCK_EvaluationResult | 113 | static enum GNUNET_BLOCK_EvaluationResult |
56 | evaluate_block_regex (void *cls, | 114 | evaluate_block_regex (void *cls, |
57 | enum GNUNET_BLOCK_Type type, | 115 | enum GNUNET_BLOCK_Type type, |
116 | struct GNUNET_BLOCK_Group *bg, | ||
58 | enum GNUNET_BLOCK_EvaluationOptions eo, | 117 | enum GNUNET_BLOCK_EvaluationOptions eo, |
59 | const struct GNUNET_HashCode *query, | 118 | const struct GNUNET_HashCode *query, |
60 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
61 | int32_t bf_mutator, | ||
62 | const void *xquery, | 119 | const void *xquery, |
63 | size_t xquery_size, | 120 | size_t xquery_size, |
64 | const void *reply_block, | 121 | const void *reply_block, |
65 | size_t reply_block_size) | 122 | size_t reply_block_size) |
66 | { | 123 | { |
124 | struct GNUNET_HashCode chash; | ||
125 | |||
67 | if (NULL == reply_block) | 126 | if (NULL == reply_block) |
68 | { | 127 | { |
69 | if (0 != xquery_size) | 128 | if (0 != xquery_size) |
@@ -112,24 +171,13 @@ evaluate_block_regex (void *cls, | |||
112 | default: | 171 | default: |
113 | break; | 172 | break; |
114 | } | 173 | } |
115 | if (NULL != bf) | 174 | GNUNET_CRYPTO_hash (reply_block, |
116 | { | 175 | reply_block_size, |
117 | struct GNUNET_HashCode chash; | 176 | &chash); |
118 | struct GNUNET_HashCode mhash; | 177 | if (GNUNET_YES == |
119 | 178 | GNUNET_BLOCK_GROUP_bf_test_and_set (bg, | |
120 | GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash); | 179 | &chash)) |
121 | GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash); | 180 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; |
122 | if (NULL != *bf) | ||
123 | { | ||
124 | if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash)) | ||
125 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; | ||
126 | } | ||
127 | else | ||
128 | { | ||
129 | *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
130 | } | ||
131 | GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash); | ||
132 | } | ||
133 | return GNUNET_BLOCK_EVALUATION_OK_MORE; | 181 | return GNUNET_BLOCK_EVALUATION_OK_MORE; |
134 | } | 182 | } |
135 | 183 | ||
@@ -144,10 +192,9 @@ evaluate_block_regex (void *cls, | |||
144 | * | 192 | * |
145 | * @param cls closure | 193 | * @param cls closure |
146 | * @param type block type | 194 | * @param type block type |
195 | * @param bg block group to evaluate against | ||
147 | * @param eo control flags | 196 | * @param eo control flags |
148 | * @param query original query (hash) | 197 | * @param query original query (hash) |
149 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
150 | * @param bf_mutator mutation value for bf | ||
151 | * @param xquery extrended query data (can be NULL, depending on type) | 198 | * @param xquery extrended query data (can be NULL, depending on type) |
152 | * @param xquery_size number of bytes in @a xquery | 199 | * @param xquery_size number of bytes in @a xquery |
153 | * @param reply_block response to validate | 200 | * @param reply_block response to validate |
@@ -157,14 +204,15 @@ evaluate_block_regex (void *cls, | |||
157 | static enum GNUNET_BLOCK_EvaluationResult | 204 | static enum GNUNET_BLOCK_EvaluationResult |
158 | evaluate_block_regex_accept (void *cls, | 205 | evaluate_block_regex_accept (void *cls, |
159 | enum GNUNET_BLOCK_Type type, | 206 | enum GNUNET_BLOCK_Type type, |
207 | struct GNUNET_BLOCK_Group *bg, | ||
160 | enum GNUNET_BLOCK_EvaluationOptions eo, | 208 | enum GNUNET_BLOCK_EvaluationOptions eo, |
161 | const struct GNUNET_HashCode * query, | 209 | const struct GNUNET_HashCode *query, |
162 | struct GNUNET_CONTAINER_BloomFilter **bf, | 210 | const void *xquery, |
163 | int32_t bf_mutator, const void *xquery, | ||
164 | size_t xquery_size, const void *reply_block, | 211 | size_t xquery_size, const void *reply_block, |
165 | size_t reply_block_size) | 212 | size_t reply_block_size) |
166 | { | 213 | { |
167 | const struct RegexAcceptBlock *rba; | 214 | const struct RegexAcceptBlock *rba; |
215 | struct GNUNET_HashCode chash; | ||
168 | 216 | ||
169 | if (0 != xquery_size) | 217 | if (0 != xquery_size) |
170 | { | 218 | { |
@@ -202,24 +250,13 @@ evaluate_block_regex_accept (void *cls, | |||
202 | GNUNET_break_op(0); | 250 | GNUNET_break_op(0); |
203 | return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; | 251 | return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; |
204 | } | 252 | } |
205 | if (NULL != bf) | 253 | GNUNET_CRYPTO_hash (reply_block, |
206 | { | 254 | reply_block_size, |
207 | struct GNUNET_HashCode chash; | 255 | &chash); |
208 | struct GNUNET_HashCode mhash; | 256 | if (GNUNET_YES == |
209 | 257 | GNUNET_BLOCK_GROUP_bf_test_and_set (bg, | |
210 | GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash); | 258 | &chash)) |
211 | GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash); | 259 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; |
212 | if (NULL != *bf) | ||
213 | { | ||
214 | if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash)) | ||
215 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; | ||
216 | } | ||
217 | else | ||
218 | { | ||
219 | *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
220 | } | ||
221 | GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash); | ||
222 | } | ||
223 | return GNUNET_BLOCK_EVALUATION_OK_MORE; | 260 | return GNUNET_BLOCK_EVALUATION_OK_MORE; |
224 | } | 261 | } |
225 | 262 | ||
@@ -232,11 +269,11 @@ evaluate_block_regex_accept (void *cls, | |||
232 | * be done with the #GNUNET_BLOCK_get_key() function. | 269 | * be done with the #GNUNET_BLOCK_get_key() function. |
233 | * | 270 | * |
234 | * @param cls closure | 271 | * @param cls closure |
272 | * @param ctx block context | ||
235 | * @param type block type | 273 | * @param type block type |
274 | * @param bg group to evaluate against | ||
236 | * @param eo control flags | 275 | * @param eo control flags |
237 | * @param query original query (hash) | 276 | * @param query original query (hash) |
238 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
239 | * @param bf_mutator mutation value for bf | ||
240 | * @param xquery extrended query data (can be NULL, depending on type) | 277 | * @param xquery extrended query data (can be NULL, depending on type) |
241 | * @param xquery_size number of bytes in xquery | 278 | * @param xquery_size number of bytes in xquery |
242 | * @param reply_block response to validate | 279 | * @param reply_block response to validate |
@@ -245,11 +282,11 @@ evaluate_block_regex_accept (void *cls, | |||
245 | */ | 282 | */ |
246 | static enum GNUNET_BLOCK_EvaluationResult | 283 | static enum GNUNET_BLOCK_EvaluationResult |
247 | block_plugin_regex_evaluate (void *cls, | 284 | block_plugin_regex_evaluate (void *cls, |
285 | struct GNUNET_BLOCK_Context *ctx, | ||
248 | enum GNUNET_BLOCK_Type type, | 286 | enum GNUNET_BLOCK_Type type, |
287 | struct GNUNET_BLOCK_Group *bg, | ||
249 | enum GNUNET_BLOCK_EvaluationOptions eo, | 288 | enum GNUNET_BLOCK_EvaluationOptions eo, |
250 | const struct GNUNET_HashCode *query, | 289 | const struct GNUNET_HashCode *query, |
251 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
252 | int32_t bf_mutator, | ||
253 | const void *xquery, | 290 | const void *xquery, |
254 | size_t xquery_size, | 291 | size_t xquery_size, |
255 | const void *reply_block, | 292 | const void *reply_block, |
@@ -262,18 +299,18 @@ block_plugin_regex_evaluate (void *cls, | |||
262 | case GNUNET_BLOCK_TYPE_REGEX: | 299 | case GNUNET_BLOCK_TYPE_REGEX: |
263 | result = evaluate_block_regex (cls, | 300 | result = evaluate_block_regex (cls, |
264 | type, | 301 | type, |
302 | bg, | ||
265 | eo, | 303 | eo, |
266 | query, | 304 | query, |
267 | bf, bf_mutator, | ||
268 | xquery, xquery_size, | 305 | xquery, xquery_size, |
269 | reply_block, reply_block_size); | 306 | reply_block, reply_block_size); |
270 | break; | 307 | break; |
271 | case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: | 308 | case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: |
272 | result = evaluate_block_regex_accept (cls, | 309 | result = evaluate_block_regex_accept (cls, |
273 | type, | 310 | type, |
311 | bg, | ||
274 | eo, | 312 | eo, |
275 | query, | 313 | query, |
276 | bf, bf_mutator, | ||
277 | xquery, xquery_size, | 314 | xquery, xquery_size, |
278 | reply_block, reply_block_size); | 315 | reply_block, reply_block_size); |
279 | break; | 316 | break; |
@@ -346,6 +383,7 @@ libgnunet_plugin_block_regex_init (void *cls) | |||
346 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | 383 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); |
347 | api->evaluate = &block_plugin_regex_evaluate; | 384 | api->evaluate = &block_plugin_regex_evaluate; |
348 | api->get_key = &block_plugin_regex_get_key; | 385 | api->get_key = &block_plugin_regex_get_key; |
386 | api->create_group = &block_plugin_regex_create_group; | ||
349 | api->types = types; | 387 | api->types = types; |
350 | return api; | 388 | return api; |
351 | } | 389 | } |
@@ -357,7 +395,7 @@ libgnunet_plugin_block_regex_init (void *cls) | |||
357 | void * | 395 | void * |
358 | libgnunet_plugin_block_regex_done (void *cls) | 396 | libgnunet_plugin_block_regex_done (void *cls) |
359 | { | 397 | { |
360 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | 398 | struct GNUNET_BLOCK_PluginFunctions *api = cls; |
361 | 399 | ||
362 | GNUNET_free (api); | 400 | GNUNET_free (api); |
363 | return NULL; | 401 | return NULL; |