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
147 * Implementation-specific state for connection to
148 * client (MQ for server).
150 struct ServerClientSocketState
153 * Handle of the client that connected to the server.
155 struct GNUNET_SERVER_Client *client;
158 * Active transmission request to the client.
160 struct GNUNET_SERVER_TransmitHandle* th;
165 * Implementation-specific state for connection to
166 * service (MQ for clients).
168 struct ClientConnectionState
171 * Did we call receive alread alreadyy?
176 * Do we also want to receive?
178 int receive_requested;
181 * Connection to the service.
183 struct GNUNET_CLIENT_Connection *connection;
186 * Active transmission request (or NULL).
188 struct GNUNET_CLIENT_TransmitHandle *th;
193 * Call the message message handler that was registered
194 * for the type of the given message in the given message queue.
196 * This function is indended to be used for the implementation
199 * @param mq message queue with the handlers
200 * @param mh message to dispatch
203 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
204 const struct GNUNET_MessageHeader *mh)
206 const struct GNUNET_MQ_MessageHandler *handler;
207 int handled = GNUNET_NO;
208 uint16_t ms = ntohs (mh->size);
210 if (NULL == mq->handlers)
212 for (handler = mq->handlers; NULL != handler->cb; handler++)
214 if (handler->type == ntohs (mh->type))
216 handled = GNUNET_YES;
217 if ( (handler->expected_size > ms) ||
218 ( (handler->expected_size != ms) &&
219 (NULL == handler->mv) ) )
221 /* Too small, or not an exact size and
222 no 'mv' handler to check rest */
223 GNUNET_MQ_inject_error (mq,
224 GNUNET_MQ_ERROR_MALFORMED);
227 if ( (NULL == handler->mv) ||
229 handler->mv (handler->cls, mh)) )
231 /* message well-formed, pass to handler */
232 handler->cb (handler->cls, mh);
236 /* Message rejected by check routine */
237 GNUNET_MQ_inject_error (mq,
238 GNUNET_MQ_ERROR_MALFORMED);
244 if (GNUNET_NO == handled)
245 LOG (GNUNET_ERROR_TYPE_WARNING,
246 "No handler for message of type %d\n",
252 * Call the error handler of a message queue with the given
253 * error code. If there is no error handler, log a warning.
255 * This function is intended to be used by the implementation
258 * @param mq message queue
259 * @param error the error type
262 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
263 enum GNUNET_MQ_Error error)
265 if (NULL == mq->error_handler)
267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
268 "mq: got error %d, but no handler installed\n",
272 mq->error_handler (mq->error_handler_cls, error);
277 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm)
279 GNUNET_assert (NULL == mqm->parent_queue);
285 * Send a message with the give message queue.
286 * May only be called once per message.
288 * @param mq message queue
289 * @param ev the envelope with the message to send.
292 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
293 struct GNUNET_MQ_Envelope *ev)
295 GNUNET_assert (NULL != mq);
296 GNUNET_assert (NULL == ev->parent_queue);
298 ev->parent_queue = mq;
299 /* is the implementation busy? queue it! */
300 if (NULL != mq->current_envelope)
302 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
307 mq->current_envelope = ev;
308 mq->send_impl (mq, ev->mh, mq->impl_state);
313 * Task run to call the send implementation for the next queued
314 * message, if any. Only useful for implementing message queues,
315 * results in undefined behavior if not used carefully.
317 * @param cls message queue to send the next message with
320 impl_send_continue (void *cls)
322 struct GNUNET_MQ_Handle *mq = cls;
323 struct GNUNET_MQ_Envelope *current_envelope;
325 mq->continue_task = NULL;
326 /* call is only valid if we're actually currently sending
328 current_envelope = mq->current_envelope;
329 GNUNET_assert (NULL != current_envelope);
330 current_envelope->parent_queue = NULL;
331 if (NULL == mq->envelope_head)
333 mq->current_envelope = NULL;
337 mq->current_envelope = mq->envelope_head;
338 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
340 mq->current_envelope);
342 mq->current_envelope->mh,
345 if (NULL != current_envelope->sent_cb)
346 current_envelope->sent_cb (current_envelope->sent_cls);
347 GNUNET_free (current_envelope);
352 * Call the send implementation for the next queued message, if any.
353 * Only useful for implementing message queues, results in undefined
354 * behavior if not used carefully.
356 * @param mq message queue to send the next message with
359 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
361 GNUNET_assert (NULL == mq->continue_task);
362 mq->continue_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
368 * Create a message queue for the specified handlers.
370 * @param send function the implements sending messages
371 * @param destroy function that implements destroying the queue
372 * @param cancel function that implements canceling a message
373 * @param impl_state for the queue, passed to 'send' and 'destroy'
374 * @param handlers array of message handlers
375 * @param error_handler handler for read and write errors
376 * @param error_handler_cls closure for @a error_handler
377 * @return a new message queue
379 struct GNUNET_MQ_Handle *
380 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
381 GNUNET_MQ_DestroyImpl destroy,
382 GNUNET_MQ_CancelImpl cancel,
384 const struct GNUNET_MQ_MessageHandler *handlers,
385 GNUNET_MQ_ErrorHandler error_handler,
386 void *error_handler_cls)
388 struct GNUNET_MQ_Handle *mq;
391 mq = GNUNET_new (struct GNUNET_MQ_Handle);
392 mq->send_impl = send;
393 mq->destroy_impl = destroy;
394 mq->cancel_impl = cancel;
395 if (NULL != handlers)
397 for (i=0;NULL != handlers[i].cb; i++) ;
398 mq->handlers = GNUNET_new_array (i,
399 struct GNUNET_MQ_MessageHandler);
400 memcpy (mq->handlers,
402 i * sizeof (struct GNUNET_MQ_MessageHandler));
404 mq->error_handler = error_handler;
405 mq->error_handler_cls = error_handler_cls;
406 mq->impl_state = impl_state;
413 * Get the message that should currently be sent.
414 * Fails if there is no current message.
415 * Only useful for implementing message queues,
416 * results in undefined behavior if not used carefully.
418 * @param mq message queue with the current message
419 * @return message to send, never NULL
421 const struct GNUNET_MessageHeader *
422 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
424 if (NULL == mq->current_envelope)
426 if (NULL == mq->current_envelope->mh)
428 return mq->current_envelope->mh;
433 * Get the implementation state associated with the
436 * While the GNUNET_MQ_Impl* callbacks receive the
437 * implementation state, continuations that are scheduled
438 * by the implementation function often only have one closure
439 * argument, with this function it is possible to get at the
440 * implementation state when only passing the GNUNET_MQ_Handle
443 * @param mq message queue with the current message
444 * @return message to send, never NULL
447 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
449 return mq->impl_state;
453 struct GNUNET_MQ_Envelope *
454 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
458 struct GNUNET_MQ_Envelope *mqm;
460 mqm = GNUNET_malloc (sizeof *mqm + size);
461 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
462 mqm->mh->size = htons (size);
463 mqm->mh->type = htons (type);
471 * Implementation of the GNUNET_MQ_msg_nested_mh macro.
473 * @param mhp pointer to the message header pointer that will be changed to allocate at
474 * the newly allocated space for the message.
475 * @param base_size size of the data before the nested message
476 * @param type type of the message in the envelope
477 * @param nested_mh the message to append to the message after base_size
479 struct GNUNET_MQ_Envelope *
480 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
483 const struct GNUNET_MessageHeader *nested_mh)
485 struct GNUNET_MQ_Envelope *mqm;
488 if (NULL == nested_mh)
489 return GNUNET_MQ_msg_ (mhp, base_size, type);
491 size = base_size + ntohs (nested_mh->size);
493 /* check for uint16_t overflow */
494 if (size < base_size)
497 mqm = GNUNET_MQ_msg_ (mhp, size, type);
498 memcpy ((char *) mqm->mh + base_size,
500 ntohs (nested_mh->size));
507 * Transmit a queued message to the session's client.
509 * @param cls consensus session
510 * @param size number of bytes available in @a buf
511 * @param buf where the callee should write the message
512 * @return number of bytes written to @a buf
515 transmit_queued (void *cls,
519 struct GNUNET_MQ_Handle *mq = cls;
520 struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
521 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
524 GNUNET_assert (NULL != buf);
525 msg_size = ntohs (msg->size);
526 GNUNET_assert (size >= msg_size);
527 memcpy (buf, msg, msg_size);
530 GNUNET_MQ_impl_send_continue (mq);
537 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
540 struct ServerClientSocketState *state = impl_state;
542 if (NULL != state->th)
544 GNUNET_SERVER_notify_transmit_ready_cancel (state->th);
548 GNUNET_assert (NULL != mq);
549 GNUNET_assert (NULL != state);
550 GNUNET_SERVER_client_drop (state->client);
556 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
557 const struct GNUNET_MessageHeader *msg,
560 struct ServerClientSocketState *state = impl_state;
562 GNUNET_assert (NULL != mq);
563 GNUNET_assert (NULL != state);
564 state->th = GNUNET_SERVER_notify_transmit_ready (state->client,
566 GNUNET_TIME_UNIT_FOREVER_REL,
567 &transmit_queued, mq);
571 struct GNUNET_MQ_Handle *
572 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
574 struct GNUNET_MQ_Handle *mq;
575 struct ServerClientSocketState *scss;
577 mq = GNUNET_new (struct GNUNET_MQ_Handle);
578 scss = GNUNET_new (struct ServerClientSocketState);
579 mq->impl_state = scss;
580 scss->client = client;
581 GNUNET_SERVER_client_keep (client);
582 mq->send_impl = server_client_send_impl;
583 mq->destroy_impl = server_client_destroy_impl;
589 * Type of a function to call when we receive a message
593 * @param msg message received, NULL on timeout or fatal error
596 handle_client_message (void *cls,
597 const struct GNUNET_MessageHeader *msg)
599 struct GNUNET_MQ_Handle *mq = cls;
600 struct ClientConnectionState *state;
602 state = mq->impl_state;
605 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
608 GNUNET_CLIENT_receive (state->connection,
609 &handle_client_message,
611 GNUNET_TIME_UNIT_FOREVER_REL);
612 GNUNET_MQ_inject_message (mq, msg);
617 * Transmit a queued message to the session's client.
619 * @param cls consensus session
620 * @param size number of bytes available in @a buf
621 * @param buf where the callee should write the message
622 * @return number of bytes written to buf
625 connection_client_transmit_queued (void *cls,
629 struct GNUNET_MQ_Handle *mq = cls;
630 const struct GNUNET_MessageHeader *msg;
631 struct ClientConnectionState *state = mq->impl_state;
634 GNUNET_assert (NULL != mq);
636 msg = GNUNET_MQ_impl_current (mq);
640 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
644 if ( (GNUNET_YES == state->receive_requested) &&
645 (GNUNET_NO == state->receive_active) )
647 state->receive_active = GNUNET_YES;
648 GNUNET_CLIENT_receive (state->connection,
649 &handle_client_message,
651 GNUNET_TIME_UNIT_FOREVER_REL);
654 msg_size = ntohs (msg->size);
655 GNUNET_assert (size >= msg_size);
656 memcpy (buf, msg, msg_size);
659 GNUNET_MQ_impl_send_continue (mq);
666 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
669 struct ClientConnectionState *state = impl_state;
671 if (NULL != state->th)
673 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
676 GNUNET_CLIENT_disconnect (state->connection);
677 GNUNET_free (impl_state);
682 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
683 const struct GNUNET_MessageHeader *msg,
686 struct ClientConnectionState *state = impl_state;
688 GNUNET_assert (NULL != state);
689 GNUNET_assert (NULL == state->th);
691 GNUNET_CLIENT_notify_transmit_ready (state->connection,
693 GNUNET_TIME_UNIT_FOREVER_REL,
695 &connection_client_transmit_queued,
697 GNUNET_assert (NULL != state->th);
702 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
705 struct ClientConnectionState *state = impl_state;
707 GNUNET_assert (NULL != state->th);
708 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
713 struct GNUNET_MQ_Handle *
714 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
715 const struct GNUNET_MQ_MessageHandler *handlers,
716 GNUNET_MQ_ErrorHandler error_handler,
717 void *error_handler_cls)
719 struct GNUNET_MQ_Handle *mq;
720 struct ClientConnectionState *state;
723 mq = GNUNET_new (struct GNUNET_MQ_Handle);
724 if (NULL != handlers)
726 for (i=0;NULL != handlers[i].cb; i++) ;
727 mq->handlers = GNUNET_new_array (i,
728 struct GNUNET_MQ_MessageHandler);
729 memcpy (mq->handlers,
731 i * sizeof (struct GNUNET_MQ_MessageHandler));
733 mq->error_handler = error_handler;
734 mq->error_handler_cls = error_handler_cls;
735 state = GNUNET_new (struct ClientConnectionState);
736 state->connection = connection;
737 mq->impl_state = state;
738 mq->send_impl = &connection_client_send_impl;
739 mq->destroy_impl = &connection_client_destroy_impl;
740 mq->cancel_impl = &connection_client_cancel_impl;
741 if (NULL != handlers)
742 state->receive_requested = GNUNET_YES;
749 * Associate the assoc_data in mq with a unique request id.
751 * @param mq message queue, id will be unique for the queue
752 * @param assoc_data to associate
755 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
760 if (NULL == mq->assoc_map)
762 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
766 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
767 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
773 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
776 if (NULL == mq->assoc_map)
778 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
783 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
788 if (NULL == mq->assoc_map)
790 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
792 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
799 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
800 GNUNET_MQ_NotifyCallback cb,
809 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
811 if (NULL != mq->destroy_impl)
813 mq->destroy_impl (mq, mq->impl_state);
815 if (NULL != mq->continue_task)
817 GNUNET_SCHEDULER_cancel (mq->continue_task);
818 mq->continue_task = NULL;
820 while (NULL != mq->envelope_head)
822 struct GNUNET_MQ_Envelope *ev;
824 ev = mq->envelope_head;
825 ev->parent_queue = NULL;
826 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
829 GNUNET_MQ_discard (ev);
831 if (NULL != mq->current_envelope)
833 /* we can only discard envelopes that
835 mq->current_envelope->parent_queue = NULL;
836 GNUNET_MQ_discard (mq->current_envelope);
837 mq->current_envelope = NULL;
839 if (NULL != mq->assoc_map)
841 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
842 mq->assoc_map = NULL;
844 GNUNET_free_non_null (mq->handlers);
849 const struct GNUNET_MessageHeader *
850 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
854 uint16_t nested_size;
855 const struct GNUNET_MessageHeader *nested_msg;
857 whole_size = ntohs (mh->size);
858 GNUNET_assert (whole_size >= base_size);
859 nested_size = whole_size - base_size;
860 if (0 == nested_size)
862 if (nested_size < sizeof (struct GNUNET_MessageHeader))
867 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
868 if (ntohs (nested_msg->size) != nested_size)
878 * Cancel sending the message. Message must have been sent with
879 * #GNUNET_MQ_send before. May not be called after the notify sent
880 * callback has been called
882 * @param ev queued envelope to cancel
885 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
887 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
889 GNUNET_assert (NULL != mq);
890 GNUNET_assert (NULL != mq->cancel_impl);
892 if (mq->current_envelope == ev)
894 // complex case, we already started with transmitting
898 // continue sending the next message, if any
899 if (NULL == mq->envelope_head)
901 mq->current_envelope = NULL;
905 mq->current_envelope = mq->envelope_head;
906 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
908 mq->current_envelope);
910 mq->current_envelope->mh,
916 // simple case, message is still waiting in the queue
917 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
922 ev->parent_queue = NULL;