summaryrefslogtreecommitdiff
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.c347
1 files changed, 176 insertions, 171 deletions
diff --git a/src/regex/plugin_block_regex.c b/src/regex/plugin_block_regex.c
index bc2d287c5..9467c02b9 100644
--- a/src/regex/plugin_block_regex.c
+++ b/src/regex/plugin_block_regex.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file regex/plugin_block_regex.c 22 * @file regex/plugin_block_regex.c
@@ -57,37 +57,37 @@
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 int),
74 BLOOMFILTER_K); 74 BLOOMFILTER_K);
75 else if (0 == strcmp (guard, 75 else if (0 == strcmp(guard,
76 "filter-size")) 76 "filter-size"))
77 bf_size = va_arg (va, unsigned int); 77 bf_size = va_arg(va, unsigned int);
78 else 78 else
79 { 79 {
80 GNUNET_break (0); 80 GNUNET_break(0);
81 bf_size = REGEX_BF_SIZE; 81 bf_size = REGEX_BF_SIZE;
82 } 82 }
83 GNUNET_break (NULL == va_arg (va, const char *)); 83 GNUNET_break(NULL == va_arg(va, const char *));
84 return GNUNET_BLOCK_GROUP_bf_create (cls, 84 return GNUNET_BLOCK_GROUP_bf_create(cls,
85 bf_size, 85 bf_size,
86 BLOOMFILTER_K, 86 BLOOMFILTER_K,
87 type, 87 type,
88 nonce, 88 nonce,
89 raw_data, 89 raw_data,
90 raw_data_size); 90 raw_data_size);
91} 91}
92 92
93 93
@@ -111,72 +111,74 @@ block_plugin_regex_create_group (void *cls,
111 * @return characterization of result 111 * @return characterization of result
112 */ 112 */
113static enum GNUNET_BLOCK_EvaluationResult 113static enum GNUNET_BLOCK_EvaluationResult
114evaluate_block_regex (void *cls, 114evaluate_block_regex(void *cls,
115 enum GNUNET_BLOCK_Type type, 115 enum GNUNET_BLOCK_Type type,
116 struct GNUNET_BLOCK_Group *bg, 116 struct GNUNET_BLOCK_Group *bg,
117 enum GNUNET_BLOCK_EvaluationOptions eo, 117 enum GNUNET_BLOCK_EvaluationOptions eo,
118 const struct GNUNET_HashCode *query, 118 const struct GNUNET_HashCode *query,
119 const void *xquery, 119 const void *xquery,
120 size_t xquery_size, 120 size_t xquery_size,
121 const void *reply_block, 121 const void *reply_block,
122 size_t reply_block_size) 122 size_t reply_block_size)
123{ 123{
124 struct GNUNET_HashCode chash; 124 struct GNUNET_HashCode chash;
125 125
126 if (NULL == reply_block) 126 if (NULL == reply_block)
127 { 127 {
128 if (0 != xquery_size) 128 if (0 != xquery_size)
129 { 129 {
130 const char *s; 130 const char *s;
131 131
132 s = (const char *) xquery; 132 s = (const char *)xquery;
133 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */ 133 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
134 { 134 {
135 GNUNET_break_op (0); 135 GNUNET_break_op(0);
136 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 136 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
137 } 137 }
138 } 138 }
139 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; 139 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
140 } 140 }
141 if (0 != xquery_size) 141 if (0 != xquery_size)
142 {
143 const char *s;
144
145 s = (const char *) xquery;
146 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
147 { 142 {
148 GNUNET_break_op (0); 143 const char *s;
149 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 144
145 s = (const char *)xquery;
146 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
147 {
148 GNUNET_break_op(0);
149 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
150 }
150 } 151 }
151 }
152 else if (NULL != query) 152 else if (NULL != query)
153 { 153 {
154 /* xquery is required for regex GETs, at least an empty string */ 154 /* xquery is required for regex GETs, at least an empty string */
155 GNUNET_break_op (0); 155 GNUNET_break_op(0);
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n", 156 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
157 type, query, xquery); 157 type, query, xquery);
158 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 158 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
159 } 159 }
160 switch (REGEX_BLOCK_check (reply_block, 160 switch (REGEX_BLOCK_check(reply_block,
161 reply_block_size, 161 reply_block_size,
162 query, 162 query,
163 xquery)) 163 xquery))
164 { 164 {
165 case GNUNET_SYSERR: 165 case GNUNET_SYSERR:
166 GNUNET_break_op(0); 166 GNUNET_break_op(0);
167 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 167 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
168
168 case GNUNET_NO: 169 case GNUNET_NO:
169 /* xquery missmatch, can happen */ 170 /* xquery missmatch, can happen */
170 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT; 171 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
172
171 default: 173 default:
172 break; 174 break;
173 } 175 }
174 GNUNET_CRYPTO_hash (reply_block, 176 GNUNET_CRYPTO_hash(reply_block,
175 reply_block_size, 177 reply_block_size,
176 &chash); 178 &chash);
177 if (GNUNET_YES == 179 if (GNUNET_YES ==
178 GNUNET_BLOCK_GROUP_bf_test_and_set (bg, 180 GNUNET_BLOCK_GROUP_bf_test_and_set(bg,
179 &chash)) 181 &chash))
180 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 182 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
181 return GNUNET_BLOCK_EVALUATION_OK_MORE; 183 return GNUNET_BLOCK_EVALUATION_OK_MORE;
182} 184}
@@ -202,60 +204,60 @@ evaluate_block_regex (void *cls,
202 * @return characterization of result 204 * @return characterization of result
203 */ 205 */
204static enum GNUNET_BLOCK_EvaluationResult 206static enum GNUNET_BLOCK_EvaluationResult
205evaluate_block_regex_accept (void *cls, 207evaluate_block_regex_accept(void *cls,
206 enum GNUNET_BLOCK_Type type, 208 enum GNUNET_BLOCK_Type type,
207 struct GNUNET_BLOCK_Group *bg, 209 struct GNUNET_BLOCK_Group *bg,
208 enum GNUNET_BLOCK_EvaluationOptions eo, 210 enum GNUNET_BLOCK_EvaluationOptions eo,
209 const struct GNUNET_HashCode *query, 211 const struct GNUNET_HashCode *query,
210 const void *xquery, 212 const void *xquery,
211 size_t xquery_size, const void *reply_block, 213 size_t xquery_size, const void *reply_block,
212 size_t reply_block_size) 214 size_t reply_block_size)
213{ 215{
214 const struct RegexAcceptBlock *rba; 216 const struct RegexAcceptBlock *rba;
215 struct GNUNET_HashCode chash; 217 struct GNUNET_HashCode chash;
216 218
217 if (0 != xquery_size) 219 if (0 != xquery_size)
218 { 220 {
219 GNUNET_break_op (0); 221 GNUNET_break_op(0);
220 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; 222 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
221 } 223 }
222 if (NULL == reply_block) 224 if (NULL == reply_block)
223 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; 225 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
224 if (sizeof (struct RegexAcceptBlock) != reply_block_size) 226 if (sizeof(struct RegexAcceptBlock) != reply_block_size)
225 { 227 {
226 GNUNET_break_op(0); 228 GNUNET_break_op(0);
227 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 229 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
228 } 230 }
229 rba = reply_block; 231 rba = reply_block;
230 if (ntohl (rba->purpose.size) != 232 if (ntohl(rba->purpose.size) !=
231 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + 233 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
232 sizeof (struct GNUNET_TIME_AbsoluteNBO) + 234 sizeof(struct GNUNET_TIME_AbsoluteNBO) +
233 sizeof (struct GNUNET_HashCode)) 235 sizeof(struct GNUNET_HashCode))
234 { 236 {
235 GNUNET_break_op(0); 237 GNUNET_break_op(0);
236 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 238 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
237 } 239 }
238 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us) 240 if (0 == GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_ntoh(rba->expiration_time)).rel_value_us)
239 { 241 {
240 /* technically invalid, but can happen without an error, so 242 /* technically invalid, but can happen without an error, so
241 we're nice by reporting it as a 'duplicate' */ 243 we're nice by reporting it as a 'duplicate' */
242 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 244 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
243 } 245 }
244 if (GNUNET_OK != 246 if (GNUNET_OK !=
245 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT, 247 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
246 &rba->purpose, 248 &rba->purpose,
247 &rba->signature, 249 &rba->signature,
248 &rba->peer.public_key)) 250 &rba->peer.public_key))
249 { 251 {
250 GNUNET_break_op(0); 252 GNUNET_break_op(0);
251 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 253 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
252 } 254 }
253 GNUNET_CRYPTO_hash (reply_block, 255 GNUNET_CRYPTO_hash(reply_block,
254 reply_block_size, 256 reply_block_size,
255 &chash); 257 &chash);
256 if (GNUNET_YES == 258 if (GNUNET_YES ==
257 GNUNET_BLOCK_GROUP_bf_test_and_set (bg, 259 GNUNET_BLOCK_GROUP_bf_test_and_set(bg,
258 &chash)) 260 &chash))
259 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; 261 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
260 return GNUNET_BLOCK_EVALUATION_OK_MORE; 262 return GNUNET_BLOCK_EVALUATION_OK_MORE;
261} 263}
@@ -281,43 +283,44 @@ evaluate_block_regex_accept (void *cls,
281 * @return characterization of result 283 * @return characterization of result
282 */ 284 */
283static enum GNUNET_BLOCK_EvaluationResult 285static enum GNUNET_BLOCK_EvaluationResult
284block_plugin_regex_evaluate (void *cls, 286block_plugin_regex_evaluate(void *cls,
285 struct GNUNET_BLOCK_Context *ctx, 287 struct GNUNET_BLOCK_Context *ctx,
286 enum GNUNET_BLOCK_Type type, 288 enum GNUNET_BLOCK_Type type,
287 struct GNUNET_BLOCK_Group *bg, 289 struct GNUNET_BLOCK_Group *bg,
288 enum GNUNET_BLOCK_EvaluationOptions eo, 290 enum GNUNET_BLOCK_EvaluationOptions eo,
289 const struct GNUNET_HashCode *query, 291 const struct GNUNET_HashCode *query,
290 const void *xquery, 292 const void *xquery,
291 size_t xquery_size, 293 size_t xquery_size,
292 const void *reply_block, 294 const void *reply_block,
293 size_t reply_block_size) 295 size_t reply_block_size)
294{ 296{
295 enum GNUNET_BLOCK_EvaluationResult result; 297 enum GNUNET_BLOCK_EvaluationResult result;
296 298
297 switch (type) 299 switch (type)
298 { 300 {
299 case GNUNET_BLOCK_TYPE_REGEX: 301 case GNUNET_BLOCK_TYPE_REGEX:
300 result = evaluate_block_regex (cls, 302 result = evaluate_block_regex(cls,
301 type, 303 type,
302 bg, 304 bg,
303 eo, 305 eo,
304 query, 306 query,
305 xquery, xquery_size, 307 xquery, xquery_size,
306 reply_block, reply_block_size); 308 reply_block, reply_block_size);
307 break; 309 break;
310
308 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: 311 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
309 result = evaluate_block_regex_accept (cls, 312 result = evaluate_block_regex_accept(cls,
310 type, 313 type,
311 bg, 314 bg,
312 eo, 315 eo,
313 query, 316 query,
314 xquery, xquery_size, 317 xquery, xquery_size,
315 reply_block, reply_block_size); 318 reply_block, reply_block_size);
316 break; 319 break;
317 320
318 default: 321 default:
319 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; 322 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
320 } 323 }
321 return result; 324 return result;
322} 325}
323 326
@@ -334,35 +337,37 @@ block_plugin_regex_evaluate (void *cls,
334 * (or if extracting a key from a block of this type does not work) 337 * (or if extracting a key from a block of this type does not work)
335 */ 338 */
336static int 339static int
337block_plugin_regex_get_key (void *cls, 340block_plugin_regex_get_key(void *cls,
338 enum GNUNET_BLOCK_Type type, 341 enum GNUNET_BLOCK_Type type,
339 const void *block, 342 const void *block,
340 size_t block_size, 343 size_t block_size,
341 struct GNUNET_HashCode *key) 344 struct GNUNET_HashCode *key)
342{ 345{
343 switch (type) 346 switch (type)
344 { 347 {
345 case GNUNET_BLOCK_TYPE_REGEX: 348 case GNUNET_BLOCK_TYPE_REGEX:
346 if (GNUNET_OK != 349 if (GNUNET_OK !=
347 REGEX_BLOCK_get_key (block, block_size, 350 REGEX_BLOCK_get_key(block, block_size,
348 key)) 351 key))
349 { 352 {
350 GNUNET_break_op (0); 353 GNUNET_break_op(0);
351 return GNUNET_NO; 354 return GNUNET_NO;
352 } 355 }
353 return GNUNET_OK; 356 return GNUNET_OK;
357
354 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: 358 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
355 if (sizeof (struct RegexAcceptBlock) != block_size) 359 if (sizeof(struct RegexAcceptBlock) != block_size)
356 { 360 {
357 GNUNET_break_op (0); 361 GNUNET_break_op(0);
358 return GNUNET_NO; 362 return GNUNET_NO;
359 } 363 }
360 *key = ((struct RegexAcceptBlock *) block)->key; 364 *key = ((struct RegexAcceptBlock *)block)->key;
361 return GNUNET_OK; 365 return GNUNET_OK;
366
362 default: 367 default:
363 GNUNET_break (0); 368 GNUNET_break(0);
364 return GNUNET_SYSERR; 369 return GNUNET_SYSERR;
365 } 370 }
366} 371}
367 372
368 373
@@ -370,7 +375,7 @@ block_plugin_regex_get_key (void *cls,
370 * Entry point for the plugin. 375 * Entry point for the plugin.
371 */ 376 */
372void * 377void *
373libgnunet_plugin_block_regex_init (void *cls) 378libgnunet_plugin_block_regex_init(void *cls)
374{ 379{
375 static enum GNUNET_BLOCK_Type types[] = 380 static enum GNUNET_BLOCK_Type types[] =
376 { 381 {
@@ -380,7 +385,7 @@ libgnunet_plugin_block_regex_init (void *cls)
380 }; 385 };
381 struct GNUNET_BLOCK_PluginFunctions *api; 386 struct GNUNET_BLOCK_PluginFunctions *api;
382 387
383 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); 388 api = GNUNET_new(struct GNUNET_BLOCK_PluginFunctions);
384 api->evaluate = &block_plugin_regex_evaluate; 389 api->evaluate = &block_plugin_regex_evaluate;
385 api->get_key = &block_plugin_regex_get_key; 390 api->get_key = &block_plugin_regex_get_key;
386 api->create_group = &block_plugin_regex_create_group; 391 api->create_group = &block_plugin_regex_create_group;
@@ -393,11 +398,11 @@ libgnunet_plugin_block_regex_init (void *cls)
393 * Exit point from the plugin. 398 * Exit point from the plugin.
394 */ 399 */
395void * 400void *
396libgnunet_plugin_block_regex_done (void *cls) 401libgnunet_plugin_block_regex_done(void *cls)
397{ 402{
398 struct GNUNET_BLOCK_PluginFunctions *api = cls; 403 struct GNUNET_BLOCK_PluginFunctions *api = cls;
399 404
400 GNUNET_free (api); 405 GNUNET_free(api);
401 return NULL; 406 return NULL;
402} 407}
403 408