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