aboutsummaryrefslogtreecommitdiff
path: root/src/regex/plugin_block_regex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regex/plugin_block_regex.c')
-rw-r--r--src/regex/plugin_block_regex.c386
1 files changed, 194 insertions, 192 deletions
diff --git a/src/regex/plugin_block_regex.c b/src/regex/plugin_block_regex.c
index 9467c02b9..e8ff158dd 100644
--- a/src/regex/plugin_block_regex.c
+++ b/src/regex/plugin_block_regex.c
@@ -57,37 +57,38 @@
57 * by this @a type of block (this is not an error) 57 * by this @a type of block (this is not an error)
58 */ 58 */
59static struct GNUNET_BLOCK_Group * 59static struct GNUNET_BLOCK_Group *
60block_plugin_regex_create_group(void *cls, 60block_plugin_regex_create_group (void *cls,
61 enum GNUNET_BLOCK_Type type, 61 enum GNUNET_BLOCK_Type type,
62 uint32_t nonce, 62 uint32_t nonce,
63 const void *raw_data, 63 const void *raw_data,
64 size_t raw_data_size, 64 size_t raw_data_size,
65 va_list va) 65 va_list va)
66{ 66{
67 unsigned int bf_size; 67 unsigned int bf_size;
68 const char *guard; 68 const char *guard;
69 69
70 guard = va_arg(va, const char *); 70 guard = va_arg (va, const char *);
71 if (0 == strcmp(guard, 71 if (0 == strcmp (guard,
72 "seen-set-size")) 72 "seen-set-size"))
73 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size(va_arg(va, unsigned int), 73 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned
74 BLOOMFILTER_K); 74 int),
75 else if (0 == strcmp(guard, 75 BLOOMFILTER_K);
76 "filter-size")) 76 else if (0 == strcmp (guard,
77 bf_size = va_arg(va, unsigned int); 77 "filter-size"))
78 bf_size = va_arg (va, unsigned int);
78 else 79 else
79 { 80 {
80 GNUNET_break(0); 81 GNUNET_break (0);
81 bf_size = REGEX_BF_SIZE; 82 bf_size = REGEX_BF_SIZE;
82 } 83 }
83 GNUNET_break(NULL == va_arg(va, const char *)); 84 GNUNET_break (NULL == va_arg (va, const char *));
84 return GNUNET_BLOCK_GROUP_bf_create(cls, 85 return GNUNET_BLOCK_GROUP_bf_create (cls,
85 bf_size, 86 bf_size,
86 BLOOMFILTER_K, 87 BLOOMFILTER_K,
87 type, 88 type,
88 nonce, 89 nonce,
89 raw_data, 90 raw_data,
90 raw_data_size); 91 raw_data_size);
91} 92}
92 93
93 94
@@ -111,74 +112,74 @@ block_plugin_regex_create_group(void *cls,
111 * @return characterization of result 112 * @return characterization of result
112 */ 113 */
113static enum GNUNET_BLOCK_EvaluationResult 114static enum GNUNET_BLOCK_EvaluationResult
114evaluate_block_regex(void *cls, 115evaluate_block_regex (void *cls,
115 enum GNUNET_BLOCK_Type type, 116 enum GNUNET_BLOCK_Type type,
116 struct GNUNET_BLOCK_Group *bg, 117 struct GNUNET_BLOCK_Group *bg,
117 enum GNUNET_BLOCK_EvaluationOptions eo, 118 enum GNUNET_BLOCK_EvaluationOptions eo,
118 const struct GNUNET_HashCode *query, 119 const struct GNUNET_HashCode *query,
119 const void *xquery, 120 const void *xquery,
120 size_t xquery_size, 121 size_t xquery_size,
121 const void *reply_block, 122 const void *reply_block,
122 size_t reply_block_size) 123 size_t reply_block_size)
123{ 124{
124 struct GNUNET_HashCode chash; 125 struct GNUNET_HashCode chash;
125 126
126 if (NULL == reply_block) 127 if (NULL == reply_block)
127 { 128 {
128 if (0 != xquery_size) 129 if (0 != xquery_size)
129 {
130 const char *s;
131
132 s = (const char *)xquery;
133 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
134 {
135 GNUNET_break_op(0);
136 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
137 }
138 }
139 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
140 }
141 if (0 != xquery_size)
142 { 130 {
143 const char *s; 131 const char *s;
144 132
145 s = (const char *)xquery; 133 s = (const char *) xquery;
146 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */ 134 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
147 { 135 {
148 GNUNET_break_op(0); 136 GNUNET_break_op (0);
149 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 137 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
150 } 138 }
151 } 139 }
152 else if (NULL != query) 140 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
141 }
142 if (0 != xquery_size)
143 {
144 const char *s;
145
146 s = (const char *) xquery;
147 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
153 { 148 {
154 /* xquery is required for regex GETs, at least an empty string */ 149 GNUNET_break_op (0);
155 GNUNET_break_op(0);
156 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
157 type, query, xquery);
158 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 150 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
159 } 151 }
160 switch (REGEX_BLOCK_check(reply_block, 152 }
161 reply_block_size, 153 else if (NULL != query)
162 query, 154 {
163 xquery)) 155 /* xquery is required for regex GETs, at least an empty string */
164 { 156 GNUNET_break_op (0);
165 case GNUNET_SYSERR: 157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
166 GNUNET_break_op(0); 158 type, query, xquery);
167 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 159 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
168 160 }
169 case GNUNET_NO: 161 switch (REGEX_BLOCK_check (reply_block,
170 /* xquery missmatch, can happen */ 162 reply_block_size,
171 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT; 163 query,
172 164 xquery))
173 default: 165 {
174 break; 166 case GNUNET_SYSERR:
175 } 167 GNUNET_break_op (0);
176 GNUNET_CRYPTO_hash(reply_block, 168 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
177 reply_block_size, 169
178 &chash); 170 case GNUNET_NO:
171 /* xquery missmatch, can happen */
172 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
173
174 default:
175 break;
176 }
177 GNUNET_CRYPTO_hash (reply_block,
178 reply_block_size,
179 &chash);
179 if (GNUNET_YES == 180 if (GNUNET_YES ==
180 GNUNET_BLOCK_GROUP_bf_test_and_set(bg, 181 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
181 &chash)) 182 &chash))
182 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 183 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
183 return GNUNET_BLOCK_EVALUATION_OK_MORE; 184 return GNUNET_BLOCK_EVALUATION_OK_MORE;
184} 185}
@@ -204,60 +205,62 @@ evaluate_block_regex(void *cls,
204 * @return characterization of result 205 * @return characterization of result
205 */ 206 */
206static enum GNUNET_BLOCK_EvaluationResult 207static enum GNUNET_BLOCK_EvaluationResult
207evaluate_block_regex_accept(void *cls, 208evaluate_block_regex_accept (void *cls,
208 enum GNUNET_BLOCK_Type type, 209 enum GNUNET_BLOCK_Type type,
209 struct GNUNET_BLOCK_Group *bg, 210 struct GNUNET_BLOCK_Group *bg,
210 enum GNUNET_BLOCK_EvaluationOptions eo, 211 enum GNUNET_BLOCK_EvaluationOptions eo,
211 const struct GNUNET_HashCode *query, 212 const struct GNUNET_HashCode *query,
212 const void *xquery, 213 const void *xquery,
213 size_t xquery_size, const void *reply_block, 214 size_t xquery_size, const void *reply_block,
214 size_t reply_block_size) 215 size_t reply_block_size)
215{ 216{
216 const struct RegexAcceptBlock *rba; 217 const struct RegexAcceptBlock *rba;
217 struct GNUNET_HashCode chash; 218 struct GNUNET_HashCode chash;
218 219
219 if (0 != xquery_size) 220 if (0 != xquery_size)
220 { 221 {
221 GNUNET_break_op(0); 222 GNUNET_break_op (0);
222 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 223 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
223 } 224 }
224 if (NULL == reply_block) 225 if (NULL == reply_block)
225 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; 226 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
226 if (sizeof(struct RegexAcceptBlock) != reply_block_size) 227 if (sizeof(struct RegexAcceptBlock) != reply_block_size)
227 { 228 {
228 GNUNET_break_op(0); 229 GNUNET_break_op (0);
229 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 230 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
230 } 231 }
231 rba = reply_block; 232 rba = reply_block;
232 if (ntohl(rba->purpose.size) != 233 if (ntohl (rba->purpose.size) !=
233 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + 234 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
234 sizeof(struct GNUNET_TIME_AbsoluteNBO) + 235 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
235 sizeof(struct GNUNET_HashCode)) 236 + sizeof(struct GNUNET_HashCode))
236 { 237 {
237 GNUNET_break_op(0); 238 GNUNET_break_op (0);
238 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 239 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
239 } 240 }
240 if (0 == GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_ntoh(rba->expiration_time)).rel_value_us) 241 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (
241 { 242 rba->expiration_time)).
242 /* technically invalid, but can happen without an error, so 243 rel_value_us)
243 we're nice by reporting it as a 'duplicate' */ 244 {
244 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 245 /* technically invalid, but can happen without an error, so
245 } 246 we're nice by reporting it as a 'duplicate' */
247 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
248 }
246 if (GNUNET_OK != 249 if (GNUNET_OK !=
247 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT, 250 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
248 &rba->purpose, 251 &rba->purpose,
249 &rba->signature, 252 &rba->signature,
250 &rba->peer.public_key)) 253 &rba->peer.public_key))
251 { 254 {
252 GNUNET_break_op(0); 255 GNUNET_break_op (0);
253 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 256 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
254 } 257 }
255 GNUNET_CRYPTO_hash(reply_block, 258 GNUNET_CRYPTO_hash (reply_block,
256 reply_block_size, 259 reply_block_size,
257 &chash); 260 &chash);
258 if (GNUNET_YES == 261 if (GNUNET_YES ==
259 GNUNET_BLOCK_GROUP_bf_test_and_set(bg, 262 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
260 &chash)) 263 &chash))
261 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 264 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
262 return GNUNET_BLOCK_EVALUATION_OK_MORE; 265 return GNUNET_BLOCK_EVALUATION_OK_MORE;
263} 266}
@@ -283,44 +286,44 @@ evaluate_block_regex_accept(void *cls,
283 * @return characterization of result 286 * @return characterization of result
284 */ 287 */
285static enum GNUNET_BLOCK_EvaluationResult 288static enum GNUNET_BLOCK_EvaluationResult
286block_plugin_regex_evaluate(void *cls, 289block_plugin_regex_evaluate (void *cls,
287 struct GNUNET_BLOCK_Context *ctx, 290 struct GNUNET_BLOCK_Context *ctx,
288 enum GNUNET_BLOCK_Type type, 291 enum GNUNET_BLOCK_Type type,
289 struct GNUNET_BLOCK_Group *bg, 292 struct GNUNET_BLOCK_Group *bg,
290 enum GNUNET_BLOCK_EvaluationOptions eo, 293 enum GNUNET_BLOCK_EvaluationOptions eo,
291 const struct GNUNET_HashCode *query, 294 const struct GNUNET_HashCode *query,
292 const void *xquery, 295 const void *xquery,
293 size_t xquery_size, 296 size_t xquery_size,
294 const void *reply_block, 297 const void *reply_block,
295 size_t reply_block_size) 298 size_t reply_block_size)
296{ 299{
297 enum GNUNET_BLOCK_EvaluationResult result; 300 enum GNUNET_BLOCK_EvaluationResult result;
298 301
299 switch (type) 302 switch (type)
300 { 303 {
301 case GNUNET_BLOCK_TYPE_REGEX: 304 case GNUNET_BLOCK_TYPE_REGEX:
302 result = evaluate_block_regex(cls, 305 result = evaluate_block_regex (cls,
303 type, 306 type,
304 bg, 307 bg,
305 eo, 308 eo,
306 query, 309 query,
307 xquery, xquery_size, 310 xquery, xquery_size,
308 reply_block, reply_block_size); 311 reply_block, reply_block_size);
309 break; 312 break;
310 313
311 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: 314 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
312 result = evaluate_block_regex_accept(cls, 315 result = evaluate_block_regex_accept (cls,
313 type, 316 type,
314 bg, 317 bg,
315 eo, 318 eo,
316 query, 319 query,
317 xquery, xquery_size, 320 xquery, xquery_size,
318 reply_block, reply_block_size); 321 reply_block, reply_block_size);
319 break; 322 break;
320 323
321 default: 324 default:
322 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; 325 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
323 } 326 }
324 return result; 327 return result;
325} 328}
326 329
@@ -337,37 +340,37 @@ block_plugin_regex_evaluate(void *cls,
337 * (or if extracting a key from a block of this type does not work) 340 * (or if extracting a key from a block of this type does not work)
338 */ 341 */
339static int 342static int
340block_plugin_regex_get_key(void *cls, 343block_plugin_regex_get_key (void *cls,
341 enum GNUNET_BLOCK_Type type, 344 enum GNUNET_BLOCK_Type type,
342 const void *block, 345 const void *block,
343 size_t block_size, 346 size_t block_size,
344 struct GNUNET_HashCode *key) 347 struct GNUNET_HashCode *key)
345{ 348{
346 switch (type) 349 switch (type)
350 {
351 case GNUNET_BLOCK_TYPE_REGEX:
352 if (GNUNET_OK !=
353 REGEX_BLOCK_get_key (block, block_size,
354 key))
355 {
356 GNUNET_break_op (0);
357 return GNUNET_NO;
358 }
359 return GNUNET_OK;
360
361 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
362 if (sizeof(struct RegexAcceptBlock) != block_size)
347 { 363 {
348 case GNUNET_BLOCK_TYPE_REGEX: 364 GNUNET_break_op (0);
349 if (GNUNET_OK != 365 return GNUNET_NO;
350 REGEX_BLOCK_get_key(block, block_size,
351 key))
352 {
353 GNUNET_break_op(0);
354 return GNUNET_NO;
355 }
356 return GNUNET_OK;
357
358 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
359 if (sizeof(struct RegexAcceptBlock) != block_size)
360 {
361 GNUNET_break_op(0);
362 return GNUNET_NO;
363 }
364 *key = ((struct RegexAcceptBlock *)block)->key;
365 return GNUNET_OK;
366
367 default:
368 GNUNET_break(0);
369 return GNUNET_SYSERR;
370 } 366 }
367 *key = ((struct RegexAcceptBlock *) block)->key;
368 return GNUNET_OK;
369
370 default:
371 GNUNET_break (0);
372 return GNUNET_SYSERR;
373 }
371} 374}
372 375
373 376
@@ -375,17 +378,16 @@ block_plugin_regex_get_key(void *cls,
375 * Entry point for the plugin. 378 * Entry point for the plugin.
376 */ 379 */
377void * 380void *
378libgnunet_plugin_block_regex_init(void *cls) 381libgnunet_plugin_block_regex_init (void *cls)
379{ 382{
380 static enum GNUNET_BLOCK_Type types[] = 383 static enum GNUNET_BLOCK_Type types[] = {
381 {
382 GNUNET_BLOCK_TYPE_REGEX, 384 GNUNET_BLOCK_TYPE_REGEX,
383 GNUNET_BLOCK_TYPE_REGEX_ACCEPT, 385 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
384 GNUNET_BLOCK_TYPE_ANY /* end of list */ 386 GNUNET_BLOCK_TYPE_ANY /* end of list */
385 }; 387 };
386 struct GNUNET_BLOCK_PluginFunctions *api; 388 struct GNUNET_BLOCK_PluginFunctions *api;
387 389
388 api = GNUNET_new(struct GNUNET_BLOCK_PluginFunctions); 390 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
389 api->evaluate = &block_plugin_regex_evaluate; 391 api->evaluate = &block_plugin_regex_evaluate;
390 api->get_key = &block_plugin_regex_get_key; 392 api->get_key = &block_plugin_regex_get_key;
391 api->create_group = &block_plugin_regex_create_group; 393 api->create_group = &block_plugin_regex_create_group;
@@ -398,11 +400,11 @@ libgnunet_plugin_block_regex_init(void *cls)
398 * Exit point from the plugin. 400 * Exit point from the plugin.
399 */ 401 */
400void * 402void *
401libgnunet_plugin_block_regex_done(void *cls) 403libgnunet_plugin_block_regex_done (void *cls)
402{ 404{
403 struct GNUNET_BLOCK_PluginFunctions *api = cls; 405 struct GNUNET_BLOCK_PluginFunctions *api = cls;
404 406
405 GNUNET_free(api); 407 GNUNET_free (api);
406 return NULL; 408 return NULL;
407} 409}
408 410