GNUnet 0.21.2
gnunet_mq_lib.h
Go to the documentation of this file.
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#include "gnunet_common.h"
22#if ! defined (__GNUNET_UTIL_LIB_H_INSIDE__)
23#error "Only <gnunet_util_lib.h> can be included directly."
24#endif
25
26
45#ifndef GNUNET_MQ_LIB_H
46#define GNUNET_MQ_LIB_H
47
48
50
63#define GNUNET_MQ_msg_extra(mvar, esize, type) \
64 GNUNET_MQ_msg_ (((struct GNUNET_MessageHeader **) &(mvar)), \
65 (esize) + sizeof *(mvar), \
66 (type))
67
78#define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra (mvar, 0, type)
79
80
87#define GNUNET_MQ_msg_header(type) \
88 GNUNET_MQ_msg_ (NULL, sizeof(struct GNUNET_MessageHeader), type)
89
90
99#define GNUNET_MQ_msg_header_extra(mh, esize, type) \
100 GNUNET_MQ_msg_ (&mh, (esize) + sizeof(struct GNUNET_MessageHeader), type \
101 )
102
103
114#define GNUNET_MQ_msg_nested_mh(mvar, type, mh) \
115 ({ \
116 struct GNUNET_MQ_Envelope *_ev; \
117 _ev = GNUNET_MQ_msg_nested_mh_ ((struct GNUNET_MessageHeader **) &(mvar), \
118 sizeof(*(mvar)), \
119 (type), \
120 (mh)); \
121 (void) (mvar)->header; /* type check */ \
122 _ev; \
123 })
124
125
134#define GNUNET_MQ_extract_nested_mh(var) \
135 GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), \
136 sizeof(*(var)))
137
138
147const struct GNUNET_MessageHeader *
149 uint16_t base_size);
150
151
155struct GNUNET_MQ_Envelope;
156
157
164const struct GNUNET_MessageHeader *
166
167
174const struct GNUNET_MQ_Envelope *
176
177
187struct GNUNET_MQ_Envelope *
189 uint16_t base_size,
190 uint16_t type,
191 const struct GNUNET_MessageHeader *nested_mh);
192
193
197struct GNUNET_MQ_Handle;
198
199
204{
211
216
221
227
234
235
240{
246
251
256
261
266
280
290
298
306
311};
312
313
320typedef void
322 void *cls,
323 const struct GNUNET_MessageHeader *msg);
324
325
334typedef enum GNUNET_GenericReturnValue
336 void *cls,
337 const struct GNUNET_MessageHeader *msg);
338
339
348typedef void
350 const struct GNUNET_MessageHeader *msg,
351 void *impl_state);
352
353
363typedef void
365 void *impl_state);
366
367
374typedef void
376 void *impl_state);
377
378
388typedef void
389(*GNUNET_MQ_ErrorHandler) (void *cls,
390 enum GNUNET_MQ_Error error);
391
392
405void
407 struct GNUNET_MQ_Envelope **env_tail,
408 struct GNUNET_MQ_Envelope *env);
409
410
423void
425 struct GNUNET_MQ_Envelope **env_tail,
426 struct GNUNET_MQ_Envelope *env);
427
428
441void
443 struct GNUNET_MQ_Envelope **env_tail,
444 struct GNUNET_MQ_Envelope *env);
445
446
459
460
475 GNUNET_MQ_MessageCallback agpl_handler,
476 void *agpl_cls);
477
478
485unsigned int
487
488
493{
502
509
513 void *cls;
514
518 uint16_t type;
519
527};
528
529
533#define GNUNET_MQ_handler_end() \
534 { \
535 NULL, NULL, NULL, 0, 0 \
536 }
537
538
567#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx) \
568 ({ \
569 void (*_cb)(void *cls, const str *msg) = &handle_ ## name; \
570 ((struct GNUNET_MQ_MessageHandler){ NULL, \
571 (GNUNET_MQ_MessageCallback) _cb, \
572 (ctx), \
573 (code), \
574 sizeof(str) }); \
575 })
576
577
619#define GNUNET_MQ_hd_var_size(name, code, str, ctx) \
620 __extension__ ({ \
621 int (*_mv)(void *cls, const str *msg) = &check_ ## name; \
622 void (*_cb)(void *cls, const str *msg) = &handle_ ## name; \
623 ((struct GNUNET_MQ_MessageHandler){ (GNUNET_MQ_MessageValidationCallback) \
624 _mv, \
625 (GNUNET_MQ_MessageCallback) _cb, \
626 (ctx), \
627 (code), \
628 sizeof(str) }); \
629 })
630
631
642#define GNUNET_MQ_check_zero_termination(m) \
643 { \
644 const char *str = (const char *) &m[1]; \
645 const struct GNUNET_MessageHeader *hdr = \
646 (const struct GNUNET_MessageHeader *) m; \
647 uint16_t slen = ntohs (hdr->size) - sizeof(*m); \
648 if ((0 == slen) || (memchr (str, 0, slen) != &str[slen - 1])) \
649 { \
650 GNUNET_break (0); \
651 return GNUNET_NO; \
652 } \
653 }
654
655
668#define GNUNET_MQ_check_boxed_message(m) \
669 { \
670 const struct GNUNET_MessageHeader *inbox = \
671 (const struct GNUNET_MessageHeader *) &m[1]; \
672 const struct GNUNET_MessageHeader *hdr = \
673 (const struct GNUNET_MessageHeader *) m; \
674 uint16_t slen = ntohs (hdr->size) - sizeof(*m); \
675 if ((slen < sizeof(struct GNUNET_MessageHeader)) || \
676 (slen != ntohs (inbox->size))) \
677 { \
678 GNUNET_break (0); \
679 return GNUNET_NO; \
680 } \
681 }
682
683
698 const struct GNUNET_MessageHeader *mh);
699
700
709struct GNUNET_MQ_Envelope *
711 uint16_t size,
712 uint16_t type);
713
714
721struct GNUNET_MQ_Envelope *
722GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
723
724
732void
734
735
743struct GNUNET_MQ_Envelope *
745
746
754struct GNUNET_MQ_Envelope *
756
757
764struct GNUNET_MQ_Envelope *
766
767
776void
779
780
789
790
802
803
811struct GNUNET_MQ_Envelope *
813
814
821void
824
825
832unsigned int
834
835
843void
845 struct GNUNET_MQ_Envelope *ev);
846
847
855void
857 const struct GNUNET_MQ_Envelope *ev);
858
859
867void
869
870
877uint32_t
879 void *assoc_data);
880
881
889void *
891 uint32_t request_id);
892
893
901void *
903 uint32_t request_id);
904
905
918struct GNUNET_MQ_Handle *
922 void *impl_state,
925 void *cls);
926
927
935void
937 void *handlers_cls);
938
939
949void
952 void *cb_cls);
953
954
960void
962
963
969
970
983 void *cb_cls);
984
990void
993
994
1005void
1007 const struct GNUNET_MessageHeader *mh);
1008
1009
1020void
1022 enum GNUNET_MQ_Error error);
1023
1024
1035void
1037
1038
1049void
1051
1052
1067void *
1069
1070
1080const struct GNUNET_MessageHeader *
1082
1083
1090{
1095
1101
1107
1113
1117#define GNUNET_MQ_PREFERENCE_COUNT 4
1119
1120
1129const char *
1131
1132
1133#endif
1134 /* end of group mq */
1136 /* end of group addition */
struct GNUNET_MQ_MessageHandlers handlers[]
Definition: 003.c:1
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
Function called on connection trouble.
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
static uint32_t type
Type string converted to DNS type value.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
API to schedule computations using continuation passing style.
uint16_t expected_size
Expected size of messages of this type.
GNUNET_MQ_MessageCallback cb
Callback, called every time a new message of the specified type has been received.
void * cls
Closure for mv and cb.
GNUNET_MQ_MessageValidationCallback mv
Callback to validate a message of the specified type.
GNUNET_GenericReturnValue
Named constants for return values.
uint16_t type
Type of the message this handler covers, in host byte order.
void GNUNET_MQ_send_copy(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MQ_Envelope *ev)
Send a copy of a message with the given message queue.
Definition: mq.c:384
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_copy(const struct GNUNET_MessageHeader *hdr)
Create a new envelope by copying an existing message.
Definition: mq.c:550
struct GNUNET_MQ_Envelope * GNUNET_MQ_env_copy(struct GNUNET_MQ_Envelope *env)
Function to copy an envelope.
Definition: mq.c:373
void GNUNET_MQ_set_options(struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_PriorityPreferences pp)
Set application-specific options for this queue.
Definition: mq.c:888
void GNUNET_MQ_send_cancel(struct GNUNET_MQ_Envelope *ev)
Cancel sending the message.
Definition: mq.c:785
void GNUNET_MQ_dll_remove(struct GNUNET_MQ_Envelope **env_head, struct GNUNET_MQ_Envelope **env_tail, struct GNUNET_MQ_Envelope *env)
Remove env from the envelope DLL starting at env_head.
Definition: mq.c:963
void GNUNET_MQ_env_set_options(struct GNUNET_MQ_Envelope *env, enum GNUNET_MQ_PriorityPreferences pp)
Set application-specific options for this envelope.
Definition: mq.c:847
unsigned int GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
Obtain the current length of the message queue.
Definition: mq.c:293
const struct GNUNET_MessageHeader * GNUNET_MQ_extract_nested_mh_(const struct GNUNET_MessageHeader *mh, uint16_t base_size)
Implementation of the #GNUNET_MQ_extract_nexted_mh macro.
Definition: mq.c:757
GNUNET_MQ_Error
Error codes for the queue.
struct GNUNET_MQ_Envelope * GNUNET_MQ_unsent_head(struct GNUNET_MQ_Handle *mq)
Remove the first envelope that has not yet been sent from the message queue and return it.
Definition: mq.c:355
void GNUNET_MQ_inject_error(struct GNUNET_MQ_Handle *mq, enum GNUNET_MQ_Error error)
Call the error handler of a message queue with the given error code.
Definition: mq.c:269
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
Definition: mq.c:482
void(* GNUNET_MQ_ErrorHandler)(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:305
void(* GNUNET_MQ_DestroyImpl)(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
void GNUNET_MQ_dll_insert_head(struct GNUNET_MQ_Envelope **env_head, struct GNUNET_MQ_Envelope **env_tail, struct GNUNET_MQ_Envelope *env)
Insert env into the envelope DLL starting at env_head Note that env must not be in any MQ while this ...
Definition: mq.c:941
enum GNUNET_GenericReturnValue(* GNUNET_MQ_MessageValidationCallback)(void *cls, const struct GNUNET_MessageHeader *msg)
Called when a message needs to be validated.
void GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *mqm)
Discard the message queue message, free all allocated resources.
Definition: mq.c:285
struct GNUNET_MQ_MessageHandler * GNUNET_MQ_copy_handlers2(const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_MessageCallback agpl_handler, void *agpl_cls)
Copy an array of handlers, appending AGPL handler.
Definition: mq.c:992
struct GNUNET_MQ_MessageHandler * GNUNET_MQ_copy_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
Copy an array of handlers.
Definition: mq.c:974
GNUNET_MQ_PreferenceKind
Enum defining all known preference categories.
void GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
Call the send implementation for the next queued message, if any.
Definition: mq.c:437
const char * GNUNET_MQ_preference_to_string(enum GNUNET_MQ_PreferenceKind type)
Convert an enum GNUNET_MQ_PreferenceType to a string.
Definition: mq.c:1030
void * GNUNET_MQ_assoc_get(struct GNUNET_MQ_Handle *mq, uint32_t request_id)
Get the data associated with a request_id in a queue.
Definition: mq.c:621
struct GNUNET_MQ_DestroyNotificationHandle * GNUNET_MQ_destroy_notify(struct GNUNET_MQ_Handle *mq, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Register function to be called whenever mq is being destroyed.
Definition: mq.c:910
enum GNUNET_MQ_PriorityPreferences GNUNET_MQ_env_combine_options(enum GNUNET_MQ_PriorityPreferences p1, enum GNUNET_MQ_PriorityPreferences p2)
Combine performance preferences set for different envelopes that are being combined into one larger e...
Definition: mq.c:869
const struct GNUNET_MQ_Envelope * GNUNET_MQ_env_next(const struct GNUNET_MQ_Envelope *env)
Return next envelope in queue.
Definition: mq.c:903
void GNUNET_MQ_inject_message(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *mh)
Call the message message handler that was registered for the type of the given message in the given m...
Definition: mq.c:187
GNUNET_MQ_PriorityPreferences
Per envelope preferences and priorities.
enum GNUNET_GenericReturnValue GNUNET_MQ_handle_message(const struct GNUNET_MQ_MessageHandler *handlers, const struct GNUNET_MessageHeader *mh)
Call the message message handler that was registered for the type of the given message in the given h...
Definition: mq.c:205
void GNUNET_MQ_destroy_notify_cancel(struct GNUNET_MQ_DestroyNotificationHandle *dnh)
Cancel registration from GNUNET_MQ_destroy_notify().
Definition: mq.c:928
void GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Call a callback once the envelope has been sent, that is, sending it can not be canceled anymore.
Definition: mq.c:655
uint32_t GNUNET_MQ_assoc_add(struct GNUNET_MQ_Handle *mq, void *assoc_data)
Associate the assoc_data in mq with a unique request id.
Definition: mq.c:592
void(* GNUNET_MQ_MessageCallback)(void *cls, const struct GNUNET_MessageHeader *msg)
Called when a message has been received.
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_(struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
Create a new envelope.
Definition: mq.c:533
const struct GNUNET_MessageHeader * GNUNET_MQ_env_get_msg(const struct GNUNET_MQ_Envelope *env)
Obtain message contained in envelope.
Definition: mq.c:896
enum GNUNET_MQ_PriorityPreferences GNUNET_MQ_env_get_options(struct GNUNET_MQ_Envelope *env)
Get performance preferences set for this envelope.
Definition: mq.c:856
void GNUNET_MQ_impl_send_in_flight(struct GNUNET_MQ_Handle *mq)
Call the send notification for the current message, but do not try to send the next message until #gn...
Definition: mq.c:461
void * GNUNET_MQ_assoc_remove(struct GNUNET_MQ_Handle *mq, uint32_t request_id)
Remove the association for a request_id.
Definition: mq.c:639
const struct GNUNET_MessageHeader * GNUNET_MQ_impl_current(struct GNUNET_MQ_Handle *mq)
Get the message that should currently be sent.
Definition: mq.c:517
void GNUNET_MQ_set_handlers_closure(struct GNUNET_MQ_Handle *mq, void *handlers_cls)
Change the closure argument in all of the handlers of the mq.
Definition: mq.c:506
struct GNUNET_MQ_Envelope * GNUNET_MQ_get_last_envelope(struct GNUNET_MQ_Handle *mq)
Function to obtain the last envelope in the queue.
Definition: mq.c:837
void * GNUNET_MQ_impl_state(struct GNUNET_MQ_Handle *mq)
Get the implementation state associated with the message queue.
Definition: mq.c:526
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_nested_mh_(struct GNUNET_MessageHeader **mhp, uint16_t base_size, uint16_t type, const struct GNUNET_MessageHeader *nested_mh)
Implementation of the GNUNET_MQ_msg_nested_mh macro.
Definition: mq.c:565
void GNUNET_MQ_dll_insert_tail(struct GNUNET_MQ_Envelope **env_head, struct GNUNET_MQ_Envelope **env_tail, struct GNUNET_MQ_Envelope *env)
Insert env into the envelope DLL starting at env_head Note that env must not be in any MQ while this ...
Definition: mq.c:952
void(* GNUNET_MQ_CancelImpl)(struct GNUNET_MQ_Handle *mq, void *impl_state)
Implementation function that cancels the currently sent message.
void(* GNUNET_MQ_SendImpl)(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Signature of functions implementing the sending functionality of a message queue.
unsigned int GNUNET_MQ_count_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
Count the handlers in a handler array.
Definition: mq.c:1017
struct GNUNET_MQ_Envelope * GNUNET_MQ_get_current_envelope(struct GNUNET_MQ_Handle *mq)
Function to obtain the current envelope from within GNUNET_MQ_SendImpl implementations.
Definition: mq.c:830
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:700
@ GNUNET_MQ_ERROR_READ
Failed to read message from the network.
@ GNUNET_MQ_ERROR_NO_MATCH
We received a message for which we have no matching handler.
@ GNUNET_MQ_ERROR_TIMEOUT
FIXME: document!
@ GNUNET_MQ_ERROR_MALFORMED
We received a message that was malformed and thus could not be passed to its handler.
@ GNUNET_MQ_ERROR_WRITE
FIXME: document!
@ GNUNET_MQ_PREFERENCE_RELIABILITY
The preferred transmission for this envelope foces on reliability.
@ GNUNET_MQ_PREFERENCE_NONE
No preference was expressed.
@ GNUNET_MQ_PREFERENCE_LATENCY
The preferred transmission for this envelope foces on minimizing latency.
@ GNUNET_MQ_PREFERENCE_BANDWIDTH
The preferred transmission for this envelope focuses on maximizing bandwidth.
@ GNUNET_MQ_PREF_OUT_OF_ORDER
Flag to indicate that out-of-order delivery is OK.
@ GNUNET_MQ_PRIO_URGENT
Urgent traffic (local peer, e.g.
@ GNUNET_MQ_PRIO_CRITICAL_CONTROL
Highest priority, control traffic (e.g.
@ GNUNET_MQ_PRIORITY_MASK
Bit mask to apply to extract the priority bits.
@ GNUNET_MQ_PRIO_BACKGROUND
Lowest priority, i.e.
@ GNUNET_MQ_PREF_CORK_ALLOWED
Flag to indicate that CORKing is acceptable.
@ GNUNET_MQ_PREF_UNRELIABLE
Flag to indicate that unreliable delivery is acceptable.
@ GNUNET_MQ_PREF_GOODPUT
Flag to indicate that high bandwidth is desired.
@ GNUNET_MQ_PREF_LOW_LATENCY
Flag to indicate that low latency is important.
@ GNUNET_MQ_PRIO_BEST_EFFORT
Best-effort traffic (e.g.
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
static void destroy(void *cls)
Function to destroy a microphone.
Definition: microphone.c:153
static unsigned int size
Size of the "table".
Definition: peer.c:68
Handle we return for callbacks registered to be notified when GNUNET_MQ_destroy() is called on a queu...
Definition: mq.c:671
GNUNET_SCHEDULER_TaskCallback cb
Function to call.
Definition: mq.c:690
void * cb_cls
Closure for cb.
Definition: mq.c:695
Handle to a message queue.
Definition: mq.c:87
void * impl_state
Implementation-specific state.
Definition: mq.c:112
Message handler for a specific message type.
Header for all communications.