gnunet_mq_lib.h 34 KB

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