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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
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/>.
20 * @author Florian Dold
21 * @author Christian Grothoff
24 * General-purpose message queue
26 * @defgroup mq MQ library
27 * General-purpose message queue
29 * @see [Documentation](https://gnunet.org/message-queue-api)
33 #ifndef GNUNET_MQ_LIB_H
34 #define GNUNET_MQ_LIB_H
36 #include "gnunet_scheduler_lib.h"
39 * Allocate an envelope, with extra space allocated after the space needed
40 * by the message struct.
41 * The allocated message will already have the type and size field set.
43 * @param mvar variable to store the allocated message in;
44 * must have a header field;
46 * @param esize extra space to allocate after the message
47 * @param type type of the message
48 * @return the MQ message
50 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
53 * Allocate a GNUNET_MQ_Envelope.
54 * The contained message will already have the type and size field set.
56 * @param mvar variable to store the allocated message in;
57 * must have a header field;
59 * @param type type of the message
60 * @return the allocated envelope
62 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
66 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
67 * The allocated message will already have the type and size field set.
69 * @param type type of the message
71 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
75 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
76 * The allocated message will already have the type and size field set.
78 * @param mh pointer that will changed to point at to the allocated message header
79 * @param esize extra space to allocate after the message header
80 * @param type type of the message
82 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
86 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
89 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
90 * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
91 * @param type message type of the allocated message, has no effect on the nested message
92 * @param mh message to nest
93 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
95 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) \
96 ({struct GNUNET_MQ_Envelope *_ev;\
97 _ev = GNUNET_MQ_msg_nested_mh_((struct GNUNET_MessageHeader**) &(mvar),\
101 (void)(mvar)->header; /* type check */\
106 * Return a pointer to the message at the end of the given message.
108 * @param var pointer to a message struct, the type of the expression determines the base size,
109 * the space after the base size is the nested message
110 * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
111 * or NULL if the given message in @a var does not have any space after the message struct
113 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
117 * Implementation of the #GNUNET_MQ_extract_nexted_mh macro.
119 * @param mh message header to extract nested message header from
120 * @param base_size size of the message before the nested message's header appears
121 * @return pointer to the nested message, does not copy the message
122 * OR NULL in case of a malformed message.
124 const struct GNUNET_MessageHeader *
125 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
130 * Opaque handle to an envelope.
132 struct GNUNET_MQ_Envelope;
136 * Obtain message contained in envelope.
138 * @param env the envelope
139 * @return message contained in the envelope
141 const struct GNUNET_MessageHeader *
142 GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env);
146 * Return next envelope in queue.
148 * @param env a queued envelope
149 * @return next one, or NULL
151 const struct GNUNET_MQ_Envelope *
152 GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env);
156 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
158 * @param mhp pointer to the message header pointer that will be changed to allocate at
159 * the newly allocated space for the message.
160 * @param base_size size of the data before the nested message
161 * @param type type of the message in the envelope
162 * @param nested_mh the message to append to the message after base_size
164 struct GNUNET_MQ_Envelope *
165 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
168 const struct GNUNET_MessageHeader *nested_mh);
172 * Opaque handle to a message queue.
174 struct GNUNET_MQ_Handle;
178 * Error codes for the queue.
183 * Failed to read message from the network.
184 * FIXME: Likely not properly distinguished
185 * from TIMEOUT case in the code!
187 GNUNET_MQ_ERROR_READ = 1,
192 GNUNET_MQ_ERROR_WRITE = 2,
197 GNUNET_MQ_ERROR_TIMEOUT = 4,
200 * We received a message that was malformed and thus
201 * could not be passed to its handler.
203 GNUNET_MQ_ERROR_MALFORMED = 8,
206 * We received a message for which we have no matching
209 GNUNET_MQ_ERROR_NO_MATCH = 16
214 * Called when a message has been received.
217 * @param msg the received message
220 (*GNUNET_MQ_MessageCallback) (void *cls,
221 const struct GNUNET_MessageHeader *msg);
225 * Called when a message needs to be validated.
228 * @param msg the received message
229 * @return #GNUNET_OK if the message is well-formed,
230 * #GNUNET_SYSERR if not
233 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
234 const struct GNUNET_MessageHeader *msg);
238 * Signature of functions implementing the
239 * sending functionality of a message queue.
241 * @param mq the message queue
242 * @param msg the message to send
243 * @param impl_state state of the implementation
246 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
247 const struct GNUNET_MessageHeader *msg,
252 * Signature of functions implementing the
253 * destruction of a message queue.
254 * Implementations must not free @a mq, but should
255 * take care of @a impl_state.
257 * @param mq the message queue to destroy
258 * @param impl_state state of the implementation
261 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
266 * Implementation function that cancels the currently sent message.
268 * @param mq message queue
269 * @param impl_state state specific to the implementation
272 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
277 * Generic error handler, called with the appropriate
278 * error code and the same closure specified at the creation of
280 * Not every message queue implementation supports an error handler.
283 * @param error error code
286 (*GNUNET_MQ_ErrorHandler) (void *cls,
287 enum GNUNET_MQ_Error error);
291 * Insert @a env into 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 insert at the tail
303 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
304 struct GNUNET_MQ_Envelope **env_tail,
305 struct GNUNET_MQ_Envelope *env);
309 * Remove @a env from the envelope DLL starting at @a env_head.
310 * Note that @a env must not be in any MQ while this function
311 * is used with DLLs defined outside of the MQ module. This
312 * is just in case some application needs to also manage a
313 * FIFO of envelopes independent of MQ itself and wants to
314 * re-use the pointers internal to @a env. Use with caution.
316 * @param[in|out] env_head of envelope DLL
317 * @param[in|out] env_tail tail of envelope DLL
318 * @param[in|out] env element to remove from the DLL
321 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
322 struct GNUNET_MQ_Envelope **env_tail,
323 struct GNUNET_MQ_Envelope *env);
327 * Copy an array of handlers.
329 * Useful if the array has been delared in local memory and needs to be
330 * persisted for future use.
332 * @param handlers Array of handlers to be copied.
333 * @return A newly allocated array of handlers.
334 * Needs to be freed with #GNUNET_free.
336 struct GNUNET_MQ_MessageHandler *
337 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
341 * Copy an array of handlers, appending AGPL handler.
343 * Useful if the array has been delared in local memory and needs to be
344 * persisted for future use.
346 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
347 * @param agpl_handler function to call for AGPL handling
348 * @param agpl_cls closure for @a agpl_handler
349 * @return A newly allocated array of handlers.
350 * Needs to be freed with #GNUNET_free.
352 struct GNUNET_MQ_MessageHandler *
353 GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
354 GNUNET_MQ_MessageCallback agpl_handler,
359 * Count the handlers in a handler array.
361 * @param handlers Array of handlers to be counted.
362 * @return The number of handlers in the array.
365 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
369 * Message handler for a specific message type.
371 struct GNUNET_MQ_MessageHandler
374 * Callback to validate a message of the specified @e type.
375 * The closure given to @e mv will be this struct (not @e ctx).
376 * Using NULL means only size-validation using
377 * @e expected_size. In this case, @e expected_size must
380 GNUNET_MQ_MessageValidationCallback mv;
383 * Callback, called every time a new message of
384 * the specified @e type has been receied.
385 * The closure given to @e mv will be this struct (not @e ctx).
387 GNUNET_MQ_MessageCallback cb;
390 * Closure for @e mv and @e cb.
395 * Type of the message this handler covers, in host byte order.
400 * Expected size of messages of this type. Minimum size of the
401 * message if @e mv is non-NULL. Messages of the given type will be
402 * discarded (and the connection closed with an error reported to
403 * the application) if they do not have the right size.
405 uint16_t expected_size;
410 * End-marker for the handlers array
412 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
416 * Defines a static function @a name which takes as a single argument
417 * a message handler for fixed-sized messages of type @a code and with
418 * a message type argument of @a str. Given such an argument, the
419 * function @name will return a `struct GNUNET_MQ_MessageHandler`
420 * for the given message type.
422 * The macro is to be used as follows:
424 * struct GNUNET_MessageTest { ... }; // must be fixed size
426 * handle_test_message (void *cls,
427 * const struct GNUNET_MessageTest *msg)
430 * struct GNUNET_MQ_MessageHandler handlers[] = {
431 * GNUNET_MQ_hd_fixed_size(test_message,
432 * GNUNET_MESSAGE_TYPE_TEST,
433 * struct GNUNET_MessageTest,
435 * GNUNET_MQ_handler_end()
438 * @param name unique basename for the functions
439 * @param code message type constant
440 * @param str type of the message (a struct)
441 * @param ctx context for the callbacks
443 #define GNUNET_MQ_hd_fixed_size(name,code,str,ctx) \
445 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
446 ((struct GNUNET_MQ_MessageHandler) { \
447 NULL, (GNUNET_MQ_MessageCallback) _cb, \
448 (ctx), (code), sizeof (str) }); \
453 * Defines a static function @a name which takes two arguments and a
454 * context-pointer for validating and handling variable-sized messages
455 * of type @a code and with a message type argument of @a str. Given
456 * such arguments, the function @name will return a `struct
457 * GNUNET_MQ_MessageHandler` for the given message type.
459 * The macro is to be used as follows:
461 * struct GNUNET_MessageTest { ... }; // can be variable size
463 * check_test (void *cls,
464 * const struct GNUNET_MessageTest *msg)
466 * const char *ctx = cls;
467 * GNUNET_assert (0 == strcmp ("context", ctx));
471 * handle_test (void *cls,
472 * const struct GNUNET_MessageTest *msg)
474 * const char *ctx = cls;
475 * GNUNET_assert (0 == strcmp ("context", ctx));
479 * struct GNUNET_MQ_MessageHandler handlers[] = {
480 * GNUNET_MQ_hd_var_size(test_message,
481 * GNUNET_MESSAGE_TYPE_TEST,
482 * struct GNUNET_MessageTest,
484 * GNUNET_MQ_handler_end()
487 * @param name unique basename for the functions
488 * @param code message type constant
489 * @param str type of the message (a struct)
490 * @param ctx context for the callbacks
492 #define GNUNET_MQ_hd_var_size(name,code,str,ctx) \
494 int (*_mv)(void *cls, const str *msg) = &check_##name; \
495 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
496 ((struct GNUNET_MQ_MessageHandler) \
497 { (GNUNET_MQ_MessageValidationCallback) _mv, \
498 (GNUNET_MQ_MessageCallback) _cb, \
499 (ctx), (code), sizeof (str) }); \
504 * Create a new envelope.
506 * @param mhp message header to store the allocated message header in, can be NULL
507 * @param size size of the message to allocate
508 * @param type type of the message, will be set in the allocated message
509 * @return the allocated MQ message
511 struct GNUNET_MQ_Envelope *
512 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
518 * Create a new envelope by copying an existing message.
520 * @param hdr header of the message to copy
521 * @return envelope containing @a hdr
523 struct GNUNET_MQ_Envelope *
524 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
528 * Discard the message queue message, free all
529 * allocated resources. Must be called in the event
530 * that a message is created but should not actually be sent.
532 * @param mqm the message to discard
535 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
539 * Function to obtain the current envelope
540 * from within #GNUNET_MQ_SendImpl implementations.
542 * @param mq message queue to interrogate
543 * @return the current envelope
545 struct GNUNET_MQ_Envelope *
546 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
550 * Function to copy an envelope. The envelope must not yet
551 * be in any queue or have any options or callbacks set.
553 * @param env envelope to copy
554 * @return copy of @a env
556 struct GNUNET_MQ_Envelope *
557 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env);
561 * Function to obtain the last envelope in the queue.
563 * @param mq message queue to interrogate
564 * @return the last envelope in the queue
566 struct GNUNET_MQ_Envelope *
567 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
571 * Set application-specific options for this envelope.
572 * Overrides the options set for the queue with
573 * #GNUNET_MQ_set_options() for this message only.
575 * @param env message 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_env_set_options (struct GNUNET_MQ_Envelope *env,
586 * Get application-specific options for this envelope.
588 * @param env message to set options for
589 * @param[out] flags set to flags to use (meaning is queue-specific)
590 * @return extra additional buffer for further data (also queue-specific)
593 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
598 * Remove the first envelope that has not yet been sent from the message
599 * queue and return it.
601 * @param mq queue to remove envelope from
602 * @return NULL if queue is empty (or has no envelope that is not under transmission)
604 struct GNUNET_MQ_Envelope *
605 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq);
609 * Set application-specific options for this queue.
611 * @param mq message queue to set options for
612 * @param flags flags to use (meaning is queue-specific)
613 * @param extra additional buffer for further data (also queue-specific)
616 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
622 * Obtain the current length of the message queue.
624 * @param mq queue to inspect
625 * @return number of queued, non-transmitted messages
628 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
632 * Send a message with the given message queue.
633 * May only be called once per message.
635 * @param mq message queue
636 * @param ev the envelope with the message to send.
639 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
640 struct GNUNET_MQ_Envelope *ev);
644 * Send a copy of a message with the given message queue.
645 * Can be called repeatedly on the same envelope.
647 * @param mq message queue
648 * @param ev the envelope with the message to send.
651 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
652 const struct GNUNET_MQ_Envelope *ev);
656 * Cancel sending the message. Message must have been sent with
657 * #GNUNET_MQ_send before. May not be called after the notify sent
658 * callback has been called
660 * @param ev queued envelope to cancel
663 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
667 * Associate the assoc_data in @a mq with a unique request id.
669 * @param mq message queue, id will be unique for the queue
670 * @param assoc_data to associate
673 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
678 * Get the data associated with a @a request_id in a queue
680 * @param mq the message queue with the association
681 * @param request_id the request id we are interested in
682 * @return the associated data
685 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
686 uint32_t request_id);
690 * Remove the association for a @a request_id
692 * @param mq the message queue with the association
693 * @param request_id the request id we want to remove
694 * @return the associated data
697 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
698 uint32_t request_id);
702 * Create a message queue for the specified handlers.
704 * @param send function the implements sending messages
705 * @param destroy function that implements destroying the queue
706 * @param cancel function that implements canceling a message
707 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
708 * @param handlers array of message handlers
709 * @param error_handler handler for read and write errors
710 * @param cls closure for message handlers and error handler
711 * @return a new message queue
713 struct GNUNET_MQ_Handle *
714 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
715 GNUNET_MQ_DestroyImpl destroy,
716 GNUNET_MQ_CancelImpl cancel,
718 const struct GNUNET_MQ_MessageHandler *handlers,
719 GNUNET_MQ_ErrorHandler error_handler,
724 * Change the closure argument in all of the `handlers` of the
727 * @param mq to modify
728 * @param handlers_cls new closure to use
731 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
736 * Call a callback once the envelope has been sent, that is,
737 * sending it can not be canceled anymore.
738 * There can be only one notify sent callback per envelope.
740 * @param ev message to call the notify callback for
741 * @param cb the notify callback
742 * @param cb_cls closure for the callback
745 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
746 GNUNET_SCHEDULER_TaskCallback cb,
751 * Destroy the message queue.
753 * @param mq message queue to destroy
756 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
760 * Handle we return for callbacks registered to be
761 * notified when #GNUNET_MQ_destroy() is called on a queue.
763 struct GNUNET_MQ_DestroyNotificationHandle;
767 * Register function to be called whenever @a mq is being
770 * @param mq message queue to watch
771 * @param cb function to call on @a mq destruction
772 * @param cb_cls closure for @a cb
773 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
775 struct GNUNET_MQ_DestroyNotificationHandle *
776 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
777 GNUNET_SCHEDULER_TaskCallback cb,
781 * Cancel registration from #GNUNET_MQ_destroy_notify().
783 * @param dnh handle for registration to cancel
786 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
790 * Call the message message handler that was registered
791 * for the type of the given message in the given message queue.
793 * This function is indended to be used for the implementation
796 * @param mq message queue with the handlers
797 * @param mh message to dispatch
800 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
801 const struct GNUNET_MessageHeader *mh);
805 * Call the error handler of a message queue with the given
806 * error code. If there is no error handler, log a warning.
808 * This function is intended to be used for the implementation
811 * @param mq message queue
812 * @param error the error type
815 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
816 enum GNUNET_MQ_Error error);
820 * Call the send implementation for the next queued message, if any.
821 * Calls the send notification for the current message unless
822 * #GNUNET_MQ_impl_send_in_flight was called for this envelope.
824 * Only useful for implementing message queues, results in undefined
825 * behavior if not used carefully.
827 * @param mq message queue to send the next message with
830 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
834 * Call the send notification for the current message, but do not
835 * try to send the next message until #gnunet_mq_impl_send_continue
838 * Only useful for implementing message queues, results in undefined
839 * behavior if not used carefully.
841 * @param mq message queue to send the next message with
844 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq);
848 * Get the implementation state associated with the
851 * While the GNUNET_MQ_Impl* callbacks receive the
852 * implementation state, continuations that are scheduled
853 * by the implementation function often only have one closure
854 * argument, with this function it is possible to get at the
855 * implementation state when only passing the `struct GNUNET_MQ_Handle`
858 * @param mq message queue with the current message
859 * @return message to send, never NULL
862 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
866 * Get the message that should currently be sent.
867 * Fails if there is no current message.
868 * Only useful for implementing message queues,
869 * results in undefined behavior if not used carefully.
871 * @param mq message queue with the current message
872 * @return message to send, never NULL
874 const struct GNUNET_MessageHeader *
875 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
880 /** @} */ /* end of group mq */