2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @author Gabor X Toth
31 #include "gnunet_util_lib.h"
32 #include "gnunet_psyc_util_lib.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "psyc-util-slicer",__VA_ARGS__)
38 * Handle for a try-and-slice instance.
40 struct GNUNET_PSYC_Slicer
43 * Method handlers: H(method_name) -> SlicerMethodCallbacks
45 struct GNUNET_CONTAINER_MultiHashMap *method_handlers;
48 * Modifier handlers: H(modifier_name) -> SlicerModifierCallbacks
50 struct GNUNET_CONTAINER_MultiHashMap *modifier_handlers;
53 * Receive handle for incoming messages.
55 struct GNUNET_PSYC_ReceiveHandle *recv;
58 * Currently being processed message.
60 const struct GNUNET_PSYC_MessageHeader *msg;
63 * Currently being processed message part.
65 const struct GNUNET_MessageHeader *pmsg;
68 * ID of currently being received message.
73 * Fragment offset of currently being received message.
75 uint64_t fragment_offset;
78 * Flags of currently being received message.
83 * Method name of currently being received message.
88 * Name of currently processed modifier.
93 * Value of currently processed modifier.
98 * Public key of the nym the current message originates from.
100 struct GNUNET_CRYPTO_EcdsaPublicKey nym_pub_key;
103 * Size of @a method_name (including terminating \0).
105 uint16_t method_name_size;
108 * Size of @a modifier_name (including terminating \0).
110 uint16_t mod_name_size;
113 * Size of modifier value fragment.
115 uint16_t mod_value_size;
118 * Full size of modifier value.
120 uint16_t mod_full_value_size;
123 * Remaining bytes from the value of the current modifier.
125 uint16_t mod_value_remaining;
128 * Operator of currently processed modifier.
135 * Callbacks for a slicer method handler.
137 struct SlicerMethodCallbacks
139 GNUNET_PSYC_MessageCallback msg_cb;
140 GNUNET_PSYC_MethodCallback method_cb;
141 GNUNET_PSYC_ModifierCallback modifier_cb;
142 GNUNET_PSYC_DataCallback data_cb;
143 GNUNET_PSYC_EndOfMessageCallback eom_cb;
148 struct SlicerMethodRemoveClosure
150 struct GNUNET_PSYC_Slicer *slicer;
151 struct SlicerMethodCallbacks rm_cbs;
156 * Callbacks for a slicer method handler.
158 struct SlicerModifierCallbacks
160 GNUNET_PSYC_ModifierCallback modifier_cb;
165 struct SlicerModifierRemoveClosure
167 struct GNUNET_PSYC_Slicer *slicer;
168 struct SlicerModifierCallbacks rm_cbs;
173 * Call a method handler for an incoming message part.
176 slicer_method_handler_notify (void *cls, const struct GNUNET_HashCode *key,
179 struct GNUNET_PSYC_Slicer *slicer = cls;
180 const struct GNUNET_MessageHeader *pmsg = slicer->pmsg;
181 struct SlicerMethodCallbacks *cbs = value;
183 uint16_t ptype = ntohs (pmsg->type);
186 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
188 if (NULL != cbs->msg_cb)
189 cbs->msg_cb (cbs->cls, slicer->msg);
190 if (NULL == cbs->method_cb)
192 struct GNUNET_PSYC_MessageMethod *
193 meth = (struct GNUNET_PSYC_MessageMethod *) pmsg;
194 cbs->method_cb (cbs->cls, slicer->msg, meth, slicer->message_id,
195 slicer->method_name);
199 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
201 if (NULL == cbs->modifier_cb)
203 struct GNUNET_PSYC_MessageModifier *
204 mod = (struct GNUNET_PSYC_MessageModifier *) pmsg;
205 cbs->modifier_cb (cbs->cls, slicer->msg, &mod->header, slicer->message_id,
206 mod->oper, (const char *) &mod[1],
207 (const void *) &mod[1] + ntohs (mod->name_size),
208 ntohs (mod->header.size) - sizeof (*mod) - ntohs (mod->name_size),
209 ntohs (mod->value_size));
213 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
215 if (NULL == cbs->modifier_cb)
217 cbs->modifier_cb (cbs->cls, slicer->msg, pmsg, slicer->message_id,
218 slicer->mod_oper, slicer->mod_name, &pmsg[1],
219 ntohs (pmsg->size) - sizeof (*pmsg),
220 slicer->mod_full_value_size);
224 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
226 if (NULL == cbs->data_cb)
228 cbs->data_cb (cbs->cls, slicer->msg, pmsg, slicer->message_id,
229 &pmsg[1], ntohs (pmsg->size) - sizeof (*pmsg));
233 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
234 if (NULL == cbs->eom_cb)
236 cbs->eom_cb (cbs->cls, slicer->msg, pmsg, slicer->message_id, GNUNET_NO);
239 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
240 if (NULL == cbs->eom_cb)
242 cbs->eom_cb (cbs->cls, slicer->msg, pmsg, slicer->message_id, GNUNET_YES);
250 * Call a method handler for an incoming message part.
253 slicer_modifier_handler_notify (void *cls, const struct GNUNET_HashCode *key,
256 struct GNUNET_PSYC_Slicer *slicer = cls;
257 struct SlicerModifierCallbacks *cbs = value;
259 cbs->modifier_cb (cbs->cls, slicer->msg, slicer->pmsg, slicer->message_id,
260 slicer->mod_oper, slicer->mod_name, slicer->mod_value,
261 slicer->mod_value_size, slicer->mod_full_value_size);
267 * Process an incoming message and call matching handlers.
272 * The message as it arrived from the network.
275 GNUNET_PSYC_slicer_message (struct GNUNET_PSYC_Slicer *slicer,
276 const struct GNUNET_PSYC_MessageHeader *msg)
278 GNUNET_PSYC_receive_message (slicer->recv, msg);
283 * Process an incoming message part and call matching handlers.
290 * Flags for the message.
291 * @see enum GNUNET_PSYC_MessageFlags
293 * The message part. as it arrived from the network.
296 GNUNET_PSYC_slicer_message_part (struct GNUNET_PSYC_Slicer *slicer,
297 const struct GNUNET_PSYC_MessageHeader *msg,
298 const struct GNUNET_MessageHeader *pmsg)
303 uint64_t message_id = GNUNET_ntohll (msg->message_id);
305 uint16_t ptype = ntohs (pmsg->type);
306 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
308 struct GNUNET_PSYC_MessageMethod *
309 meth = (struct GNUNET_PSYC_MessageMethod *) pmsg;
310 slicer->method_name_size = ntohs (meth->header.size) - sizeof (*meth);
311 slicer->method_name = GNUNET_malloc (slicer->method_name_size);
312 GNUNET_memcpy (slicer->method_name, &meth[1], slicer->method_name_size);
313 slicer->message_id = message_id;
317 GNUNET_assert (message_id == slicer->message_id);
320 char *nym_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&msg->slave_pub_key);
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "Slicer received message of type %u and size %u, "
323 "with ID %" PRIu64 " and method %s from %s\n",
324 ptype, ntohs (pmsg->size), message_id, slicer->method_name, nym_str);
325 GNUNET_free (nym_str);
327 /* try-and-slice modifier */
331 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
333 struct GNUNET_PSYC_MessageModifier *
334 mod = (struct GNUNET_PSYC_MessageModifier *) pmsg;
335 slicer->mod_oper = mod->oper;
336 slicer->mod_name_size = ntohs (mod->name_size);
337 slicer->mod_name = GNUNET_malloc (slicer->mod_name_size);
338 GNUNET_memcpy (slicer->mod_name, &mod[1], slicer->mod_name_size);
339 slicer->mod_value = (char *) &mod[1] + slicer->mod_name_size;
340 slicer->mod_full_value_size = ntohs (mod->value_size);
341 slicer->mod_value_remaining = slicer->mod_full_value_size;
342 slicer->mod_value_size
343 = ntohs (mod->header.size) - sizeof (*mod) - slicer->mod_name_size;
346 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
347 if (ptype == GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT)
349 slicer->mod_value = (char *) &pmsg[1];
350 slicer->mod_value_size = ntohs (pmsg->size) - sizeof (*pmsg);
352 slicer->mod_value_remaining -= slicer->mod_value_size;
353 char *name = GNUNET_malloc (slicer->mod_name_size);
354 GNUNET_memcpy (name, slicer->mod_name, slicer->mod_name_size);
357 struct GNUNET_HashCode key;
358 uint16_t name_len = strlen (name);
359 GNUNET_CRYPTO_hash (name, name_len, &key);
360 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->modifier_handlers, &key,
361 slicer_modifier_handler_notify,
363 char *p = strrchr (name, '_');
371 /* try-and-slice method */
373 char *name = GNUNET_malloc (slicer->method_name_size);
374 GNUNET_memcpy (name, slicer->method_name, slicer->method_name_size);
377 struct GNUNET_HashCode key;
378 uint16_t name_len = strlen (name);
379 GNUNET_CRYPTO_hash (name, name_len, &key);
380 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->method_handlers, &key,
381 slicer_method_handler_notify,
383 char *p = strrchr (name, '_');
390 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
391 GNUNET_free (slicer->method_name);
393 if (0 == slicer->mod_value_remaining && NULL != slicer->mod_name)
395 GNUNET_free (slicer->mod_name);
396 slicer->mod_name = NULL;
397 slicer->mod_name_size = 0;
398 slicer->mod_value_size = 0;
399 slicer->mod_full_value_size = 0;
400 slicer->mod_oper = 0;
409 * Create a try-and-slice instance.
411 * A slicer processes incoming messages and notifies callbacks about matching
412 * methods or modifiers encountered.
414 * @return A new try-and-slice construct.
416 struct GNUNET_PSYC_Slicer *
417 GNUNET_PSYC_slicer_create (void)
419 struct GNUNET_PSYC_Slicer *slicer = GNUNET_malloc (sizeof (*slicer));
420 slicer->method_handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
421 slicer->modifier_handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
422 slicer->recv = GNUNET_PSYC_receive_create (NULL,
423 (GNUNET_PSYC_MessagePartCallback)
424 GNUNET_PSYC_slicer_message_part,
431 * Add a method to the try-and-slice instance.
433 * The callbacks are called for messages with a matching @a method_name prefix.
436 * The try-and-slice instance to extend.
438 * Name of the given method, use empty string to match all.
440 * Method handler invoked upon a matching message.
442 * Modifier handler, invoked after @a method_cb
443 * for each modifier in the message.
445 * Data handler, invoked after @a modifier_cb for each data fragment.
447 * Invoked upon reaching the end of a matching message.
449 * Closure for the callbacks.
452 GNUNET_PSYC_slicer_method_add (struct GNUNET_PSYC_Slicer *slicer,
453 const char *method_name,
454 GNUNET_PSYC_MessageCallback msg_cb,
455 GNUNET_PSYC_MethodCallback method_cb,
456 GNUNET_PSYC_ModifierCallback modifier_cb,
457 GNUNET_PSYC_DataCallback data_cb,
458 GNUNET_PSYC_EndOfMessageCallback eom_cb,
461 struct GNUNET_HashCode key;
462 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
464 struct SlicerMethodCallbacks *cbs = GNUNET_malloc (sizeof (*cbs));
465 cbs->msg_cb = msg_cb,
466 cbs->method_cb = method_cb;
467 cbs->modifier_cb = modifier_cb;
468 cbs->data_cb = data_cb;
469 cbs->eom_cb = eom_cb;
472 GNUNET_CONTAINER_multihashmap_put (slicer->method_handlers, &key, cbs,
473 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
478 slicer_method_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
480 struct SlicerMethodRemoveClosure *rm_cls = cls;
481 struct GNUNET_PSYC_Slicer *slicer = rm_cls->slicer;
482 struct SlicerMethodCallbacks *rm_cbs = &rm_cls->rm_cbs;
483 struct SlicerMethodCallbacks *cbs = value;
485 if ((NULL == rm_cbs->msg_cb || cbs->msg_cb == rm_cbs->msg_cb)
486 && (NULL == rm_cbs->method_cb || cbs->method_cb == rm_cbs->method_cb)
487 && (NULL == rm_cbs->modifier_cb || cbs->modifier_cb == rm_cbs->modifier_cb)
488 && (NULL == rm_cbs->data_cb || cbs->data_cb == rm_cbs->data_cb)
489 && (NULL == rm_cbs->eom_cb || cbs->eom_cb == rm_cbs->eom_cb))
491 GNUNET_CONTAINER_multihashmap_remove (slicer->method_handlers, key, cbs);
500 * Remove a registered method from the try-and-slice instance.
502 * Removes one matching handler registered with the given
503 * @a method_name and callbacks.
506 * The try-and-slice instance.
508 * Name of the method to remove.
516 * End of message handler.
518 * @return #GNUNET_OK if a method handler was removed,
519 * #GNUNET_NO if no handler matched the given method name and callbacks.
522 GNUNET_PSYC_slicer_method_remove (struct GNUNET_PSYC_Slicer *slicer,
523 const char *method_name,
524 GNUNET_PSYC_MessageCallback msg_cb,
525 GNUNET_PSYC_MethodCallback method_cb,
526 GNUNET_PSYC_ModifierCallback modifier_cb,
527 GNUNET_PSYC_DataCallback data_cb,
528 GNUNET_PSYC_EndOfMessageCallback eom_cb)
530 struct GNUNET_HashCode key;
531 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
533 struct SlicerMethodRemoveClosure rm_cls;
534 rm_cls.slicer = slicer;
535 struct SlicerMethodCallbacks *rm_cbs = &rm_cls.rm_cbs;
536 rm_cbs->msg_cb = msg_cb;
537 rm_cbs->method_cb = method_cb;
538 rm_cbs->modifier_cb = modifier_cb;
539 rm_cbs->data_cb = data_cb;
540 rm_cbs->eom_cb = eom_cb;
544 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->method_handlers, &key,
545 slicer_method_remove,
553 * Watch a place for changed objects.
556 * The try-and-slice instance.
557 * @param object_filter
558 * Object prefix to match.
560 * Function to call when encountering a state modifier.
562 * Closure for callback.
565 GNUNET_PSYC_slicer_modifier_add (struct GNUNET_PSYC_Slicer *slicer,
566 const char *object_filter,
567 GNUNET_PSYC_ModifierCallback modifier_cb,
570 struct SlicerModifierCallbacks *cbs = GNUNET_malloc (sizeof *cbs);
571 cbs->modifier_cb = modifier_cb;
574 struct GNUNET_HashCode key;
575 GNUNET_CRYPTO_hash (object_filter, strlen (object_filter), &key);
576 GNUNET_CONTAINER_multihashmap_put (slicer->modifier_handlers, &key, cbs,
577 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
582 slicer_modifier_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
584 struct SlicerModifierRemoveClosure *rm_cls = cls;
585 struct GNUNET_PSYC_Slicer *slicer = rm_cls->slicer;
586 struct SlicerModifierCallbacks *rm_cbs = &rm_cls->rm_cbs;
587 struct SlicerModifierCallbacks *cbs = value;
589 if (cbs->modifier_cb == rm_cbs->modifier_cb)
591 GNUNET_CONTAINER_multihashmap_remove (slicer->modifier_handlers, key, cbs);
600 * Remove a registered modifier from the try-and-slice instance.
602 * Removes one matching handler registered with the given
603 * @a object_filter and @a modifier_cb.
606 * The try-and-slice instance.
607 * @param object_filter
608 * Object prefix to match.
610 * Function to call when encountering a state modifier changes.
613 GNUNET_PSYC_slicer_modifier_remove (struct GNUNET_PSYC_Slicer *slicer,
614 const char *object_filter,
615 GNUNET_PSYC_ModifierCallback modifier_cb)
617 struct GNUNET_HashCode key;
618 GNUNET_CRYPTO_hash (object_filter, strlen (object_filter), &key);
620 struct SlicerModifierRemoveClosure rm_cls;
621 rm_cls.slicer = slicer;
622 struct SlicerModifierCallbacks *rm_cbs = &rm_cls.rm_cbs;
623 rm_cbs->modifier_cb = modifier_cb;
627 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->modifier_handlers, &key,
628 slicer_modifier_remove,
636 slicer_method_free (void *cls, const struct GNUNET_HashCode *key, void *value)
638 struct SlicerMethodCallbacks *cbs = value;
645 slicer_modifier_free (void *cls, const struct GNUNET_HashCode *key, void *value)
647 struct SlicerModifierCallbacks *cbs = value;
654 * Remove all registered method handlers.
660 GNUNET_PSYC_slicer_method_clear (struct GNUNET_PSYC_Slicer *slicer)
662 GNUNET_CONTAINER_multihashmap_iterate (slicer->method_handlers,
663 slicer_method_free, NULL);
664 GNUNET_CONTAINER_multihashmap_clear (slicer->method_handlers);
669 * Remove all registered modifier handlers.
675 GNUNET_PSYC_slicer_modifier_clear (struct GNUNET_PSYC_Slicer *slicer)
677 GNUNET_CONTAINER_multihashmap_iterate (slicer->modifier_handlers,
678 slicer_modifier_free, NULL);
679 GNUNET_CONTAINER_multihashmap_clear (slicer->modifier_handlers);
684 * Remove all registered method & modifier handlers.
690 GNUNET_PSYC_slicer_clear (struct GNUNET_PSYC_Slicer *slicer)
692 GNUNET_PSYC_slicer_method_clear (slicer);
693 GNUNET_PSYC_slicer_modifier_clear (slicer);
698 * Destroy a given try-and-slice instance.
704 GNUNET_PSYC_slicer_destroy (struct GNUNET_PSYC_Slicer *slicer)
706 GNUNET_PSYC_slicer_clear (slicer);
707 GNUNET_CONTAINER_multihashmap_destroy (slicer->method_handlers);
708 GNUNET_CONTAINER_multihashmap_destroy (slicer->modifier_handlers);
709 GNUNET_PSYC_receive_destroy (slicer->recv);
710 GNUNET_free (slicer);