tlv.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 _TLV_H_
  35. #define _TLV_H_
  36. #include <sys/types.h>
  37. #include <inttypes.h>
  38. #include "dynar.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. enum tlv_opt_type {
  43. TLV_OPT_MSG_SEQ_NUMBER = 0,
  44. TLV_OPT_CLUSTER_NAME = 1,
  45. TLV_OPT_TLS_SUPPORTED = 2,
  46. TLV_OPT_TLS_CLIENT_CERT_REQUIRED = 3,
  47. TLV_OPT_SUPPORTED_MESSAGES = 4,
  48. TLV_OPT_SUPPORTED_OPTIONS = 5,
  49. TLV_OPT_REPLY_ERROR_CODE = 6,
  50. TLV_OPT_SERVER_MAXIMUM_REQUEST_SIZE = 7,
  51. TLV_OPT_SERVER_MAXIMUM_REPLY_SIZE = 8,
  52. TLV_OPT_NODE_ID = 9,
  53. TLV_OPT_SUPPORTED_DECISION_ALGORITHMS = 10,
  54. TLV_OPT_DECISION_ALGORITHM = 11,
  55. TLV_OPT_HEARTBEAT_INTERVAL = 12,
  56. };
  57. enum tlv_tls_supported {
  58. TLV_TLS_UNSUPPORTED = 0,
  59. TLV_TLS_SUPPORTED = 1,
  60. TLV_TLS_REQUIRED = 2,
  61. };
  62. enum tlv_reply_error_code {
  63. TLV_REPLY_ERROR_CODE_NO_ERROR = 0,
  64. TLV_REPLY_ERROR_CODE_UNSUPPORTED_NEEDED_MESSAGE = 1,
  65. TLV_REPLY_ERROR_CODE_UNSUPPORTED_NEEDED_OPTION = 2,
  66. TLV_REPLY_ERROR_CODE_TLS_REQUIRED = 3,
  67. TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE = 4,
  68. TLV_REPLY_ERROR_CODE_MESSAGE_TOO_LONG = 5,
  69. TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED = 6,
  70. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION = 7,
  71. TLV_REPLY_ERROR_CODE_UNEXPECTED_MESSAGE = 8,
  72. TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG = 9,
  73. TLV_REPLY_ERROR_CODE_INTERNAL_ERROR = 10,
  74. TLV_REPLY_ERROR_CODE_INIT_REQUIRED = 11,
  75. TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM = 12,
  76. TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL = 13,
  77. };
  78. enum tlv_decision_algorithm_type {
  79. TLV_DECISION_ALGORITHM_TYPE_TEST = 0,
  80. };
  81. struct tlv_iterator {
  82. const struct dynar *msg;
  83. size_t current_pos;
  84. size_t msg_header_len;
  85. };
  86. extern int tlv_add(struct dynar *msg, enum tlv_opt_type opt_type, uint16_t opt_len,
  87. const void *value);
  88. extern int tlv_add_u32(struct dynar *msg, enum tlv_opt_type opt_type, uint32_t u32);
  89. extern int tlv_add_u8(struct dynar *msg, enum tlv_opt_type opt_type, uint8_t u8);
  90. extern int tlv_add_u16(struct dynar *msg, enum tlv_opt_type opt_type, uint16_t u16);
  91. extern int tlv_add_string(struct dynar *msg, enum tlv_opt_type opt_type, const char *str);
  92. extern int tlv_add_u16_array(struct dynar *msg, enum tlv_opt_type opt_type,
  93. const uint16_t *array, size_t array_size);
  94. extern int tlv_add_supported_options(struct dynar *msg,
  95. const enum tlv_opt_type *supported_options, size_t no_supported_options);
  96. extern int tlv_add_msg_seq_number(struct dynar *msg, uint32_t msg_seq_number);
  97. extern int tlv_add_cluster_name(struct dynar *msg, const char *cluster_name);
  98. extern int tlv_add_tls_supported(struct dynar *msg, enum tlv_tls_supported tls_supported);
  99. extern int tlv_add_tls_client_cert_required(struct dynar *msg, int tls_client_cert_required);
  100. extern int tlv_add_reply_error_code(struct dynar *msg, enum tlv_reply_error_code error_code);
  101. extern int tlv_add_node_id(struct dynar *msg, uint32_t node_id);
  102. extern int tlv_add_server_maximum_request_size(struct dynar *msg,
  103. size_t server_maximum_request_size);
  104. extern int tlv_add_server_maximum_reply_size(struct dynar *msg,
  105. size_t server_maximum_reply_size);
  106. extern int tlv_add_supported_decision_algorithms(struct dynar *msg,
  107. const enum tlv_decision_algorithm_type *supported_algorithms, size_t no_supported_algorithms);
  108. extern int tlv_add_decision_algorithm(struct dynar *msg,
  109. enum tlv_decision_algorithm_type decision_algorithm);
  110. extern int tlv_add_heartbeat_interval(struct dynar *msg, uint32_t heartbeat_interval);
  111. extern void tlv_iter_init(const struct dynar *msg, size_t msg_header_len,
  112. struct tlv_iterator *tlv_iter);
  113. extern enum tlv_opt_type tlv_iter_get_type(const struct tlv_iterator *tlv_iter);
  114. extern uint16_t tlv_iter_get_len(const struct tlv_iterator *tlv_iter);
  115. extern const char *tlv_iter_get_data(const struct tlv_iterator *tlv_iter);
  116. extern int tlv_iter_next(struct tlv_iterator *tlv_iter);
  117. extern int tlv_iter_decode_u8(struct tlv_iterator *tlv_iter, uint8_t *res);
  118. extern int tlv_iter_decode_tls_supported(struct tlv_iterator *tlv_iter,
  119. enum tlv_tls_supported *tls_supported);
  120. extern int tlv_iter_decode_u32(struct tlv_iterator *tlv_iter, uint32_t *res);
  121. extern int tlv_iter_decode_str(struct tlv_iterator *tlv_iter, char **str, size_t *str_len);
  122. extern int tlv_iter_decode_client_cert_required(struct tlv_iterator *tlv_iter,
  123. uint8_t *client_cert_required);
  124. extern int tlv_iter_decode_u16_array(struct tlv_iterator *tlv_iter,
  125. uint16_t **u16a, size_t *no_items);
  126. extern int tlv_iter_decode_supported_options(struct tlv_iterator *tlv_iter,
  127. enum tlv_opt_type **supported_options, size_t *no_supported_options);
  128. extern int tlv_iter_decode_supported_decision_algorithms(struct tlv_iterator *tlv_iter,
  129. enum tlv_decision_algorithm_type **supported_decision_algorithms, size_t *no_supported_decision_algorithms);
  130. extern int tlv_iter_decode_u16(struct tlv_iterator *tlv_iter, uint16_t *u16);
  131. extern int tlv_iter_decode_reply_error_code(struct tlv_iterator *tlv_iter,
  132. enum tlv_reply_error_code *reply_error_code);
  133. extern int tlv_iter_decode_decision_algorithm(struct tlv_iterator *tlv_iter,
  134. enum tlv_decision_algorithm_type *decision_algorithm);
  135. extern void tlv_get_supported_options(enum tlv_opt_type **supported_options,
  136. size_t *no_supported_options);
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif /* _TLV_H_ */