Fix perf_crypto_rsa.c after various changes
[oweals/gnunet.git] / src / util / mq.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2014 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @author Florian Dold
23  * @file util/mq.c
24  * @brief general purpose request queue
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "mq",__VA_ARGS__)
30
31
32 struct GNUNET_MQ_Envelope
33 {
34   /**
35    * Messages are stored in a linked list.
36    * Each queue has its own list of envelopes.
37    */
38   struct GNUNET_MQ_Envelope *next;
39
40   /**
41    * Messages are stored in a linked list
42    * Each queue has its own list of envelopes.
43    */
44   struct GNUNET_MQ_Envelope *prev;
45
46   /**
47    * Actual allocated message header,
48    * usually points to the end of the containing GNUNET_MQ_Envelope
49    */
50   struct GNUNET_MessageHeader *mh;
51
52   /**
53    * Queue the message is queued in, NULL if message is not queued.
54    */
55   struct GNUNET_MQ_Handle *parent_queue;
56
57   /**
58    * Called after the message was sent irrevocably.
59    */
60   GNUNET_MQ_NotifyCallback sent_cb;
61
62   /**
63    * Closure for @e send_cb
64    */
65   void *sent_cls;
66 };
67
68
69 /**
70  * Handle to a message queue.
71  */
72 struct GNUNET_MQ_Handle
73 {
74   /**
75    * Handlers array, or NULL if the queue should not receive messages
76    */
77   const struct GNUNET_MQ_MessageHandler *handlers;
78
79   /**
80    * Closure for the handler callbacks,
81    * as well as for the error handler.
82    */
83   void *handlers_cls;
84
85   /**
86    * Actual implementation of message sending,
87    * called when a message is added
88    */
89   GNUNET_MQ_SendImpl send_impl;
90
91   /**
92    * Implementation-dependent queue destruction function
93    */
94   GNUNET_MQ_DestroyImpl destroy_impl;
95
96   /**
97    * Implementation-dependent send cancel function
98    */
99   GNUNET_MQ_CancelImpl cancel_impl;
100
101   /**
102    * Implementation-specific state
103    */
104   void *impl_state;
105
106   /**
107    * Callback will be called when an error occurs.
108    */
109   GNUNET_MQ_ErrorHandler error_handler;
110
111   /**
112    * Linked list of messages pending to be sent
113    */
114   struct GNUNET_MQ_Envelope *envelope_head;
115
116   /**
117    * Linked list of messages pending to be sent
118    */
119   struct GNUNET_MQ_Envelope *envelope_tail;
120
121   /**
122    * Message that is currently scheduled to be
123    * sent. Not the head of the message queue, as the implementation
124    * needs to know if sending has been already scheduled or not.
125    */
126   struct GNUNET_MQ_Envelope *current_envelope;
127
128   /**
129    * Map of associations, lazily allocated
130    */
131   struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
132
133   /**
134    * Task scheduled during #GNUNET_MQ_impl_send_continue.
135    */
136   struct GNUNET_SCHEDULER_Task * continue_task;
137
138   /**
139    * Next id that should be used for the @e assoc_map,
140    * initialized lazily to a random value together with
141    * @e assoc_map
142    */
143   uint32_t assoc_id;
144 };
145
146
147 /**
148  * Implementation-specific state for connection to
149  * client (MQ for server).
150  */
151 struct ServerClientSocketState
152 {
153   /**
154    * Handle of the client that connected to the server.
155    */
156   struct GNUNET_SERVER_Client *client;
157
158   /**
159    * Active transmission request to the client.
160    */
161   struct GNUNET_SERVER_TransmitHandle* th;
162 };
163
164
165 /**
166  * Implementation-specific state for connection to
167  * service (MQ for clients).
168  */
169 struct ClientConnectionState
170 {
171   /**
172    * Did we call receive alread alreadyy?
173    */
174   int receive_active;
175
176   /**
177    * Do we also want to receive?
178    */
179   int receive_requested;
180
181   /**
182    * Connection to the service.
183    */
184   struct GNUNET_CLIENT_Connection *connection;
185
186   /**
187    * Active transmission request (or NULL).
188    */
189   struct GNUNET_CLIENT_TransmitHandle *th;
190 };
191
192
193 /**
194  * Call the message message handler that was registered
195  * for the type of the given message in the given message queue.
196  *
197  * This function is indended to be used for the implementation
198  * of message queues.
199  *
200  * @param mq message queue with the handlers
201  * @param mh message to dispatch
202  */
203 void
204 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
205                           const struct GNUNET_MessageHeader *mh)
206 {
207   const struct GNUNET_MQ_MessageHandler *handler;
208   int handled = GNUNET_NO;
209
210   if (NULL == mq->handlers)
211   {
212     LOG (GNUNET_ERROR_TYPE_WARNING,
213          "No handler for message of type %d\n",
214          ntohs (mh->type));
215     return;
216   }
217   for (handler = mq->handlers; NULL != handler->cb; handler++)
218   {
219     if (handler->type == ntohs (mh->type))
220     {
221       handler->cb (mq->handlers_cls, mh);
222       handled = GNUNET_YES;
223       break;
224     }
225   }
226   if (GNUNET_NO == handled)
227     LOG (GNUNET_ERROR_TYPE_WARNING,
228          "No handler for message of type %d\n",
229          ntohs (mh->type));
230 }
231
232
233 /**
234  * Call the error handler of a message queue with the given
235  * error code.  If there is no error handler, log a warning.
236  *
237  * This function is intended to be used by the implementation
238  * of message queues.
239  *
240  * @param mq message queue
241  * @param error the error type
242  */
243 void
244 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
245                         enum GNUNET_MQ_Error error)
246 {
247   if (NULL == mq->error_handler)
248   {
249     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
250                 "mq: got error %d, but no handler installed\n",
251                 (int) error);
252     return;
253   }
254   mq->error_handler (mq->handlers_cls, error);
255 }
256
257
258 void
259 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm)
260 {
261   GNUNET_assert (NULL == mqm->parent_queue);
262   GNUNET_free (mqm);
263 }
264
265
266 /**
267  * Send a message with the give message queue.
268  * May only be called once per message.
269  *
270  * @param mq message queue
271  * @param ev the envelope with the message to send.
272  */
273 void
274 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
275                 struct GNUNET_MQ_Envelope *ev)
276 {
277   GNUNET_assert (NULL != mq);
278   GNUNET_assert (NULL == ev->parent_queue);
279
280   ev->parent_queue = mq;
281   /* is the implementation busy? queue it! */
282   if (NULL != mq->current_envelope)
283   {
284     GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
285                                       mq->envelope_tail,
286                                       ev);
287     return;
288   }
289   mq->current_envelope = ev;
290   mq->send_impl (mq, ev->mh, mq->impl_state);
291 }
292
293
294 /**
295  * Task run to call the send implementation for the next queued
296  * message, if any.  Only useful for implementing message queues,
297  * results in undefined behavior if not used carefully.
298  *
299  * @param cls message queue to send the next message with
300  */
301 static void
302 impl_send_continue (void *cls)
303 {
304   struct GNUNET_MQ_Handle *mq = cls;
305   struct GNUNET_MQ_Envelope *current_envelope;
306
307   mq->continue_task = NULL;
308   /* call is only valid if we're actually currently sending
309    * a message */
310   current_envelope = mq->current_envelope;
311   GNUNET_assert (NULL != current_envelope);
312   current_envelope->parent_queue = NULL;
313   if (NULL == mq->envelope_head)
314   {
315     mq->current_envelope = NULL;
316   }
317   else
318   {
319     mq->current_envelope = mq->envelope_head;
320     GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
321                                  mq->envelope_tail,
322                                  mq->current_envelope);
323     mq->send_impl (mq,
324                    mq->current_envelope->mh,
325                    mq->impl_state);
326   }
327   if (NULL != current_envelope->sent_cb)
328     current_envelope->sent_cb (current_envelope->sent_cls);
329   GNUNET_free (current_envelope);
330 }
331
332
333 /**
334  * Call the send implementation for the next queued message, if any.
335  * Only useful for implementing message queues, results in undefined
336  * behavior if not used carefully.
337  *
338  * @param mq message queue to send the next message with
339  */
340 void
341 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
342 {
343   GNUNET_assert (NULL == mq->continue_task);
344   mq->continue_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
345                                                 mq);
346 }
347
348
349 /**
350  * Create a message queue for the specified handlers.
351  *
352  * @param send function the implements sending messages
353  * @param destroy function that implements destroying the queue
354  * @param cancel function that implements canceling a message
355  * @param impl_state for the queue, passed to 'send' and 'destroy'
356  * @param handlers array of message handlers
357  * @param error_handler handler for read and write errors
358  * @param cls closure for message handlers and error handler
359  * @return a new message queue
360  */
361 struct GNUNET_MQ_Handle *
362 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
363                                GNUNET_MQ_DestroyImpl destroy,
364                                GNUNET_MQ_CancelImpl cancel,
365                                void *impl_state,
366                                const struct GNUNET_MQ_MessageHandler *handlers,
367                                GNUNET_MQ_ErrorHandler error_handler,
368                                void *cls)
369 {
370   struct GNUNET_MQ_Handle *mq;
371
372   mq = GNUNET_new (struct GNUNET_MQ_Handle);
373   mq->send_impl = send;
374   mq->destroy_impl = destroy;
375   mq->cancel_impl = cancel;
376   mq->handlers = handlers;
377   mq->handlers_cls = cls;
378   mq->impl_state = impl_state;
379
380   return mq;
381 }
382
383
384 /**
385  * Get the message that should currently be sent.
386  * Fails if there is no current message.
387  * Only useful for implementing message queues,
388  * results in undefined behavior if not used carefully.
389  *
390  * @param mq message queue with the current message
391  * @return message to send, never NULL
392  */
393 const struct GNUNET_MessageHeader *
394 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
395 {
396   if (NULL == mq->current_envelope)
397     GNUNET_assert (0);
398   if (NULL == mq->current_envelope->mh)
399     GNUNET_assert (0);
400   return mq->current_envelope->mh;
401 }
402
403
404 /**
405  * Get the implementation state associated with the
406  * message queue.
407  *
408  * While the GNUNET_MQ_Impl* callbacks receive the
409  * implementation state, continuations that are scheduled
410  * by the implementation function often only have one closure
411  * argument, with this function it is possible to get at the
412  * implementation state when only passing the GNUNET_MQ_Handle
413  * as closure.
414  *
415  * @param mq message queue with the current message
416  * @return message to send, never NULL
417  */
418 void *
419 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
420 {
421   return mq->impl_state;
422 }
423
424
425 struct GNUNET_MQ_Envelope *
426 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
427                 uint16_t size,
428                 uint16_t type)
429 {
430   struct GNUNET_MQ_Envelope *mqm;
431
432   mqm = GNUNET_malloc (sizeof *mqm + size);
433   mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
434   mqm->mh->size = htons (size);
435   mqm->mh->type = htons (type);
436   if (NULL != mhp)
437     *mhp = mqm->mh;
438   return mqm;
439 }
440
441
442 /**
443  * Implementation of the GNUNET_MQ_msg_nested_mh macro.
444  *
445  * @param mhp pointer to the message header pointer that will be changed to allocate at
446  *        the newly allocated space for the message.
447  * @param base_size size of the data before the nested message
448  * @param type type of the message in the envelope
449  * @param nested_mh the message to append to the message after base_size
450  */
451 struct GNUNET_MQ_Envelope *
452 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
453                           uint16_t base_size,
454                           uint16_t type,
455                           const struct GNUNET_MessageHeader *nested_mh)
456 {
457   struct GNUNET_MQ_Envelope *mqm;
458   uint16_t size;
459
460   if (NULL == nested_mh)
461     return GNUNET_MQ_msg_ (mhp, base_size, type);
462
463   size = base_size + ntohs (nested_mh->size);
464
465   /* check for uint16_t overflow */
466   if (size < base_size)
467     return NULL;
468
469   mqm = GNUNET_MQ_msg_ (mhp, size, type);
470   memcpy ((char *) mqm->mh + base_size,
471           nested_mh,
472           ntohs (nested_mh->size));
473
474   return mqm;
475 }
476
477
478 /**
479  * Transmit a queued message to the session's client.
480  *
481  * @param cls consensus session
482  * @param size number of bytes available in @a buf
483  * @param buf where the callee should write the message
484  * @return number of bytes written to @a buf
485  */
486 static size_t
487 transmit_queued (void *cls, size_t size,
488                  void *buf)
489 {
490   struct GNUNET_MQ_Handle *mq = cls;
491   struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
492   const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
493   size_t msg_size;
494
495   GNUNET_assert (NULL != buf);
496
497   msg_size = ntohs (msg->size);
498   GNUNET_assert (size >= msg_size);
499   memcpy (buf, msg, msg_size);
500   state->th = NULL;
501
502   GNUNET_MQ_impl_send_continue (mq);
503
504   return msg_size;
505 }
506
507
508 static void
509 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
510                             void *impl_state)
511 {
512   struct ServerClientSocketState *state = impl_state;
513
514   if (NULL != state->th)
515   {
516     GNUNET_SERVER_notify_transmit_ready_cancel (state->th);
517     state->th = NULL;
518   }
519
520   GNUNET_assert (NULL != mq);
521   GNUNET_assert (NULL != state);
522   GNUNET_SERVER_client_drop (state->client);
523   GNUNET_free (state);
524 }
525
526
527 static void
528 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
529                          const struct GNUNET_MessageHeader *msg,
530                          void *impl_state)
531 {
532   struct ServerClientSocketState *state = impl_state;
533
534   GNUNET_assert (NULL != mq);
535   GNUNET_assert (NULL != state);
536   state->th = GNUNET_SERVER_notify_transmit_ready (state->client,
537                                                    ntohs (msg->size),
538                                                    GNUNET_TIME_UNIT_FOREVER_REL,
539                                                    &transmit_queued, mq);
540 }
541
542
543 struct GNUNET_MQ_Handle *
544 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
545 {
546   struct GNUNET_MQ_Handle *mq;
547   struct ServerClientSocketState *scss;
548
549   mq = GNUNET_new (struct GNUNET_MQ_Handle);
550   scss = GNUNET_new (struct ServerClientSocketState);
551   mq->impl_state = scss;
552   scss->client = client;
553   GNUNET_SERVER_client_keep (client);
554   mq->send_impl = server_client_send_impl;
555   mq->destroy_impl = server_client_destroy_impl;
556   return mq;
557 }
558
559
560 /**
561  * Type of a function to call when we receive a message
562  * from the service.
563  *
564  * @param cls closure
565  * @param msg message received, NULL on timeout or fatal error
566  */
567 static void
568 handle_client_message (void *cls,
569                        const struct GNUNET_MessageHeader *msg)
570 {
571   struct GNUNET_MQ_Handle *mq = cls;
572   struct ClientConnectionState *state;
573
574   state = mq->impl_state;
575
576   if (NULL == msg)
577   {
578     GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
579     return;
580   }
581   GNUNET_CLIENT_receive (state->connection,
582                          &handle_client_message,
583                          mq,
584                          GNUNET_TIME_UNIT_FOREVER_REL);
585   GNUNET_MQ_inject_message (mq, msg);
586 }
587
588
589 /**
590  * Transmit a queued message to the session's client.
591  *
592  * @param cls consensus session
593  * @param size number of bytes available in @a buf
594  * @param buf where the callee should write the message
595  * @return number of bytes written to buf
596  */
597 static size_t
598 connection_client_transmit_queued (void *cls,
599                                    size_t size,
600                                    void *buf)
601 {
602   struct GNUNET_MQ_Handle *mq = cls;
603   const struct GNUNET_MessageHeader *msg;
604   struct ClientConnectionState *state = mq->impl_state;
605   size_t msg_size;
606
607   GNUNET_assert (NULL != mq);
608   msg = GNUNET_MQ_impl_current (mq);
609
610   if (NULL == buf)
611   {
612     GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
613     return 0;
614   }
615
616   if ( (GNUNET_YES == state->receive_requested) &&
617        (GNUNET_NO == state->receive_active) )
618   {
619     state->receive_active = GNUNET_YES;
620     GNUNET_CLIENT_receive (state->connection, handle_client_message, mq,
621                            GNUNET_TIME_UNIT_FOREVER_REL);
622   }
623
624   msg_size = ntohs (msg->size);
625   GNUNET_assert (size >= msg_size);
626   memcpy (buf, msg, msg_size);
627   state->th = NULL;
628
629   GNUNET_MQ_impl_send_continue (mq);
630
631   return msg_size;
632 }
633
634
635 static void
636 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
637                                 void *impl_state)
638 {
639   GNUNET_free (impl_state);
640 }
641
642
643 static void
644 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
645                              const struct GNUNET_MessageHeader *msg,
646                              void *impl_state)
647 {
648   struct ClientConnectionState *state = impl_state;
649
650   GNUNET_assert (NULL != state);
651   GNUNET_assert (NULL == state->th);
652   state->th =
653       GNUNET_CLIENT_notify_transmit_ready (state->connection,
654                                            ntohs (msg->size),
655                                            GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
656                                            &connection_client_transmit_queued, mq);
657   GNUNET_assert (NULL != state->th);
658 }
659
660
661 static void
662 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
663                                void *impl_state)
664 {
665   struct ClientConnectionState *state = impl_state;
666   GNUNET_assert (NULL != state->th);
667   GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
668   state->th = NULL;
669 }
670
671
672 struct GNUNET_MQ_Handle *
673 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
674                                        const struct GNUNET_MQ_MessageHandler *handlers,
675                                        GNUNET_MQ_ErrorHandler error_handler,
676                                        void *cls)
677 {
678   struct GNUNET_MQ_Handle *mq;
679   struct ClientConnectionState *state;
680
681   GNUNET_assert (NULL != connection);
682
683   mq = GNUNET_new (struct GNUNET_MQ_Handle);
684   mq->handlers = handlers;
685   mq->error_handler = error_handler;
686   mq->handlers_cls = cls;
687   state = GNUNET_new (struct ClientConnectionState);
688   state->connection = connection;
689   mq->impl_state = state;
690   mq->send_impl = connection_client_send_impl;
691   mq->destroy_impl = connection_client_destroy_impl;
692   mq->cancel_impl = connection_client_cancel_impl;
693   if (NULL != handlers)
694     state->receive_requested = GNUNET_YES;
695
696   return mq;
697 }
698
699
700 void
701 GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
702                             const struct GNUNET_MQ_MessageHandler *new_handlers,
703                             void *cls)
704 {
705   /* FIXME: notify implementation? */
706   /* FIXME: what about NULL handlers? abort receive? */
707   mq->handlers = new_handlers;
708   mq->handlers_cls = cls;
709 }
710
711
712 /**
713  * Associate the assoc_data in mq with a unique request id.
714  *
715  * @param mq message queue, id will be unique for the queue
716  * @param assoc_data to associate
717  */
718 uint32_t
719 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
720                      void *assoc_data)
721 {
722   uint32_t id;
723
724   if (NULL == mq->assoc_map)
725   {
726     mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
727     mq->assoc_id = 1;
728   }
729   id = mq->assoc_id++;
730   GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
731                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
732   return id;
733 }
734
735
736 void *
737 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
738                      uint32_t request_id)
739 {
740   if (NULL == mq->assoc_map)
741     return NULL;
742   return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
743 }
744
745
746 void *
747 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
748                         uint32_t request_id)
749 {
750   void *val;
751
752   if (NULL == mq->assoc_map)
753     return NULL;
754   val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
755                                              request_id);
756   GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
757                                               request_id);
758   return val;
759 }
760
761
762 void
763 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
764                        GNUNET_MQ_NotifyCallback cb,
765                        void *cls)
766 {
767   mqm->sent_cb = cb;
768   mqm->sent_cls = cls;
769 }
770
771
772 void
773 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
774 {
775   if (NULL != mq->destroy_impl)
776   {
777     mq->destroy_impl (mq, mq->impl_state);
778   }
779   if (NULL != mq->continue_task)
780   {
781     GNUNET_SCHEDULER_cancel (mq->continue_task);
782     mq->continue_task = NULL;
783   }
784   while (NULL != mq->envelope_head)
785   {
786     struct GNUNET_MQ_Envelope *ev;
787     ev = mq->envelope_head;
788     ev->parent_queue = NULL;
789     GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
790                                  mq->envelope_tail,
791                                  ev);
792     GNUNET_MQ_discard (ev);
793   }
794   if (NULL != mq->current_envelope)
795   {
796     /* we can only discard envelopes that
797      * are not queued! */
798     mq->current_envelope->parent_queue = NULL;
799     GNUNET_MQ_discard (mq->current_envelope);
800     mq->current_envelope = NULL;
801   }
802   if (NULL != mq->assoc_map)
803   {
804     GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
805     mq->assoc_map = NULL;
806   }
807
808   GNUNET_free (mq);
809 }
810
811
812 const struct GNUNET_MessageHeader *
813 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
814                               uint16_t base_size)
815 {
816   uint16_t whole_size;
817   uint16_t nested_size;
818   const struct GNUNET_MessageHeader *nested_msg;
819
820   whole_size = ntohs (mh->size);
821   GNUNET_assert (whole_size >= base_size);
822   nested_size = whole_size - base_size;
823   if (0 == nested_size)
824     return NULL;
825   if (nested_size < sizeof (struct GNUNET_MessageHeader))
826   {
827     GNUNET_break_op (0);
828     return NULL;
829   }
830   nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
831   if (ntohs (nested_msg->size) != nested_size)
832   {
833     GNUNET_break_op (0);
834     return NULL;
835   }
836   return nested_msg;
837 }
838
839
840 /**
841  * Cancel sending the message. Message must have been sent with
842  * #GNUNET_MQ_send before.  May not be called after the notify sent
843  * callback has been called
844  *
845  * @param ev queued envelope to cancel
846  */
847 void
848 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
849 {
850   struct GNUNET_MQ_Handle *mq = ev->parent_queue;
851
852   GNUNET_assert (NULL != mq);
853   GNUNET_assert (NULL != mq->cancel_impl);
854
855   if (mq->current_envelope == ev)
856   {
857     // complex case, we already started with transmitting
858     // the message
859     mq->cancel_impl (mq,
860                      mq->impl_state);
861     // continue sending the next message, if any
862     if (NULL == mq->envelope_head)
863     {
864       mq->current_envelope = NULL;
865     }
866     else
867     {
868       mq->current_envelope = mq->envelope_head;
869       GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
870                                    mq->envelope_tail,
871                                    mq->current_envelope);
872       mq->send_impl (mq,
873                      mq->current_envelope->mh,
874                      mq->impl_state);
875     }
876   }
877   else
878   {
879     // simple case, message is still waiting in the queue
880     GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
881                                  mq->envelope_tail,
882                                  ev);
883   }
884
885   ev->parent_queue = NULL;
886   ev->mh = NULL;
887   GNUNET_free (ev);
888 }
889
890 /* end of mq.c */