2 This file is part of GNUnet.
3 Copyright (C) 2012-2014 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
24 * @brief general purpose request queue
27 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "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 * usually points to the end of the containing GNUNET_MQ_Envelope
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_MQ_NotifyCallback sent_cb;
63 * Closure for @e send_cb
70 * Handle to a message queue.
72 struct GNUNET_MQ_Handle
75 * Handlers array, or NULL if the queue should not receive messages
77 struct GNUNET_MQ_MessageHandler *handlers;
80 * Actual implementation of message sending,
81 * called when a message is added
83 GNUNET_MQ_SendImpl send_impl;
86 * Implementation-dependent queue destruction function
88 GNUNET_MQ_DestroyImpl destroy_impl;
91 * Implementation-dependent send cancel function
93 GNUNET_MQ_CancelImpl cancel_impl;
96 * Implementation-specific state
101 * Callback will be called when an error occurs.
103 GNUNET_MQ_ErrorHandler error_handler;
106 * Closure for the error handler.
108 void *error_handler_cls;
111 * Linked list of messages pending to be sent
113 struct GNUNET_MQ_Envelope *envelope_head;
116 * Linked list of messages pending to be sent
118 struct GNUNET_MQ_Envelope *envelope_tail;
121 * Message that is currently scheduled to be
122 * sent. Not the head of the message queue, as the implementation
123 * needs to know if sending has been already scheduled or not.
125 struct GNUNET_MQ_Envelope *current_envelope;
128 * Map of associations, lazily allocated
130 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
133 * Task scheduled during #GNUNET_MQ_impl_send_continue.
135 struct GNUNET_SCHEDULER_Task *continue_task;
138 * Next id that should be used for the @e assoc_map,
139 * initialized lazily to a random value together with
145 * Number of entries we have in the envelope-DLL.
147 unsigned int queue_length;
152 * Implementation-specific state for connection to
153 * client (MQ for server).
155 struct ServerClientSocketState
158 * Handle of the client that connected to the server.
160 struct GNUNET_SERVER_Client *client;
163 * Active transmission request to the client.
165 struct GNUNET_SERVER_TransmitHandle* th;
170 * Implementation-specific state for connection to
171 * service (MQ for clients).
173 struct ClientConnectionState
176 * Did we call receive alread alreadyy?
181 * Do we also want to receive?
183 int receive_requested;
186 * Connection to the service.
188 struct GNUNET_CLIENT_Connection *connection;
191 * Active transmission request (or NULL).
193 struct GNUNET_CLIENT_TransmitHandle *th;
198 * Call the message message handler that was registered
199 * for the type of the given message in the given message queue.
201 * This function is indended to be used for the implementation
204 * @param mq message queue with the handlers
205 * @param mh message to dispatch
208 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
209 const struct GNUNET_MessageHeader *mh)
211 const struct GNUNET_MQ_MessageHandler *handler;
212 int handled = GNUNET_NO;
213 uint16_t ms = ntohs (mh->size);
215 if (NULL == mq->handlers)
217 for (handler = mq->handlers; NULL != handler->cb; handler++)
219 if (handler->type == ntohs (mh->type))
221 handled = GNUNET_YES;
222 if ( (handler->expected_size > ms) ||
223 ( (handler->expected_size != ms) &&
224 (NULL == handler->mv) ) )
226 /* Too small, or not an exact size and
227 no 'mv' handler to check rest */
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
229 "Received malformed message of type %u\n",
230 (unsigned int) handler->type);
231 GNUNET_MQ_inject_error (mq,
232 GNUNET_MQ_ERROR_MALFORMED);
235 if ( (NULL == handler->mv) ||
237 handler->mv (handler->cls, mh)) )
239 /* message well-formed, pass to handler */
240 handler->cb (handler->cls, mh);
244 /* Message rejected by check routine */
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 "Received malformed message of type %u\n",
247 (unsigned int) handler->type);
248 GNUNET_MQ_inject_error (mq,
249 GNUNET_MQ_ERROR_MALFORMED);
255 if (GNUNET_NO == handled)
256 LOG (GNUNET_ERROR_TYPE_WARNING,
257 "No handler for message of type %d\n",
263 * Call the error handler of a message queue with the given
264 * error code. If there is no error handler, log a warning.
266 * This function is intended to be used by the implementation
269 * @param mq message queue
270 * @param error the error type
273 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
274 enum GNUNET_MQ_Error error)
276 if (NULL == mq->error_handler)
278 LOG (GNUNET_ERROR_TYPE_WARNING,
279 "Got error %d, but no handler installed\n",
283 mq->error_handler (mq->error_handler_cls,
289 * Discard the message queue message, free all
290 * allocated resources. Must be called in the event
291 * that a message is created but should not actually be sent.
293 * @param mqm the message to discard
296 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm)
298 GNUNET_assert (NULL == mqm->parent_queue);
304 * Obtain the current length of the message queue.
306 * @param mq queue to inspect
307 * @return number of queued, non-transmitted messages
310 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
312 return mq->queue_length;
317 * Send a message with the give message queue.
318 * May only be called once per message.
320 * @param mq message queue
321 * @param ev the envelope with the message to send.
324 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
325 struct GNUNET_MQ_Envelope *ev)
327 GNUNET_assert (NULL != mq);
328 GNUNET_assert (NULL == ev->parent_queue);
330 ev->parent_queue = mq;
331 /* is the implementation busy? queue it! */
332 if (NULL != mq->current_envelope)
334 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
340 mq->current_envelope = ev;
341 mq->send_impl (mq, ev->mh, mq->impl_state);
346 * Send a copy of a message with the give message queue.
347 * Can be called repeatedly on the same envelope.
349 * @param mq message queue
350 * @param ev the envelope with the message to send.
353 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
354 const struct GNUNET_MQ_Envelope *ev)
356 struct GNUNET_MQ_Envelope *env;
359 msize = ntohs (ev->mh->size);
360 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
362 env->mh = (struct GNUNET_MessageHeader *) &env[1];
363 env->sent_cb = ev->sent_cb;
364 env->sent_cls = ev->sent_cls;
375 * Task run to call the send implementation for the next queued
376 * message, if any. Only useful for implementing message queues,
377 * results in undefined behavior if not used carefully.
379 * @param cls message queue to send the next message with
382 impl_send_continue (void *cls)
384 struct GNUNET_MQ_Handle *mq = cls;
385 struct GNUNET_MQ_Envelope *current_envelope;
387 mq->continue_task = NULL;
388 /* call is only valid if we're actually currently sending
390 current_envelope = mq->current_envelope;
391 GNUNET_assert (NULL != current_envelope);
392 current_envelope->parent_queue = NULL;
393 if (NULL == mq->envelope_head)
395 mq->current_envelope = NULL;
399 mq->current_envelope = mq->envelope_head;
400 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
402 mq->current_envelope);
405 mq->current_envelope->mh,
408 if (NULL != current_envelope->sent_cb)
409 current_envelope->sent_cb (current_envelope->sent_cls);
410 GNUNET_free (current_envelope);
415 * Call the send implementation for the next queued message, if any.
416 * Only useful for implementing message queues, results in undefined
417 * behavior if not used carefully.
419 * @param mq message queue to send the next message with
422 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
424 GNUNET_assert (NULL == mq->continue_task);
425 mq->continue_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
431 * Create a message queue for the specified handlers.
433 * @param send function the implements sending messages
434 * @param destroy function that implements destroying the queue
435 * @param cancel function that implements canceling a message
436 * @param impl_state for the queue, passed to 'send' and 'destroy'
437 * @param handlers array of message handlers
438 * @param error_handler handler for read and write errors
439 * @param error_handler_cls closure for @a error_handler
440 * @return a new message queue
442 struct GNUNET_MQ_Handle *
443 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
444 GNUNET_MQ_DestroyImpl destroy,
445 GNUNET_MQ_CancelImpl cancel,
447 const struct GNUNET_MQ_MessageHandler *handlers,
448 GNUNET_MQ_ErrorHandler error_handler,
449 void *error_handler_cls)
451 struct GNUNET_MQ_Handle *mq;
454 mq = GNUNET_new (struct GNUNET_MQ_Handle);
455 mq->send_impl = send;
456 mq->destroy_impl = destroy;
457 mq->cancel_impl = cancel;
458 if (NULL != handlers)
460 for (i=0;NULL != handlers[i].cb; i++) ;
461 mq->handlers = GNUNET_new_array (i + 1,
462 struct GNUNET_MQ_MessageHandler);
463 memcpy (mq->handlers,
465 i * sizeof (struct GNUNET_MQ_MessageHandler));
467 mq->error_handler = error_handler;
468 mq->error_handler_cls = error_handler_cls;
469 mq->impl_state = impl_state;
476 * Get the message that should currently be sent.
477 * Fails if there is no current message.
478 * Only useful for implementing message queues,
479 * results in undefined behavior if not used carefully.
481 * @param mq message queue with the current message
482 * @return message to send, never NULL
484 const struct GNUNET_MessageHeader *
485 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
487 if (NULL == mq->current_envelope)
489 if (NULL == mq->current_envelope->mh)
491 return mq->current_envelope->mh;
496 * Get the implementation state associated with the
499 * While the GNUNET_MQ_Impl* callbacks receive the
500 * implementation state, continuations that are scheduled
501 * by the implementation function often only have one closure
502 * argument, with this function it is possible to get at the
503 * implementation state when only passing the GNUNET_MQ_Handle
506 * @param mq message queue with the current message
507 * @return message to send, never NULL
510 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
512 return mq->impl_state;
516 struct GNUNET_MQ_Envelope *
517 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
521 struct GNUNET_MQ_Envelope *mqm;
523 mqm = GNUNET_malloc (sizeof *mqm + size);
524 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
525 mqm->mh->size = htons (size);
526 mqm->mh->type = htons (type);
534 * Create a new envelope by copying an existing message.
536 * @param hdr header of the message to copy
537 * @return envelope containing @a hdr
539 struct GNUNET_MQ_Envelope *
540 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
542 struct GNUNET_MQ_Envelope *mqm;
543 uint16_t size = ntohs (hdr->size);
545 mqm = GNUNET_malloc (sizeof (*mqm) + size);
546 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
555 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
557 * @param mhp pointer to the message header pointer that will be changed to allocate at
558 * the newly allocated space for the message.
559 * @param base_size size of the data before the nested message
560 * @param type type of the message in the envelope
561 * @param nested_mh the message to append to the message after base_size
563 struct GNUNET_MQ_Envelope *
564 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
567 const struct GNUNET_MessageHeader *nested_mh)
569 struct GNUNET_MQ_Envelope *mqm;
572 if (NULL == nested_mh)
573 return GNUNET_MQ_msg_ (mhp, base_size, type);
575 size = base_size + ntohs (nested_mh->size);
577 /* check for uint16_t overflow */
578 if (size < base_size)
581 mqm = GNUNET_MQ_msg_ (mhp, size, type);
582 memcpy ((char *) mqm->mh + base_size,
584 ntohs (nested_mh->size));
591 * Transmit a queued message to the session's client.
593 * @param cls consensus session
594 * @param size number of bytes available in @a buf
595 * @param buf where the callee should write the message
596 * @return number of bytes written to @a buf
599 transmit_queued (void *cls,
603 struct GNUNET_MQ_Handle *mq = cls;
604 struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
605 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
608 GNUNET_assert (NULL != buf);
609 msg_size = ntohs (msg->size);
610 GNUNET_assert (size >= msg_size);
611 memcpy (buf, msg, msg_size);
614 GNUNET_MQ_impl_send_continue (mq);
621 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
624 struct ServerClientSocketState *state = impl_state;
626 if (NULL != state->th)
628 GNUNET_SERVER_notify_transmit_ready_cancel (state->th);
632 GNUNET_assert (NULL != mq);
633 GNUNET_assert (NULL != state);
634 GNUNET_SERVER_client_drop (state->client);
640 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
641 const struct GNUNET_MessageHeader *msg,
644 struct ServerClientSocketState *state = impl_state;
646 GNUNET_assert (NULL != mq);
647 GNUNET_assert (NULL != state);
648 state->th = GNUNET_SERVER_notify_transmit_ready (state->client,
650 GNUNET_TIME_UNIT_FOREVER_REL,
651 &transmit_queued, mq);
655 struct GNUNET_MQ_Handle *
656 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
658 struct GNUNET_MQ_Handle *mq;
659 struct ServerClientSocketState *scss;
661 mq = GNUNET_new (struct GNUNET_MQ_Handle);
662 scss = GNUNET_new (struct ServerClientSocketState);
663 mq->impl_state = scss;
664 scss->client = client;
665 GNUNET_SERVER_client_keep (client);
666 mq->send_impl = server_client_send_impl;
667 mq->destroy_impl = server_client_destroy_impl;
673 * Type of a function to call when we receive a message
677 * @param msg message received, NULL on timeout or fatal error
680 handle_client_message (void *cls,
681 const struct GNUNET_MessageHeader *msg)
683 struct GNUNET_MQ_Handle *mq = cls;
684 struct ClientConnectionState *state;
686 state = mq->impl_state;
689 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
692 GNUNET_CLIENT_receive (state->connection,
693 &handle_client_message,
695 GNUNET_TIME_UNIT_FOREVER_REL);
696 GNUNET_MQ_inject_message (mq, msg);
701 * Transmit a queued message to the session's client.
703 * @param cls consensus session
704 * @param size number of bytes available in @a buf
705 * @param buf where the callee should write the message
706 * @return number of bytes written to buf
709 connection_client_transmit_queued (void *cls,
713 struct GNUNET_MQ_Handle *mq = cls;
714 const struct GNUNET_MessageHeader *msg;
715 struct ClientConnectionState *state = mq->impl_state;
718 GNUNET_assert (NULL != mq);
720 msg = GNUNET_MQ_impl_current (mq);
724 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
728 if ( (GNUNET_YES == state->receive_requested) &&
729 (GNUNET_NO == state->receive_active) )
731 state->receive_active = GNUNET_YES;
732 GNUNET_CLIENT_receive (state->connection,
733 &handle_client_message,
735 GNUNET_TIME_UNIT_FOREVER_REL);
738 msg_size = ntohs (msg->size);
739 GNUNET_assert (size >= msg_size);
740 memcpy (buf, msg, msg_size);
743 GNUNET_MQ_impl_send_continue (mq);
750 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
753 struct ClientConnectionState *state = impl_state;
755 if (NULL != state->th)
757 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
760 GNUNET_CLIENT_disconnect (state->connection);
761 GNUNET_free (impl_state);
766 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
767 const struct GNUNET_MessageHeader *msg,
770 struct ClientConnectionState *state = impl_state;
772 GNUNET_assert (NULL != state);
773 GNUNET_assert (NULL == state->th);
775 GNUNET_CLIENT_notify_transmit_ready (state->connection,
777 GNUNET_TIME_UNIT_FOREVER_REL,
779 &connection_client_transmit_queued,
781 GNUNET_assert (NULL != state->th);
786 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
789 struct ClientConnectionState *state = impl_state;
791 GNUNET_assert (NULL != state->th);
792 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
797 struct GNUNET_MQ_Handle *
798 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
799 const struct GNUNET_MQ_MessageHandler *handlers,
800 GNUNET_MQ_ErrorHandler error_handler,
801 void *error_handler_cls)
803 struct GNUNET_MQ_Handle *mq;
804 struct ClientConnectionState *state;
807 mq = GNUNET_new (struct GNUNET_MQ_Handle);
808 if (NULL != handlers)
810 for (i=0;NULL != handlers[i].cb; i++) ;
811 mq->handlers = GNUNET_new_array (i,
812 struct GNUNET_MQ_MessageHandler);
813 memcpy (mq->handlers,
815 i * sizeof (struct GNUNET_MQ_MessageHandler));
817 mq->error_handler = error_handler;
818 mq->error_handler_cls = error_handler_cls;
819 state = GNUNET_new (struct ClientConnectionState);
820 state->connection = connection;
821 mq->impl_state = state;
822 mq->send_impl = &connection_client_send_impl;
823 mq->destroy_impl = &connection_client_destroy_impl;
824 mq->cancel_impl = &connection_client_cancel_impl;
825 if (NULL != handlers)
826 state->receive_requested = GNUNET_YES;
833 * Associate the assoc_data in mq with a unique request id.
835 * @param mq message queue, id will be unique for the queue
836 * @param assoc_data to associate
839 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
844 if (NULL == mq->assoc_map)
846 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
850 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
851 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
857 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
860 if (NULL == mq->assoc_map)
862 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
867 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
872 if (NULL == mq->assoc_map)
874 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
876 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
883 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
884 GNUNET_MQ_NotifyCallback cb,
893 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
895 if (NULL != mq->destroy_impl)
897 mq->destroy_impl (mq, mq->impl_state);
899 if (NULL != mq->continue_task)
901 GNUNET_SCHEDULER_cancel (mq->continue_task);
902 mq->continue_task = NULL;
904 while (NULL != mq->envelope_head)
906 struct GNUNET_MQ_Envelope *ev;
908 ev = mq->envelope_head;
909 ev->parent_queue = NULL;
910 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
914 GNUNET_MQ_discard (ev);
916 GNUNET_assert (0 == mq->queue_length);
917 if (NULL != mq->current_envelope)
919 /* we can only discard envelopes that
921 mq->current_envelope->parent_queue = NULL;
922 GNUNET_MQ_discard (mq->current_envelope);
923 mq->current_envelope = NULL;
925 if (NULL != mq->assoc_map)
927 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
928 mq->assoc_map = NULL;
930 GNUNET_free_non_null (mq->handlers);
935 const struct GNUNET_MessageHeader *
936 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
940 uint16_t nested_size;
941 const struct GNUNET_MessageHeader *nested_msg;
943 whole_size = ntohs (mh->size);
944 GNUNET_assert (whole_size >= base_size);
945 nested_size = whole_size - base_size;
946 if (0 == nested_size)
948 if (nested_size < sizeof (struct GNUNET_MessageHeader))
953 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
954 if (ntohs (nested_msg->size) != nested_size)
964 * Cancel sending the message. Message must have been sent with
965 * #GNUNET_MQ_send before. May not be called after the notify sent
966 * callback has been called
968 * @param ev queued envelope to cancel
971 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
973 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
975 GNUNET_assert (NULL != mq);
976 GNUNET_assert (NULL != mq->cancel_impl);
978 if (mq->current_envelope == ev)
980 // complex case, we already started with transmitting
984 // continue sending the next message, if any
985 if (NULL == mq->envelope_head)
987 mq->current_envelope = NULL;
991 mq->current_envelope = mq->envelope_head;
992 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
994 mq->current_envelope);
997 mq->current_envelope->mh,
1003 // simple case, message is still waiting in the queue
1004 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
1010 ev->parent_queue = NULL;