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 * Obtain message contained in envelope.
140 * @param env the envelope
141 * @return message contained in the envelope
143 const struct GNUNET_MessageHeader *
144 GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env);
148 * Return next envelope in queue.
150 * @param env a queued envelope
151 * @return next one, or NULL
153 const struct GNUNET_MQ_Envelope *
154 GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env);
158 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
160 * @param mhp pointer to the message header pointer that will be changed to allocate at
161 * the newly allocated space for the message.
162 * @param base_size size of the data before the nested message
163 * @param type type of the message in the envelope
164 * @param nested_mh the message to append to the message after base_size
166 struct GNUNET_MQ_Envelope *
167 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
170 const struct GNUNET_MessageHeader *nested_mh);
174 * Opaque handle to a message queue.
176 struct GNUNET_MQ_Handle;
180 * Error codes for the queue.
185 * Failed to read message from the network.
186 * FIXME: Likely not properly distinguished
187 * from TIMEOUT case in the code!
189 GNUNET_MQ_ERROR_READ = 1,
194 GNUNET_MQ_ERROR_WRITE = 2,
199 GNUNET_MQ_ERROR_TIMEOUT = 4,
202 * We received a message that was malformed and thus
203 * could not be passed to its handler.
205 GNUNET_MQ_ERROR_MALFORMED = 8,
208 * We received a message for which we have no matching
211 GNUNET_MQ_ERROR_NO_MATCH = 16
216 * Called when a message has been received.
219 * @param msg the received message
222 (*GNUNET_MQ_MessageCallback) (void *cls,
223 const struct GNUNET_MessageHeader *msg);
227 * Called when a message needs to be validated.
230 * @param msg the received message
231 * @return #GNUNET_OK if the message is well-formed,
232 * #GNUNET_SYSERR if not
235 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
236 const struct GNUNET_MessageHeader *msg);
240 * Signature of functions implementing the
241 * sending functionality of a message queue.
243 * @param mq the message queue
244 * @param msg the message to send
245 * @param impl_state state of the implementation
248 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
249 const struct GNUNET_MessageHeader *msg,
254 * Signature of functions implementing the
255 * destruction of a message queue.
256 * Implementations must not free @a mq, but should
257 * take care of @a impl_state.
259 * @param mq the message queue to destroy
260 * @param impl_state state of the implementation
263 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
268 * Implementation function that cancels the currently sent message.
270 * @param mq message queue
271 * @param impl_state state specific to the implementation
274 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
279 * Generic error handler, called with the appropriate
280 * error code and the same closure specified at the creation of
282 * Not every message queue implementation supports an error handler.
285 * @param error error code
288 (*GNUNET_MQ_ErrorHandler) (void *cls,
289 enum GNUNET_MQ_Error error);
293 * Insert @a env into the envelope DLL starting at @a env_head
294 * Note that @a env must not be in any MQ while this function
295 * is used with DLLs defined outside of the MQ module. This
296 * is just in case some application needs to also manage a
297 * FIFO of envelopes independent of MQ itself and wants to
298 * re-use the pointers internal to @a env. Use with caution.
300 * @param[in|out] env_head of envelope DLL
301 * @param[in|out] env_tail tail of envelope DLL
302 * @param[in|out] env element to insert at the tail
305 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
306 struct GNUNET_MQ_Envelope **env_tail,
307 struct GNUNET_MQ_Envelope *env);
311 * Remove @a env from the envelope DLL starting at @a env_head.
312 * Note that @a env must not be in any MQ while this function
313 * is used with DLLs defined outside of the MQ module. This
314 * is just in case some application needs to also manage a
315 * FIFO of envelopes independent of MQ itself and wants to
316 * re-use the pointers internal to @a env. Use with caution.
318 * @param[in|out] env_head of envelope DLL
319 * @param[in|out] env_tail tail of envelope DLL
320 * @param[in|out] env element to remove from the DLL
323 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
324 struct GNUNET_MQ_Envelope **env_tail,
325 struct GNUNET_MQ_Envelope *env);
329 * Copy an array of handlers.
331 * Useful if the array has been delared in local memory and needs to be
332 * persisted for future use.
334 * @param handlers Array of handlers to be copied.
335 * @return A newly allocated array of handlers.
336 * Needs to be freed with #GNUNET_free.
338 struct GNUNET_MQ_MessageHandler *
339 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
343 * Count the handlers in a handler array.
345 * @param handlers Array of handlers to be counted.
346 * @return The number of handlers in the array.
349 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
353 * Message handler for a specific message type.
355 struct GNUNET_MQ_MessageHandler
358 * Callback to validate a message of the specified @e type.
359 * The closure given to @e mv will be this struct (not @e ctx).
360 * Using NULL means only size-validation using
361 * @e expected_size. In this case, @e expected_size must
364 GNUNET_MQ_MessageValidationCallback mv;
367 * Callback, called every time a new message of
368 * the specified @e type has been receied.
369 * The closure given to @e mv will be this struct (not @e ctx).
371 GNUNET_MQ_MessageCallback cb;
374 * Closure for @e mv and @e cb.
379 * Type of the message this handler covers, in host byte order.
384 * Expected size of messages of this type. Minimum size of the
385 * message if @e mv is non-NULL. Messages of the given type will be
386 * discarded (and the connection closed with an error reported to
387 * the application) if they do not have the right size.
389 uint16_t expected_size;
394 * End-marker for the handlers array
396 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
400 * Defines a static function @a name which takes as a single argument
401 * a message handler for fixed-sized messages of type @a code and with
402 * a message type argument of @a str. Given such an argument, the
403 * function @name will return a `struct GNUNET_MQ_MessageHandler`
404 * for the given message type.
406 * The macro is to be used as follows:
408 * struct GNUNET_MessageTest { ... }; // must be fixed size
410 * handle_test_message (void *cls,
411 * const struct GNUNET_MessageTest *msg)
414 * struct GNUNET_MQ_MessageHandler handlers[] = {
415 * GNUNET_MQ_hd_fixed_size(test_message,
416 * GNUNET_MESSAGE_TYPE_TEST,
417 * struct GNUNET_MessageTest,
419 * GNUNET_MQ_handler_end()
422 * @param name unique basename for the functions
423 * @param code message type constant
424 * @param str type of the message (a struct)
425 * @param ctx context for the callbacks
427 #define GNUNET_MQ_hd_fixed_size(name,code,str,ctx) \
429 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
430 ((struct GNUNET_MQ_MessageHandler) { \
431 NULL, (GNUNET_MQ_MessageCallback) _cb, \
432 (ctx), (code), sizeof (str) }); \
437 * Defines a static function @a name which takes two arguments and a
438 * context-pointer for validating and handling variable-sized messages
439 * of type @a code and with a message type argument of @a str. Given
440 * such arguments, the function @name will return a `struct
441 * GNUNET_MQ_MessageHandler` for the given message type.
443 * The macro is to be used as follows:
445 * struct GNUNET_MessageTest { ... }; // can be variable size
447 * check_test (void *cls,
448 * const struct GNUNET_MessageTest *msg)
450 * const char *ctx = cls;
451 * GNUNET_assert (0 == strcmp ("context", ctx));
455 * handle_test (void *cls,
456 * const struct GNUNET_MessageTest *msg)
458 * const char *ctx = cls;
459 * GNUNET_assert (0 == strcmp ("context", ctx));
463 * struct GNUNET_MQ_MessageHandler handlers[] = {
464 * GNUNET_MQ_hd_var_size(test_message,
465 * GNUNET_MESSAGE_TYPE_TEST,
466 * struct GNUNET_MessageTest,
468 * GNUNET_MQ_handler_end()
471 * @param name unique basename for the functions
472 * @param code message type constant
473 * @param str type of the message (a struct)
474 * @param ctx context for the callbacks
476 #define GNUNET_MQ_hd_var_size(name,code,str,ctx) \
478 int (*_mv)(void *cls, const str *msg) = &check_##name; \
479 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
480 ((struct GNUNET_MQ_MessageHandler) \
481 { (GNUNET_MQ_MessageValidationCallback) _mv, \
482 (GNUNET_MQ_MessageCallback) _cb, \
483 (ctx), (code), sizeof (str) }); \
488 * Create a new envelope.
490 * @param mhp message header to store the allocated message header in, can be NULL
491 * @param size size of the message to allocate
492 * @param type type of the message, will be set in the allocated message
493 * @return the allocated MQ message
495 struct GNUNET_MQ_Envelope *
496 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
502 * Create a new envelope by copying an existing message.
504 * @param hdr header of the message to copy
505 * @return envelope containing @a hdr
507 struct GNUNET_MQ_Envelope *
508 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
512 * Discard the message queue message, free all
513 * allocated resources. Must be called in the event
514 * that a message is created but should not actually be sent.
516 * @param mqm the message to discard
519 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
523 * Function to obtain the current envelope
524 * from within #GNUNET_MQ_SendImpl implementations.
526 * @param mq message queue to interrogate
527 * @return the current envelope
529 struct GNUNET_MQ_Envelope *
530 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
534 * Function to copy an envelope. The envelope must not yet
535 * be in any queue or have any options or callbacks set.
537 * @param env envelope to copy
538 * @return copy of @a env
540 struct GNUNET_MQ_Envelope *
541 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env);
545 * Function to obtain the last envelope in the queue.
547 * @param mq message queue to interrogate
548 * @return the last envelope in the queue
550 struct GNUNET_MQ_Envelope *
551 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
555 * Set application-specific options for this envelope.
556 * Overrides the options set for the queue with
557 * #GNUNET_MQ_set_options() for this message only.
559 * @param env message to set options for
560 * @param flags flags to use (meaning is queue-specific)
561 * @param extra additional buffer for further data (also queue-specific)
564 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
570 * Get application-specific options for this envelope.
572 * @param env message to set options for
573 * @param[out] flags set to flags to use (meaning is queue-specific)
574 * @return extra additional buffer for further data (also queue-specific)
577 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
582 * Remove the first envelope that has not yet been sent from the message
583 * queue and return it.
585 * @param mq queue to remove envelope from
586 * @return NULL if queue is empty (or has no envelope that is not under transmission)
588 struct GNUNET_MQ_Envelope *
589 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq);
593 * Set application-specific options for this queue.
595 * @param mq message queue to set options for
596 * @param flags flags to use (meaning is queue-specific)
597 * @param extra additional buffer for further data (also queue-specific)
600 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
606 * Obtain the current length of the message queue.
608 * @param mq queue to inspect
609 * @return number of queued, non-transmitted messages
612 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
616 * Send a message with the given message queue.
617 * May only be called once per message.
619 * @param mq message queue
620 * @param ev the envelope with the message to send.
623 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
624 struct GNUNET_MQ_Envelope *ev);
628 * Send a copy of a message with the given message queue.
629 * Can be called repeatedly on the same envelope.
631 * @param mq message queue
632 * @param ev the envelope with the message to send.
635 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
636 const struct GNUNET_MQ_Envelope *ev);
640 * Cancel sending the message. Message must have been sent with
641 * #GNUNET_MQ_send before. May not be called after the notify sent
642 * callback has been called
644 * @param ev queued envelope to cancel
647 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
651 * Associate the assoc_data in @a mq with a unique request id.
653 * @param mq message queue, id will be unique for the queue
654 * @param assoc_data to associate
657 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
662 * Get the data associated with a @a request_id in a queue
664 * @param mq the message queue with the association
665 * @param request_id the request id we are interested in
666 * @return the associated data
669 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
670 uint32_t request_id);
674 * Remove the association for a @a request_id
676 * @param mq the message queue with the association
677 * @param request_id the request id we want to remove
678 * @return the associated data
681 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
682 uint32_t request_id);
686 * Create a message queue for the specified handlers.
688 * @param send function the implements sending messages
689 * @param destroy function that implements destroying the queue
690 * @param cancel function that implements canceling a message
691 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
692 * @param handlers array of message handlers
693 * @param error_handler handler for read and write errors
694 * @param cls closure for message handlers and error handler
695 * @return a new message queue
697 struct GNUNET_MQ_Handle *
698 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
699 GNUNET_MQ_DestroyImpl destroy,
700 GNUNET_MQ_CancelImpl cancel,
702 const struct GNUNET_MQ_MessageHandler *handlers,
703 GNUNET_MQ_ErrorHandler error_handler,
708 * Change the closure argument in all of the `handlers` of the
711 * @param mq to modify
712 * @param handlers_cls new closure to use
715 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
720 * Call a callback once the envelope has been sent, that is,
721 * sending it can not be canceled anymore.
722 * There can be only one notify sent callback per envelope.
724 * @param ev message to call the notify callback for
725 * @param cb the notify callback
726 * @param cb_cls closure for the callback
729 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
730 GNUNET_SCHEDULER_TaskCallback cb,
735 * Destroy the message queue.
737 * @param mq message queue to destroy
740 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
744 * Handle we return for callbacks registered to be
745 * notified when #GNUNET_MQ_destroy() is called on a queue.
747 struct GNUNET_MQ_DestroyNotificationHandle;
751 * Register function to be called whenever @a mq is being
754 * @param mq message queue to watch
755 * @param cb function to call on @a mq destruction
756 * @param cb_cls closure for @a cb
757 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
759 struct GNUNET_MQ_DestroyNotificationHandle *
760 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
761 GNUNET_SCHEDULER_TaskCallback cb,
765 * Cancel registration from #GNUNET_MQ_destroy_notify().
767 * @param dnh handle for registration to cancel
770 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
774 * Call the message message handler that was registered
775 * for the type of the given message in the given message queue.
777 * This function is indended to be used for the implementation
780 * @param mq message queue with the handlers
781 * @param mh message to dispatch
784 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
785 const struct GNUNET_MessageHeader *mh);
789 * Call the error handler of a message queue with the given
790 * error code. If there is no error handler, log a warning.
792 * This function is intended to be used for the implementation
795 * @param mq message queue
796 * @param error the error type
799 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
800 enum GNUNET_MQ_Error error);
804 * Call the send implementation for the next queued message, if any.
805 * Calls the send notification for the current message unless
806 * #GNUNET_MQ_impl_send_in_flight was called for this envelope.
808 * Only useful for implementing message queues, results in undefined
809 * behavior if not used carefully.
811 * @param mq message queue to send the next message with
814 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
818 * Call the send notification for the current message, but do not
819 * try to send the next message until #gnunet_mq_impl_send_continue
822 * Only useful for implementing message queues, results in undefined
823 * behavior if not used carefully.
825 * @param mq message queue to send the next message with
828 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq);
832 * Get the implementation state associated with the
835 * While the GNUNET_MQ_Impl* callbacks receive the
836 * implementation state, continuations that are scheduled
837 * by the implementation function often only have one closure
838 * argument, with this function it is possible to get at the
839 * implementation state when only passing the `struct GNUNET_MQ_Handle`
842 * @param mq message queue with the current message
843 * @return message to send, never NULL
846 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
850 * Get the message that should currently be sent.
851 * Fails if there is no current message.
852 * Only useful for implementing message queues,
853 * results in undefined behavior if not used carefully.
855 * @param mq message queue with the current message
856 * @return message to send, never NULL
858 const struct GNUNET_MessageHeader *
859 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
864 /** @} */ /* end of group mq */