4
0

msg.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2015-2020 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. #include "node-list.h"
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. enum msg_type {
  45. MSG_TYPE_PREINIT = 0,
  46. MSG_TYPE_PREINIT_REPLY = 1,
  47. MSG_TYPE_STARTTLS = 2,
  48. MSG_TYPE_INIT = 3,
  49. MSG_TYPE_INIT_REPLY = 4,
  50. MSG_TYPE_SERVER_ERROR = 5,
  51. MSG_TYPE_SET_OPTION = 6,
  52. MSG_TYPE_SET_OPTION_REPLY = 7,
  53. MSG_TYPE_ECHO_REQUEST = 8,
  54. MSG_TYPE_ECHO_REPLY = 9,
  55. MSG_TYPE_NODE_LIST = 10,
  56. MSG_TYPE_NODE_LIST_REPLY = 11,
  57. MSG_TYPE_ASK_FOR_VOTE = 12,
  58. MSG_TYPE_ASK_FOR_VOTE_REPLY = 13,
  59. MSG_TYPE_VOTE_INFO = 14,
  60. MSG_TYPE_VOTE_INFO_REPLY = 15,
  61. MSG_TYPE_HEURISTICS_CHANGE = 16,
  62. MSG_TYPE_HEURISTICS_CHANGE_REPLY = 17,
  63. };
  64. struct msg_decoded {
  65. enum msg_type type;
  66. uint8_t seq_number_set;
  67. uint32_t seq_number; /* Only valid if seq_number_set != 0 */
  68. size_t cluster_name_len;
  69. /* Valid only if != NULL. Trailing \0 is added but not counted in cluster_name_len */
  70. char *cluster_name;
  71. uint8_t tls_supported_set;
  72. enum tlv_tls_supported tls_supported; /* Valid only if tls_supported_set != 0. */
  73. uint8_t tls_client_cert_required_set;
  74. uint8_t tls_client_cert_required; /* Valid only if tls_client_cert_required_set != 0 */
  75. size_t no_supported_messages;
  76. enum msg_type *supported_messages; /* Valid only if != NULL */
  77. size_t no_supported_options;
  78. enum tlv_opt_type *supported_options; /* Valid only if != NULL */
  79. uint8_t reply_error_code_set;
  80. enum tlv_reply_error_code reply_error_code; /* Valid only if reply_error_code_set != 0 */
  81. uint8_t server_maximum_request_size_set;
  82. /* Valid only if server_maximum_request_size_set != 0 */
  83. size_t server_maximum_request_size;
  84. uint8_t server_maximum_reply_size_set;
  85. size_t server_maximum_reply_size; /* Valid only if server_maximum_reply_size_set != 0 */
  86. uint8_t node_id_set;
  87. uint32_t node_id;
  88. size_t no_supported_decision_algorithms;
  89. /* Valid only if != NULL */
  90. enum tlv_decision_algorithm_type *supported_decision_algorithms;
  91. uint8_t decision_algorithm_set;
  92. /* Valid only if decision_algorithm_set != 0 */
  93. enum tlv_decision_algorithm_type decision_algorithm;
  94. uint8_t heartbeat_interval_set;
  95. uint32_t heartbeat_interval; /* Valid only if heartbeat_interval_set != 0 */
  96. uint8_t ring_id_set;
  97. struct tlv_ring_id ring_id; /* Valid only if ring_id_set != 0 */
  98. uint8_t config_version_set;
  99. uint64_t config_version; /* Valid only if config_version_set != 0 */
  100. uint32_t data_center_id; /* Valid only if != 0 */
  101. enum tlv_node_state node_state; /* Valid only if != TLV_NODE_STATE_NOT_SET */
  102. struct node_list nodes; /* Valid only if node_list_is_empty(nodes) != 0 */
  103. uint8_t node_list_type_set;
  104. enum tlv_node_list_type node_list_type; /* Valid only if node_list_type_set != 0 */
  105. uint8_t vote_set;
  106. enum tlv_vote vote; /* Valid only if vote_set != 0 */
  107. uint8_t quorate_set;
  108. enum tlv_quorate quorate; /* Valid only if quorate_set != 0 */
  109. uint8_t tie_breaker_set;
  110. struct tlv_tie_breaker tie_breaker;
  111. enum tlv_heuristics heuristics; /* Always valid but can be TLV_HEURISTICS_UNDEFINED */
  112. enum tlv_keep_active_partition_tie_breaker keep_active_partition_tie_breaker;
  113. uint8_t keep_active_partition_tie_breaker_set;
  114. };
  115. extern size_t msg_create_preinit(struct dynar *msg, const char *cluster_name,
  116. int add_msg_seq_number, uint32_t msg_seq_number);
  117. extern size_t msg_create_preinit_reply(struct dynar *msg, int add_msg_seq_number,
  118. uint32_t msg_seq_number, enum tlv_tls_supported tls_supported, int tls_client_cert_required);
  119. extern size_t msg_create_starttls(struct dynar *msg, int add_msg_seq_number,
  120. uint32_t msg_seq_number);
  121. extern size_t msg_create_init(struct dynar *msg, int add_msg_seq_number,
  122. uint32_t msg_seq_number, enum tlv_decision_algorithm_type decision_algorithm,
  123. const enum msg_type *supported_msgs, size_t no_supported_msgs,
  124. const enum tlv_opt_type *supported_opts, size_t no_supported_opts, uint32_t node_id,
  125. uint32_t heartbeat_interval, const struct tlv_tie_breaker *tie_breaker,
  126. const struct tlv_ring_id *ring_id);
  127. extern size_t msg_create_server_error(struct dynar *msg, int add_msg_seq_number,
  128. uint32_t msg_seq_number, enum tlv_reply_error_code reply_error_code);
  129. extern size_t msg_create_init_reply(struct dynar *msg, int add_msg_seq_number,
  130. uint32_t msg_seq_number, enum tlv_reply_error_code reply_error_code,
  131. const enum msg_type *supported_msgs, size_t no_supported_msgs,
  132. const enum tlv_opt_type *supported_opts, size_t no_supported_opts,
  133. size_t server_maximum_request_size, size_t server_maximum_reply_size,
  134. const enum tlv_decision_algorithm_type *supported_decision_algorithms,
  135. size_t no_supported_decision_algorithms);
  136. extern size_t msg_create_set_option(struct dynar *msg,
  137. int add_msg_seq_number, uint32_t msg_seq_number,
  138. int add_heartbeat_interval, uint32_t heartbeat_interval,
  139. int add_keep_active_partition_tie_breaker,
  140. enum tlv_keep_active_partition_tie_breaker keep_active_partiton_tie_breaker);
  141. extern size_t msg_create_set_option_reply(struct dynar *msg,
  142. int add_msg_seq_number, uint32_t msg_seq_number,
  143. int add_heartbeat_interval, uint32_t heartbeat_interval,
  144. int add_keep_active_partition_tie_breaker,
  145. enum tlv_keep_active_partition_tie_breaker keep_active_partiton_tie_breaker);
  146. extern size_t msg_create_echo_request(struct dynar *msg, int add_msg_seq_number,
  147. uint32_t msg_seq_number);
  148. extern size_t msg_create_echo_reply(struct dynar *msg,
  149. const struct dynar *echo_request_msg);
  150. extern size_t msg_create_node_list(struct dynar *msg,
  151. uint32_t msg_seq_number, enum tlv_node_list_type node_list_type,
  152. int add_ring_id, const struct tlv_ring_id *ring_id,
  153. int add_config_version, uint64_t config_version,
  154. int add_quorate, enum tlv_quorate quorate,
  155. int add_heuristics, enum tlv_heuristics heuristics,
  156. const struct node_list *nodes);
  157. extern size_t msg_create_node_list_reply(struct dynar *msg, uint32_t msg_seq_number,
  158. enum tlv_node_list_type node_list_type, const struct tlv_ring_id *ring_id,
  159. enum tlv_vote vote);
  160. extern size_t msg_create_ask_for_vote(struct dynar *msg, uint32_t msg_seq_number);
  161. extern size_t msg_create_ask_for_vote_reply(struct dynar *msg, uint32_t msg_seq_number,
  162. const struct tlv_ring_id *ring_id, enum tlv_vote vote);
  163. extern size_t msg_create_vote_info(struct dynar *msg, uint32_t msg_seq_number,
  164. const struct tlv_ring_id *ring_id, enum tlv_vote vote);
  165. extern size_t msg_create_vote_info_reply(struct dynar *msg, uint32_t msg_seq_number);
  166. extern size_t msg_create_heuristics_change(struct dynar *msg, uint32_t msg_seq_number,
  167. enum tlv_heuristics heuristics);
  168. extern size_t msg_create_heuristics_change_reply(struct dynar *msg,
  169. uint32_t msg_seq_number, const struct tlv_ring_id *ring_id, enum tlv_heuristics heuristics,
  170. enum tlv_vote vote);
  171. extern size_t msg_get_header_length(void);
  172. extern uint32_t msg_get_len(const struct dynar *msg);
  173. extern enum msg_type msg_get_type(const struct dynar *msg);
  174. extern int msg_is_valid_msg_type(const struct dynar *msg);
  175. extern void msg_decoded_init(struct msg_decoded *decoded_msg);
  176. extern void msg_decoded_destroy(struct msg_decoded *decoded_msg);
  177. extern int msg_decode(const struct dynar *msg, struct msg_decoded *decoded_msg);
  178. extern void msg_get_supported_messages(enum msg_type **supported_messages,
  179. size_t *no_supported_messages);
  180. extern const char * msg_type_to_str(enum msg_type type);
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. #endif /* _MSG_H_ */