RECLAIM/OIDC: code cleanup
[oweals/gnunet.git] / src / include / gnunet_mq_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2016 GNUnet e.V.
4
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.
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      Affero General Public License for more details.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @author Florian Dold
23  * @author Christian Grothoff
24  *
25  * @file
26  * General-purpose message queue
27  *
28  * @defgroup mq  MQ library
29  * General-purpose message queue
30  *
31  * @see [Documentation](https://gnunet.org/message-queue-api)
32  *
33  * @{
34  */
35 #ifndef GNUNET_MQ_LIB_H
36 #define GNUNET_MQ_LIB_H
37
38 #include "gnunet_scheduler_lib.h"
39
40 /**
41  * Allocate an envelope, with extra space allocated after the space needed
42  * by the message struct.
43  * The allocated message will already have the type and size field set.
44  *
45  * @param mvar variable to store the allocated message in;
46  *             must have a header field;
47  *             can be NULL
48  * @param esize extra space to allocate after the message
49  * @param type type of the message
50  * @return the MQ message
51  */
52 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
53
54 /**
55  * Allocate a GNUNET_MQ_Envelope.
56  * The contained message will already have the type and size field set.
57  *
58  * @param mvar variable to store the allocated message in;
59  *             must have a header field;
60  *             can be NULL
61  * @param type type of the message
62  * @return the allocated envelope
63  */
64 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
65
66
67 /**
68  * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
69  * The allocated message will already have the type and size field set.
70  *
71  * @param type type of the message
72  */
73 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
74
75
76 /**
77  * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
78  * The allocated message will already have the type and size field set.
79  *
80  * @param mh pointer that will changed to point at to the allocated message header
81  * @param esize extra space to allocate after the message header
82  * @param type type of the message
83  */
84 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
85
86
87 /**
88  * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
89  * message struct.
90  *
91  * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
92  *        whose size is 'sizeof(*mvar) + ntohs (mh->size)'
93  * @param type message type of the allocated message, has no effect on the nested message
94  * @param mh message to nest
95  * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
96  */
97 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) \
98   ({struct GNUNET_MQ_Envelope *_ev;\
99     _ev = GNUNET_MQ_msg_nested_mh_((struct GNUNET_MessageHeader**) &(mvar),\
100                                    sizeof (*(mvar)),\
101                                    (type),\
102                                    (mh));\
103    (void)(mvar)->header; /* type check */\
104    _ev;})
105
106
107 /**
108  * Return a pointer to the message at the end of the given message.
109  *
110  * @param var pointer to a message struct, the type of the expression determines the base size,
111  *        the space after the base size is the nested message
112  * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
113  *         or NULL if the given message in @a var does not have any space after the message struct
114  */
115 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
116
117
118 /**
119  * Implementation of the #GNUNET_MQ_extract_nexted_mh macro.
120  *
121  * @param mh message header to extract nested message header from
122  * @param base_size size of the message before the nested message's header appears
123  * @return pointer to the nested message, does not copy the message
124  *         OR NULL in case of a malformed message.
125  */
126 const struct GNUNET_MessageHeader *
127 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
128                               uint16_t base_size);
129
130
131 /**
132  * Opaque handle to an envelope.
133  */
134 struct GNUNET_MQ_Envelope;
135
136
137 /**
138  * Obtain message contained in envelope.
139  *
140  * @param env the envelope
141  * @return message contained in the envelope
142  */
143 const struct GNUNET_MessageHeader *
144 GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env);
145
146
147 /**
148  * Return next envelope in queue.
149  *
150  * @param env a queued envelope
151  * @return next one, or NULL
152  */
153 const struct GNUNET_MQ_Envelope *
154 GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env);
155
156
157 /**
158  * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
159  *
160  * @param mhp pointer to the message header pointer that will be changed to allocate at
161  *        the newly allocated space for the message.
162  * @param base_size size of the data before the nested message
163  * @param type type of the message in the envelope
164  * @param nested_mh the message to append to the message after base_size
165  */
166 struct GNUNET_MQ_Envelope *
167 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
168                           uint16_t base_size,
169                           uint16_t type,
170                           const struct GNUNET_MessageHeader *nested_mh);
171
172
173 /**
174  * Opaque handle to a message queue.
175  */
176 struct GNUNET_MQ_Handle;
177
178
179 /**
180  * Error codes for the queue.
181  */
182 enum GNUNET_MQ_Error
183 {
184   /**
185    * Failed to read message from the network.
186    * FIXME: Likely not properly distinguished
187    * from TIMEOUT case in the code!
188    */
189   GNUNET_MQ_ERROR_READ = 1,
190
191   /**
192    * FIXME: document!
193    */
194   GNUNET_MQ_ERROR_WRITE = 2,
195
196   /**
197    * FIXME: document!
198    */
199   GNUNET_MQ_ERROR_TIMEOUT = 4,
200
201   /**
202    * We received a message that was malformed and thus
203    * could not be passed to its handler.
204    */
205   GNUNET_MQ_ERROR_MALFORMED = 8,
206
207   /**
208    * We received a message for which we have no matching
209    * handler.
210    */
211   GNUNET_MQ_ERROR_NO_MATCH = 16
212 };
213
214
215 /**
216  * Called when a message has been received.
217  *
218  * @param cls closure
219  * @param msg the received message
220  */
221 typedef void
222 (*GNUNET_MQ_MessageCallback) (void *cls,
223                               const struct GNUNET_MessageHeader *msg);
224
225
226 /**
227  * Called when a message needs to be validated.
228  *
229  * @param cls closure
230  * @param msg the received message
231  * @return #GNUNET_OK if the message is well-formed,
232  *         #GNUNET_SYSERR if not
233  */
234 typedef int
235 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
236                                         const struct GNUNET_MessageHeader *msg);
237
238
239 /**
240  * Signature of functions implementing the
241  * sending functionality of a message queue.
242  *
243  * @param mq the message queue
244  * @param msg the message to send
245  * @param impl_state state of the implementation
246  */
247 typedef void
248 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
249                        const struct GNUNET_MessageHeader *msg,
250                        void *impl_state);
251
252
253 /**
254  * Signature of functions implementing the
255  * destruction of a message queue.
256  * Implementations must not free @a mq, but should
257  * take care of @a impl_state.
258  *
259  * @param mq the message queue to destroy
260  * @param impl_state state of the implementation
261  */
262 typedef void
263 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
264                           void *impl_state);
265
266
267 /**
268  * Implementation function that cancels the currently sent message.
269  *
270  * @param mq message queue
271  * @param impl_state state specific to the implementation
272  */
273 typedef void
274 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
275                          void *impl_state);
276
277
278 /**
279  * Generic error handler, called with the appropriate
280  * error code and the same closure specified at the creation of
281  * the message queue.
282  * Not every message queue implementation supports an error handler.
283  *
284  * @param cls closure
285  * @param error error code
286  */
287 typedef void
288 (*GNUNET_MQ_ErrorHandler) (void *cls,
289                            enum GNUNET_MQ_Error error);
290
291
292 /**
293  * Insert @a env into the envelope DLL starting at @a env_head
294  * Note that @a env must not be in any MQ while this function
295  * is used with DLLs defined outside of the MQ module.  This
296  * is just in case some application needs to also manage a
297  * FIFO of envelopes independent of MQ itself and wants to
298  * re-use the pointers internal to @a env.  Use with caution.
299  *
300  * @param[in|out] env_head of envelope DLL
301  * @param[in|out] env_tail tail of envelope DLL
302  * @param[in|out] env element to insert at the tail
303  */
304 void
305 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
306                            struct GNUNET_MQ_Envelope **env_tail,
307                            struct GNUNET_MQ_Envelope *env);
308
309
310 /**
311  * Remove @a env from the envelope DLL starting at @a env_head.
312  * Note that @a env must not be in any MQ while this function
313  * is used with DLLs defined outside of the MQ module. This
314  * is just in case some application needs to also manage a
315  * FIFO of envelopes independent of MQ itself and wants to
316  * re-use the pointers internal to @a env.  Use with caution.
317  *
318  * @param[in|out] env_head of envelope DLL
319  * @param[in|out] env_tail tail of envelope DLL
320  * @param[in|out] env element to remove from the DLL
321  */
322 void
323 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
324                       struct GNUNET_MQ_Envelope **env_tail,
325                       struct GNUNET_MQ_Envelope *env);
326
327
328 /**
329  * Copy an array of handlers.
330  *
331  * Useful if the array has been delared in local memory and needs to be
332  * persisted for future use.
333  *
334  * @param handlers Array of handlers to be copied.
335  * @return A newly allocated array of handlers.
336  *         Needs to be freed with #GNUNET_free.
337  */
338 struct GNUNET_MQ_MessageHandler *
339 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
340
341
342 /**
343  * Copy an array of handlers, appending AGPL handler.
344  *
345  * Useful if the array has been delared in local memory and needs to be
346  * persisted for future use.
347  *
348  * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
349  * @param agpl_handler function to call for AGPL handling
350  * @param agpl_cls closure for @a agpl_handler
351  * @return A newly allocated array of handlers.
352  *         Needs to be freed with #GNUNET_free.
353  */
354 struct GNUNET_MQ_MessageHandler *
355 GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
356                           GNUNET_MQ_MessageCallback agpl_handler,
357                           void *agpl_cls);
358
359
360 /**
361  * Count the handlers in a handler array.
362  *
363  * @param handlers Array of handlers to be counted.
364  * @return The number of handlers in the array.
365  */
366 unsigned int
367 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers);
368
369
370 /**
371  * Message handler for a specific message type.
372  */
373 struct GNUNET_MQ_MessageHandler
374 {
375   /**
376    * Callback to validate a message of the specified @e type.
377    * The closure given to @e mv will be this struct (not @e ctx).
378    * Using NULL means only size-validation using
379    * @e expected_size.  In this case, @e expected_size must
380    * be non-zero.
381    */
382   GNUNET_MQ_MessageValidationCallback mv;
383
384   /**
385    * Callback, called every time a new message of
386    * the specified @e type has been receied.
387    * The closure given to @e mv will be this struct (not @e ctx).
388    */
389   GNUNET_MQ_MessageCallback cb;
390
391   /**
392    * Closure for @e mv and @e cb.
393    */
394   void *cls;
395
396   /**
397    * Type of the message this handler covers, in host byte order.
398    */
399   uint16_t type;
400
401   /**
402    * Expected size of messages of this type.  Minimum size of the
403    * message if @e mv is non-NULL.  Messages of the given type will be
404    * discarded (and the connection closed with an error reported to
405    * the application) if they do not have the right size.
406    */
407   uint16_t expected_size;
408 };
409
410
411 /**
412  * End-marker for the handlers array
413  */
414 #define GNUNET_MQ_handler_end() { NULL, NULL, NULL, 0, 0 }
415
416
417 /**
418  * Defines a static function @a name which takes as a single argument
419  * a message handler for fixed-sized messages of type @a code and with
420  * a message type argument of @a str.  Given such an argument, the
421  * function @name will return a `struct GNUNET_MQ_MessageHandler`
422  * for the given message type.
423  *
424  * The macro is to be used as follows:
425  * <code>
426  * struct GNUNET_MessageTest { ... }; // must be fixed size
427  * static void
428  * handle_test_message (void *cls,
429  *                      const struct GNUNET_MessageTest *msg)
430  * { ... }
431  *
432  * struct GNUNET_MQ_MessageHandler handlers[] = {
433  *   GNUNET_MQ_hd_fixed_size(test_message,
434  *                           GNUNET_MESSAGE_TYPE_TEST,
435  *                           struct GNUNET_MessageTest,
436  *                           "context"),
437  *   GNUNET_MQ_handler_end()
438  * };
439  *
440  * @param name unique basename for the functions
441  * @param code message type constant
442  * @param str type of the message (a struct)
443  * @param ctx context for the callbacks
444  */
445 #define GNUNET_MQ_hd_fixed_size(name,code,str,ctx)           \
446   ({                                                         \
447     void (*_cb)(void *cls, const str *msg) = &handle_##name; \
448     ((struct GNUNET_MQ_MessageHandler) {                     \
449       NULL, (GNUNET_MQ_MessageCallback) _cb,                 \
450       (ctx), (code), sizeof (str) });                        \
451   })
452
453
454 /**
455  * Defines a static function @a name which takes two arguments and a
456  * context-pointer for validating and handling variable-sized messages
457  * of type @a code and with a message type argument of @a str.  Given
458  * such arguments, the function @name will return a `struct
459  * GNUNET_MQ_MessageHandler` for the given message type.
460  *
461  * The macro is to be used as follows:
462  * <code>
463  * struct GNUNET_MessageTest { ... }; // can be variable size
464  * static int
465  * check_test (void *cls,
466  *             const struct GNUNET_MessageTest *msg)
467  * {
468  *   const char *ctx = cls;
469  *   GNUNET_assert (0 == strcmp ("context", ctx));
470  *   // ...
471  * }
472  * static void
473  * handle_test (void *cls,
474  *              const struct GNUNET_MessageTest *msg)
475  * {
476  *   const char *ctx = cls;
477  *   GNUNET_assert (0 == strcmp ("context", ctx));
478  *   // ...
479  * }
480  *
481  * struct GNUNET_MQ_MessageHandler handlers[] = {
482  *   GNUNET_MQ_hd_var_size(test_message,
483  *                         GNUNET_MESSAGE_TYPE_TEST,
484  *                         struct GNUNET_MessageTest,
485  *                         "context"),
486  *   GNUNET_MQ_handler_end()
487  * };
488  *
489  * @param name unique basename for the functions
490  * @param code message type constant
491  * @param str type of the message (a struct)
492  * @param ctx context for the callbacks
493  */
494 #define GNUNET_MQ_hd_var_size(name,code,str,ctx)             \
495   __extension__ ({                                           \
496     int (*_mv)(void *cls, const str *msg) = &check_##name;   \
497     void (*_cb)(void *cls, const str *msg) = &handle_##name; \
498     ((struct GNUNET_MQ_MessageHandler)                       \
499       { (GNUNET_MQ_MessageValidationCallback) _mv,           \
500         (GNUNET_MQ_MessageCallback) _cb,                     \
501         (ctx), (code), sizeof (str) });                      \
502   })
503
504
505 /**
506  * Insert code for a "check_" function that verifies that
507  * a given variable-length message received over the network
508  * is followed by a 0-terminated string.  If the message @a m
509  * is not followed by a 0-terminated string, an error is logged
510  * and the function is returned with #GNUNET_NO.
511  *
512  * @param an IPC message with proper type to determine
513  *  the size, starting with a `struct GNUNET_MessageHeader`
514  */
515 #define GNUNET_MQ_check_zero_termination(m)           \
516   {                                                   \
517     const char *str = (const char *) &m[1];           \
518     const struct GNUNET_MessageHeader *hdr =          \
519       (const struct GNUNET_MessageHeader *) m;        \
520     uint16_t slen = ntohs (hdr->size) - sizeof (*m);  \
521     if ( (0 == slen) ||                               \
522          (memchr (str, 0, slen) != &str[slen - 1]) )  \
523     {                                                 \
524       GNUNET_break (0);                               \
525       return GNUNET_NO;                               \
526     }                                                 \
527   }
528
529
530 /**
531  * Insert code for a "check_" function that verifies that
532  * a given variable-length message received over the network
533  * is followed by another variable-length message that fits
534  * exactly with the given size.  If the message @a m
535  * is not followed by another `struct GNUNET_MessageHeader`
536  * with a size that adds up to the total size, an error is logged
537  * and the function is returned with #GNUNET_NO.
538  *
539  * @param an IPC message with proper type to determine
540  *  the size, starting with a `struct GNUNET_MessageHeader`
541  */
542 #define GNUNET_MQ_check_boxed_message(m)                \
543   {                                                     \
544     const struct GNUNET_MessageHeader *inbox =          \
545       (const struct GNUNET_MessageHeader *) &m[1];      \
546     const struct GNUNET_MessageHeader *hdr =            \
547       (const struct GNUNET_MessageHeader *) m;          \
548     uint16_t slen = ntohs (hdr->size) - sizeof (*m);    \
549     if ( (slen < sizeof (struct GNUNET_MessageHeader))||\
550          (slen != ntohs (inbox->size)) )                \
551     {                                                   \
552       GNUNET_break (0);                                 \
553       return GNUNET_NO;                                 \
554     }                                                   \
555   }
556
557
558 /**
559  * Call the message message handler that was registered
560  * for the type of the given message in the given @a handlers list.
561  *
562  * This function is indended to be used for the implementation
563  * of message queues.
564  *
565  * @param handlers a set of handlers
566  * @param mh message to dispatch
567  * @return #GNUNET_OK on success, #GNUNET_NO if no handler matched,
568  *         #GNUNET_SYSERR if message was rejected by check function
569  */
570 int
571 GNUNET_MQ_handle_message (const struct GNUNET_MQ_MessageHandler *handlers,
572                           const struct GNUNET_MessageHeader *mh);
573
574
575 /**
576  * Create a new envelope.
577  *
578  * @param mhp message header to store the allocated message header in, can be NULL
579  * @param size size of the message to allocate
580  * @param type type of the message, will be set in the allocated message
581  * @return the allocated MQ message
582  */
583 struct GNUNET_MQ_Envelope *
584 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
585                 uint16_t size,
586                 uint16_t type);
587
588
589 /**
590  * Create a new envelope by copying an existing message.
591  *
592  * @param hdr header of the message to copy
593  * @return envelope containing @a hdr
594  */
595 struct GNUNET_MQ_Envelope *
596 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
597
598
599 /**
600  * Discard the message queue message, free all
601  * allocated resources. Must be called in the event
602  * that a message is created but should not actually be sent.
603  *
604  * @param mqm the message to discard
605  */
606 void
607 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
608
609
610 /**
611  * Function to obtain the current envelope
612  * from within #GNUNET_MQ_SendImpl implementations.
613  *
614  * @param mq message queue to interrogate
615  * @return the current envelope
616  */
617 struct GNUNET_MQ_Envelope *
618 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq);
619
620
621 /**
622  * Function to copy an envelope.  The envelope must not yet
623  * be in any queue or have any options or callbacks set.
624  *
625  * @param env envelope to copy
626  * @return copy of @a env
627  */
628 struct GNUNET_MQ_Envelope *
629 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env);
630
631
632 /**
633  * Function to obtain the last envelope in the queue.
634  *
635  * @param mq message queue to interrogate
636  * @return the last envelope in the queue
637  */
638 struct GNUNET_MQ_Envelope *
639 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq);
640
641
642 /**
643  * Set application-specific options for this envelope.
644  * Overrides the options set for the queue with
645  * #GNUNET_MQ_set_options() for this message only.
646  *
647  * @param env message to set options for
648  * @param flags flags to use (meaning is queue-specific)
649  * @param extra additional buffer for further data (also queue-specific)
650  */
651 void
652 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
653                            uint64_t flags,
654                            const void *extra);
655
656
657 /**
658  * Get application-specific options for this envelope.
659  *
660  * @param env message to set options for
661  * @param[out] flags set to flags to use (meaning is queue-specific)
662  * @return extra additional buffer for further data (also queue-specific)
663  */
664 const void *
665 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
666                            uint64_t *flags);
667
668
669 /**
670  * Remove the first envelope that has not yet been sent from the message
671  * queue and return it.
672  *
673  * @param mq queue to remove envelope from
674  * @return NULL if queue is empty (or has no envelope that is not under transmission)
675  */
676 struct GNUNET_MQ_Envelope *
677 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq);
678
679
680 /**
681  * Set application-specific options for this queue.
682  *
683  * @param mq message queue to set options for
684  * @param flags flags to use (meaning is queue-specific)
685  * @param extra additional buffer for further data (also queue-specific)
686  */
687 void
688 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
689                        uint64_t flags,
690                        const void *extra);
691
692
693 /**
694  * Obtain the current length of the message queue.
695  *
696  * @param mq queue to inspect
697  * @return number of queued, non-transmitted messages
698  */
699 unsigned int
700 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
701
702
703 /**
704  * Send a message with the given message queue.
705  * May only be called once per message.
706  *
707  * @param mq message queue
708  * @param ev the envelope with the message to send.
709  */
710 void
711 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
712                 struct GNUNET_MQ_Envelope *ev);
713
714
715 /**
716  * Send a copy of a message with the given message queue.
717  * Can be called repeatedly on the same envelope.
718  *
719  * @param mq message queue
720  * @param ev the envelope with the message to send.
721  */
722 void
723 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
724                      const struct GNUNET_MQ_Envelope *ev);
725
726
727 /**
728  * Cancel sending the message. Message must have been sent with
729  * #GNUNET_MQ_send before.  May not be called after the notify sent
730  * callback has been called
731  *
732  * @param ev queued envelope to cancel
733  */
734 void
735 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
736
737
738 /**
739  * Associate the assoc_data in @a mq with a unique request id.
740  *
741  * @param mq message queue, id will be unique for the queue
742  * @param assoc_data to associate
743  */
744 uint32_t
745 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
746                      void *assoc_data);
747
748
749 /**
750  * Get the data associated with a @a request_id in a queue
751  *
752  * @param mq the message queue with the association
753  * @param request_id the request id we are interested in
754  * @return the associated data
755  */
756 void *
757 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
758                      uint32_t request_id);
759
760
761 /**
762  * Remove the association for a @a request_id
763  *
764  * @param mq the message queue with the association
765  * @param request_id the request id we want to remove
766  * @return the associated data
767  */
768 void *
769 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
770                         uint32_t request_id);
771
772
773 /**
774  * Create a message queue for the specified handlers.
775  *
776  * @param send function the implements sending messages
777  * @param destroy function that implements destroying the queue
778  * @param cancel function that implements canceling a message
779  * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
780  * @param handlers array of message handlers
781  * @param error_handler handler for read and write errors
782  * @param cls closure for message handlers and error handler
783  * @return a new message queue
784  */
785 struct GNUNET_MQ_Handle *
786 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
787                                GNUNET_MQ_DestroyImpl destroy,
788                                GNUNET_MQ_CancelImpl cancel,
789                                void *impl_state,
790                                const struct GNUNET_MQ_MessageHandler *handlers,
791                                GNUNET_MQ_ErrorHandler error_handler,
792                                void *cls);
793
794
795 /**
796  * Change the closure argument in all of the `handlers` of the
797  * @a mq.
798  *
799  * @param mq to modify
800  * @param handlers_cls new closure to use
801  */
802 void
803 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
804                                 void *handlers_cls);
805
806
807 /**
808  * Call a callback once the envelope has been sent, that is,
809  * sending it can not be canceled anymore.
810  * There can be only one notify sent callback per envelope.
811  *
812  * @param ev message to call the notify callback for
813  * @param cb the notify callback
814  * @param cb_cls closure for the callback
815  */
816 void
817 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
818                        GNUNET_SCHEDULER_TaskCallback cb,
819                        void *cb_cls);
820
821
822 /**
823  * Destroy the message queue.
824  *
825  * @param mq message queue to destroy
826  */
827 void
828 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
829
830
831 /**
832  * Handle we return for callbacks registered to be
833  * notified when #GNUNET_MQ_destroy() is called on a queue.
834  */
835 struct GNUNET_MQ_DestroyNotificationHandle;
836
837
838 /**
839  * Register function to be called whenever @a mq is being
840  * destroyed.
841  *
842  * @param mq message queue to watch
843  * @param cb function to call on @a mq destruction
844  * @param cb_cls closure for @a cb
845  * @return handle for #GNUNET_MQ_destroy_notify_cancel().
846  */
847 struct GNUNET_MQ_DestroyNotificationHandle *
848 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
849                           GNUNET_SCHEDULER_TaskCallback cb,
850                           void *cb_cls);
851
852 /**
853  * Cancel registration from #GNUNET_MQ_destroy_notify().
854  *
855  * @param dnh handle for registration to cancel
856  */
857 void
858 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh);
859
860
861 /**
862  * Call the message message handler that was registered
863  * for the type of the given message in the given message queue.
864  *
865  * This function is indended to be used for the implementation
866  * of message queues.
867  *
868  * @param mq message queue with the handlers
869  * @param mh message to dispatch
870  */
871 void
872 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
873                           const struct GNUNET_MessageHeader *mh);
874
875
876 /**
877  * Call the error handler of a message queue with the given
878  * error code.  If there is no error handler, log a warning.
879  *
880  * This function is intended to be used for the implementation
881  * of message queues.
882  *
883  * @param mq message queue
884  * @param error the error type
885  */
886 void
887 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
888                         enum GNUNET_MQ_Error error);
889
890
891 /**
892  * Call the send implementation for the next queued message, if any.
893  * Calls the send notification for the current message unless
894  * #GNUNET_MQ_impl_send_in_flight was called for this envelope.
895  *
896  * Only useful for implementing message queues, results in undefined
897  * behavior if not used carefully.
898  *
899  * @param mq message queue to send the next message with
900  */
901 void
902 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
903
904
905 /**
906  * Call the send notification for the current message, but do not
907  * try to send the next message until #gnunet_mq_impl_send_continue
908  * is called.
909  *
910  * Only useful for implementing message queues, results in undefined
911  * behavior if not used carefully.
912  *
913  * @param mq message queue to send the next message with
914  */
915 void
916 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq);
917
918
919 /**
920  * Get the implementation state associated with the
921  * message queue.
922  *
923  * While the GNUNET_MQ_Impl* callbacks receive the
924  * implementation state, continuations that are scheduled
925  * by the implementation function often only have one closure
926  * argument, with this function it is possible to get at the
927  * implementation state when only passing the `struct GNUNET_MQ_Handle`
928  * as closure.
929  *
930  * @param mq message queue with the current message
931  * @return message to send, never NULL
932  */
933 void *
934 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
935
936
937 /**
938  * Get the message that should currently be sent.
939  * Fails if there is no current message.
940  * Only useful for implementing message queues,
941  * results in undefined behavior if not used carefully.
942  *
943  * @param mq message queue with the current message
944  * @return message to send, never NULL
945  */
946 const struct GNUNET_MessageHeader *
947 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
948
949
950
951 /**
952  * Enum defining all known preference categories.
953  */
954 enum GNUNET_MQ_PreferenceKind
955 {
956
957   /**
958    * No preference was expressed.
959    */
960   GNUNET_MQ_PREFERENCE_NONE = 0,
961
962   /**
963    * The preferred transmission for this envelope focuses on
964    * maximizing bandwidth.
965    */
966   GNUNET_MQ_PREFERENCE_BANDWIDTH = 1,
967
968   /**
969    * The preferred transmission for this envelope foces on
970    * minimizing latency.
971    */
972   GNUNET_MQ_PREFERENCE_LATENCY = 2,
973
974   /**
975    * The preferred transmission for this envelope foces on
976    * reliability.
977    */
978   GNUNET_MQ_PREFERENCE_RELIABILITY = 3
979
980   /**
981    * Number of preference values allowed.
982    */
983 #define GNUNET_MQ_PREFERENCE_COUNT 4
984
985 };
986
987
988 /**
989  * Convert an `enum GNUNET_MQ_PreferenceType` to a string
990  *
991  * @param type the preference type
992  * @return a string or NULL if invalid
993  */
994 const char *
995 GNUNET_MQ_preference_to_string (enum GNUNET_MQ_PreferenceKind type);
996
997
998
999
1000 #endif
1001
1002 /** @} */ /* end of group mq */