2 This file is part of GNUnet.
3 Copyright (C) 2012-2019 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Florian Dold
24 * @brief general purpose request queue
27 #include "gnunet_util_lib.h"
29 #define LOG(kind, ...) GNUNET_log_from(kind, "util-mq", __VA_ARGS__)
32 struct GNUNET_MQ_Envelope {
34 * Messages are stored in a linked list.
35 * Each queue has its own list of envelopes.
37 struct GNUNET_MQ_Envelope *next;
40 * Messages are stored in a linked list
41 * Each queue has its own list of envelopes.
43 struct GNUNET_MQ_Envelope *prev;
46 * Actual allocated message header.
47 * The GNUNET_MQ_Envelope header is allocated at
48 * the end of the message.
50 struct GNUNET_MessageHeader *mh;
53 * Queue the message is queued in, NULL if message is not queued.
55 struct GNUNET_MQ_Handle *parent_queue;
58 * Called after the message was sent irrevocably.
60 GNUNET_SCHEDULER_TaskCallback sent_cb;
63 * Closure for @e send_cb
68 * Flags that were set for this envelope by
69 * #GNUNET_MQ_env_set_options(). Only valid if
70 * @e have_custom_options is set.
72 enum GNUNET_MQ_PriorityPreferences priority;
75 * Did the application call #GNUNET_MQ_env_set_options()?
77 int have_custom_options;
82 * Handle to a message queue.
84 struct GNUNET_MQ_Handle {
86 * Handlers array, or NULL if the queue should not receive messages
88 struct GNUNET_MQ_MessageHandler *handlers;
91 * Actual implementation of message sending,
92 * called when a message is added
94 GNUNET_MQ_SendImpl send_impl;
97 * Implementation-dependent queue destruction function
99 GNUNET_MQ_DestroyImpl destroy_impl;
102 * Implementation-dependent send cancel function
104 GNUNET_MQ_CancelImpl cancel_impl;
107 * Implementation-specific state
112 * Callback will be called when an error occurs.
114 GNUNET_MQ_ErrorHandler error_handler;
117 * Closure for the error handler.
119 void *error_handler_cls;
122 * Task to asynchronously run #impl_send_continue().
124 struct GNUNET_SCHEDULER_Task *send_task;
127 * Linked list of messages pending to be sent
129 struct GNUNET_MQ_Envelope *envelope_head;
132 * Linked list of messages pending to be sent
134 struct GNUNET_MQ_Envelope *envelope_tail;
137 * Message that is currently scheduled to be
138 * sent. Not the head of the message queue, as the implementation
139 * needs to know if sending has been already scheduled or not.
141 struct GNUNET_MQ_Envelope *current_envelope;
144 * Map of associations, lazily allocated
146 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
149 * Functions to call on queue destruction; kept in a DLL.
151 struct GNUNET_MQ_DestroyNotificationHandle *dnh_head;
154 * Functions to call on queue destruction; kept in a DLL.
156 struct GNUNET_MQ_DestroyNotificationHandle *dnh_tail;
159 * Flags that were set for this queue by
160 * #GNUNET_MQ_set_options(). Default is 0.
162 enum GNUNET_MQ_PriorityPreferences priority;
165 * Next id that should be used for the @e assoc_map,
166 * initialized lazily to a random value together with
172 * Number of entries we have in the envelope-DLL.
174 unsigned int queue_length;
177 * #GNUNET_YES if GNUNET_MQ_impl_evacuate was called.
178 * FIXME: is this dead?
183 * #GNUNET_YES if GNUNET_MQ_impl_send_in_flight() was called.
190 * Call the message message handler that was registered
191 * for the type of the given message in the given message queue.
193 * This function is indended to be used for the implementation
196 * @param mq message queue with the handlers
197 * @param mh message to dispatch
200 GNUNET_MQ_inject_message(struct GNUNET_MQ_Handle *mq,
201 const struct GNUNET_MessageHeader *mh)
205 ret = GNUNET_MQ_handle_message(mq->handlers, mh);
206 if (GNUNET_SYSERR == ret)
208 GNUNET_MQ_inject_error(mq, GNUNET_MQ_ERROR_MALFORMED);
215 * Call the message message handler that was registered
216 * for the type of the given message in the given @a handlers list.
218 * This function is indended to be used for the implementation
221 * @param handlers a set of handlers
222 * @param mh message to dispatch
223 * @return #GNUNET_OK on success, #GNUNET_NO if no handler matched,
224 * #GNUNET_SYSERR if message was rejected by check function
227 GNUNET_MQ_handle_message(const struct GNUNET_MQ_MessageHandler *handlers,
228 const struct GNUNET_MessageHeader *mh)
230 const struct GNUNET_MQ_MessageHandler *handler;
231 int handled = GNUNET_NO;
232 uint16_t msize = ntohs(mh->size);
233 uint16_t mtype = ntohs(mh->type);
235 LOG(GNUNET_ERROR_TYPE_DEBUG,
236 "Received message of type %u and size %u\n",
240 if (NULL == handlers)
242 for (handler = handlers; NULL != handler->cb; handler++)
244 if (handler->type == mtype)
246 handled = GNUNET_YES;
247 if ((handler->expected_size > msize) ||
248 ((handler->expected_size != msize) && (NULL == handler->mv)))
250 /* Too small, or not an exact size and
251 no 'mv' handler to check rest */
252 LOG(GNUNET_ERROR_TYPE_ERROR,
253 "Received malformed message of type %u\n",
254 (unsigned int)handler->type);
255 return GNUNET_SYSERR;
257 if ((NULL == handler->mv) ||
258 (GNUNET_OK == handler->mv(handler->cls, mh)))
260 /* message well-formed, pass to handler */
261 handler->cb(handler->cls, mh);
265 /* Message rejected by check routine */
266 LOG(GNUNET_ERROR_TYPE_ERROR,
267 "Received malformed message of type %u\n",
268 (unsigned int)handler->type);
269 return GNUNET_SYSERR;
275 if (GNUNET_NO == handled)
277 LOG(GNUNET_ERROR_TYPE_INFO,
278 "No handler for message of type %u and size %u\n",
288 * Call the error handler of a message queue with the given
289 * error code. If there is no error handler, log a warning.
291 * This function is intended to be used by the implementation
294 * @param mq message queue
295 * @param error the error type
298 GNUNET_MQ_inject_error(struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_Error error)
300 if (NULL == mq->error_handler)
302 LOG(GNUNET_ERROR_TYPE_WARNING,
303 "Got error %d, but no handler installed\n",
307 mq->error_handler(mq->error_handler_cls, error);
312 * Discard the message queue message, free all
313 * allocated resources. Must be called in the event
314 * that a message is created but should not actually be sent.
316 * @param mqm the message to discard
319 GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *ev)
321 GNUNET_assert(NULL == ev->parent_queue);
327 * Obtain the current length of the message queue.
329 * @param mq queue to inspect
330 * @return number of queued, non-transmitted messages
333 GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
335 if (GNUNET_YES != mq->in_flight)
337 return mq->queue_length;
339 return mq->queue_length - 1;
344 * Send a message with the given message queue.
345 * May only be called once per message.
347 * @param mq message queue
348 * @param ev the envelope with the message to send.
351 GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
353 GNUNET_assert(NULL != mq);
354 GNUNET_assert(NULL == ev->parent_queue);
357 if (mq->queue_length >= 10000)
359 /* This would seem like a bug... */
360 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
361 "MQ with %u entries extended by message of type %u (FC broken?)\n",
362 (unsigned int)mq->queue_length,
363 (unsigned int)ntohs(ev->mh->type));
365 ev->parent_queue = mq;
366 /* is the implementation busy? queue it! */
367 if ((NULL != mq->current_envelope) || (NULL != mq->send_task))
369 GNUNET_CONTAINER_DLL_insert_tail(mq->envelope_head, mq->envelope_tail, ev);
372 GNUNET_assert(NULL == mq->envelope_head);
373 mq->current_envelope = ev;
375 LOG(GNUNET_ERROR_TYPE_DEBUG,
376 "sending message of type %u, queue empty (MQ: %p)\n",
380 mq->send_impl(mq, ev->mh, mq->impl_state);
385 * Remove the first envelope that has not yet been sent from the message
386 * queue and return it.
388 * @param mq queue to remove envelope from
389 * @return NULL if queue is empty (or has no envelope that is not under transmission)
391 struct GNUNET_MQ_Envelope *
392 GNUNET_MQ_unsent_head(struct GNUNET_MQ_Handle *mq)
394 struct GNUNET_MQ_Envelope *env;
396 env = mq->envelope_head;
397 GNUNET_CONTAINER_DLL_remove(mq->envelope_head, mq->envelope_tail, env);
399 env->parent_queue = NULL;
405 * Function to copy an envelope. The envelope must not yet
406 * be in any queue or have any options or callbacks set.
408 * @param env envelope to copy
409 * @return copy of @a env
411 struct GNUNET_MQ_Envelope *
412 GNUNET_MQ_env_copy(struct GNUNET_MQ_Envelope *env)
414 GNUNET_assert(NULL == env->next);
415 GNUNET_assert(NULL == env->parent_queue);
416 GNUNET_assert(NULL == env->sent_cb);
417 GNUNET_assert(GNUNET_NO == env->have_custom_options);
418 return GNUNET_MQ_msg_copy(env->mh);
423 * Send a copy of a message with the given message queue.
424 * Can be called repeatedly on the same envelope.
426 * @param mq message queue
427 * @param ev the envelope with the message to send.
430 GNUNET_MQ_send_copy(struct GNUNET_MQ_Handle *mq,
431 const struct GNUNET_MQ_Envelope *ev)
433 struct GNUNET_MQ_Envelope *env;
436 msize = ntohs(ev->mh->size);
437 env = GNUNET_malloc(sizeof(struct GNUNET_MQ_Envelope) + msize);
438 env->mh = (struct GNUNET_MessageHeader *)&env[1];
439 env->sent_cb = ev->sent_cb;
440 env->sent_cls = ev->sent_cls;
441 GNUNET_memcpy(&env[1], ev->mh, msize);
442 GNUNET_MQ_send(mq, env);
447 * Task run to call the send implementation for the next queued
448 * message, if any. Only useful for implementing message queues,
449 * results in undefined behavior if not used carefully.
451 * @param cls message queue to send the next message with
454 impl_send_continue(void *cls)
456 struct GNUNET_MQ_Handle *mq = cls;
458 mq->send_task = NULL;
459 /* call is only valid if we're actually currently sending
461 if (NULL == mq->envelope_head)
463 mq->current_envelope = mq->envelope_head;
464 GNUNET_CONTAINER_DLL_remove(mq->envelope_head,
466 mq->current_envelope);
468 LOG(GNUNET_ERROR_TYPE_DEBUG,
469 "sending message of type %u from queue\n",
470 ntohs(mq->current_envelope->mh->type));
472 mq->send_impl(mq, mq->current_envelope->mh, mq->impl_state);
477 * Call the send implementation for the next queued message, if any.
478 * Only useful for implementing message queues, results in undefined
479 * behavior if not used carefully.
481 * @param mq message queue to send the next message with
484 GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
486 struct GNUNET_MQ_Envelope *current_envelope;
487 GNUNET_SCHEDULER_TaskCallback cb;
489 GNUNET_assert(0 < mq->queue_length);
491 mq->in_flight = GNUNET_NO;
492 current_envelope = mq->current_envelope;
493 current_envelope->parent_queue = NULL;
494 mq->current_envelope = NULL;
495 GNUNET_assert(NULL == mq->send_task);
496 mq->send_task = GNUNET_SCHEDULER_add_now(&impl_send_continue, mq);
497 if (NULL != (cb = current_envelope->sent_cb))
499 current_envelope->sent_cb = NULL;
500 cb(current_envelope->sent_cls);
502 GNUNET_free(current_envelope);
507 * Call the send notification for the current message, but do not
508 * try to send the next message until #GNUNET_MQ_impl_send_continue
511 * Only useful for implementing message queues, results in undefined
512 * behavior if not used carefully.
514 * @param mq message queue to send the next message with
517 GNUNET_MQ_impl_send_in_flight(struct GNUNET_MQ_Handle *mq)
519 struct GNUNET_MQ_Envelope *current_envelope;
520 GNUNET_SCHEDULER_TaskCallback cb;
522 mq->in_flight = GNUNET_YES;
523 /* call is only valid if we're actually currently sending
525 current_envelope = mq->current_envelope;
526 GNUNET_assert(NULL != current_envelope);
527 /* can't call cancel from now on anymore */
528 current_envelope->parent_queue = NULL;
529 if (NULL != (cb = current_envelope->sent_cb))
531 current_envelope->sent_cb = NULL;
532 cb(current_envelope->sent_cls);
538 * Create a message queue for the specified handlers.
540 * @param send function the implements sending messages
541 * @param destroy function that implements destroying the queue
542 * @param cancel function that implements canceling a message
543 * @param impl_state for the queue, passed to 'send' and 'destroy'
544 * @param handlers array of message handlers
545 * @param error_handler handler for read and write errors
546 * @param error_handler_cls closure for @a error_handler
547 * @return a new message queue
549 struct GNUNET_MQ_Handle *
550 GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send,
551 GNUNET_MQ_DestroyImpl destroy,
552 GNUNET_MQ_CancelImpl cancel,
554 const struct GNUNET_MQ_MessageHandler *handlers,
555 GNUNET_MQ_ErrorHandler error_handler,
556 void *error_handler_cls)
558 struct GNUNET_MQ_Handle *mq;
560 mq = GNUNET_new(struct GNUNET_MQ_Handle);
561 mq->send_impl = send;
562 mq->destroy_impl = destroy;
563 mq->cancel_impl = cancel;
564 mq->handlers = GNUNET_MQ_copy_handlers(handlers);
565 mq->error_handler = error_handler;
566 mq->error_handler_cls = error_handler_cls;
567 mq->impl_state = impl_state;
574 * Change the closure argument in all of the `handlers` of the
577 * @param mq to modify
578 * @param handlers_cls new closure to use
581 GNUNET_MQ_set_handlers_closure(struct GNUNET_MQ_Handle *mq, void *handlers_cls)
583 if (NULL == mq->handlers)
585 for (unsigned int i = 0; NULL != mq->handlers[i].cb; i++)
586 mq->handlers[i].cls = handlers_cls;
591 * Get the message that should currently be sent.
592 * Fails if there is no current message.
593 * Only useful for implementing message queues,
594 * results in undefined behavior if not used carefully.
596 * @param mq message queue with the current message
597 * @return message to send, never NULL
599 const struct GNUNET_MessageHeader *
600 GNUNET_MQ_impl_current(struct GNUNET_MQ_Handle *mq)
602 GNUNET_assert(NULL != mq->current_envelope);
603 GNUNET_assert(NULL != mq->current_envelope->mh);
604 return mq->current_envelope->mh;
609 * Get the implementation state associated with the
612 * While the GNUNET_MQ_Impl* callbacks receive the
613 * implementation state, continuations that are scheduled
614 * by the implementation function often only have one closure
615 * argument, with this function it is possible to get at the
616 * implementation state when only passing the GNUNET_MQ_Handle
619 * @param mq message queue with the current message
620 * @return message to send, never NULL
623 GNUNET_MQ_impl_state(struct GNUNET_MQ_Handle *mq)
625 return mq->impl_state;
629 struct GNUNET_MQ_Envelope *
630 GNUNET_MQ_msg_(struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
632 struct GNUNET_MQ_Envelope *ev;
634 ev = GNUNET_malloc(size + sizeof(struct GNUNET_MQ_Envelope));
635 ev->mh = (struct GNUNET_MessageHeader *)&ev[1];
636 ev->mh->size = htons(size);
637 ev->mh->type = htons(type);
645 * Create a new envelope by copying an existing message.
647 * @param hdr header of the message to copy
648 * @return envelope containing @a hdr
650 struct GNUNET_MQ_Envelope *
651 GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr)
653 struct GNUNET_MQ_Envelope *mqm;
654 uint16_t size = ntohs(hdr->size);
656 mqm = GNUNET_malloc(sizeof(*mqm) + size);
657 mqm->mh = (struct GNUNET_MessageHeader *)&mqm[1];
658 GNUNET_memcpy(mqm->mh, hdr, size);
664 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
666 * @param mhp pointer to the message header pointer that will be changed to allocate at
667 * the newly allocated space for the message.
668 * @param base_size size of the data before the nested message
669 * @param type type of the message in the envelope
670 * @param nested_mh the message to append to the message after base_size
672 struct GNUNET_MQ_Envelope *
673 GNUNET_MQ_msg_nested_mh_(struct GNUNET_MessageHeader **mhp,
676 const struct GNUNET_MessageHeader *nested_mh)
678 struct GNUNET_MQ_Envelope *mqm;
681 if (NULL == nested_mh)
682 return GNUNET_MQ_msg_(mhp, base_size, type);
684 size = base_size + ntohs(nested_mh->size);
686 /* check for uint16_t overflow */
687 if (size < base_size)
690 mqm = GNUNET_MQ_msg_(mhp, size, type);
691 GNUNET_memcpy((char *)mqm->mh + base_size,
693 ntohs(nested_mh->size));
700 * Associate the assoc_data in mq with a unique request id.
702 * @param mq message queue, id will be unique for the queue
703 * @param assoc_data to associate
706 GNUNET_MQ_assoc_add(struct GNUNET_MQ_Handle *mq, void *assoc_data)
710 if (NULL == mq->assoc_map)
712 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create(8);
716 GNUNET_assert(GNUNET_OK ==
717 GNUNET_CONTAINER_multihashmap32_put(
721 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
727 * Get the data associated with a @a request_id in a queue
729 * @param mq the message queue with the association
730 * @param request_id the request id we are interested in
731 * @return the associated data
734 GNUNET_MQ_assoc_get(struct GNUNET_MQ_Handle *mq, uint32_t request_id)
736 if (NULL == mq->assoc_map)
738 return GNUNET_CONTAINER_multihashmap32_get(mq->assoc_map, request_id);
743 * Remove the association for a @a request_id
745 * @param mq the message queue with the association
746 * @param request_id the request id we want to remove
747 * @return the associated data
750 GNUNET_MQ_assoc_remove(struct GNUNET_MQ_Handle *mq, uint32_t request_id)
754 if (NULL == mq->assoc_map)
756 val = GNUNET_CONTAINER_multihashmap32_get(mq->assoc_map, request_id);
757 GNUNET_CONTAINER_multihashmap32_remove_all(mq->assoc_map, request_id);
763 * Call a callback once the envelope has been sent, that is,
764 * sending it can not be canceled anymore.
765 * There can be only one notify sent callback per envelope.
767 * @param ev message to call the notify callback for
768 * @param cb the notify callback
769 * @param cb_cls closure for the callback
772 GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev,
773 GNUNET_SCHEDULER_TaskCallback cb,
776 /* allow setting *OR* clearing callback */
777 GNUNET_assert((NULL == ev->sent_cb) || (NULL == cb));
779 ev->sent_cls = cb_cls;
784 * Handle we return for callbacks registered to be
785 * notified when #GNUNET_MQ_destroy() is called on a queue.
787 struct GNUNET_MQ_DestroyNotificationHandle {
791 struct GNUNET_MQ_DestroyNotificationHandle *prev;
796 struct GNUNET_MQ_DestroyNotificationHandle *next;
799 * Queue to notify about.
801 struct GNUNET_MQ_Handle *mq;
806 GNUNET_SCHEDULER_TaskCallback cb;
816 * Destroy the message queue.
818 * @param mq message queue to destroy
821 GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
823 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
825 if (NULL != mq->destroy_impl)
827 mq->destroy_impl(mq, mq->impl_state);
829 if (NULL != mq->send_task)
831 GNUNET_SCHEDULER_cancel(mq->send_task);
832 mq->send_task = NULL;
834 while (NULL != mq->envelope_head)
836 struct GNUNET_MQ_Envelope *ev;
838 ev = mq->envelope_head;
839 ev->parent_queue = NULL;
840 GNUNET_CONTAINER_DLL_remove(mq->envelope_head, mq->envelope_tail, ev);
841 GNUNET_assert(0 < mq->queue_length);
843 LOG(GNUNET_ERROR_TYPE_DEBUG,
844 "MQ destroy drops message of type %u\n",
845 ntohs(ev->mh->type));
846 GNUNET_MQ_discard(ev);
848 if (NULL != mq->current_envelope)
850 /* we can only discard envelopes that
852 mq->current_envelope->parent_queue = NULL;
853 LOG(GNUNET_ERROR_TYPE_DEBUG,
854 "MQ destroy drops current message of type %u\n",
855 ntohs(mq->current_envelope->mh->type));
856 GNUNET_MQ_discard(mq->current_envelope);
857 mq->current_envelope = NULL;
858 GNUNET_assert(0 < mq->queue_length);
861 GNUNET_assert(0 == mq->queue_length);
862 while (NULL != (dnh = mq->dnh_head))
864 dnh->cb(dnh->cb_cls);
865 GNUNET_MQ_destroy_notify_cancel(dnh);
867 if (NULL != mq->assoc_map)
869 GNUNET_CONTAINER_multihashmap32_destroy(mq->assoc_map);
870 mq->assoc_map = NULL;
872 GNUNET_free_non_null(mq->handlers);
877 const struct GNUNET_MessageHeader *
878 GNUNET_MQ_extract_nested_mh_(const struct GNUNET_MessageHeader *mh,
882 uint16_t nested_size;
883 const struct GNUNET_MessageHeader *nested_msg;
885 whole_size = ntohs(mh->size);
886 GNUNET_assert(whole_size >= base_size);
887 nested_size = whole_size - base_size;
888 if (0 == nested_size)
890 if (nested_size < sizeof(struct GNUNET_MessageHeader))
895 nested_msg = (const struct GNUNET_MessageHeader *)((char *)mh + base_size);
896 if (ntohs(nested_msg->size) != nested_size)
906 * Cancel sending the message. Message must have been sent with
907 * #GNUNET_MQ_send before. May not be called after the notify sent
908 * callback has been called
910 * @param ev queued envelope to cancel
913 GNUNET_MQ_send_cancel(struct GNUNET_MQ_Envelope *ev)
915 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
917 GNUNET_assert(NULL != mq);
918 GNUNET_assert(NULL != mq->cancel_impl);
920 mq->evacuate_called = GNUNET_NO;
922 if (mq->current_envelope == ev)
924 /* complex case, we already started with transmitting
925 the message using the callbacks. */
926 GNUNET_assert(GNUNET_NO == mq->in_flight);
927 GNUNET_assert(0 < mq->queue_length);
929 mq->cancel_impl(mq, mq->impl_state);
930 /* continue sending the next message, if any */
931 mq->current_envelope = mq->envelope_head;
932 if (NULL != mq->current_envelope)
934 GNUNET_CONTAINER_DLL_remove(mq->envelope_head,
936 mq->current_envelope);
938 LOG(GNUNET_ERROR_TYPE_DEBUG,
939 "sending canceled message of type %u queue\n",
940 ntohs(ev->mh->type));
942 mq->send_impl(mq, mq->current_envelope->mh, mq->impl_state);
947 /* simple case, message is still waiting in the queue */
948 GNUNET_CONTAINER_DLL_remove(mq->envelope_head, mq->envelope_tail, ev);
949 GNUNET_assert(0 < mq->queue_length);
953 if (GNUNET_YES != mq->evacuate_called)
955 ev->parent_queue = NULL;
964 * Function to obtain the current envelope
965 * from within #GNUNET_MQ_SendImpl implementations.
967 * @param mq message queue to interrogate
968 * @return the current envelope
970 struct GNUNET_MQ_Envelope *
971 GNUNET_MQ_get_current_envelope(struct GNUNET_MQ_Handle *mq)
973 return mq->current_envelope;
978 * Function to obtain the last envelope in the queue.
980 * @param mq message queue to interrogate
981 * @return the last envelope in the queue
983 struct GNUNET_MQ_Envelope *
984 GNUNET_MQ_get_last_envelope(struct GNUNET_MQ_Handle *mq)
986 if (NULL != mq->envelope_tail)
987 return mq->envelope_tail;
989 return mq->current_envelope;
994 * Set application-specific preferences for this envelope.
995 * Overrides the options set for the queue with
996 * #GNUNET_MQ_set_options() for this message only.
998 * @param env message to set options for
999 * @param pp priorities and preferences to apply
1002 GNUNET_MQ_env_set_options(struct GNUNET_MQ_Envelope *env,
1003 enum GNUNET_MQ_PriorityPreferences pp)
1006 env->have_custom_options = GNUNET_YES;
1011 * Get application-specific options for this envelope.
1013 * @param env message to set options for
1014 * @return priorities and preferences to apply for @a env
1016 enum GNUNET_MQ_PriorityPreferences
1017 GNUNET_MQ_env_get_options(struct GNUNET_MQ_Envelope *env)
1019 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1021 if (GNUNET_YES == env->have_custom_options)
1022 return env->priority;
1025 return mq->priority;
1030 * Combine performance preferences set for different
1031 * envelopes that are being combined into one larger envelope.
1033 * @param p1 one set of preferences
1034 * @param p2 second set of preferences
1035 * @return combined priority and preferences to use
1037 enum GNUNET_MQ_PriorityPreferences
1038 GNUNET_MQ_env_combine_options(enum GNUNET_MQ_PriorityPreferences p1,
1039 enum GNUNET_MQ_PriorityPreferences p2)
1041 enum GNUNET_MQ_PriorityPreferences ret;
1043 ret = GNUNET_MAX(p1 & GNUNET_MQ_PRIORITY_MASK, p2 & GNUNET_MQ_PRIORITY_MASK);
1044 ret |= ((p1 & GNUNET_MQ_PREF_UNRELIABLE) & (p2 & GNUNET_MQ_PREF_UNRELIABLE));
1046 ((p1 & GNUNET_MQ_PREF_LOW_LATENCY) | (p2 & GNUNET_MQ_PREF_LOW_LATENCY));
1048 ((p1 & GNUNET_MQ_PREF_CORK_ALLOWED) & (p2 & GNUNET_MQ_PREF_CORK_ALLOWED));
1049 ret |= ((p1 & GNUNET_MQ_PREF_GOODPUT) & (p2 & GNUNET_MQ_PREF_GOODPUT));
1051 ((p1 & GNUNET_MQ_PREF_OUT_OF_ORDER) & (p2 & GNUNET_MQ_PREF_OUT_OF_ORDER));
1057 * Set application-specific default options for this queue.
1059 * @param mq message queue to set options for
1060 * @param pp priorities and preferences to apply
1063 GNUNET_MQ_set_options(struct GNUNET_MQ_Handle *mq,
1064 enum GNUNET_MQ_PriorityPreferences pp)
1071 * Obtain message contained in envelope.
1073 * @param env the envelope
1074 * @return message contained in the envelope
1076 const struct GNUNET_MessageHeader *
1077 GNUNET_MQ_env_get_msg(const struct GNUNET_MQ_Envelope *env)
1084 * Return next envelope in queue.
1086 * @param env a queued envelope
1087 * @return next one, or NULL
1089 const struct GNUNET_MQ_Envelope *
1090 GNUNET_MQ_env_next(const struct GNUNET_MQ_Envelope *env)
1097 * Register function to be called whenever @a mq is being
1100 * @param mq message queue to watch
1101 * @param cb function to call on @a mq destruction
1102 * @param cb_cls closure for @a cb
1103 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1105 struct GNUNET_MQ_DestroyNotificationHandle *
1106 GNUNET_MQ_destroy_notify(struct GNUNET_MQ_Handle *mq,
1107 GNUNET_SCHEDULER_TaskCallback cb,
1110 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1112 dnh = GNUNET_new(struct GNUNET_MQ_DestroyNotificationHandle);
1115 dnh->cb_cls = cb_cls;
1116 GNUNET_CONTAINER_DLL_insert(mq->dnh_head, mq->dnh_tail, dnh);
1122 * Cancel registration from #GNUNET_MQ_destroy_notify().
1124 * @param dnh handle for registration to cancel
1127 GNUNET_MQ_destroy_notify_cancel(
1128 struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1130 struct GNUNET_MQ_Handle *mq = dnh->mq;
1132 GNUNET_CONTAINER_DLL_remove(mq->dnh_head, mq->dnh_tail, dnh);
1138 * Insert @a env into the envelope DLL starting at @a env_head
1139 * Note that @a env must not be in any MQ while this function
1140 * is used with DLLs defined outside of the MQ module. This
1141 * is just in case some application needs to also manage a
1142 * FIFO of envelopes independent of MQ itself and wants to
1143 * re-use the pointers internal to @a env. Use with caution.
1145 * @param[in|out] env_head of envelope DLL
1146 * @param[in|out] env_tail tail of envelope DLL
1147 * @param[in|out] env element to insert at the tail
1150 GNUNET_MQ_dll_insert_head(struct GNUNET_MQ_Envelope **env_head,
1151 struct GNUNET_MQ_Envelope **env_tail,
1152 struct GNUNET_MQ_Envelope *env)
1154 GNUNET_CONTAINER_DLL_insert(*env_head, *env_tail, env);
1159 * Insert @a env into the envelope DLL starting at @a env_head
1160 * Note that @a env must not be in any MQ while this function
1161 * is used with DLLs defined outside of the MQ module. This
1162 * is just in case some application needs to also manage a
1163 * FIFO of envelopes independent of MQ itself and wants to
1164 * re-use the pointers internal to @a env. Use with caution.
1166 * @param[in|out] env_head of envelope DLL
1167 * @param[in|out] env_tail tail of envelope DLL
1168 * @param[in|out] env element to insert at the tail
1171 GNUNET_MQ_dll_insert_tail(struct GNUNET_MQ_Envelope **env_head,
1172 struct GNUNET_MQ_Envelope **env_tail,
1173 struct GNUNET_MQ_Envelope *env)
1175 GNUNET_CONTAINER_DLL_insert_tail(*env_head, *env_tail, env);
1180 * Remove @a env from the envelope DLL starting at @a env_head.
1181 * Note that @a env must not be in any MQ while this function
1182 * is used with DLLs defined outside of the MQ module. This
1183 * is just in case some application needs to also manage a
1184 * FIFO of envelopes independent of MQ itself and wants to
1185 * re-use the pointers internal to @a env. Use with caution.
1187 * @param[in|out] env_head of envelope DLL
1188 * @param[in|out] env_tail tail of envelope DLL
1189 * @param[in|out] env element to remove from the DLL
1192 GNUNET_MQ_dll_remove(struct GNUNET_MQ_Envelope **env_head,
1193 struct GNUNET_MQ_Envelope **env_tail,
1194 struct GNUNET_MQ_Envelope *env)
1196 GNUNET_CONTAINER_DLL_remove(*env_head, *env_tail, env);
1201 * Copy an array of handlers.
1203 * Useful if the array has been delared in local memory and needs to be
1204 * persisted for future use.
1206 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1207 * @return A newly allocated array of handlers.
1208 * Needs to be freed with #GNUNET_free.
1210 struct GNUNET_MQ_MessageHandler *
1211 GNUNET_MQ_copy_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
1213 struct GNUNET_MQ_MessageHandler *copy;
1216 if (NULL == handlers)
1219 count = GNUNET_MQ_count_handlers(handlers);
1220 copy = GNUNET_new_array(count + 1, struct GNUNET_MQ_MessageHandler);
1223 count * sizeof(struct GNUNET_MQ_MessageHandler));
1229 * Copy an array of handlers, appending AGPL handler.
1231 * Useful if the array has been delared in local memory and needs to be
1232 * persisted for future use.
1234 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1235 * @param agpl_handler function to call for AGPL handling
1236 * @param agpl_cls closure for @a agpl_handler
1237 * @return A newly allocated array of handlers.
1238 * Needs to be freed with #GNUNET_free.
1240 struct GNUNET_MQ_MessageHandler *
1241 GNUNET_MQ_copy_handlers2(const struct GNUNET_MQ_MessageHandler *handlers,
1242 GNUNET_MQ_MessageCallback agpl_handler,
1245 struct GNUNET_MQ_MessageHandler *copy;
1248 if (NULL == handlers)
1250 count = GNUNET_MQ_count_handlers(handlers);
1251 copy = GNUNET_new_array(count + 2, struct GNUNET_MQ_MessageHandler);
1254 count * sizeof(struct GNUNET_MQ_MessageHandler));
1255 copy[count].mv = NULL;
1256 copy[count].cb = agpl_handler;
1257 copy[count].cls = agpl_cls;
1258 copy[count].type = GNUNET_MESSAGE_TYPE_REQUEST_AGPL;
1259 copy[count].expected_size = sizeof(struct GNUNET_MessageHeader);
1265 * Count the handlers in a handler array.
1267 * @param handlers Array of handlers to be counted.
1268 * @return The number of handlers in the array.
1271 GNUNET_MQ_count_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
1275 if (NULL == handlers)
1278 for (i = 0; NULL != handlers[i].cb; i++)
1286 * Convert an `enum GNUNET_MQ_PreferenceType` to a string
1288 * @param type the preference type
1289 * @return a string or NULL if invalid
1292 GNUNET_MQ_preference_to_string(enum GNUNET_MQ_PreferenceKind type)
1296 case GNUNET_MQ_PREFERENCE_NONE:
1299 case GNUNET_MQ_PREFERENCE_BANDWIDTH:
1302 case GNUNET_MQ_PREFERENCE_LATENCY:
1305 case GNUNET_MQ_PREFERENCE_RELIABILITY:
1306 return "RELIABILITY";