GNUnet  0.17.6
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 
39 #ifndef GNUNET_MQ_LIB_H
40 #define GNUNET_MQ_LIB_H
41 
42 #include "gnunet_scheduler_lib.h"
43 
56 #define GNUNET_MQ_msg_extra(mvar, esize, type) \
57  GNUNET_MQ_msg_ (((struct GNUNET_MessageHeader **) &(mvar)), \
58  (esize) + sizeof *(mvar), \
59  (type))
60 
71 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra (mvar, 0, type)
72 
73 
80 #define GNUNET_MQ_msg_header(type) \
81  GNUNET_MQ_msg_ (NULL, sizeof(struct GNUNET_MessageHeader), type)
82 
83 
92 #define GNUNET_MQ_msg_header_extra(mh, esize, type) \
93  GNUNET_MQ_msg_ (&mh, (esize) + sizeof(struct GNUNET_MessageHeader), type)
94 
95 
106 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) \
107  ({ \
108  struct GNUNET_MQ_Envelope *_ev; \
109  _ev = GNUNET_MQ_msg_nested_mh_ ((struct GNUNET_MessageHeader **) &(mvar), \
110  sizeof(*(mvar)), \
111  (type), \
112  (mh)); \
113  (void) (mvar)->header; /* type check */ \
114  _ev; \
115  })
116 
117 
126 #define GNUNET_MQ_extract_nested_mh(var) \
127  GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), \
128  sizeof(*(var)))
129 
130 
139 const struct GNUNET_MessageHeader *
141  uint16_t base_size);
142 
143 
147 struct GNUNET_MQ_Envelope;
148 
149 
156 const struct GNUNET_MessageHeader *
158 
159 
166 const struct GNUNET_MQ_Envelope *
168 
169 
179 struct GNUNET_MQ_Envelope *
181  uint16_t base_size,
182  uint16_t type,
183  const struct GNUNET_MessageHeader *nested_mh);
184 
185 
189 struct GNUNET_MQ_Handle;
190 
191 
196 {
203 
208 
213 
219 
225 };
226 
227 
232 {
238 
243 
248 
253 
258 
272 
282 
290 
298 
303 };
304 
305 
312 typedef void
314  void *cls,
315  const struct GNUNET_MessageHeader *msg);
316 
317 
326 typedef int
328  void *cls,
329  const struct GNUNET_MessageHeader *msg);
330 
331 
340 typedef void
342  const struct GNUNET_MessageHeader *msg,
343  void *impl_state);
344 
345 
355 typedef void
357  void *impl_state);
358 
359 
366 typedef void
368  void *impl_state);
369 
370 
380 typedef void
381 (*GNUNET_MQ_ErrorHandler) (void *cls,
382  enum GNUNET_MQ_Error error);
383 
384 
397 void
399  struct GNUNET_MQ_Envelope **env_tail,
400  struct GNUNET_MQ_Envelope *env);
401 
402 
415 void
417  struct GNUNET_MQ_Envelope **env_tail,
418  struct GNUNET_MQ_Envelope *env);
419 
420 
433 void
434 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
435  struct GNUNET_MQ_Envelope **env_tail,
436  struct GNUNET_MQ_Envelope *env);
437 
438 
451 
452 
467  GNUNET_MQ_MessageCallback agpl_handler,
468  void *agpl_cls);
469 
470 
477 unsigned int
479 
480 
485 {
494 
501 
505  void *cls;
506 
510  uint16_t type;
511 
518  uint16_t expected_size;
519 };
520 
521 
525 #define GNUNET_MQ_handler_end() \
526  { \
527  NULL, NULL, NULL, 0, 0 \
528  }
529 
530 
559 #define GNUNET_MQ_hd_fixed_size(name, code, str, ctx) \
560  ({ \
561  void (*_cb)(void *cls, const str *msg) = &handle_ ## name; \
562  ((struct GNUNET_MQ_MessageHandler){ NULL, \
563  (GNUNET_MQ_MessageCallback) _cb, \
564  (ctx), \
565  (code), \
566  sizeof(str) }); \
567  })
568 
569 
610 #define GNUNET_MQ_hd_var_size(name, code, str, ctx) \
611  __extension__ ({ \
612  int (*_mv)(void *cls, const str *msg) = &check_ ## name; \
613  void (*_cb)(void *cls, const str *msg) = &handle_ ## name; \
614  ((struct GNUNET_MQ_MessageHandler){ (GNUNET_MQ_MessageValidationCallback) \
615  _mv, \
616  (GNUNET_MQ_MessageCallback) _cb, \
617  (ctx), \
618  (code), \
619  sizeof(str) }); \
620  })
621 
622 
633 #define GNUNET_MQ_check_zero_termination(m) \
634  { \
635  const char *str = (const char *) &m[1]; \
636  const struct GNUNET_MessageHeader *hdr = \
637  (const struct GNUNET_MessageHeader *) m; \
638  uint16_t slen = ntohs (hdr->size) - sizeof(*m); \
639  if ((0 == slen) || (memchr (str, 0, slen) != &str[slen - 1])) \
640  { \
641  GNUNET_break (0); \
642  return GNUNET_NO; \
643  } \
644  }
645 
646 
659 #define GNUNET_MQ_check_boxed_message(m) \
660  { \
661  const struct GNUNET_MessageHeader *inbox = \
662  (const struct GNUNET_MessageHeader *) &m[1]; \
663  const struct GNUNET_MessageHeader *hdr = \
664  (const struct GNUNET_MessageHeader *) m; \
665  uint16_t slen = ntohs (hdr->size) - sizeof(*m); \
666  if ((slen < sizeof(struct GNUNET_MessageHeader)) || \
667  (slen != ntohs (inbox->size))) \
668  { \
669  GNUNET_break (0); \
670  return GNUNET_NO; \
671  } \
672  }
673 
674 
687 int
689  const struct GNUNET_MessageHeader *mh);
690 
691 
700 struct GNUNET_MQ_Envelope *
702  uint16_t size,
703  uint16_t type);
704 
705 
712 struct GNUNET_MQ_Envelope *
713 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
714 
715 
723 void
725 
726 
734 struct GNUNET_MQ_Envelope *
736 
737 
745 struct GNUNET_MQ_Envelope *
747 
748 
755 struct GNUNET_MQ_Envelope *
757 
758 
767 void
770 
771 
780 
781 
793 
794 
802 struct GNUNET_MQ_Envelope *
804 
805 
812 void
815 
816 
823 unsigned int
825 
826 
834 void
836  struct GNUNET_MQ_Envelope *ev);
837 
838 
846 void
848  const struct GNUNET_MQ_Envelope *ev);
849 
850 
858 void
860 
861 
868 uint32_t
870  void *assoc_data);
871 
872 
880 void *
882  uint32_t request_id);
883 
884 
892 void *
894  uint32_t request_id);
895 
896 
909 struct GNUNET_MQ_Handle *
912  GNUNET_MQ_CancelImpl cancel,
913  void *impl_state,
914  const struct GNUNET_MQ_MessageHandler *handlers,
916  void *cls);
917 
918 
926 void
928  void *handlers_cls);
929 
930 
940 void
943  void *cb_cls);
944 
945 
951 void
953 
954 
960 
961 
974  void *cb_cls);
975 
981 void
984 
985 
996 void
998  const struct GNUNET_MessageHeader *mh);
999 
1000 
1011 void
1013  enum GNUNET_MQ_Error error);
1014 
1015 
1026 void
1028 
1029 
1040 void
1042 
1043 
1058 void *
1060 
1061 
1071 const struct GNUNET_MessageHeader *
1073 
1074 
1081 {
1086 
1092 
1098 
1104 
1108 #define GNUNET_MQ_PREFERENCE_COUNT 4
1109 };
1110 
1111 
1120 const char *
1122 
1123 
1124 #endif
1125  /* end of group mq */
1127  /* 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:372
struct GNUNET_MQ_Envelope * GNUNET_MQ_msg_(struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
Create a new envelope.
Definition: mq.c:518
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:742
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:873
void GNUNET_MQ_send_cancel(struct GNUNET_MQ_Envelope *ev)
Cancel sending the message.
Definition: mq.c:770
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:948
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:832
unsigned int GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
Obtain the current length of the message queue.
Definition: mq.c:291
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:895
const struct GNUNET_MQ_Envelope * GNUNET_MQ_env_next(const struct GNUNET_MQ_Envelope *env)
Return next envelope in queue.
Definition: mq.c:888
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:346
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:267
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:302
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:822
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:926
void * GNUNET_MQ_impl_state(struct GNUNET_MQ_Handle *mq)
Get the implementation state associated with the message queue.
Definition: mq.c:511
const char * GNUNET_MQ_preference_to_string(enum GNUNET_MQ_PreferenceKind type)
Convert an enum GNUNET_MQ_PreferenceType to a string.
Definition: mq.c:1015
void GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *mqm)
Discard the message queue message, free all allocated resources.
Definition: mq.c:283
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:467
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:423
void * GNUNET_MQ_assoc_remove(struct GNUNET_MQ_Handle *mq, uint32_t request_id)
Remove the association for a request_id.
Definition: mq.c:624
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:854
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:186
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:913
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:640
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:535
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:577
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:203
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:606
const struct GNUNET_MessageHeader * GNUNET_MQ_impl_current(struct GNUNET_MQ_Handle *mq)
Get the message that should currently be sent.
Definition: mq.c:502
enum GNUNET_MQ_PriorityPreferences GNUNET_MQ_env_get_options(struct GNUNET_MQ_Envelope *env)
Get performance preferences set for this envelope.
Definition: mq.c:841
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:446
struct GNUNET_MQ_Envelope * GNUNET_MQ_env_copy(struct GNUNET_MQ_Envelope *env)
Function to copy an envelope.
Definition: mq.c:361
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:491
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:977
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:937
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:959
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:815
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:550
unsigned int GNUNET_MQ_count_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
Count the handlers in a handler array.
Definition: mq.c:1002
const struct GNUNET_MessageHeader * GNUNET_MQ_env_get_msg(const struct GNUNET_MQ_Envelope *env)
Obtain message contained in envelope.
Definition: mq.c:881
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:685
@ 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:67
Handle we return for callbacks registered to be notified when GNUNET_MQ_destroy() is called on a queu...
Definition: mq.c:656
GNUNET_SCHEDULER_TaskCallback cb
Function to call.
Definition: mq.c:675
void * cb_cls
Closure for cb.
Definition: mq.c:680
Handle to a message queue.
Definition: mq.c:86
void * impl_state
Implementation-specific state.
Definition: mq.c:111
Message handler for a specific message type.
Header for all communications.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model