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)
40 * Allocate an envelope, with extra space allocated after the space needed
41 * by the message struct.
42 * The allocated message will already have the type and size field set.
44 * @param mvar variable to store the allocated message in;
45 * must have a header field;
47 * @param esize extra space to allocate after the message
48 * @param type type of the message
49 * @return the MQ message
51 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
54 * Allocate a GNUNET_MQ_Envelope.
55 * The contained message will already have the type and size field set.
57 * @param mvar variable to store the allocated message in;
58 * must have a header field;
60 * @param type type of the message
61 * @return the allocated envelope
63 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
67 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
68 * The allocated message will already have the type and size field set.
70 * @param type type of the message
72 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
76 * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
77 * The allocated message will already have the type and size field set.
79 * @param mh pointer that will changed to point at to the allocated message header
80 * @param esize extra space to allocate after the message header
81 * @param type type of the message
83 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
87 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
90 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
91 * whose size is 'sizeof(*mvar) + ntohs (mh->size)'
92 * @param type message type of the allocated message, has no effect on the nested message
93 * @param mh message to nest
94 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
96 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) \
97 ({struct GNUNET_MQ_Envelope *_ev;\
98 _ev = GNUNET_MQ_msg_nested_mh_((struct GNUNET_MessageHeader**) &(mvar),\
102 (void)(mvar)->header; /* type check */\
107 * Return a pointer to the message at the end of the given message.
109 * @param var pointer to a message struct, the type of the expression determines the base size,
110 * the space after the base size is the nested message
111 * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
112 * or NULL if the given message in @a var does not have any space after the message struct
114 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
118 * Implementation of the #GNUNET_MQ_extract_nexted_mh macro.
120 * @param mh message header to extract nested message header from
121 * @param base_size size of the message before the nested message's header appears
122 * @return pointer to the nested message, does not copy the message
123 * OR NULL in case of a malformed message.
125 const struct GNUNET_MessageHeader *
126 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
131 * Opaque handle to an envelope.
133 struct GNUNET_MQ_Envelope;
137 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
139 * @param mhp pointer to the message header pointer that will be changed to allocate at
140 * the newly allocated space for the message.
141 * @param base_size size of the data before the nested message
142 * @param type type of the message in the envelope
143 * @param nested_mh the message to append to the message after base_size
145 struct GNUNET_MQ_Envelope *
146 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
149 const struct GNUNET_MessageHeader *nested_mh);
153 * Opaque handle to a message queue.
155 struct GNUNET_MQ_Handle;
159 * Error codes for the queue.
164 * Failed to read message from the network.
165 * FIXME: Likely not properly distinguished
166 * from TIMEOUT case in the code!
168 GNUNET_MQ_ERROR_READ = 1,
173 GNUNET_MQ_ERROR_WRITE = 2,
178 GNUNET_MQ_ERROR_TIMEOUT = 4,
181 * We received a message that was malformed and thus
182 * could not be passed to its handler.
184 GNUNET_MQ_ERROR_MALFORMED = 8,
187 * We received a message for which we have no matching
190 GNUNET_MQ_ERROR_NO_MATCH = 16
195 * Called when a message has been received.
198 * @param msg the received message
201 (*GNUNET_MQ_MessageCallback) (void *cls,
202 const struct GNUNET_MessageHeader *msg);
206 * Called when a message needs to be validated.
209 * @param msg the received message
210 * @return #GNUNET_OK if the message is well-formed,
211 * #GNUNET_SYSERR if not
214 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
215 const struct GNUNET_MessageHeader *msg);
219 * Signature of functions implementing the
220 * sending functionality of a message queue.
222 * @param mq the message queue
223 * @param msg the message to send
224 * @param impl_state state of the implementation
227 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
228 const struct GNUNET_MessageHeader *msg,
233 * Signature of functions implementing the
234 * destruction of a message queue.
235 * Implementations must not free @a mq, but should
236 * take care of @a impl_state.
238 * @param mq the message queue to destroy
239 * @param impl_state state of the implementation
242 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
247 * Implementation function that cancels the currently sent message.
249 * @param mq message queue
250 * @param impl_state state specific to the implementation
253 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
258 * Generic error handler, called with the appropriate
259 * error code and the same closure specified at the creation of
261 * Not every message queue implementation supports an error handler.
264 * @param error error code
267 (*GNUNET_MQ_ErrorHandler) (void *cls,
268 enum GNUNET_MQ_Error error);
272 * Insert @a env into the envelope DLL starting at @a env_head
273 * Note that @a env must not be in any MQ while this function
274 * is used with DLLs defined outside of the MQ module. This
275 * is just in case some application needs to also manage a
276 * FIFO of envelopes independent of MQ itself and wants to
277 * re-use the pointers internal to @a env. Use with caution.
279 * @param[in|out] env_head of envelope DLL
280 * @param[in|out] env_tail tail of envelope DLL
281 * @param[in|out] env element to insert at the tail
284 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
285 struct GNUNET_MQ_Envelope **env_tail,
286 struct GNUNET_MQ_Envelope *env);
290 * Remove @a env from the envelope DLL starting at @a env_head.
291 * Note that @a env must not be in any MQ while this function
292 * is used with DLLs defined outside of the MQ module. This
293 * is just in case some application needs to also manage a
294 * FIFO of envelopes independent of MQ itself and wants to
295 * re-use the pointers internal to @a env. Use with caution.
297 * @param[in|out] env_head of envelope DLL
298 * @param[in|out] env_tail tail of envelope DLL
299 * @param[in|out] env element to remove from the DLL
302 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
303 struct GNUNET_MQ_Envelope **env_tail,
304 struct GNUNET_MQ_Envelope *env);
308 * Message handler for a specific message type.
310 struct GNUNET_MQ_MessageHandler
313 * Callback to validate a message of the specified @e type.
314 * The closure given to @e mv will be this struct (not @e ctx).
315 * Using NULL means only size-validation using
316 * @e expected_size. In this case, @e expected_size must
319 GNUNET_MQ_MessageValidationCallback mv;
322 * Callback, called every time a new message of
323 * the specified @e type has been receied.
324 * The closure given to @e mv will be this struct (not @e ctx).
326 GNUNET_MQ_MessageCallback cb;
329 * Closure for @e mv and @e cb.
334 * Type of the message this handler covers, in host byte order.
339 * Expected size of messages of this type. Minimum size of the
340 * message if @e mv is non-NULL. Messages of the given type will be
341 * discarded (and the connection closed with an error reported to
342 * the application) if they do not have the right size.
344 uint16_t expected_size;
349 * End-marker for the handlers array
351 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
355 * Defines a static function @a name which takes as a single argument
356 * a message handler for fixed-sized messages of type @a code and with
357 * a message type argument of @a str. Given such an argument, the
358 * function @name will return a `struct GNUNET_MQ_MessageHandler`
359 * for the given message type.
361 * The macro is to be used as follows:
363 * struct GNUNET_MessageTest { ... }; // must be fixed size
365 * handle_test_message (void *cls,
366 * const struct GNUNET_MessageTest *msg)
369 * struct GNUNET_MQ_MessageHandler handlers[] = {
370 * GNUNET_MQ_hd_fixed_size(test_message,
371 * GNUNET_MESSAGE_TYPE_TEST,
372 * struct GNUNET_MessageTest,
374 * GNUNET_MQ_handler_end()
377 * @param name unique basename for the functions
378 * @param code message type constant
379 * @param str type of the message (a struct)
380 * @param ctx context for the callbacks
382 #define GNUNET_MQ_hd_fixed_size(name,code,str,ctx) \
384 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
385 ((struct GNUNET_MQ_MessageHandler) { \
386 NULL, (GNUNET_MQ_MessageCallback) _cb, \
387 (ctx), (code), sizeof (str) }); \
392 * Defines a static function @a name which takes two arguments and a
393 * context-pointer for validating and handling variable-sized messages
394 * of type @a code and with a message type argument of @a str. Given
395 * such arguments, the function @name will return a `struct
396 * GNUNET_MQ_MessageHandler` for the given message type.
398 * The macro is to be used as follows:
400 * struct GNUNET_MessageTest { ... }; // can be variable size
402 * check_test (void *cls,
403 * const struct GNUNET_MessageTest *msg)
405 * const char *ctx = cls;
406 * GNUNET_assert (0 == strcmp ("context", ctx));
410 * handle_test (void *cls,
411 * const struct GNUNET_MessageTest *msg)
413 * const char *ctx = cls;
414 * GNUNET_assert (0 == strcmp ("context", ctx));
418 * struct GNUNET_MQ_MessageHandler handlers[] = {
419 * GNUNET_MQ_hd_var_size(test_message,
420 * GNUNET_MESSAGE_TYPE_TEST,
421 * struct GNUNET_MessageTest,
423 * GNUNET_MQ_handler_end()
426 * @param name unique basename for the functions
427 * @param code message type constant
428 * @param str type of the message (a struct)
429 * @param ctx context for the callbacks
431 #define GNUNET_MQ_hd_var_size(name,code,str,ctx) \
433 int (*_mv)(void *cls, const str *msg) = &check_##name; \
434 void (*_cb)(void *cls, const str *msg) = &handle_##name; \
435 ((struct GNUNET_MQ_MessageHandler) \
436 { (GNUNET_MQ_MessageValidationCallback) _mv, \
437 (GNUNET_MQ_MessageCallback) _cb, \
438 (ctx), (code), sizeof (str) }); \
443 * Create a new envelope.
445 * @param mhp message header to store the allocated message header in, can be NULL
446 * @param size size of the message to allocate
447 * @param type type of the message, will be set in the allocated message
448 * @return the allocated MQ message
450 struct GNUNET_MQ_Envelope *
451 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
458 * Create a new envelope by copying an existing message.
460 * @param hdr header of the message to copy
461 * @return envelope containing @a hdr
463 struct GNUNET_MQ_Envelope *
464 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
468 * Discard the message queue message, free all
469 * allocated resources. Must be called in the event
470 * that a message is created but should not actually be sent.
472 * @param mqm the message to discard
475 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
479 * Function to obtain the current envelope
480 * from within #GNUNET_MQ_SendImpl implementations.
482 * @param mq message queue to interrogate
483 * @return the current envelope
485 struct GNUNET_MQ_Envelope *
486 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
490 * Function to obtain the last envelope in the queue.
492 * @param mq message queue to interrogate
493 * @return the last envelope in the queue
495 struct GNUNET_MQ_Envelope *
496 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
500 * Set application-specific options for this envelope.
501 * Overrides the options set for the queue with
502 * #GNUNET_MQ_set_options() for this message only.
504 * @param env message to set options for
505 * @param flags flags to use (meaning is queue-specific)
506 * @param extra additional buffer for further data (also queue-specific)
509 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
515 * Get application-specific options for this envelope.
517 * @param env message to set options for
518 * @param[out] flags set to flags to use (meaning is queue-specific)
519 * @return extra additional buffer for further data (also queue-specific)
522 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
527 * Set application-specific options for this queue.
529 * @param mq message queue to set options for
530 * @param flags flags to use (meaning is queue-specific)
531 * @param extra additional buffer for further data (also queue-specific)
534 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
540 * Obtain the current length of the message queue.
542 * @param mq queue to inspect
543 * @return number of queued, non-transmitted messages
546 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
550 * Send a message with the given message queue.
551 * May only be called once per message.
553 * @param mq message queue
554 * @param ev the envelope with the message to send.
557 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
558 struct GNUNET_MQ_Envelope *ev);
562 * Send a copy of a message with the given message queue.
563 * Can be called repeatedly on the same envelope.
565 * @param mq message queue
566 * @param ev the envelope with the message to send.
569 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
570 const struct GNUNET_MQ_Envelope *ev);
574 * Cancel sending the message. Message must have been sent with
575 * #GNUNET_MQ_send before. May not be called after the notify sent
576 * callback has been called
578 * @param ev queued envelope to cancel
581 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
585 * Associate the assoc_data in @a mq with a unique request id.
587 * @param mq message queue, id will be unique for the queue
588 * @param assoc_data to associate
591 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
596 * Get the data associated with a @a request_id in a queue
598 * @param mq the message queue with the association
599 * @param request_id the request id we are interested in
600 * @return the associated data
603 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
604 uint32_t request_id);
608 * Remove the association for a @a request_id
610 * @param mq the message queue with the association
611 * @param request_id the request id we want to remove
612 * @return the associated data
615 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
616 uint32_t request_id);
620 * Create a message queue for the specified handlers.
622 * @param send function the implements sending messages
623 * @param destroy function that implements destroying the queue
624 * @param cancel function that implements canceling a message
625 * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
626 * @param handlers array of message handlers
627 * @param error_handler handler for read and write errors
628 * @param cls closure for message handlers and error handler
629 * @return a new message queue
631 struct GNUNET_MQ_Handle *
632 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
633 GNUNET_MQ_DestroyImpl destroy,
634 GNUNET_MQ_CancelImpl cancel,
636 const struct GNUNET_MQ_MessageHandler *handlers,
637 GNUNET_MQ_ErrorHandler error_handler,
642 * Change the closure argument in all of the `handlers` of the
645 * @param mq to modify
646 * @param handlers_cls new closure to use
649 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
654 * Call a callback once the envelope has been sent, that is,
655 * sending it can not be canceled anymore.
656 * There can be only one notify sent callback per envelope.
658 * @param ev message to call the notify callback for
659 * @param cb the notify callback
660 * @param cb_cls closure for the callback
663 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
664 GNUNET_SCHEDULER_TaskCallback cb,
669 * Destroy the message queue.
671 * @param mq message queue to destroy
674 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
678 * Handle we return for callbacks registered to be
679 * notified when #GNUNET_MQ_destroy() is called on a queue.
681 struct GNUNET_MQ_DestroyNotificationHandle;
685 * Register function to be called whenever @a mq is being
688 * @param mq message queue to watch
689 * @param cb function to call on @a mq destruction
690 * @param cb_cls closure for @a cb
691 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
693 struct GNUNET_MQ_DestroyNotificationHandle *
694 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
695 GNUNET_SCHEDULER_TaskCallback cb,
699 * Cancel registration from #GNUNET_MQ_destroy_notify().
701 * @param dnh handle for registration to cancel
704 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
708 * Call the message message handler that was registered
709 * for the type of the given message in the given message queue.
711 * This function is indended to be used for the implementation
714 * @param mq message queue with the handlers
715 * @param mh message to dispatch
718 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
719 const struct GNUNET_MessageHeader *mh);
723 * Call the error handler of a message queue with the given
724 * error code. If there is no error handler, log a warning.
726 * This function is intended to be used for the implementation
729 * @param mq message queue
730 * @param error the error type
733 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
734 enum GNUNET_MQ_Error error);
738 * Call the send implementation for the next queued message, if any.
739 * Calls the send notification for the current message unless
740 * #GNUNET_MQ_impl_send_in_flight was called for this envelope.
742 * Only useful for implementing message queues, results in undefined
743 * behavior if not used carefully.
745 * @param mq message queue to send the next message with
748 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
752 * Call the send notification for the current message, but do not
753 * try to send the next message until #gnunet_mq_impl_send_continue
756 * only useful for implementing message queues, results in undefined
757 * behavior if not used carefully.
759 * @param mq message queue to send the next message with
762 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq);
766 * Get the implementation state associated with the
769 * While the GNUNET_MQ_Impl* callbacks receive the
770 * implementation state, continuations that are scheduled
771 * by the implementation function often only have one closure
772 * argument, with this function it is possible to get at the
773 * implementation state when only passing the `struct GNUNET_MQ_Handle`
776 * @param mq message queue with the current message
777 * @return message to send, never NULL
780 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
784 * Get the message that should currently be sent.
785 * Fails if there is no current message.
786 * Only useful for implementing message queues,
787 * results in undefined behavior if not used carefully.
789 * @param mq message queue with the current message
790 * @return message to send, never NULL
792 const struct GNUNET_MessageHeader *
793 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
798 /** @} */ /* end of group mq */