msg.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2015 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _MSG_H_
  35. #define _MSG_H_
  36. #include <sys/types.h>
  37. #include <inttypes.h>
  38. #include "dynar.h"
  39. #include "tlv.h"
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. enum msg_type {
  44. MSG_TYPE_PREINIT = 0,
  45. MSG_TYPE_PREINIT_REPLY = 1,
  46. MSG_TYPE_STARTTLS = 2,
  47. MSG_TYPE_INIT = 3,
  48. MSG_TYPE_INIT_REPLY = 4,
  49. MSG_TYPE_SERVER_ERROR = 5,
  50. MSG_TYPE_SET_OPTION = 6,
  51. MSG_TYPE_SET_OPTION_REPLY = 7,
  52. MSG_TYPE_ECHO_REQUEST = 8,
  53. MSG_TYPE_ECHO_REPLY = 9,
  54. };
  55. struct msg_decoded {
  56. enum msg_type type;
  57. uint8_t seq_number_set;
  58. uint32_t seq_number; // Only valid if seq_number_set != 0
  59. size_t cluster_name_len;
  60. char *cluster_name; // Valid only if != NULL. Trailing \0 is added but not counted in cluster_name_len
  61. uint8_t tls_supported_set;
  62. enum tlv_tls_supported tls_supported; // Valid only if tls_supported_set != 0.
  63. uint8_t tls_client_cert_required_set;
  64. uint8_t tls_client_cert_required; // Valid only if tls_client_cert_required_set != 0
  65. size_t no_supported_messages;
  66. enum msg_type *supported_messages; // Valid only if != NULL
  67. size_t no_supported_options;
  68. enum tlv_opt_type *supported_options; // Valid only if != NULL
  69. uint8_t reply_error_code_set;
  70. enum tlv_reply_error_code reply_error_code; // Valid only if reply_error_code_set != 0
  71. uint8_t server_maximum_request_size_set;
  72. size_t server_maximum_request_size; // Valid only if server_maximum_request_size_set != 0
  73. uint8_t server_maximum_reply_size_set;
  74. size_t server_maximum_reply_size; // Valid only if server_maximum_reply_size_set != 0
  75. uint8_t node_id_set;
  76. uint32_t node_id;
  77. size_t no_supported_decision_algorithms;
  78. enum tlv_decision_algorithm_type *supported_decision_algorithms; // Valid only if != NULL
  79. uint8_t decision_algorithm_set;
  80. enum tlv_decision_algorithm_type decision_algorithm; // Valid only if decision_algorithm_set != 0
  81. uint8_t heartbeat_interval_set;
  82. uint32_t heartbeat_interval; // Valid only if heartbeat_interval_set != 0
  83. };
  84. extern size_t msg_create_preinit(struct dynar *msg, const char *cluster_name,
  85. int add_msg_seq_number, uint32_t msg_seq_number);
  86. extern size_t msg_create_preinit_reply(struct dynar *msg, int add_msg_seq_number,
  87. uint32_t msg_seq_number, enum tlv_tls_supported tls_supported, int tls_client_cert_required);
  88. extern size_t msg_create_starttls(struct dynar *msg, int add_msg_seq_number,
  89. uint32_t msg_seq_number);
  90. extern size_t msg_create_init(struct dynar *msg, int add_msg_seq_number, uint32_t msg_seq_number,
  91. const enum msg_type *supported_msgs, size_t no_supported_msgs,
  92. const enum tlv_opt_type *supported_opts, size_t no_supported_opts, uint32_t node_id);
  93. extern size_t msg_create_server_error(struct dynar *msg, int add_msg_seq_number, uint32_t msg_seq_number,
  94. enum tlv_reply_error_code reply_error_code);
  95. extern size_t msg_create_init_reply(struct dynar *msg, int add_msg_seq_number, uint32_t msg_seq_number,
  96. const enum msg_type *supported_msgs, size_t no_supported_msgs,
  97. const enum tlv_opt_type *supported_opts, size_t no_supported_opts,
  98. size_t server_maximum_request_size, size_t server_maximum_reply_size,
  99. const enum tlv_decision_algorithm_type *supported_decision_algorithms, size_t no_supported_decision_algorithms);
  100. extern size_t msg_create_set_option(struct dynar *msg,
  101. int add_msg_seq_number, uint32_t msg_seq_number,
  102. int add_decision_algorithm, enum tlv_decision_algorithm_type decision_algorithm,
  103. int add_heartbeat_interval, uint32_t heartbeat_interval);
  104. extern size_t msg_create_set_option_reply(struct dynar *msg,
  105. int add_msg_seq_number, uint32_t msg_seq_number,
  106. enum tlv_decision_algorithm_type decision_algorithm, uint32_t heartbeat_interval);
  107. extern size_t msg_create_echo_request(struct dynar *msg, int add_msg_seq_number, uint32_t msg_seq_number);
  108. extern size_t msg_create_echo_reply(struct dynar *msg, const struct dynar *echo_request_msg);
  109. extern size_t msg_get_header_length(void);
  110. extern uint32_t msg_get_len(const struct dynar *msg);
  111. extern enum msg_type msg_get_type(const struct dynar *msg);
  112. extern int msg_is_valid_msg_type(const struct dynar *msg);
  113. extern void msg_decoded_init(struct msg_decoded *decoded_msg);
  114. extern void msg_decoded_destroy(struct msg_decoded *decoded_msg);
  115. extern int msg_decode(const struct dynar *msg, struct msg_decoded *decoded_msg);
  116. extern void msg_get_supported_messages(enum msg_type **supported_messages,
  117. size_t *no_supported_messages);
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif /* _MSG_H_ */