2 This file is part of GNUnet.
3 Copyright (C) 2012-2016 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 Florian Dold
23 * @author Christian Grothoff
26 * General-purpose message queue
28 * @defgroup mq MQ library
29 * General-purpose message queue
31 * @see [Documentation](https://gnunet.org/message-queue-api)
35 #ifndef GNUNET_MQ_LIB_H
36 #define GNUNET_MQ_LIB_H
38 #include "gnunet_scheduler_lib.h"
41 * Allocate an envelope, with extra space allocated after the space needed
42 * by the message struct.
43 * The allocated message will already have the type and size field set.
45 * @param mvar variable to store the allocated message in;
46 * must have a header field;
48 * @param esize extra space to allocate after the message
49 * @param type type of the message
50 * @return the MQ message
52 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
55 * Allocate a GNUNET_MQ_Envelope.
56 * The contained message will already have the type and size field set.
58 * @param mvar variable to store the allocated message in;
59 * must have a header field;
61 * @param type type of the message
62 * @return the allocated envelope
64 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
68 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
69 * The allocated message will already have the type and size field set.
71 * @param type type of the message
73 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
77 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
78 * The allocated message will already have the type and size field set.
80 * @param mh pointer that will changed to point at to the allocated message header
81 * @param esize extra space to allocate after the message header
82 * @param type type of the message
84 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
88 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
91 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
92 * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
93 * @param type message type of the allocated message, has no effect on the nested message
94 * @param mh message to nest
95 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
97 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) \
98 ({struct GNUNET_MQ_Envelope *_ev;\
99 _ev = GNUNET_MQ_msg_nested_mh_((struct GNUNET_MessageHeader**) &(mvar),\
103 (void)(mvar)->header; /* type check */\
108 * Return a pointer to the message at the end of the given message.
110 * @param var pointer to a message struct, the type of the expression determines the base size,
111 * the space after the base size is the nested message
112 * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
113 * or NULL if the given message in @a var does not have any space after the message struct
115 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
119 * Implementation of the #GNUNET_MQ_extract_nexted_mh macro.
121 * @param mh message header to extract nested message header from
122 * @param base_size size of the message before the nested message's header appears
123 * @return pointer to the nested message, does not copy the message
124 * OR NULL in case of a malformed message.
126 const struct GNUNET_MessageHeader *
127 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
132 * Opaque handle to an envelope.
134 struct GNUNET_MQ_Envelope;
138 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
140 * @param mhp pointer to the message header pointer that will be changed to allocate at
141 * the newly allocated space for the message.
142 * @param base_size size of the data before the nested message
143 * @param type type of the message in the envelope
144 * @param nested_mh the message to append to the message after base_size
146 struct GNUNET_MQ_Envelope *
147 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
150 const struct GNUNET_MessageHeader *nested_mh);
154 * Opaque handle to a message queue.
156 struct GNUNET_MQ_Handle;
160 * Error codes for the queue.
165 * Failed to read message from the network.
166 * FIXME: Likely not properly distinguished
167 * from TIMEOUT case in the code!
169 GNUNET_MQ_ERROR_READ = 1,
174 GNUNET_MQ_ERROR_WRITE = 2,
179 GNUNET_MQ_ERROR_TIMEOUT = 4,
182 * We received a message that was malformed and thus
183 * could not be passed to its handler.
185 GNUNET_MQ_ERROR_MALFORMED = 8,
188 * We received a message for which we have no matching
191 GNUNET_MQ_ERROR_NO_MATCH = 16
196 * Called when a message has been received.
199 * @param msg the received message
202 (*GNUNET_MQ_MessageCallback) (void *cls,
203 const struct GNUNET_MessageHeader *msg);
207 * Called when a message needs to be validated.
210 * @param msg the received message
211 * @return #GNUNET_OK if the message is well-formed,
212 * #GNUNET_SYSERR if not
215 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
216 const struct GNUNET_MessageHeader *msg);
220 * Signature of functions implementing the
221 * sending functionality of a message queue.
223 * @param mq the message queue
224 * @param msg the message to send
225 * @param impl_state state of the implementation
228 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
229 const struct GNUNET_MessageHeader *msg,
234 * Signature of functions implementing the
235 * destruction of a message queue.
236 * Implementations must not free @a mq, but should
237 * take care of @a impl_state.
239 * @param mq the message queue to destroy
240 * @param impl_state state of the implementation
243 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
248 * Implementation function that cancels the currently sent message.
250 * @param mq message queue
251 * @param impl_state state specific to the implementation
254 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
259 * Generic error handler, called with the appropriate
260 * error code and the same closure specified at the creation of
262 * Not every message queue implementation supports an error handler.
265 * @param error error code
268 (*GNUNET_MQ_ErrorHandler) (void *cls,
269 enum GNUNET_MQ_Error error);
273 * Insert @a env into the envelope DLL starting at @a env_head
274 * Note that @a env must not be in any MQ while this function
275 * is used with DLLs defined outside of the MQ module. This
276 * is just in case some application needs to also manage a
277 * FIFO of envelopes independent of MQ itself and wants to
278 * re-use the pointers internal to @a env. Use with caution.
280 * @param[in|out] env_head of envelope DLL
281 * @param[in|out] env_tail tail of envelope DLL
282 * @param[in|out] env element to insert at the tail
285 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
286 struct GNUNET_MQ_Envelope **env_tail,
287 struct GNUNET_MQ_Envelope *env);
291 * Remove @a env from the envelope DLL starting at @a env_head.
292 * Note that @a env must not be in any MQ while this function
293 * is used with DLLs defined outside of the MQ module. This
294 * is just in case some application needs to also manage a
295 * FIFO of envelopes independent of MQ itself and wants to
296 * re-use the pointers internal to @a env. Use with caution.
298 * @param[in|out] env_head of envelope DLL
299 * @param[in|out] env_tail tail of envelope DLL
300 * @param[in|out] env element to remove from the DLL
303 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
304 struct GNUNET_MQ_Envelope **env_tail,
305 struct GNUNET_MQ_Envelope *env);
309 * Copy an array of handlers.
311 * Useful if the array has been delared in local memory and needs to be
312 * persisted for future use.
314 * @param handlers Array of handlers to be copied.
315 * @return A newly allocated array of handlers.
316 * Needs to be freed with #GNUNET_free.
318 struct GNUNET_MQ_MessageHandler *
319 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
323 * Count the handlers in a handler array.
325 * @param handlers Array of handlers to be counted.
326 * @return The number of handlers in the array.
329 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
333 * Message handler for a specific message type.
335 struct GNUNET_MQ_MessageHandler
338 * Callback to validate a message of the specified @e type.
339 * The closure given to @e mv will be this struct (not @e ctx).
340 * Using NULL means only size-validation using
341 * @e expected_size. In this case, @e expected_size must
344 GNUNET_MQ_MessageValidationCallback mv;
347 * Callback, called every time a new message of
348 * the specified @e type has been receied.
349 * The closure given to @e mv will be this struct (not @e ctx).
351 GNUNET_MQ_MessageCallback cb;
354 * Closure for @e mv and @e cb.
359 * Type of the message this handler covers, in host byte order.
364 * Expected size of messages of this type. Minimum size of the
365 * message if @e mv is non-NULL. Messages of the given type will be
366 * discarded (and the connection closed with an error reported to
367 * the application) if they do not have the right size.
369 uint16_t expected_size;
374 * End-marker for the handlers array
376 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
380 * Defines a static function @a name which takes as a single argument
381 * a message handler for fixed-sized messages of type @a code and with
382 * a message type argument of @a str. Given such an argument, the
383 * function @name will return a `struct GNUNET_MQ_MessageHandler`
384 * for the given message type.
386 * The macro is to be used as follows:
388 * struct GNUNET_MessageTest { ... }; // must be fixed size
390 * handle_test_message (void *cls,
391 * const struct GNUNET_MessageTest *msg)
394 * struct GNUNET_MQ_MessageHandler handlers[] = {
395 * GNUNET_MQ_hd_fixed_size(test_message,
396 * GNUNET_MESSAGE_TYPE_TEST,
397 * struct GNUNET_MessageTest,
399 * GNUNET_MQ_handler_end()
402 * @param name unique basename for the functions
403 * @param code message type constant
404 * @param str type of the message (a struct)
405 * @param ctx context for the callbacks
407 #define GNUNET_MQ_hd_fixed_size(name,code,str,ctx) \
409 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
410 ((struct GNUNET_MQ_MessageHandler) { \
411 NULL, (GNUNET_MQ_MessageCallback) _cb, \
412 (ctx), (code), sizeof (str) }); \
417 * Defines a static function @a name which takes two arguments and a
418 * context-pointer for validating and handling variable-sized messages
419 * of type @a code and with a message type argument of @a str. Given
420 * such arguments, the function @name will return a `struct
421 * GNUNET_MQ_MessageHandler` for the given message type.
423 * The macro is to be used as follows:
425 * struct GNUNET_MessageTest { ... }; // can be variable size
427 * check_test (void *cls,
428 * const struct GNUNET_MessageTest *msg)
430 * const char *ctx = cls;
431 * GNUNET_assert (0 == strcmp ("context", ctx));
435 * handle_test (void *cls,
436 * const struct GNUNET_MessageTest *msg)
438 * const char *ctx = cls;
439 * GNUNET_assert (0 == strcmp ("context", ctx));
443 * struct GNUNET_MQ_MessageHandler handlers[] = {
444 * GNUNET_MQ_hd_var_size(test_message,
445 * GNUNET_MESSAGE_TYPE_TEST,
446 * struct GNUNET_MessageTest,
448 * GNUNET_MQ_handler_end()
451 * @param name unique basename for the functions
452 * @param code message type constant
453 * @param str type of the message (a struct)
454 * @param ctx context for the callbacks
456 #define GNUNET_MQ_hd_var_size(name,code,str,ctx) \
458 int (*_mv)(void *cls, const str *msg) = &check_##name; \
459 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
460 ((struct GNUNET_MQ_MessageHandler) \
461 { (GNUNET_MQ_MessageValidationCallback) _mv, \
462 (GNUNET_MQ_MessageCallback) _cb, \
463 (ctx), (code), sizeof (str) }); \
468 * Create a new envelope.
470 * @param mhp message header to store the allocated message header in, can be NULL
471 * @param size size of the message to allocate
472 * @param type type of the message, will be set in the allocated message
473 * @return the allocated MQ message
475 struct GNUNET_MQ_Envelope *
476 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
482 * Create a new envelope by copying an existing message.
484 * @param hdr header of the message to copy
485 * @return envelope containing @a hdr
487 struct GNUNET_MQ_Envelope *
488 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
492 * Discard the message queue message, free all
493 * allocated resources. Must be called in the event
494 * that a message is created but should not actually be sent.
496 * @param mqm the message to discard
499 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
503 * Function to obtain the current envelope
504 * from within #GNUNET_MQ_SendImpl implementations.
506 * @param mq message queue to interrogate
507 * @return the current envelope
509 struct GNUNET_MQ_Envelope *
510 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
514 * Function to copy an envelope. The envelope must not yet
515 * be in any queue or have any options or callbacks set.
517 * @param env envelope to copy
518 * @return copy of @a env
520 struct GNUNET_MQ_Envelope *
521 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env);
525 * Function to obtain the last envelope in the queue.
527 * @param mq message queue to interrogate
528 * @return the last envelope in the queue
530 struct GNUNET_MQ_Envelope *
531 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
535 * Set application-specific options for this envelope.
536 * Overrides the options set for the queue with
537 * #GNUNET_MQ_set_options() for this message only.
539 * @param env message to set options for
540 * @param flags flags to use (meaning is queue-specific)
541 * @param extra additional buffer for further data (also queue-specific)
544 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
550 * Get application-specific options for this envelope.
552 * @param env message to set options for
553 * @param[out] flags set to flags to use (meaning is queue-specific)
554 * @return extra additional buffer for further data (also queue-specific)
557 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
562 * Remove the first envelope that has not yet been sent from the message
563 * queue and return it.
565 * @param mq queue to remove envelope from
566 * @return NULL if queue is empty (or has no envelope that is not under transmission)
568 struct GNUNET_MQ_Envelope *
569 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq);
573 * Set application-specific options for this queue.
575 * @param mq message queue to set options for
576 * @param flags flags to use (meaning is queue-specific)
577 * @param extra additional buffer for further data (also queue-specific)
580 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
586 * Obtain the current length of the message queue.
588 * @param mq queue to inspect
589 * @return number of queued, non-transmitted messages
592 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
596 * Send a message with the given message queue.
597 * May only be called once per message.
599 * @param mq message queue
600 * @param ev the envelope with the message to send.
603 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
604 struct GNUNET_MQ_Envelope *ev);
608 * Send a copy of a message with the given message queue.
609 * Can be called repeatedly on the same envelope.
611 * @param mq message queue
612 * @param ev the envelope with the message to send.
615 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
616 const struct GNUNET_MQ_Envelope *ev);
620 * Cancel sending the message. Message must have been sent with
621 * #GNUNET_MQ_send before. May not be called after the notify sent
622 * callback has been called
624 * @param ev queued envelope to cancel
627 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
631 * Associate the assoc_data in @a mq with a unique request id.
633 * @param mq message queue, id will be unique for the queue
634 * @param assoc_data to associate
637 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
642 * Get the data associated with a @a request_id in a queue
644 * @param mq the message queue with the association
645 * @param request_id the request id we are interested in
646 * @return the associated data
649 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
650 uint32_t request_id);
654 * Remove the association for a @a request_id
656 * @param mq the message queue with the association
657 * @param request_id the request id we want to remove
658 * @return the associated data
661 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
662 uint32_t request_id);
666 * Create a message queue for the specified handlers.
668 * @param send function the implements sending messages
669 * @param destroy function that implements destroying the queue
670 * @param cancel function that implements canceling a message
671 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
672 * @param handlers array of message handlers
673 * @param error_handler handler for read and write errors
674 * @param cls closure for message handlers and error handler
675 * @return a new message queue
677 struct GNUNET_MQ_Handle *
678 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
679 GNUNET_MQ_DestroyImpl destroy,
680 GNUNET_MQ_CancelImpl cancel,
682 const struct GNUNET_MQ_MessageHandler *handlers,
683 GNUNET_MQ_ErrorHandler error_handler,
688 * Change the closure argument in all of the `handlers` of the
691 * @param mq to modify
692 * @param handlers_cls new closure to use
695 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
700 * Call a callback once the envelope has been sent, that is,
701 * sending it can not be canceled anymore.
702 * There can be only one notify sent callback per envelope.
704 * @param ev message to call the notify callback for
705 * @param cb the notify callback
706 * @param cb_cls closure for the callback
709 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
710 GNUNET_SCHEDULER_TaskCallback cb,
715 * Destroy the message queue.
717 * @param mq message queue to destroy
720 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
724 * Handle we return for callbacks registered to be
725 * notified when #GNUNET_MQ_destroy() is called on a queue.
727 struct GNUNET_MQ_DestroyNotificationHandle;
731 * Register function to be called whenever @a mq is being
734 * @param mq message queue to watch
735 * @param cb function to call on @a mq destruction
736 * @param cb_cls closure for @a cb
737 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
739 struct GNUNET_MQ_DestroyNotificationHandle *
740 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
741 GNUNET_SCHEDULER_TaskCallback cb,
745 * Cancel registration from #GNUNET_MQ_destroy_notify().
747 * @param dnh handle for registration to cancel
750 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
754 * Call the message message handler that was registered
755 * for the type of the given message in the given message queue.
757 * This function is indended to be used for the implementation
760 * @param mq message queue with the handlers
761 * @param mh message to dispatch
764 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
765 const struct GNUNET_MessageHeader *mh);
769 * Call the error handler of a message queue with the given
770 * error code. If there is no error handler, log a warning.
772 * This function is intended to be used for the implementation
775 * @param mq message queue
776 * @param error the error type
779 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
780 enum GNUNET_MQ_Error error);
784 * Call the send implementation for the next queued message, if any.
785 * Calls the send notification for the current message unless
786 * #GNUNET_MQ_impl_send_in_flight was called for this envelope.
788 * Only useful for implementing message queues, results in undefined
789 * behavior if not used carefully.
791 * @param mq message queue to send the next message with
794 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
798 * Call the send notification for the current message, but do not
799 * try to send the next message until #gnunet_mq_impl_send_continue
802 * Only useful for implementing message queues, results in undefined
803 * behavior if not used carefully.
805 * @param mq message queue to send the next message with
808 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq);
812 * Get the implementation state associated with the
815 * While the GNUNET_MQ_Impl* callbacks receive the
816 * implementation state, continuations that are scheduled
817 * by the implementation function often only have one closure
818 * argument, with this function it is possible to get at the
819 * implementation state when only passing the `struct GNUNET_MQ_Handle`
822 * @param mq message queue with the current message
823 * @return message to send, never NULL
826 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
830 * Get the message that should currently be sent.
831 * Fails if there is no current message.
832 * Only useful for implementing message queues,
833 * results in undefined behavior if not used carefully.
835 * @param mq message queue with the current message
836 * @return message to send, never NULL
838 const struct GNUNET_MessageHeader *
839 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
844 /** @} */ /* end of group mq */