GNUnet  0.19.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 #if !defined (__GNUNET_UTIL_LIB_H_INSIDE__)
22 #error "Only <gnunet_util_lib.h> can be included directly."
23 #endif
24 
25 
44 #ifndef GNUNET_MQ_LIB_H
45 #define GNUNET_MQ_LIB_H
46 
47 
48 #include "gnunet_scheduler_lib.h"
49 
62 #define GNUNET_MQ_msg_extra(mvar, esize, type) \
63  GNUNET_MQ_msg_ (((struct GNUNET_MessageHeader **) &(mvar)), \
64  (esize) + sizeof *(mvar), \
65  (type))
66 
77 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra (mvar, 0, type)
78 
79 
86 #define GNUNET_MQ_msg_header(type) \
87  GNUNET_MQ_msg_ (NULL, sizeof(struct GNUNET_MessageHeader), type)
88 
89 
98 #define GNUNET_MQ_msg_header_extra(mh, esize, type) \
99  GNUNET_MQ_msg_ (&mh, (esize) + sizeof(struct GNUNET_MessageHeader), type)
100 
101 
112 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) \
113  ({ \
114  struct GNUNET_MQ_Envelope *_ev; \
115  _ev = GNUNET_MQ_msg_nested_mh_ ((struct GNUNET_MessageHeader **) &(mvar), \
116  sizeof(*(mvar)), \
117  (type), \
118  (mh)); \
119  (void) (mvar)->header; /* type check */ \
120  _ev; \
121  })
122 
123 
132 #define GNUNET_MQ_extract_nested_mh(var) \
133  GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), \
134  sizeof(*(var)))
135 
136 
145 const struct GNUNET_MessageHeader *
147  uint16_t base_size);
148 
149 
153 struct GNUNET_MQ_Envelope;
154 
155 
162 const struct GNUNET_MessageHeader *
164 
165 
172 const struct GNUNET_MQ_Envelope *
174 
175 
185 struct GNUNET_MQ_Envelope *
187  uint16_t base_size,
188  uint16_t type,
189  const struct GNUNET_MessageHeader *nested_mh);
190 
191 
195 struct GNUNET_MQ_Handle;
196 
197 
202 {
209 
214 
219 
225 
231 };
232 
233 
238 {
244 
249 
254 
259 
264 
278 
288 
296 
304 
309 };
310 
311 
318 typedef void
320  void *cls,
321  const struct GNUNET_MessageHeader *msg);
322 
323 
332 typedef int
334  void *cls,
335  const struct GNUNET_MessageHeader *msg);
336 
337 
346 typedef void
348  const struct GNUNET_MessageHeader *msg,
349  void *impl_state);
350 
351 
361 typedef void
363  void *impl_state);
364 
365 
372 typedef void
374  void *impl_state);
375 
376 
386 typedef void
387 (*GNUNET_MQ_ErrorHandler) (void *cls,
388  enum GNUNET_MQ_Error error);
389 
390 
403 void
405  struct GNUNET_MQ_Envelope **env_tail,
406  struct GNUNET_MQ_Envelope *env);
407 
408 
421 void
423  struct GNUNET_MQ_Envelope **env_tail,
424  struct GNUNET_MQ_Envelope *env);
425 
426 
439 void
440 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
441  struct GNUNET_MQ_Envelope **env_tail,
442  struct GNUNET_MQ_Envelope *env);
443 
444 
457 
458 
473  GNUNET_MQ_MessageCallback agpl_handler,
474  void *agpl_cls);
475 
476 
483 unsigned int
485 
486 
491 {
500 
507 
511  void *cls;
512 
516  uint16_t type;
517 
524  uint16_t expected_size;
525 };
526 
527 
531 #define GNUNET_MQ_handler_end() \
532  { \
533  NULL, NULL, NULL, 0, 0 \
534  }
535 
536 
565 #define GNUNET_MQ_hd_fixed_size(name, code, str, ctx) \
566  ({ \
567  void (*_cb)(void *cls, const str *msg) = &handle_ ## name; \
568  ((struct GNUNET_MQ_MessageHandler){ NULL, \
569  (GNUNET_MQ_MessageCallback) _cb, \
570  (ctx), \
571  (code), \
572  sizeof(str) }); \
573  })
574 
575 
617 #define GNUNET_MQ_hd_var_size(name, code, str, ctx) \
618  __extension__ ({ \
619  int (*_mv)(void *cls, const str *msg) = &check_ ## name; \
620  void (*_cb)(void *cls, const str *msg) = &handle_ ## name; \
621  ((struct GNUNET_MQ_MessageHandler){ (GNUNET_MQ_MessageValidationCallback) \
622  _mv, \
623  (GNUNET_MQ_MessageCallback) _cb, \
624  (ctx), \
625  (code), \
626  sizeof(str) }); \
627  })
628 
629 
640 #define GNUNET_MQ_check_zero_termination(m) \
641  { \
642  const char *str = (const char *) &m[1]; \
643  const struct GNUNET_MessageHeader *hdr = \
644  (const struct GNUNET_MessageHeader *) m; \
645  uint16_t slen = ntohs (hdr->size) - sizeof(*m); \
646  if ((0 == slen) || (memchr (str, 0, slen) != &str[slen - 1])) \
647  { \
648  GNUNET_break (0); \
649  return GNUNET_NO; \
650  } \
651  }
652 
653 
666 #define GNUNET_MQ_check_boxed_message(m) \
667  { \
668  const struct GNUNET_MessageHeader *inbox = \
669  (const struct GNUNET_MessageHeader *) &m[1]; \
670  const struct GNUNET_MessageHeader *hdr = \
671  (const struct GNUNET_MessageHeader *) m; \
672  uint16_t slen = ntohs (hdr->size) - sizeof(*m); \
673  if ((slen < sizeof(struct GNUNET_MessageHeader)) || \
674  (slen != ntohs (inbox->size))) \
675  { \
676  GNUNET_break (0); \
677  return GNUNET_NO; \
678  } \
679  }
680 
681 
694 int
696  const struct GNUNET_MessageHeader *mh);
697 
698 
707 struct GNUNET_MQ_Envelope *
709  uint16_t size,
710  uint16_t type);
711 
712 
719 struct GNUNET_MQ_Envelope *
720 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
721 
722 
730 void
732 
733 
741 struct GNUNET_MQ_Envelope *
743 
744 
752 struct GNUNET_MQ_Envelope *
754 
755 
762 struct GNUNET_MQ_Envelope *
764 
765 
774 void
777 
778 
787 
788 
800 
801 
809 struct GNUNET_MQ_Envelope *
811 
812 
819 void
822 
823 
830 unsigned int
832 
833 
841 void
843  struct GNUNET_MQ_Envelope *ev);
844 
845 
853 void
855  const struct GNUNET_MQ_Envelope *ev);
856 
857 
865 void
867 
868 
875 uint32_t
877  void *assoc_data);
878 
879 
887 void *
889  uint32_t request_id);
890 
891 
899 void *
901  uint32_t request_id);
902 
903 
916 struct GNUNET_MQ_Handle *
919  GNUNET_MQ_CancelImpl cancel,
920  void *impl_state,
921  const struct GNUNET_MQ_MessageHandler *handlers,
923  void *cls);
924 
925 
933 void
935  void *handlers_cls);
936 
937 
947 void
950  void *cb_cls);
951 
952 
958 void
960 
961 
967 
968 
981  void *cb_cls);
982 
988 void
991 
992 
1003 void
1005  const struct GNUNET_MessageHeader *mh);
1006 
1007 
1018 void
1020  enum GNUNET_MQ_Error error);
1021 
1022 
1033 void
1035 
1036 
1047 void
1049 
1050 
1065 void *
1067 
1068 
1078 const struct GNUNET_MessageHeader *
1080 
1081 
1088 {
1093 
1099 
1105 
1111 
1115 #define GNUNET_MQ_PREFERENCE_COUNT 4
1116 };
1117 
1118 
1127 const char *
1129 
1130 
1131 #endif
1132  /* end of group mq */
1134  /* end of group addition */
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)
We encountered an error handling the MQ to the ATS service.
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
static void destroy(void *cls)
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.
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:370
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_(struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
Create a new envelope.
Definition: mq.c:516
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:740
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:871
void GNUNET_MQ_send_cancel(struct GNUNET_MQ_Envelope *ev)
Cancel sending the message.
Definition: mq.c:768
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:946
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:830
unsigned int GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
Obtain the current length of the message queue.
Definition: mq.c:293
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:893
const struct GNUNET_MQ_Envelope * GNUNET_MQ_env_next(const struct GNUNET_MQ_Envelope *env)
Return next envelope in queue.
Definition: mq.c:886
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:344
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
int(* GNUNET_MQ_MessageValidationCallback)(void *cls, const struct GNUNET_MessageHeader *msg)
Called when a message needs to be validated.
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:304
void(* GNUNET_MQ_DestroyImpl)(struct GNUNET_MQ_Handle *mq, void *impl_state)
Signature of functions implementing the destruction of a message queue.
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:820
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:924
void * GNUNET_MQ_impl_state(struct GNUNET_MQ_Handle *mq)
Get the implementation state associated with the message queue.
Definition: mq.c:509
const char * GNUNET_MQ_preference_to_string(enum GNUNET_MQ_PreferenceKind type)
Convert an enum GNUNET_MQ_PreferenceType to a string.
Definition: mq.c:1013
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_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:465
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:421
void * GNUNET_MQ_assoc_remove(struct GNUNET_MQ_Handle *mq, uint32_t request_id)
Remove the association for a request_id.
Definition: mq.c:622
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:852
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.
void GNUNET_MQ_destroy_notify_cancel(struct GNUNET_MQ_DestroyNotificationHandle *dnh)
Cancel registration from GNUNET_MQ_destroy_notify().
Definition: mq.c:911
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:638
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:533
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:575
void(* GNUNET_MQ_MessageCallback)(void *cls, const struct GNUNET_MessageHeader *msg)
Called when a message has been received.
int 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_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:604
const struct GNUNET_MessageHeader * GNUNET_MQ_impl_current(struct GNUNET_MQ_Handle *mq)
Get the message that should currently be sent.
Definition: mq.c:500
enum GNUNET_MQ_PriorityPreferences GNUNET_MQ_env_get_options(struct GNUNET_MQ_Envelope *env)
Get performance preferences set for this envelope.
Definition: mq.c:839
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:444
struct GNUNET_MQ_Envelope * GNUNET_MQ_env_copy(struct GNUNET_MQ_Envelope *env)
Function to copy an envelope.
Definition: mq.c:359
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:489
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:975
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:935
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.
struct GNUNET_MQ_MessageHandler * GNUNET_MQ_copy_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
Copy an array of handlers.
Definition: mq.c:957
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:813
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:548
unsigned int GNUNET_MQ_count_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
Count the handlers in a handler array.
Definition: mq.c:1000
const struct GNUNET_MessageHeader * GNUNET_MQ_env_get_msg(const struct GNUNET_MQ_Envelope *env)
Obtain message contained in envelope.
Definition: mq.c:879
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
@ 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 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:654
GNUNET_SCHEDULER_TaskCallback cb
Function to call.
Definition: mq.c:673
void * cb_cls
Closure for cb.
Definition: mq.c:678
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.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model