msg.h 7.2 KB

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