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
35 * Messages are stored in a linked list.
36 * Each queue has its own list of envelopes.
38 struct GNUNET_MQ_Envelope *next;
41 * Messages are stored in a linked list
42 * Each queue has its own list of envelopes.
44 struct GNUNET_MQ_Envelope *prev;
47 * Actual allocated message header.
48 * The GNUNET_MQ_Envelope header is allocated at
49 * the end of the message.
51 struct GNUNET_MessageHeader *mh;
54 * Queue the message is queued in, NULL if message is not queued.
56 struct GNUNET_MQ_Handle *parent_queue;
59 * Called after the message was sent irrevocably.
61 GNUNET_SCHEDULER_TaskCallback sent_cb;
64 * Closure for @e send_cb
69 * Flags that were set for this envelope by
70 * #GNUNET_MQ_env_set_options(). Only valid if
71 * @e have_custom_options is set.
73 enum GNUNET_MQ_PriorityPreferences priority;
76 * Did the application call #GNUNET_MQ_env_set_options()?
78 int have_custom_options;
83 * Handle to a message queue.
85 struct GNUNET_MQ_Handle
88 * Handlers array, or NULL if the queue should not receive messages
90 struct GNUNET_MQ_MessageHandler *handlers;
93 * Actual implementation of message sending,
94 * called when a message is added
96 GNUNET_MQ_SendImpl send_impl;
99 * Implementation-dependent queue destruction function
101 GNUNET_MQ_DestroyImpl destroy_impl;
104 * Implementation-dependent send cancel function
106 GNUNET_MQ_CancelImpl cancel_impl;
109 * Implementation-specific state
114 * Callback will be called when an error occurs.
116 GNUNET_MQ_ErrorHandler error_handler;
119 * Closure for the error handler.
121 void *error_handler_cls;
124 * Task to asynchronously run #impl_send_continue().
126 struct GNUNET_SCHEDULER_Task *send_task;
129 * Linked list of messages pending to be sent
131 struct GNUNET_MQ_Envelope *envelope_head;
134 * Linked list of messages pending to be sent
136 struct GNUNET_MQ_Envelope *envelope_tail;
139 * Message that is currently scheduled to be
140 * sent. Not the head of the message queue, as the implementation
141 * needs to know if sending has been already scheduled or not.
143 struct GNUNET_MQ_Envelope *current_envelope;
146 * Map of associations, lazily allocated
148 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
151 * Functions to call on queue destruction; kept in a DLL.
153 struct GNUNET_MQ_DestroyNotificationHandle *dnh_head;
156 * Functions to call on queue destruction; kept in a DLL.
158 struct GNUNET_MQ_DestroyNotificationHandle *dnh_tail;
161 * Flags that were set for this queue by
162 * #GNUNET_MQ_set_options(). Default is 0.
164 enum GNUNET_MQ_PriorityPreferences priority;
167 * Next id that should be used for the @e assoc_map,
168 * initialized lazily to a random value together with
174 * Number of entries we have in the envelope-DLL.
176 unsigned int queue_length;
179 * #GNUNET_YES if GNUNET_MQ_impl_evacuate was called.
180 * FIXME: is this dead?
185 * #GNUNET_YES if GNUNET_MQ_impl_send_in_flight() was called.
192 * Call the message message handler that was registered
193 * for the type of the given message in the given message queue.
195 * This function is indended to be used for the implementation
198 * @param mq message queue with the handlers
199 * @param mh message to dispatch
202 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
203 const struct GNUNET_MessageHeader *mh)
207 ret = GNUNET_MQ_handle_message (mq->handlers, mh);
208 if (GNUNET_SYSERR == ret)
210 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_MALFORMED);
217 * Call the message message handler that was registered
218 * for the type of the given message in the given @a handlers list.
220 * This function is indended to be used for the implementation
223 * @param handlers a set of handlers
224 * @param mh message to dispatch
225 * @return #GNUNET_OK on success, #GNUNET_NO if no handler matched,
226 * #GNUNET_SYSERR if message was rejected by check function
229 GNUNET_MQ_handle_message (const struct GNUNET_MQ_MessageHandler *handlers,
230 const struct GNUNET_MessageHeader *mh)
232 const struct GNUNET_MQ_MessageHandler *handler;
233 int handled = GNUNET_NO;
234 uint16_t msize = ntohs (mh->size);
235 uint16_t mtype = ntohs (mh->type);
237 LOG (GNUNET_ERROR_TYPE_DEBUG,
238 "Received message of type %u and size %u\n",
242 if (NULL == handlers)
244 for (handler = handlers; NULL != handler->cb; handler++)
246 if (handler->type == mtype)
248 handled = GNUNET_YES;
249 if ((handler->expected_size > msize) ||
250 ((handler->expected_size != msize) && (NULL == handler->mv)))
252 /* Too small, or not an exact size and
253 no 'mv' handler to check rest */
254 LOG (GNUNET_ERROR_TYPE_ERROR,
255 "Received malformed message of type %u\n",
256 (unsigned int) handler->type);
257 return GNUNET_SYSERR;
259 if ((NULL == handler->mv) ||
260 (GNUNET_OK == handler->mv (handler->cls, mh)))
262 /* message well-formed, pass to handler */
263 handler->cb (handler->cls, mh);
267 /* Message rejected by check routine */
268 LOG (GNUNET_ERROR_TYPE_ERROR,
269 "Received malformed message of type %u\n",
270 (unsigned int) handler->type);
271 return GNUNET_SYSERR;
277 if (GNUNET_NO == handled)
279 LOG (GNUNET_ERROR_TYPE_INFO,
280 "No handler for message of type %u and size %u\n",
290 * Call the error handler of a message queue with the given
291 * error code. If there is no error handler, log a warning.
293 * This function is intended to be used by the implementation
296 * @param mq message queue
297 * @param error the error type
300 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_Error error)
302 if (NULL == mq->error_handler)
304 LOG (GNUNET_ERROR_TYPE_WARNING,
305 "Got error %d, but no handler installed\n",
309 mq->error_handler (mq->error_handler_cls, error);
314 * Discard the message queue message, free all
315 * allocated resources. Must be called in the event
316 * that a message is created but should not actually be sent.
318 * @param mqm the message to discard
321 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
323 GNUNET_assert (NULL == ev->parent_queue);
329 * Obtain the current length of the message queue.
331 * @param mq queue to inspect
332 * @return number of queued, non-transmitted messages
335 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
337 if (GNUNET_YES != mq->in_flight)
339 return mq->queue_length;
341 return mq->queue_length - 1;
346 * Send a message with the given message queue.
347 * May only be called once per message.
349 * @param mq message queue
350 * @param ev the envelope with the message to send.
353 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
354 struct GNUNET_MQ_Envelope *ev)
356 GNUNET_assert (NULL != mq);
357 GNUNET_assert (NULL == ev->parent_queue);
360 if (mq->queue_length >= 10000)
362 /* This would seem like a bug... */
363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364 "MQ with %u entries extended by message of type %u (FC broken?)\n",
365 (unsigned int) mq->queue_length,
366 (unsigned int) ntohs (ev->mh->type));
368 ev->parent_queue = mq;
369 /* is the implementation busy? queue it! */
370 if ((NULL != mq->current_envelope) || (NULL != mq->send_task))
372 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
377 GNUNET_assert (NULL == mq->envelope_head);
378 mq->current_envelope = ev;
380 LOG (GNUNET_ERROR_TYPE_DEBUG,
381 "sending message of type %u, queue empty (MQ: %p)\n",
382 ntohs (ev->mh->type),
392 * Remove the first envelope that has not yet been sent from the message
393 * queue and return it.
395 * @param mq queue to remove envelope from
396 * @return NULL if queue is empty (or has no envelope that is not under transmission)
398 struct GNUNET_MQ_Envelope *
399 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq)
401 struct GNUNET_MQ_Envelope *env;
403 env = mq->envelope_head;
404 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, env);
406 env->parent_queue = NULL;
412 * Function to copy an envelope. The envelope must not yet
413 * be in any queue or have any options or callbacks set.
415 * @param env envelope to copy
416 * @return copy of @a env
418 struct GNUNET_MQ_Envelope *
419 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env)
421 GNUNET_assert (NULL == env->next);
422 GNUNET_assert (NULL == env->parent_queue);
423 GNUNET_assert (NULL == env->sent_cb);
424 GNUNET_assert (GNUNET_NO == env->have_custom_options);
425 return GNUNET_MQ_msg_copy (env->mh);
430 * Send a copy of a message with the given message queue.
431 * Can be called repeatedly on the same envelope.
433 * @param mq message queue
434 * @param ev the envelope with the message to send.
437 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
438 const struct GNUNET_MQ_Envelope *ev)
440 struct GNUNET_MQ_Envelope *env;
443 msize = ntohs (ev->mh->size);
444 env = GNUNET_malloc (sizeof(struct GNUNET_MQ_Envelope) + msize);
445 env->mh = (struct GNUNET_MessageHeader *) &env[1];
446 env->sent_cb = ev->sent_cb;
447 env->sent_cls = ev->sent_cls;
448 GNUNET_memcpy (&env[1], ev->mh, msize);
449 GNUNET_MQ_send (mq, env);
454 * Task run to call the send implementation for the next queued
455 * message, if any. Only useful for implementing message queues,
456 * results in undefined behavior if not used carefully.
458 * @param cls message queue to send the next message with
461 impl_send_continue (void *cls)
463 struct GNUNET_MQ_Handle *mq = cls;
465 mq->send_task = NULL;
466 /* call is only valid if we're actually currently sending
468 if (NULL == mq->envelope_head)
470 mq->current_envelope = mq->envelope_head;
471 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
473 mq->current_envelope);
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "sending message of type %u from queue\n",
477 ntohs (mq->current_envelope->mh->type));
480 mq->current_envelope->mh,
486 * Call the send implementation for the next queued message, if any.
487 * Only useful for implementing message queues, results in undefined
488 * behavior if not used carefully.
490 * @param mq message queue to send the next message with
493 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
495 struct GNUNET_MQ_Envelope *current_envelope;
496 GNUNET_SCHEDULER_TaskCallback cb;
498 GNUNET_assert (0 < mq->queue_length);
500 mq->in_flight = GNUNET_NO;
501 current_envelope = mq->current_envelope;
502 current_envelope->parent_queue = NULL;
503 mq->current_envelope = NULL;
504 GNUNET_assert (NULL == mq->send_task);
505 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_continue, mq);
506 if (NULL != (cb = current_envelope->sent_cb))
508 current_envelope->sent_cb = NULL;
509 cb (current_envelope->sent_cls);
511 GNUNET_free (current_envelope);
516 * Call the send notification for the current message, but do not
517 * try to send the next message until #GNUNET_MQ_impl_send_continue
520 * Only useful for implementing message queues, results in undefined
521 * behavior if not used carefully.
523 * @param mq message queue to send the next message with
526 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
528 struct GNUNET_MQ_Envelope *current_envelope;
529 GNUNET_SCHEDULER_TaskCallback cb;
531 mq->in_flight = GNUNET_YES;
532 /* call is only valid if we're actually currently sending
534 current_envelope = mq->current_envelope;
535 GNUNET_assert (NULL != current_envelope);
536 /* can't call cancel from now on anymore */
537 current_envelope->parent_queue = NULL;
538 if (NULL != (cb = current_envelope->sent_cb))
540 current_envelope->sent_cb = NULL;
541 cb (current_envelope->sent_cls);
547 * Create a message queue for the specified handlers.
549 * @param send function the implements sending messages
550 * @param destroy function that implements destroying the queue
551 * @param cancel function that implements canceling a message
552 * @param impl_state for the queue, passed to 'send' and 'destroy'
553 * @param handlers array of message handlers
554 * @param error_handler handler for read and write errors
555 * @param error_handler_cls closure for @a error_handler
556 * @return a new message queue
558 struct GNUNET_MQ_Handle *
559 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
560 GNUNET_MQ_DestroyImpl destroy,
561 GNUNET_MQ_CancelImpl cancel,
563 const struct GNUNET_MQ_MessageHandler *handlers,
564 GNUNET_MQ_ErrorHandler error_handler,
565 void *error_handler_cls)
567 struct GNUNET_MQ_Handle *mq;
569 mq = GNUNET_new (struct GNUNET_MQ_Handle);
570 mq->send_impl = send;
571 mq->destroy_impl = destroy;
572 mq->cancel_impl = cancel;
573 mq->handlers = GNUNET_MQ_copy_handlers (handlers);
574 mq->error_handler = error_handler;
575 mq->error_handler_cls = error_handler_cls;
576 mq->impl_state = impl_state;
583 * Change the closure argument in all of the `handlers` of the
586 * @param mq to modify
587 * @param handlers_cls new closure to use
590 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq, void *handlers_cls)
592 if (NULL == mq->handlers)
594 for (unsigned int i = 0; NULL != mq->handlers[i].cb; i++)
595 mq->handlers[i].cls = handlers_cls;
600 * Get the message that should currently be sent.
601 * Fails if there is no current message.
602 * Only useful for implementing message queues,
603 * results in undefined behavior if not used carefully.
605 * @param mq message queue with the current message
606 * @return message to send, never NULL
608 const struct GNUNET_MessageHeader *
609 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
611 GNUNET_assert (NULL != mq->current_envelope);
612 GNUNET_assert (NULL != mq->current_envelope->mh);
613 return mq->current_envelope->mh;
618 * Get the implementation state associated with the
621 * While the GNUNET_MQ_Impl* callbacks receive the
622 * implementation state, continuations that are scheduled
623 * by the implementation function often only have one closure
624 * argument, with this function it is possible to get at the
625 * implementation state when only passing the GNUNET_MQ_Handle
628 * @param mq message queue with the current message
629 * @return message to send, never NULL
632 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
634 return mq->impl_state;
638 struct GNUNET_MQ_Envelope *
639 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
641 struct GNUNET_MQ_Envelope *ev;
643 ev = GNUNET_malloc (size + sizeof(struct GNUNET_MQ_Envelope));
644 ev->mh = (struct GNUNET_MessageHeader *) &ev[1];
645 ev->mh->size = htons (size);
646 ev->mh->type = htons (type);
654 * Create a new envelope by copying an existing message.
656 * @param hdr header of the message to copy
657 * @return envelope containing @a hdr
659 struct GNUNET_MQ_Envelope *
660 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
662 struct GNUNET_MQ_Envelope *mqm;
663 uint16_t size = ntohs (hdr->size);
665 mqm = GNUNET_malloc (sizeof(*mqm) + size);
666 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
667 GNUNET_memcpy (mqm->mh, hdr, size);
673 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
675 * @param mhp pointer to the message header pointer that will be changed to allocate at
676 * the newly allocated space for the message.
677 * @param base_size size of the data before the nested message
678 * @param type type of the message in the envelope
679 * @param nested_mh the message to append to the message after base_size
681 struct GNUNET_MQ_Envelope *
682 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
685 const struct GNUNET_MessageHeader *nested_mh)
687 struct GNUNET_MQ_Envelope *mqm;
690 if (NULL == nested_mh)
691 return GNUNET_MQ_msg_ (mhp, base_size, type);
693 size = base_size + ntohs (nested_mh->size);
695 /* check for uint16_t overflow */
696 if (size < base_size)
699 mqm = GNUNET_MQ_msg_ (mhp, size, type);
700 GNUNET_memcpy ((char *) mqm->mh + base_size,
702 ntohs (nested_mh->size));
709 * Associate the assoc_data in mq with a unique request id.
711 * @param mq message queue, id will be unique for the queue
712 * @param assoc_data to associate
715 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data)
719 if (NULL == mq->assoc_map)
721 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
725 GNUNET_assert (GNUNET_OK ==
726 GNUNET_CONTAINER_multihashmap32_put (
730 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
736 * Get the data associated with a @a request_id in a queue
738 * @param mq the message queue with the association
739 * @param request_id the request id we are interested in
740 * @return the associated data
743 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
745 if (NULL == mq->assoc_map)
747 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
752 * Remove the association for a @a request_id
754 * @param mq the message queue with the association
755 * @param request_id the request id we want to remove
756 * @return the associated data
759 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
763 if (NULL == mq->assoc_map)
765 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
766 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map, request_id);
772 * Call a callback once the envelope has been sent, that is,
773 * sending it can not be canceled anymore.
774 * There can be only one notify sent callback per envelope.
776 * @param ev message to call the notify callback for
777 * @param cb the notify callback
778 * @param cb_cls closure for the callback
781 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
782 GNUNET_SCHEDULER_TaskCallback cb,
785 /* allow setting *OR* clearing callback */
786 GNUNET_assert ((NULL == ev->sent_cb) || (NULL == cb));
788 ev->sent_cls = cb_cls;
793 * Handle we return for callbacks registered to be
794 * notified when #GNUNET_MQ_destroy() is called on a queue.
796 struct GNUNET_MQ_DestroyNotificationHandle
801 struct GNUNET_MQ_DestroyNotificationHandle *prev;
806 struct GNUNET_MQ_DestroyNotificationHandle *next;
809 * Queue to notify about.
811 struct GNUNET_MQ_Handle *mq;
816 GNUNET_SCHEDULER_TaskCallback cb;
826 * Destroy the message queue.
828 * @param mq message queue to destroy
831 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
833 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
835 if (NULL != mq->destroy_impl)
837 mq->destroy_impl (mq, mq->impl_state);
839 if (NULL != mq->send_task)
841 GNUNET_SCHEDULER_cancel (mq->send_task);
842 mq->send_task = NULL;
844 while (NULL != mq->envelope_head)
846 struct GNUNET_MQ_Envelope *ev;
848 ev = mq->envelope_head;
849 ev->parent_queue = NULL;
850 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, ev);
851 GNUNET_assert (0 < mq->queue_length);
853 LOG (GNUNET_ERROR_TYPE_DEBUG,
854 "MQ destroy drops message of type %u\n",
855 ntohs (ev->mh->type));
856 GNUNET_MQ_discard (ev);
858 if (NULL != mq->current_envelope)
860 /* we can only discard envelopes that
862 mq->current_envelope->parent_queue = NULL;
863 LOG (GNUNET_ERROR_TYPE_DEBUG,
864 "MQ destroy drops current message of type %u\n",
865 ntohs (mq->current_envelope->mh->type));
866 GNUNET_MQ_discard (mq->current_envelope);
867 mq->current_envelope = NULL;
868 GNUNET_assert (0 < mq->queue_length);
871 GNUNET_assert (0 == mq->queue_length);
872 while (NULL != (dnh = mq->dnh_head))
874 dnh->cb (dnh->cb_cls);
875 GNUNET_MQ_destroy_notify_cancel (dnh);
877 if (NULL != mq->assoc_map)
879 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
880 mq->assoc_map = NULL;
882 GNUNET_free_non_null (mq->handlers);
887 const struct GNUNET_MessageHeader *
888 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
892 uint16_t nested_size;
893 const struct GNUNET_MessageHeader *nested_msg;
895 whole_size = ntohs (mh->size);
896 GNUNET_assert (whole_size >= base_size);
897 nested_size = whole_size - base_size;
898 if (0 == nested_size)
900 if (nested_size < sizeof(struct GNUNET_MessageHeader))
905 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
906 if (ntohs (nested_msg->size) != nested_size)
916 * Cancel sending the message. Message must have been sent with
917 * #GNUNET_MQ_send before. May not be called after the notify sent
918 * callback has been called
920 * @param ev queued envelope to cancel
923 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
925 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
927 GNUNET_assert (NULL != mq);
928 GNUNET_assert (NULL != mq->cancel_impl);
930 mq->evacuate_called = GNUNET_NO;
932 if (mq->current_envelope == ev)
934 /* complex case, we already started with transmitting
935 the message using the callbacks. */
936 GNUNET_assert (GNUNET_NO == mq->in_flight);
937 GNUNET_assert (0 < mq->queue_length);
941 /* continue sending the next message, if any */
942 mq->current_envelope = mq->envelope_head;
943 if (NULL != mq->current_envelope)
945 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
947 mq->current_envelope);
949 LOG (GNUNET_ERROR_TYPE_DEBUG,
950 "sending canceled message of type %u queue\n",
951 ntohs (ev->mh->type));
953 mq->current_envelope->mh,
959 /* simple case, message is still waiting in the queue */
960 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
963 GNUNET_assert (0 < mq->queue_length);
967 if (GNUNET_YES != mq->evacuate_called)
969 ev->parent_queue = NULL;
978 * Function to obtain the current envelope
979 * from within #GNUNET_MQ_SendImpl implementations.
981 * @param mq message queue to interrogate
982 * @return the current envelope
984 struct GNUNET_MQ_Envelope *
985 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
987 return mq->current_envelope;
992 * Function to obtain the last envelope in the queue.
994 * @param mq message queue to interrogate
995 * @return the last envelope in the queue
997 struct GNUNET_MQ_Envelope *
998 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
1000 if (NULL != mq->envelope_tail)
1001 return mq->envelope_tail;
1003 return mq->current_envelope;
1008 * Set application-specific preferences for this envelope.
1009 * Overrides the options set for the queue with
1010 * #GNUNET_MQ_set_options() for this message only.
1012 * @param env message to set options for
1013 * @param pp priorities and preferences to apply
1016 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1017 enum GNUNET_MQ_PriorityPreferences pp)
1020 env->have_custom_options = GNUNET_YES;
1025 * Get application-specific options for this envelope.
1027 * @param env message to set options for
1028 * @return priorities and preferences to apply for @a env
1030 enum GNUNET_MQ_PriorityPreferences
1031 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env)
1033 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1035 if (GNUNET_YES == env->have_custom_options)
1036 return env->priority;
1039 return mq->priority;
1044 * Combine performance preferences set for different
1045 * envelopes that are being combined into one larger envelope.
1047 * @param p1 one set of preferences
1048 * @param p2 second set of preferences
1049 * @return combined priority and preferences to use
1051 enum GNUNET_MQ_PriorityPreferences
1052 GNUNET_MQ_env_combine_options (enum GNUNET_MQ_PriorityPreferences p1,
1053 enum GNUNET_MQ_PriorityPreferences p2)
1055 enum GNUNET_MQ_PriorityPreferences ret;
1057 ret = GNUNET_MAX (p1 & GNUNET_MQ_PRIORITY_MASK, p2 & GNUNET_MQ_PRIORITY_MASK);
1058 ret |= ((p1 & GNUNET_MQ_PREF_UNRELIABLE) & (p2 & GNUNET_MQ_PREF_UNRELIABLE));
1060 ((p1 & GNUNET_MQ_PREF_LOW_LATENCY) | (p2 & GNUNET_MQ_PREF_LOW_LATENCY));
1062 ((p1 & GNUNET_MQ_PREF_CORK_ALLOWED) & (p2 & GNUNET_MQ_PREF_CORK_ALLOWED));
1063 ret |= ((p1 & GNUNET_MQ_PREF_GOODPUT) & (p2 & GNUNET_MQ_PREF_GOODPUT));
1065 ((p1 & GNUNET_MQ_PREF_OUT_OF_ORDER) & (p2 & GNUNET_MQ_PREF_OUT_OF_ORDER));
1071 * Set application-specific default options for this queue.
1073 * @param mq message queue to set options for
1074 * @param pp priorities and preferences to apply
1077 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1078 enum GNUNET_MQ_PriorityPreferences pp)
1085 * Obtain message contained in envelope.
1087 * @param env the envelope
1088 * @return message contained in the envelope
1090 const struct GNUNET_MessageHeader *
1091 GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env)
1098 * Return next envelope in queue.
1100 * @param env a queued envelope
1101 * @return next one, or NULL
1103 const struct GNUNET_MQ_Envelope *
1104 GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env)
1111 * Register function to be called whenever @a mq is being
1114 * @param mq message queue to watch
1115 * @param cb function to call on @a mq destruction
1116 * @param cb_cls closure for @a cb
1117 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1119 struct GNUNET_MQ_DestroyNotificationHandle *
1120 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1121 GNUNET_SCHEDULER_TaskCallback cb,
1124 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1126 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1129 dnh->cb_cls = cb_cls;
1130 GNUNET_CONTAINER_DLL_insert (mq->dnh_head, mq->dnh_tail, dnh);
1136 * Cancel registration from #GNUNET_MQ_destroy_notify().
1138 * @param dnh handle for registration to cancel
1141 GNUNET_MQ_destroy_notify_cancel (struct
1142 GNUNET_MQ_DestroyNotificationHandle *dnh)
1144 struct GNUNET_MQ_Handle *mq = dnh->mq;
1146 GNUNET_CONTAINER_DLL_remove (mq->dnh_head, mq->dnh_tail, dnh);
1152 * Insert @a env into the envelope DLL starting at @a env_head
1153 * Note that @a env must not be in any MQ while this function
1154 * is used with DLLs defined outside of the MQ module. This
1155 * is just in case some application needs to also manage a
1156 * FIFO of envelopes independent of MQ itself and wants to
1157 * re-use the pointers internal to @a env. Use with caution.
1159 * @param[in|out] env_head of envelope DLL
1160 * @param[in|out] env_tail tail of envelope DLL
1161 * @param[in|out] env element to insert at the tail
1164 GNUNET_MQ_dll_insert_head (struct GNUNET_MQ_Envelope **env_head,
1165 struct GNUNET_MQ_Envelope **env_tail,
1166 struct GNUNET_MQ_Envelope *env)
1168 GNUNET_CONTAINER_DLL_insert (*env_head, *env_tail, env);
1173 * Insert @a env into the envelope DLL starting at @a env_head
1174 * Note that @a env must not be in any MQ while this function
1175 * is used with DLLs defined outside of the MQ module. This
1176 * is just in case some application needs to also manage a
1177 * FIFO of envelopes independent of MQ itself and wants to
1178 * re-use the pointers internal to @a env. Use with caution.
1180 * @param[in|out] env_head of envelope DLL
1181 * @param[in|out] env_tail tail of envelope DLL
1182 * @param[in|out] env element to insert at the tail
1185 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
1186 struct GNUNET_MQ_Envelope **env_tail,
1187 struct GNUNET_MQ_Envelope *env)
1189 GNUNET_CONTAINER_DLL_insert_tail (*env_head, *env_tail, env);
1194 * Remove @a env from the envelope DLL starting at @a env_head.
1195 * Note that @a env must not be in any MQ while this function
1196 * is used with DLLs defined outside of the MQ module. This
1197 * is just in case some application needs to also manage a
1198 * FIFO of envelopes independent of MQ itself and wants to
1199 * re-use the pointers internal to @a env. Use with caution.
1201 * @param[in|out] env_head of envelope DLL
1202 * @param[in|out] env_tail tail of envelope DLL
1203 * @param[in|out] env element to remove from the DLL
1206 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
1207 struct GNUNET_MQ_Envelope **env_tail,
1208 struct GNUNET_MQ_Envelope *env)
1210 GNUNET_CONTAINER_DLL_remove (*env_head, *env_tail, env);
1215 * Copy an array of handlers.
1217 * Useful if the array has been delared in local memory and needs to be
1218 * persisted for future use.
1220 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1221 * @return A newly allocated array of handlers.
1222 * Needs to be freed with #GNUNET_free.
1224 struct GNUNET_MQ_MessageHandler *
1225 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1227 struct GNUNET_MQ_MessageHandler *copy;
1230 if (NULL == handlers)
1233 count = GNUNET_MQ_count_handlers (handlers);
1234 copy = GNUNET_new_array (count + 1, struct GNUNET_MQ_MessageHandler);
1235 GNUNET_memcpy (copy,
1237 count * sizeof(struct GNUNET_MQ_MessageHandler));
1243 * Copy an array of handlers, appending AGPL handler.
1245 * Useful if the array has been delared in local memory and needs to be
1246 * persisted for future use.
1248 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1249 * @param agpl_handler function to call for AGPL handling
1250 * @param agpl_cls closure for @a agpl_handler
1251 * @return A newly allocated array of handlers.
1252 * Needs to be freed with #GNUNET_free.
1254 struct GNUNET_MQ_MessageHandler *
1255 GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
1256 GNUNET_MQ_MessageCallback agpl_handler,
1259 struct GNUNET_MQ_MessageHandler *copy;
1262 if (NULL == handlers)
1264 count = GNUNET_MQ_count_handlers (handlers);
1265 copy = GNUNET_new_array (count + 2, struct GNUNET_MQ_MessageHandler);
1266 GNUNET_memcpy (copy,
1268 count * sizeof(struct GNUNET_MQ_MessageHandler));
1269 copy[count].mv = NULL;
1270 copy[count].cb = agpl_handler;
1271 copy[count].cls = agpl_cls;
1272 copy[count].type = GNUNET_MESSAGE_TYPE_REQUEST_AGPL;
1273 copy[count].expected_size = sizeof(struct GNUNET_MessageHeader);
1279 * Count the handlers in a handler array.
1281 * @param handlers Array of handlers to be counted.
1282 * @return The number of handlers in the array.
1285 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1289 if (NULL == handlers)
1292 for (i = 0; NULL != handlers[i].cb; i++)
1300 * Convert an `enum GNUNET_MQ_PreferenceType` to a string
1302 * @param type the preference type
1303 * @return a string or NULL if invalid
1306 GNUNET_MQ_preference_to_string (enum GNUNET_MQ_PreferenceKind type)
1310 case GNUNET_MQ_PREFERENCE_NONE:
1313 case GNUNET_MQ_PREFERENCE_BANDWIDTH:
1316 case GNUNET_MQ_PREFERENCE_LATENCY:
1319 case GNUNET_MQ_PREFERENCE_RELIABILITY:
1320 return "RELIABILITY";