tlv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. #include <sys/types.h>
  35. #include <arpa/inet.h>
  36. #include <inttypes.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. /*
  40. * 64-bit variant of ntoh is not exactly standard...
  41. */
  42. #if defined(__linux__)
  43. #include <endian.h>
  44. #elif defined(__FreeBSD__) || defined(__NetBSD__)
  45. #include <sys/endian.h>
  46. #elif defined(__OpenBSD__)
  47. #include <sys/types.h>
  48. #define be64toh(x) betoh64(x)
  49. #endif
  50. #include "tlv.h"
  51. #define TLV_TYPE_LENGTH 2
  52. #define TLV_LENGTH_LENGTH 2
  53. #define TLV_STATIC_SUPPORTED_OPTIONS_SIZE 22
  54. enum tlv_opt_type tlv_static_supported_options[TLV_STATIC_SUPPORTED_OPTIONS_SIZE] = {
  55. TLV_OPT_MSG_SEQ_NUMBER,
  56. TLV_OPT_CLUSTER_NAME,
  57. TLV_OPT_TLS_SUPPORTED,
  58. TLV_OPT_TLS_CLIENT_CERT_REQUIRED,
  59. TLV_OPT_SUPPORTED_MESSAGES,
  60. TLV_OPT_SUPPORTED_OPTIONS,
  61. TLV_OPT_REPLY_ERROR_CODE,
  62. TLV_OPT_SERVER_MAXIMUM_REQUEST_SIZE,
  63. TLV_OPT_SERVER_MAXIMUM_REPLY_SIZE,
  64. TLV_OPT_NODE_ID,
  65. TLV_OPT_SUPPORTED_DECISION_ALGORITHMS,
  66. TLV_OPT_DECISION_ALGORITHM,
  67. TLV_OPT_HEARTBEAT_INTERVAL,
  68. TLV_OPT_RING_ID,
  69. TLV_OPT_CONFIG_VERSION,
  70. TLV_OPT_DATA_CENTER_ID,
  71. TLV_OPT_NODE_STATE,
  72. TLV_OPT_NODE_INFO,
  73. TLV_OPT_NODE_LIST_TYPE,
  74. TLV_OPT_VOTE,
  75. TLV_OPT_QUORATE,
  76. TLV_OPT_TIE_BREAKER,
  77. };
  78. int
  79. tlv_add(struct dynar *msg, enum tlv_opt_type opt_type, uint16_t opt_len, const void *value)
  80. {
  81. uint16_t nlen;
  82. uint16_t nopt_type;
  83. if (dynar_size(msg) + sizeof(nopt_type) + sizeof(nlen) + opt_len > dynar_max_size(msg)) {
  84. return (-1);
  85. }
  86. nopt_type = htons((uint16_t)opt_type);
  87. nlen = htons(opt_len);
  88. dynar_cat(msg, &nopt_type, sizeof(nopt_type));
  89. dynar_cat(msg, &nlen, sizeof(nlen));
  90. dynar_cat(msg, value, opt_len);
  91. return (0);
  92. }
  93. int
  94. tlv_add_u32(struct dynar *msg, enum tlv_opt_type opt_type, uint32_t u32)
  95. {
  96. uint32_t nu32;
  97. nu32 = htonl(u32);
  98. return (tlv_add(msg, opt_type, sizeof(nu32), &nu32));
  99. }
  100. int
  101. tlv_add_u8(struct dynar *msg, enum tlv_opt_type opt_type, uint8_t u8)
  102. {
  103. return (tlv_add(msg, opt_type, sizeof(u8), &u8));
  104. }
  105. int
  106. tlv_add_u16(struct dynar *msg, enum tlv_opt_type opt_type, uint16_t u16)
  107. {
  108. uint16_t nu16;
  109. nu16 = htons(u16);
  110. return (tlv_add(msg, opt_type, sizeof(nu16), &nu16));
  111. }
  112. int
  113. tlv_add_u64(struct dynar *msg, enum tlv_opt_type opt_type, uint64_t u64)
  114. {
  115. uint64_t nu64;
  116. nu64 = htobe64(u64);
  117. return (tlv_add(msg, opt_type, sizeof(nu64), &nu64));
  118. }
  119. int
  120. tlv_add_string(struct dynar *msg, enum tlv_opt_type opt_type, const char *str)
  121. {
  122. return (tlv_add(msg, opt_type, strlen(str), str));
  123. }
  124. int
  125. tlv_add_msg_seq_number(struct dynar *msg, uint32_t msg_seq_number)
  126. {
  127. return (tlv_add_u32(msg, TLV_OPT_MSG_SEQ_NUMBER, msg_seq_number));
  128. }
  129. int
  130. tlv_add_cluster_name(struct dynar *msg, const char *cluster_name)
  131. {
  132. return (tlv_add_string(msg, TLV_OPT_CLUSTER_NAME, cluster_name));
  133. }
  134. int
  135. tlv_add_tls_supported(struct dynar *msg, enum tlv_tls_supported tls_supported)
  136. {
  137. return (tlv_add_u8(msg, TLV_OPT_TLS_SUPPORTED, tls_supported));
  138. }
  139. int
  140. tlv_add_tls_client_cert_required(struct dynar *msg, int tls_client_cert_required)
  141. {
  142. return (tlv_add_u8(msg, TLV_OPT_TLS_CLIENT_CERT_REQUIRED, tls_client_cert_required));
  143. }
  144. int
  145. tlv_add_u16_array(struct dynar *msg, enum tlv_opt_type opt_type, const uint16_t *array,
  146. size_t array_size)
  147. {
  148. size_t i;
  149. uint16_t *nu16a;
  150. uint16_t opt_len;
  151. int res;
  152. nu16a = malloc(sizeof(uint16_t) * array_size);
  153. if (nu16a == NULL) {
  154. return (-1);
  155. }
  156. for (i = 0; i < array_size; i++) {
  157. nu16a[i] = htons(array[i]);
  158. }
  159. opt_len = sizeof(uint16_t) * array_size;
  160. res = tlv_add(msg, opt_type, opt_len, nu16a);
  161. free(nu16a);
  162. return (res);
  163. }
  164. int
  165. tlv_add_supported_options(struct dynar *msg, const enum tlv_opt_type *supported_options,
  166. size_t no_supported_options)
  167. {
  168. uint16_t *u16a;
  169. size_t i;
  170. int res;
  171. u16a = malloc(sizeof(*u16a) * no_supported_options);
  172. if (u16a == NULL) {
  173. return (-1);
  174. }
  175. for (i = 0; i < no_supported_options; i++) {
  176. u16a[i] = (uint16_t)supported_options[i];
  177. }
  178. res = (tlv_add_u16_array(msg, TLV_OPT_SUPPORTED_OPTIONS, u16a, no_supported_options));
  179. free(u16a);
  180. return (res);
  181. }
  182. int
  183. tlv_add_supported_decision_algorithms(struct dynar *msg,
  184. const enum tlv_decision_algorithm_type *supported_algorithms, size_t no_supported_algorithms)
  185. {
  186. uint16_t *u16a;
  187. size_t i;
  188. int res;
  189. u16a = malloc(sizeof(*u16a) * no_supported_algorithms);
  190. if (u16a == NULL) {
  191. return (-1);
  192. }
  193. for (i = 0; i < no_supported_algorithms; i++) {
  194. u16a[i] = (uint16_t)supported_algorithms[i];
  195. }
  196. res = (tlv_add_u16_array(msg, TLV_OPT_SUPPORTED_DECISION_ALGORITHMS, u16a,
  197. no_supported_algorithms));
  198. free(u16a);
  199. return (res);
  200. }
  201. int
  202. tlv_add_reply_error_code(struct dynar *msg, enum tlv_reply_error_code error_code)
  203. {
  204. return (tlv_add_u16(msg, TLV_OPT_REPLY_ERROR_CODE, (uint16_t)error_code));
  205. }
  206. int
  207. tlv_add_server_maximum_request_size(struct dynar *msg, size_t server_maximum_request_size)
  208. {
  209. return (tlv_add_u32(msg, TLV_OPT_SERVER_MAXIMUM_REQUEST_SIZE, server_maximum_request_size));
  210. }
  211. int
  212. tlv_add_server_maximum_reply_size(struct dynar *msg, size_t server_maximum_reply_size)
  213. {
  214. return (tlv_add_u32(msg, TLV_OPT_SERVER_MAXIMUM_REPLY_SIZE, server_maximum_reply_size));
  215. }
  216. int
  217. tlv_add_node_id(struct dynar *msg, uint32_t node_id)
  218. {
  219. return (tlv_add_u32(msg, TLV_OPT_NODE_ID, node_id));
  220. }
  221. int
  222. tlv_add_decision_algorithm(struct dynar *msg, enum tlv_decision_algorithm_type decision_algorithm)
  223. {
  224. return (tlv_add_u16(msg, TLV_OPT_DECISION_ALGORITHM, (uint16_t)decision_algorithm));
  225. }
  226. int
  227. tlv_add_heartbeat_interval(struct dynar *msg, uint32_t heartbeat_interval)
  228. {
  229. return (tlv_add_u32(msg, TLV_OPT_HEARTBEAT_INTERVAL, heartbeat_interval));
  230. }
  231. int
  232. tlv_add_ring_id(struct dynar *msg, const struct tlv_ring_id *ring_id)
  233. {
  234. uint64_t nu64;
  235. uint32_t nu32;
  236. char tmp_buf[12];
  237. nu32 = htonl(ring_id->node_id);
  238. nu64 = htobe64(ring_id->seq);
  239. memcpy(tmp_buf, &nu32, sizeof(nu32));
  240. memcpy(tmp_buf + sizeof(nu32), &nu64, sizeof(nu64));
  241. return (tlv_add(msg, TLV_OPT_RING_ID, sizeof(tmp_buf), tmp_buf));
  242. }
  243. int
  244. tlv_add_tie_breaker(struct dynar *msg, const struct tlv_tie_breaker *tie_breaker)
  245. {
  246. uint32_t nu32;
  247. uint8_t u8;
  248. char tmp_buf[5];
  249. u8 = tie_breaker->mode;
  250. nu32 = (tie_breaker->mode == TLV_TIE_BREAKER_MODE_NODE_ID ?
  251. htonl(tie_breaker->node_id) : 0);
  252. memcpy(tmp_buf, &u8, sizeof(u8));
  253. memcpy(tmp_buf + sizeof(u8), &nu32, sizeof(nu32));
  254. return (tlv_add(msg, TLV_OPT_TIE_BREAKER, sizeof(tmp_buf), tmp_buf));
  255. }
  256. int
  257. tlv_add_config_version(struct dynar *msg, uint64_t config_version)
  258. {
  259. return (tlv_add_u64(msg, TLV_OPT_CONFIG_VERSION, config_version));
  260. }
  261. int
  262. tlv_add_data_center_id(struct dynar *msg, uint32_t data_center_id)
  263. {
  264. return (tlv_add_u32(msg, TLV_OPT_DATA_CENTER_ID, data_center_id));
  265. }
  266. int
  267. tlv_add_node_state(struct dynar *msg, enum tlv_node_state node_state)
  268. {
  269. return (tlv_add_u8(msg, TLV_OPT_NODE_STATE, node_state));
  270. }
  271. int
  272. tlv_add_node_info(struct dynar *msg, const struct tlv_node_info *node_info)
  273. {
  274. struct dynar opt_value;
  275. int res;
  276. res = 0;
  277. /*
  278. * Create sub message,
  279. */
  280. dynar_init(&opt_value, 1024);
  281. if ((res = tlv_add_node_id(&opt_value, node_info->node_id)) != 0) {
  282. goto exit_dynar_destroy;
  283. }
  284. if (node_info->data_center_id != 0) {
  285. if ((res = tlv_add_data_center_id(&opt_value, node_info->data_center_id)) != 0) {
  286. goto exit_dynar_destroy;
  287. }
  288. }
  289. if (node_info->node_state != TLV_NODE_STATE_NOT_SET) {
  290. if ((res = tlv_add_node_state(&opt_value, node_info->node_state)) != 0) {
  291. goto exit_dynar_destroy;
  292. }
  293. }
  294. res = tlv_add(msg, TLV_OPT_NODE_INFO, dynar_size(&opt_value), dynar_data(&opt_value));
  295. if (res != 0) {
  296. goto exit_dynar_destroy;
  297. }
  298. exit_dynar_destroy:
  299. dynar_destroy(&opt_value);
  300. return (res);
  301. }
  302. int
  303. tlv_add_node_list_type(struct dynar *msg, enum tlv_node_list_type node_list_type)
  304. {
  305. return (tlv_add_u8(msg, TLV_OPT_NODE_LIST_TYPE, node_list_type));
  306. }
  307. int
  308. tlv_add_vote(struct dynar *msg, enum tlv_vote vote)
  309. {
  310. return (tlv_add_u8(msg, TLV_OPT_VOTE, vote));
  311. }
  312. int
  313. tlv_add_quorate(struct dynar *msg, enum tlv_quorate quorate)
  314. {
  315. return (tlv_add_u8(msg, TLV_OPT_QUORATE, quorate));
  316. }
  317. void
  318. tlv_iter_init_str(const char *msg, size_t msg_len, size_t msg_header_len,
  319. struct tlv_iterator *tlv_iter)
  320. {
  321. tlv_iter->msg = msg;
  322. tlv_iter->msg_len = msg_len;
  323. tlv_iter->current_pos = 0;
  324. tlv_iter->msg_header_len = msg_header_len;
  325. tlv_iter->iter_next_called = 0;
  326. }
  327. void
  328. tlv_iter_init(const struct dynar *msg, size_t msg_header_len, struct tlv_iterator *tlv_iter)
  329. {
  330. tlv_iter_init_str(dynar_data(msg), dynar_size(msg), msg_header_len, tlv_iter);
  331. }
  332. enum tlv_opt_type
  333. tlv_iter_get_type(const struct tlv_iterator *tlv_iter)
  334. {
  335. uint16_t ntype;
  336. uint16_t type;
  337. memcpy(&ntype, tlv_iter->msg + tlv_iter->current_pos, sizeof(ntype));
  338. type = ntohs(ntype);
  339. return (type);
  340. }
  341. uint16_t
  342. tlv_iter_get_len(const struct tlv_iterator *tlv_iter)
  343. {
  344. uint16_t nlen;
  345. uint16_t len;
  346. memcpy(&nlen, tlv_iter->msg + tlv_iter->current_pos + TLV_TYPE_LENGTH, sizeof(nlen));
  347. len = ntohs(nlen);
  348. return (len);
  349. }
  350. const char *
  351. tlv_iter_get_data(const struct tlv_iterator *tlv_iter)
  352. {
  353. return (tlv_iter->msg + tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH);
  354. }
  355. int
  356. tlv_iter_next(struct tlv_iterator *tlv_iter)
  357. {
  358. uint16_t len;
  359. if (tlv_iter->iter_next_called == 0) {
  360. tlv_iter->iter_next_called = 1;
  361. tlv_iter->current_pos = tlv_iter->msg_header_len;
  362. goto check_tlv_validity;
  363. }
  364. len = tlv_iter_get_len(tlv_iter);
  365. if (tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len >=
  366. tlv_iter->msg_len) {
  367. return (0);
  368. }
  369. tlv_iter->current_pos += TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len;
  370. check_tlv_validity:
  371. /*
  372. * Check if tlv is valid = is not larger than whole message
  373. */
  374. len = tlv_iter_get_len(tlv_iter);
  375. if (tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len > tlv_iter->msg_len) {
  376. return (-1);
  377. }
  378. return (1);
  379. }
  380. int
  381. tlv_iter_decode_u32(struct tlv_iterator *tlv_iter, uint32_t *res)
  382. {
  383. const char *opt_data;
  384. uint16_t opt_len;
  385. uint32_t nu32;
  386. opt_len = tlv_iter_get_len(tlv_iter);
  387. opt_data = tlv_iter_get_data(tlv_iter);
  388. if (opt_len != sizeof(nu32)) {
  389. return (-1);
  390. }
  391. memcpy(&nu32, opt_data, sizeof(nu32));
  392. *res = ntohl(nu32);
  393. return (0);
  394. }
  395. int
  396. tlv_iter_decode_u8(struct tlv_iterator *tlv_iter, uint8_t *res)
  397. {
  398. const char *opt_data;
  399. uint16_t opt_len;
  400. opt_len = tlv_iter_get_len(tlv_iter);
  401. opt_data = tlv_iter_get_data(tlv_iter);
  402. if (opt_len != sizeof(*res)) {
  403. return (-1);
  404. }
  405. memcpy(res, opt_data, sizeof(*res));
  406. return (0);
  407. }
  408. int
  409. tlv_iter_decode_client_cert_required(struct tlv_iterator *tlv_iter, uint8_t *client_cert_required)
  410. {
  411. return (tlv_iter_decode_u8(tlv_iter, client_cert_required));
  412. }
  413. int
  414. tlv_iter_decode_str(struct tlv_iterator *tlv_iter, char **str, size_t *str_len)
  415. {
  416. const char *opt_data;
  417. uint16_t opt_len;
  418. char *tmp_str;
  419. opt_len = tlv_iter_get_len(tlv_iter);
  420. opt_data = tlv_iter_get_data(tlv_iter);
  421. tmp_str = malloc(opt_len + 1);
  422. if (tmp_str == NULL) {
  423. return (-1);
  424. }
  425. memcpy(tmp_str, opt_data, opt_len);
  426. tmp_str[opt_len] = '\0';
  427. *str = tmp_str;
  428. *str_len = opt_len;
  429. return (0);
  430. }
  431. int
  432. tlv_iter_decode_u16_array(struct tlv_iterator *tlv_iter, uint16_t **u16a, size_t *no_items)
  433. {
  434. uint16_t opt_len;
  435. uint16_t *u16a_res;
  436. size_t i;
  437. opt_len = tlv_iter_get_len(tlv_iter);
  438. if (opt_len % sizeof(uint16_t) != 0) {
  439. return (-1);
  440. }
  441. *no_items = opt_len / sizeof(uint16_t);
  442. u16a_res = malloc(sizeof(uint16_t) * *no_items);
  443. if (u16a_res == NULL) {
  444. return (-2);
  445. }
  446. memcpy(u16a_res, tlv_iter_get_data(tlv_iter), opt_len);
  447. for (i = 0; i < *no_items; i++) {
  448. u16a_res[i] = ntohs(u16a_res[i]);
  449. }
  450. *u16a = u16a_res;
  451. return (0);
  452. }
  453. int
  454. tlv_iter_decode_supported_options(struct tlv_iterator *tlv_iter,
  455. enum tlv_opt_type **supported_options, size_t *no_supported_options)
  456. {
  457. uint16_t *u16a;
  458. enum tlv_opt_type *tlv_opt_array;
  459. size_t i;
  460. int res;
  461. res = tlv_iter_decode_u16_array(tlv_iter, &u16a, no_supported_options);
  462. if (res != 0) {
  463. return (res);
  464. }
  465. tlv_opt_array = malloc(sizeof(enum tlv_opt_type) * *no_supported_options);
  466. if (tlv_opt_array == NULL) {
  467. free(u16a);
  468. return (-2);
  469. }
  470. for (i = 0; i < *no_supported_options; i++) {
  471. tlv_opt_array[i] = (enum tlv_opt_type)u16a[i];
  472. }
  473. free(u16a);
  474. *supported_options = tlv_opt_array;
  475. return (0);
  476. }
  477. int
  478. tlv_iter_decode_supported_decision_algorithms(struct tlv_iterator *tlv_iter,
  479. enum tlv_decision_algorithm_type **supported_decision_algorithms,
  480. size_t *no_supported_decision_algorithms)
  481. {
  482. uint16_t *u16a;
  483. enum tlv_decision_algorithm_type *tlv_decision_algorithm_type_array;
  484. size_t i;
  485. int res;
  486. res = tlv_iter_decode_u16_array(tlv_iter, &u16a, no_supported_decision_algorithms);
  487. if (res != 0) {
  488. return (res);
  489. }
  490. tlv_decision_algorithm_type_array = malloc(
  491. sizeof(enum tlv_decision_algorithm_type) * *no_supported_decision_algorithms);
  492. if (tlv_decision_algorithm_type_array == NULL) {
  493. free(u16a);
  494. return (-2);
  495. }
  496. for (i = 0; i < *no_supported_decision_algorithms; i++) {
  497. tlv_decision_algorithm_type_array[i] = (enum tlv_decision_algorithm_type)u16a[i];
  498. }
  499. free(u16a);
  500. *supported_decision_algorithms = tlv_decision_algorithm_type_array;
  501. return (0);
  502. }
  503. int
  504. tlv_iter_decode_u16(struct tlv_iterator *tlv_iter, uint16_t *u16)
  505. {
  506. const char *opt_data;
  507. uint16_t opt_len;
  508. uint16_t nu16;
  509. opt_len = tlv_iter_get_len(tlv_iter);
  510. opt_data = tlv_iter_get_data(tlv_iter);
  511. if (opt_len != sizeof(nu16)) {
  512. return (-1);
  513. }
  514. memcpy(&nu16, opt_data, sizeof(nu16));
  515. *u16 = ntohs(nu16);
  516. return (0);
  517. }
  518. int
  519. tlv_iter_decode_u64(struct tlv_iterator *tlv_iter, uint64_t *u64)
  520. {
  521. const char *opt_data;
  522. uint64_t opt_len;
  523. uint64_t nu64;
  524. opt_len = tlv_iter_get_len(tlv_iter);
  525. opt_data = tlv_iter_get_data(tlv_iter);
  526. if (opt_len != sizeof(nu64)) {
  527. return (-1);
  528. }
  529. memcpy(&nu64, opt_data, sizeof(nu64));
  530. *u64 = be64toh(nu64);
  531. return (0);
  532. }
  533. int
  534. tlv_iter_decode_reply_error_code(struct tlv_iterator *tlv_iter,
  535. enum tlv_reply_error_code *reply_error_code)
  536. {
  537. return (tlv_iter_decode_u16(tlv_iter, (uint16_t *)reply_error_code));
  538. }
  539. int
  540. tlv_iter_decode_tls_supported(struct tlv_iterator *tlv_iter, enum tlv_tls_supported *tls_supported)
  541. {
  542. uint8_t u8;
  543. enum tlv_tls_supported tmp_tls_supported;
  544. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  545. return (-1);
  546. }
  547. tmp_tls_supported = u8;
  548. if (tmp_tls_supported != TLV_TLS_UNSUPPORTED &&
  549. tmp_tls_supported != TLV_TLS_SUPPORTED &&
  550. tmp_tls_supported != TLV_TLS_REQUIRED) {
  551. return (-4);
  552. }
  553. *tls_supported = tmp_tls_supported;
  554. return (0);
  555. }
  556. int
  557. tlv_iter_decode_decision_algorithm(struct tlv_iterator *tlv_iter,
  558. enum tlv_decision_algorithm_type *decision_algorithm)
  559. {
  560. uint16_t u16;
  561. if (tlv_iter_decode_u16(tlv_iter, &u16) != 0) {
  562. return (-1);
  563. }
  564. *decision_algorithm = (enum tlv_decision_algorithm_type)u16;
  565. return (0);
  566. }
  567. int
  568. tlv_iter_decode_ring_id(struct tlv_iterator *tlv_iter, struct tlv_ring_id *ring_id)
  569. {
  570. const char *opt_data;
  571. uint16_t opt_len;
  572. uint32_t nu32;
  573. uint64_t nu64;
  574. char tmp_buf[12];
  575. opt_len = tlv_iter_get_len(tlv_iter);
  576. opt_data = tlv_iter_get_data(tlv_iter);
  577. if (opt_len != sizeof(tmp_buf)) {
  578. return (-1);
  579. }
  580. memcpy(&nu32, opt_data, sizeof(nu32));
  581. memcpy(&nu64, opt_data + sizeof(nu32), sizeof(nu64));
  582. ring_id->node_id = ntohl(nu32);
  583. ring_id->seq = be64toh(nu64);
  584. return (0);
  585. }
  586. int
  587. tlv_iter_decode_tie_breaker(struct tlv_iterator *tlv_iter, struct tlv_tie_breaker *tie_breaker)
  588. {
  589. const char *opt_data;
  590. uint16_t opt_len;
  591. uint32_t nu32;
  592. uint8_t u8;
  593. enum tlv_tie_breaker_mode tie_breaker_mode;
  594. char tmp_buf[5];
  595. opt_len = tlv_iter_get_len(tlv_iter);
  596. opt_data = tlv_iter_get_data(tlv_iter);
  597. if (opt_len != sizeof(tmp_buf)) {
  598. return (-1);
  599. }
  600. memcpy(&u8, opt_data, sizeof(u8));
  601. tie_breaker_mode = u8;
  602. if (tie_breaker_mode != TLV_TIE_BREAKER_MODE_LOWEST &&
  603. tie_breaker_mode != TLV_TIE_BREAKER_MODE_HIGHEST &&
  604. tie_breaker_mode != TLV_TIE_BREAKER_MODE_NODE_ID) {
  605. return (-4);
  606. }
  607. memcpy(&nu32, opt_data + sizeof(u8), sizeof(nu32));
  608. tie_breaker->mode = tie_breaker_mode;
  609. tie_breaker->node_id = (tie_breaker->mode == TLV_TIE_BREAKER_MODE_NODE_ID ?
  610. ntohl(nu32) : 0);
  611. return (0);
  612. }
  613. int
  614. tlv_iter_decode_node_state(struct tlv_iterator *tlv_iter, enum tlv_node_state *node_state)
  615. {
  616. uint8_t u8;
  617. enum tlv_node_state tmp_node_state;
  618. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  619. return (-1);
  620. }
  621. tmp_node_state = u8;
  622. if (tmp_node_state != TLV_NODE_STATE_MEMBER &&
  623. tmp_node_state != TLV_NODE_STATE_DEAD &&
  624. tmp_node_state != TLV_NODE_STATE_LEAVING) {
  625. return (-4);
  626. }
  627. *node_state = tmp_node_state;
  628. return (0);
  629. }
  630. int
  631. tlv_iter_decode_node_info(struct tlv_iterator *tlv_iter, struct tlv_node_info *node_info)
  632. {
  633. struct tlv_iterator data_tlv_iter;
  634. int iter_res;
  635. int res;
  636. enum tlv_opt_type opt_type;
  637. struct tlv_node_info tmp_node_info;
  638. memset(&tmp_node_info, 0, sizeof(tmp_node_info));
  639. tlv_iter_init_str(tlv_iter_get_data(tlv_iter), tlv_iter_get_len(tlv_iter), 0,
  640. &data_tlv_iter);
  641. while ((iter_res = tlv_iter_next(&data_tlv_iter)) > 0) {
  642. opt_type = tlv_iter_get_type(&data_tlv_iter);
  643. switch (opt_type) {
  644. case TLV_OPT_NODE_ID:
  645. if ((res = tlv_iter_decode_u32(&data_tlv_iter,
  646. &tmp_node_info.node_id)) != 0) {
  647. return (res);
  648. }
  649. break;
  650. case TLV_OPT_DATA_CENTER_ID:
  651. if ((res = tlv_iter_decode_u32(&data_tlv_iter,
  652. &tmp_node_info.data_center_id)) != 0) {
  653. return (res);
  654. }
  655. break;
  656. case TLV_OPT_NODE_STATE:
  657. if ((res = tlv_iter_decode_node_state(&data_tlv_iter,
  658. &tmp_node_info.node_state)) != 0) {
  659. return (res);
  660. }
  661. break;
  662. default:
  663. /*
  664. * Other options are not processed
  665. */
  666. break;
  667. }
  668. }
  669. if (iter_res != 0) {
  670. return (-3);
  671. }
  672. if (tmp_node_info.node_id == 0) {
  673. return (-4);
  674. }
  675. memcpy(node_info, &tmp_node_info, sizeof(tmp_node_info));
  676. return (0);
  677. }
  678. int
  679. tlv_iter_decode_node_list_type(struct tlv_iterator *tlv_iter,
  680. enum tlv_node_list_type *node_list_type)
  681. {
  682. uint8_t u8;
  683. enum tlv_node_list_type tmp_node_list_type;
  684. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  685. return (-1);
  686. }
  687. tmp_node_list_type = u8;
  688. if (tmp_node_list_type != TLV_NODE_LIST_TYPE_INITIAL_CONFIG &&
  689. tmp_node_list_type != TLV_NODE_LIST_TYPE_CHANGED_CONFIG &&
  690. tmp_node_list_type != TLV_NODE_LIST_TYPE_MEMBERSHIP &&
  691. tmp_node_list_type != TLV_NODE_LIST_TYPE_QUORUM) {
  692. return (-4);
  693. }
  694. *node_list_type = tmp_node_list_type;
  695. return (0);
  696. }
  697. int
  698. tlv_iter_decode_vote(struct tlv_iterator *tlv_iter, enum tlv_vote *vote)
  699. {
  700. uint8_t u8;
  701. enum tlv_vote tmp_vote;
  702. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  703. return (-1);
  704. }
  705. tmp_vote = u8;
  706. if (tmp_vote != TLV_VOTE_ACK &&
  707. tmp_vote != TLV_VOTE_NACK &&
  708. tmp_vote != TLV_VOTE_ASK_LATER &&
  709. tmp_vote != TLV_VOTE_WAIT_FOR_REPLY &&
  710. tmp_vote != TLV_VOTE_NO_CHANGE) {
  711. return (-4);
  712. }
  713. *vote = tmp_vote;
  714. return (0);
  715. }
  716. int
  717. tlv_iter_decode_quorate(struct tlv_iterator *tlv_iter, enum tlv_quorate *quorate)
  718. {
  719. uint8_t u8;
  720. enum tlv_quorate tmp_quorate;
  721. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  722. return (-1);
  723. }
  724. tmp_quorate = u8;
  725. if (tmp_quorate != TLV_QUORATE_QUORATE &&
  726. tmp_quorate != TLV_QUORATE_INQUORATE) {
  727. return (-4);
  728. }
  729. *quorate = tmp_quorate;
  730. return (0);
  731. }
  732. void
  733. tlv_get_supported_options(enum tlv_opt_type **supported_options, size_t *no_supported_options)
  734. {
  735. *supported_options = tlv_static_supported_options;
  736. *no_supported_options = TLV_STATIC_SUPPORTED_OPTIONS_SIZE;
  737. }
  738. int
  739. tlv_ring_id_eq(const struct tlv_ring_id *rid1, const struct tlv_ring_id *rid2)
  740. {
  741. return (rid1->node_id == rid2->node_id && rid1->seq == rid2->seq);
  742. }
  743. int
  744. tlv_tie_breaker_eq(const struct tlv_tie_breaker *tb1, const struct tlv_tie_breaker *tb2)
  745. {
  746. if (tb1->mode == tb2->mode && tb1->mode == TLV_TIE_BREAKER_MODE_NODE_ID) {
  747. return (tb1->node_id == tb2->node_id);
  748. }
  749. return (tb1->mode == tb2->mode);
  750. }
  751. const char *
  752. tlv_vote_to_str(enum tlv_vote vote)
  753. {
  754. switch (vote) {
  755. case TLV_VOTE_UNDEFINED: break;
  756. case TLV_VOTE_ACK: return ("ACK"); break;
  757. case TLV_VOTE_NACK: return ("NACK"); break;
  758. case TLV_VOTE_ASK_LATER: return ("Ask later"); break;
  759. case TLV_VOTE_WAIT_FOR_REPLY: return ("Wait for reply"); break;
  760. case TLV_VOTE_NO_CHANGE: return ("No change"); break;
  761. }
  762. return ("Unknown vote value");
  763. }
  764. const char *
  765. tlv_node_state_to_str(enum tlv_node_state state)
  766. {
  767. switch (state) {
  768. case TLV_NODE_STATE_NOT_SET: return ("not set"); break;
  769. case TLV_NODE_STATE_MEMBER: return ("member"); break;
  770. case TLV_NODE_STATE_DEAD: return ("dead"); break;
  771. case TLV_NODE_STATE_LEAVING: return ("leaving"); break;
  772. }
  773. return ("Unhandled node state");
  774. }
  775. const char *
  776. tlv_tls_supported_to_str(enum tlv_tls_supported tls_supported)
  777. {
  778. switch (tls_supported) {
  779. case TLV_TLS_UNSUPPORTED: return ("Unsupported"); break;
  780. case TLV_TLS_SUPPORTED: return ("Supported"); break;
  781. case TLV_TLS_REQUIRED: return ("Required"); break;
  782. }
  783. return ("Unhandled tls supported state");
  784. }
  785. const char *
  786. tlv_decision_algorithm_type_to_str(enum tlv_decision_algorithm_type algorithm)
  787. {
  788. switch (algorithm) {
  789. case TLV_DECISION_ALGORITHM_TYPE_TEST: return ("Test"); break;
  790. case TLV_DECISION_ALGORITHM_TYPE_FFSPLIT: return ("Fifty-Fifty split"); break;
  791. case TLV_DECISION_ALGORITHM_TYPE_2NODELMS: return ("2 Node LMS"); break;
  792. case TLV_DECISION_ALGORITHM_TYPE_LMS: return ("LMS"); break;
  793. }
  794. return ("Unknown algorithm");
  795. }