msg.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2015-2016 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. };
  62. struct msg_decoded {
  63. enum msg_type type;
  64. uint8_t seq_number_set;
  65. uint32_t seq_number; /* Only valid if seq_number_set != 0 */
  66. size_t cluster_name_len;
  67. /* Valid only if != NULL. Trailing \0 is added but not counted in cluster_name_len */
  68. char *cluster_name;
  69. uint8_t tls_supported_set;
  70. enum tlv_tls_supported tls_supported; /* Valid only if tls_supported_set != 0. */
  71. uint8_t tls_client_cert_required_set;
  72. uint8_t tls_client_cert_required; /* Valid only if tls_client_cert_required_set != 0 */
  73. size_t no_supported_messages;
  74. enum msg_type *supported_messages; /* Valid only if != NULL */
  75. size_t no_supported_options;
  76. enum tlv_opt_type *supported_options; /* Valid only if != NULL */
  77. uint8_t reply_error_code_set;
  78. enum tlv_reply_error_code reply_error_code; /* Valid only if reply_error_code_set != 0 */
  79. uint8_t server_maximum_request_size_set;
  80. /* Valid only if server_maximum_request_size_set != 0 */
  81. size_t server_maximum_request_size;
  82. uint8_t server_maximum_reply_size_set;
  83. size_t server_maximum_reply_size; /* Valid only if server_maximum_reply_size_set != 0 */
  84. uint8_t node_id_set;
  85. uint32_t node_id;
  86. size_t no_supported_decision_algorithms;
  87. /* Valid only if != NULL */
  88. enum tlv_decision_algorithm_type *supported_decision_algorithms;
  89. uint8_t decision_algorithm_set;
  90. /* Valid only if decision_algorithm_set != 0 */
  91. enum tlv_decision_algorithm_type decision_algorithm;
  92. uint8_t heartbeat_interval_set;
  93. uint32_t heartbeat_interval; /* Valid only if heartbeat_interval_set != 0 */
  94. uint8_t ring_id_set;
  95. struct tlv_ring_id ring_id; /* Valid only if ring_id_set != 0 */
  96. uint8_t config_version_set;
  97. uint64_t config_version; /* Valid only if config_version_set != 0 */
  98. uint32_t data_center_id; /* Valid only if != 0 */
  99. enum tlv_node_state node_state; /* Valid only if != TLV_NODE_STATE_NOT_SET */
  100. struct node_list nodes; /* Valid only if node_list_is_empty(nodes) != 0 */
  101. int node_list_type_set;
  102. enum tlv_node_list_type node_list_type; /* Valid only if node_list_type_set != 0 */
  103. int vote_set;
  104. enum tlv_vote vote; /* Valid only if vote_set != 0 */
  105. int quorate_set;
  106. enum tlv_quorate quorate; /* Valid only if quorate_set != 0 */
  107. int tie_breaker_set;
  108. struct tlv_tie_breaker tie_breaker;
  109. };
  110. extern size_t msg_create_preinit(struct dynar *msg, const char *cluster_name,
  111. int add_msg_seq_number, uint32_t msg_seq_number);
  112. extern size_t msg_create_preinit_reply(struct dynar *msg, int add_msg_seq_number,
  113. uint32_t msg_seq_number, enum tlv_tls_supported tls_supported, int tls_client_cert_required);
  114. extern size_t msg_create_starttls(struct dynar *msg, int add_msg_seq_number,
  115. uint32_t msg_seq_number);
  116. extern size_t msg_create_init(struct dynar *msg, int add_msg_seq_number,
  117. uint32_t msg_seq_number, enum tlv_decision_algorithm_type decision_algorithm,
  118. const enum msg_type *supported_msgs, size_t no_supported_msgs,
  119. const enum tlv_opt_type *supported_opts, size_t no_supported_opts, uint32_t node_id,
  120. uint32_t heartbeat_interval, const struct tlv_tie_breaker *tie_breaker,
  121. const struct tlv_ring_id *ring_id);
  122. extern size_t msg_create_server_error(struct dynar *msg, int add_msg_seq_number,
  123. uint32_t msg_seq_number, enum tlv_reply_error_code reply_error_code);
  124. extern size_t msg_create_init_reply(struct dynar *msg, int add_msg_seq_number,
  125. uint32_t msg_seq_number, enum tlv_reply_error_code reply_error_code,
  126. const enum msg_type *supported_msgs, size_t no_supported_msgs,
  127. const enum tlv_opt_type *supported_opts, size_t no_supported_opts,
  128. size_t server_maximum_request_size, size_t server_maximum_reply_size,
  129. const enum tlv_decision_algorithm_type *supported_decision_algorithms,
  130. size_t no_supported_decision_algorithms);
  131. extern size_t msg_create_set_option(struct dynar *msg,
  132. int add_msg_seq_number, uint32_t msg_seq_number,
  133. int add_heartbeat_interval, uint32_t heartbeat_interval);
  134. extern size_t msg_create_set_option_reply(struct dynar *msg,
  135. int add_msg_seq_number, uint32_t msg_seq_number, uint32_t heartbeat_interval);
  136. extern size_t msg_create_echo_request(struct dynar *msg, int add_msg_seq_number,
  137. uint32_t msg_seq_number);
  138. extern size_t msg_create_echo_reply(struct dynar *msg,
  139. const struct dynar *echo_request_msg);
  140. extern size_t msg_create_node_list(struct dynar *msg,
  141. uint32_t msg_seq_number, enum tlv_node_list_type node_list_type,
  142. int add_ring_id, const struct tlv_ring_id *ring_id,
  143. int add_config_version, uint64_t config_version,
  144. int add_quorate, enum tlv_quorate quorate,
  145. const struct node_list *nodes);
  146. extern size_t msg_create_node_list_reply(struct dynar *msg, uint32_t msg_seq_number,
  147. enum tlv_node_list_type node_list_type, const struct tlv_ring_id *ring_id,
  148. enum tlv_vote vote);
  149. extern size_t msg_create_ask_for_vote(struct dynar *msg, uint32_t msg_seq_number);
  150. extern size_t msg_create_ask_for_vote_reply(struct dynar *msg, uint32_t msg_seq_number,
  151. const struct tlv_ring_id *ring_id, enum tlv_vote vote);
  152. extern size_t msg_create_vote_info(struct dynar *msg, uint32_t msg_seq_number,
  153. const struct tlv_ring_id *ring_id, enum tlv_vote vote);
  154. extern size_t msg_create_vote_info_reply(struct dynar *msg, uint32_t msg_seq_number);
  155. extern size_t msg_get_header_length(void);
  156. extern uint32_t msg_get_len(const struct dynar *msg);
  157. extern enum msg_type msg_get_type(const struct dynar *msg);
  158. extern int msg_is_valid_msg_type(const struct dynar *msg);
  159. extern void msg_decoded_init(struct msg_decoded *decoded_msg);
  160. extern void msg_decoded_destroy(struct msg_decoded *decoded_msg);
  161. extern int msg_decode(const struct dynar *msg, struct msg_decoded *decoded_msg);
  162. extern void msg_get_supported_messages(enum msg_type **supported_messages,
  163. size_t *no_supported_messages);
  164. extern const char * msg_type_to_str(enum msg_type type);
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif /* _MSG_H_ */