tlv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. #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 21
  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. };
  77. int
  78. tlv_add(struct dynar *msg, enum tlv_opt_type opt_type, uint16_t opt_len, const void *value)
  79. {
  80. uint16_t nlen;
  81. uint16_t nopt_type;
  82. if (dynar_size(msg) + sizeof(nopt_type) + sizeof(nlen) + opt_len > dynar_max_size(msg)) {
  83. return (-1);
  84. }
  85. nopt_type = htons((uint16_t)opt_type);
  86. nlen = htons(opt_len);
  87. dynar_cat(msg, &nopt_type, sizeof(nopt_type));
  88. dynar_cat(msg, &nlen, sizeof(nlen));
  89. dynar_cat(msg, value, opt_len);
  90. return (0);
  91. }
  92. int
  93. tlv_add_u32(struct dynar *msg, enum tlv_opt_type opt_type, uint32_t u32)
  94. {
  95. uint32_t nu32;
  96. nu32 = htonl(u32);
  97. return (tlv_add(msg, opt_type, sizeof(nu32), &nu32));
  98. }
  99. int
  100. tlv_add_u8(struct dynar *msg, enum tlv_opt_type opt_type, uint8_t u8)
  101. {
  102. return (tlv_add(msg, opt_type, sizeof(u8), &u8));
  103. }
  104. int
  105. tlv_add_u16(struct dynar *msg, enum tlv_opt_type opt_type, uint16_t u16)
  106. {
  107. uint16_t nu16;
  108. nu16 = htons(u16);
  109. return (tlv_add(msg, opt_type, sizeof(nu16), &nu16));
  110. }
  111. int
  112. tlv_add_u64(struct dynar *msg, enum tlv_opt_type opt_type, uint64_t u64)
  113. {
  114. uint64_t nu64;
  115. nu64 = htobe64(u64);
  116. return (tlv_add(msg, opt_type, sizeof(nu64), &nu64));
  117. }
  118. int
  119. tlv_add_string(struct dynar *msg, enum tlv_opt_type opt_type, const char *str)
  120. {
  121. return (tlv_add(msg, opt_type, strlen(str), str));
  122. }
  123. int
  124. tlv_add_msg_seq_number(struct dynar *msg, uint32_t msg_seq_number)
  125. {
  126. return (tlv_add_u32(msg, TLV_OPT_MSG_SEQ_NUMBER, msg_seq_number));
  127. }
  128. int
  129. tlv_add_cluster_name(struct dynar *msg, const char *cluster_name)
  130. {
  131. return (tlv_add_string(msg, TLV_OPT_CLUSTER_NAME, cluster_name));
  132. }
  133. int
  134. tlv_add_tls_supported(struct dynar *msg, enum tlv_tls_supported tls_supported)
  135. {
  136. return (tlv_add_u8(msg, TLV_OPT_TLS_SUPPORTED, tls_supported));
  137. }
  138. int
  139. tlv_add_tls_client_cert_required(struct dynar *msg, int tls_client_cert_required)
  140. {
  141. return (tlv_add_u8(msg, TLV_OPT_TLS_CLIENT_CERT_REQUIRED, tls_client_cert_required));
  142. }
  143. int
  144. tlv_add_u16_array(struct dynar *msg, enum tlv_opt_type opt_type, const uint16_t *array,
  145. size_t array_size)
  146. {
  147. size_t i;
  148. uint16_t *nu16a;
  149. uint16_t opt_len;
  150. int res;
  151. nu16a = malloc(sizeof(uint16_t) * array_size);
  152. if (nu16a == NULL) {
  153. return (-1);
  154. }
  155. for (i = 0; i < array_size; i++) {
  156. nu16a[i] = htons(array[i]);
  157. }
  158. opt_len = sizeof(uint16_t) * array_size;
  159. res = tlv_add(msg, opt_type, opt_len, nu16a);
  160. free(nu16a);
  161. return (res);
  162. }
  163. int
  164. tlv_add_supported_options(struct dynar *msg, const enum tlv_opt_type *supported_options,
  165. size_t no_supported_options)
  166. {
  167. uint16_t *u16a;
  168. size_t i;
  169. int res;
  170. u16a = malloc(sizeof(*u16a) * no_supported_options);
  171. if (u16a == NULL) {
  172. return (-1);
  173. }
  174. for (i = 0; i < no_supported_options; i++) {
  175. u16a[i] = (uint16_t)supported_options[i];
  176. }
  177. res = (tlv_add_u16_array(msg, TLV_OPT_SUPPORTED_OPTIONS, u16a, no_supported_options));
  178. free(u16a);
  179. return (res);
  180. }
  181. int
  182. tlv_add_supported_decision_algorithms(struct dynar *msg,
  183. const enum tlv_decision_algorithm_type *supported_algorithms, size_t no_supported_algorithms)
  184. {
  185. uint16_t *u16a;
  186. size_t i;
  187. int res;
  188. u16a = malloc(sizeof(*u16a) * no_supported_algorithms);
  189. if (u16a == NULL) {
  190. return (-1);
  191. }
  192. for (i = 0; i < no_supported_algorithms; i++) {
  193. u16a[i] = (uint16_t)supported_algorithms[i];
  194. }
  195. res = (tlv_add_u16_array(msg, TLV_OPT_SUPPORTED_DECISION_ALGORITHMS, u16a,
  196. no_supported_algorithms));
  197. free(u16a);
  198. return (res);
  199. }
  200. int
  201. tlv_add_reply_error_code(struct dynar *msg, enum tlv_reply_error_code error_code)
  202. {
  203. return (tlv_add_u16(msg, TLV_OPT_REPLY_ERROR_CODE, (uint16_t)error_code));
  204. }
  205. int
  206. tlv_add_server_maximum_request_size(struct dynar *msg, size_t server_maximum_request_size)
  207. {
  208. return (tlv_add_u32(msg, TLV_OPT_SERVER_MAXIMUM_REQUEST_SIZE, server_maximum_request_size));
  209. }
  210. int
  211. tlv_add_server_maximum_reply_size(struct dynar *msg, size_t server_maximum_reply_size)
  212. {
  213. return (tlv_add_u32(msg, TLV_OPT_SERVER_MAXIMUM_REPLY_SIZE, server_maximum_reply_size));
  214. }
  215. int
  216. tlv_add_node_id(struct dynar *msg, uint32_t node_id)
  217. {
  218. return (tlv_add_u32(msg, TLV_OPT_NODE_ID, node_id));
  219. }
  220. int
  221. tlv_add_decision_algorithm(struct dynar *msg, enum tlv_decision_algorithm_type decision_algorithm)
  222. {
  223. return (tlv_add_u16(msg, TLV_OPT_DECISION_ALGORITHM, (uint16_t)decision_algorithm));
  224. }
  225. int
  226. tlv_add_heartbeat_interval(struct dynar *msg, uint32_t heartbeat_interval)
  227. {
  228. return (tlv_add_u32(msg, TLV_OPT_HEARTBEAT_INTERVAL, heartbeat_interval));
  229. }
  230. int
  231. tlv_add_ring_id(struct dynar *msg, const struct tlv_ring_id *ring_id)
  232. {
  233. uint64_t nu64;
  234. uint32_t nu32;
  235. char tmp_buf[12];
  236. nu32 = htonl(ring_id->node_id);
  237. nu64 = htobe64(ring_id->seq);
  238. memcpy(tmp_buf, &nu32, sizeof(nu32));
  239. memcpy(tmp_buf + sizeof(nu32), &nu64, sizeof(nu64));
  240. return (tlv_add(msg, TLV_OPT_RING_ID, sizeof(tmp_buf), tmp_buf));
  241. }
  242. int
  243. tlv_add_config_version(struct dynar *msg, uint64_t config_version)
  244. {
  245. return (tlv_add_u64(msg, TLV_OPT_CONFIG_VERSION, config_version));
  246. }
  247. int
  248. tlv_add_data_center_id(struct dynar *msg, uint32_t data_center_id)
  249. {
  250. return (tlv_add_u32(msg, TLV_OPT_DATA_CENTER_ID, data_center_id));
  251. }
  252. int
  253. tlv_add_node_state(struct dynar *msg, enum tlv_node_state node_state)
  254. {
  255. return (tlv_add_u8(msg, TLV_OPT_NODE_STATE, node_state));
  256. }
  257. int
  258. tlv_add_node_info(struct dynar *msg, const struct tlv_node_info *node_info)
  259. {
  260. struct dynar opt_value;
  261. int res;
  262. res = 0;
  263. /*
  264. * Create sub message,
  265. */
  266. dynar_init(&opt_value, 1024);
  267. if ((res = tlv_add_node_id(&opt_value, node_info->node_id)) != 0) {
  268. goto exit_dynar_destroy;
  269. }
  270. if (node_info->data_center_id != 0) {
  271. if ((res = tlv_add_data_center_id(&opt_value, node_info->data_center_id)) != 0) {
  272. goto exit_dynar_destroy;
  273. }
  274. }
  275. if (node_info->node_state != TLV_NODE_STATE_NOT_SET) {
  276. if ((res = tlv_add_node_state(&opt_value, node_info->node_state)) != 0) {
  277. goto exit_dynar_destroy;
  278. }
  279. }
  280. res = tlv_add(msg, TLV_OPT_NODE_INFO, dynar_size(&opt_value), dynar_data(&opt_value));
  281. if (res != 0) {
  282. goto exit_dynar_destroy;
  283. }
  284. exit_dynar_destroy:
  285. dynar_destroy(&opt_value);
  286. return (res);
  287. }
  288. int
  289. tlv_add_node_list_type(struct dynar *msg, enum tlv_node_list_type node_list_type)
  290. {
  291. return (tlv_add_u8(msg, TLV_OPT_NODE_LIST_TYPE, node_list_type));
  292. }
  293. int
  294. tlv_add_vote(struct dynar *msg, enum tlv_vote vote)
  295. {
  296. return (tlv_add_u8(msg, TLV_OPT_VOTE, vote));
  297. }
  298. int
  299. tlv_add_quorate(struct dynar *msg, enum tlv_quorate quorate)
  300. {
  301. return (tlv_add_u8(msg, TLV_OPT_QUORATE, quorate));
  302. }
  303. void
  304. tlv_iter_init_str(const char *msg, size_t msg_len, size_t msg_header_len,
  305. struct tlv_iterator *tlv_iter)
  306. {
  307. tlv_iter->msg = msg;
  308. tlv_iter->msg_len = msg_len;
  309. tlv_iter->current_pos = 0;
  310. tlv_iter->msg_header_len = msg_header_len;
  311. tlv_iter->iter_next_called = 0;
  312. }
  313. void
  314. tlv_iter_init(const struct dynar *msg, size_t msg_header_len, struct tlv_iterator *tlv_iter)
  315. {
  316. tlv_iter_init_str(dynar_data(msg), dynar_size(msg), msg_header_len, tlv_iter);
  317. }
  318. enum tlv_opt_type
  319. tlv_iter_get_type(const struct tlv_iterator *tlv_iter)
  320. {
  321. uint16_t ntype;
  322. uint16_t type;
  323. memcpy(&ntype, tlv_iter->msg + tlv_iter->current_pos, sizeof(ntype));
  324. type = ntohs(ntype);
  325. return (type);
  326. }
  327. uint16_t
  328. tlv_iter_get_len(const struct tlv_iterator *tlv_iter)
  329. {
  330. uint16_t nlen;
  331. uint16_t len;
  332. memcpy(&nlen, tlv_iter->msg + tlv_iter->current_pos + TLV_TYPE_LENGTH, sizeof(nlen));
  333. len = ntohs(nlen);
  334. return (len);
  335. }
  336. const char *
  337. tlv_iter_get_data(const struct tlv_iterator *tlv_iter)
  338. {
  339. return (tlv_iter->msg + tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH);
  340. }
  341. int
  342. tlv_iter_next(struct tlv_iterator *tlv_iter)
  343. {
  344. uint16_t len;
  345. if (tlv_iter->iter_next_called == 0) {
  346. tlv_iter->iter_next_called = 1;
  347. tlv_iter->current_pos = tlv_iter->msg_header_len;
  348. goto check_tlv_validity;
  349. }
  350. len = tlv_iter_get_len(tlv_iter);
  351. if (tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len >=
  352. tlv_iter->msg_len) {
  353. return (0);
  354. }
  355. tlv_iter->current_pos += TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len;
  356. check_tlv_validity:
  357. /*
  358. * Check if tlv is valid = is not larger than whole message
  359. */
  360. len = tlv_iter_get_len(tlv_iter);
  361. if (tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len > tlv_iter->msg_len) {
  362. return (-1);
  363. }
  364. return (1);
  365. }
  366. int
  367. tlv_iter_decode_u32(struct tlv_iterator *tlv_iter, uint32_t *res)
  368. {
  369. const char *opt_data;
  370. uint16_t opt_len;
  371. uint32_t nu32;
  372. opt_len = tlv_iter_get_len(tlv_iter);
  373. opt_data = tlv_iter_get_data(tlv_iter);
  374. if (opt_len != sizeof(nu32)) {
  375. return (-1);
  376. }
  377. memcpy(&nu32, opt_data, sizeof(nu32));
  378. *res = ntohl(nu32);
  379. return (0);
  380. }
  381. int
  382. tlv_iter_decode_u8(struct tlv_iterator *tlv_iter, uint8_t *res)
  383. {
  384. const char *opt_data;
  385. uint16_t opt_len;
  386. opt_len = tlv_iter_get_len(tlv_iter);
  387. opt_data = tlv_iter_get_data(tlv_iter);
  388. if (opt_len != sizeof(*res)) {
  389. return (-1);
  390. }
  391. memcpy(res, opt_data, sizeof(*res));
  392. return (0);
  393. }
  394. int
  395. tlv_iter_decode_client_cert_required(struct tlv_iterator *tlv_iter, uint8_t *client_cert_required)
  396. {
  397. return (tlv_iter_decode_u8(tlv_iter, client_cert_required));
  398. }
  399. int
  400. tlv_iter_decode_str(struct tlv_iterator *tlv_iter, char **str, size_t *str_len)
  401. {
  402. const char *opt_data;
  403. uint16_t opt_len;
  404. char *tmp_str;
  405. opt_len = tlv_iter_get_len(tlv_iter);
  406. opt_data = tlv_iter_get_data(tlv_iter);
  407. tmp_str = malloc(opt_len + 1);
  408. if (tmp_str == NULL) {
  409. return (-1);
  410. }
  411. memcpy(tmp_str, opt_data, opt_len);
  412. tmp_str[opt_len] = '\0';
  413. *str = tmp_str;
  414. *str_len = opt_len;
  415. return (0);
  416. }
  417. int
  418. tlv_iter_decode_u16_array(struct tlv_iterator *tlv_iter, uint16_t **u16a, size_t *no_items)
  419. {
  420. uint16_t opt_len;
  421. uint16_t *u16a_res;
  422. size_t i;
  423. opt_len = tlv_iter_get_len(tlv_iter);
  424. if (opt_len % sizeof(uint16_t) != 0) {
  425. return (-1);
  426. }
  427. *no_items = opt_len / sizeof(uint16_t);
  428. u16a_res = malloc(sizeof(uint16_t) * *no_items);
  429. if (u16a_res == NULL) {
  430. return (-2);
  431. }
  432. memcpy(u16a_res, tlv_iter_get_data(tlv_iter), opt_len);
  433. for (i = 0; i < *no_items; i++) {
  434. u16a_res[i] = ntohs(u16a_res[i]);
  435. }
  436. *u16a = u16a_res;
  437. return (0);
  438. }
  439. int
  440. tlv_iter_decode_supported_options(struct tlv_iterator *tlv_iter,
  441. enum tlv_opt_type **supported_options, size_t *no_supported_options)
  442. {
  443. uint16_t *u16a;
  444. enum tlv_opt_type *tlv_opt_array;
  445. size_t i;
  446. int res;
  447. res = tlv_iter_decode_u16_array(tlv_iter, &u16a, no_supported_options);
  448. if (res != 0) {
  449. return (res);
  450. }
  451. tlv_opt_array = malloc(sizeof(enum tlv_opt_type) * *no_supported_options);
  452. if (tlv_opt_array == NULL) {
  453. free(u16a);
  454. return (-2);
  455. }
  456. for (i = 0; i < *no_supported_options; i++) {
  457. tlv_opt_array[i] = (enum tlv_opt_type)u16a[i];
  458. }
  459. free(u16a);
  460. *supported_options = tlv_opt_array;
  461. return (0);
  462. }
  463. int
  464. tlv_iter_decode_supported_decision_algorithms(struct tlv_iterator *tlv_iter,
  465. enum tlv_decision_algorithm_type **supported_decision_algorithms,
  466. size_t *no_supported_decision_algorithms)
  467. {
  468. uint16_t *u16a;
  469. enum tlv_decision_algorithm_type *tlv_decision_algorithm_type_array;
  470. size_t i;
  471. int res;
  472. res = tlv_iter_decode_u16_array(tlv_iter, &u16a, no_supported_decision_algorithms);
  473. if (res != 0) {
  474. return (res);
  475. }
  476. tlv_decision_algorithm_type_array = malloc(
  477. sizeof(enum tlv_decision_algorithm_type) * *no_supported_decision_algorithms);
  478. if (tlv_decision_algorithm_type_array == NULL) {
  479. free(u16a);
  480. return (-2);
  481. }
  482. for (i = 0; i < *no_supported_decision_algorithms; i++) {
  483. tlv_decision_algorithm_type_array[i] = (enum tlv_decision_algorithm_type)u16a[i];
  484. }
  485. free(u16a);
  486. *supported_decision_algorithms = tlv_decision_algorithm_type_array;
  487. return (0);
  488. }
  489. int
  490. tlv_iter_decode_u16(struct tlv_iterator *tlv_iter, uint16_t *u16)
  491. {
  492. const char *opt_data;
  493. uint16_t opt_len;
  494. uint16_t nu16;
  495. opt_len = tlv_iter_get_len(tlv_iter);
  496. opt_data = tlv_iter_get_data(tlv_iter);
  497. if (opt_len != sizeof(nu16)) {
  498. return (-1);
  499. }
  500. memcpy(&nu16, opt_data, sizeof(nu16));
  501. *u16 = ntohs(nu16);
  502. return (0);
  503. }
  504. int
  505. tlv_iter_decode_u64(struct tlv_iterator *tlv_iter, uint64_t *u64)
  506. {
  507. const char *opt_data;
  508. uint64_t opt_len;
  509. uint64_t nu64;
  510. opt_len = tlv_iter_get_len(tlv_iter);
  511. opt_data = tlv_iter_get_data(tlv_iter);
  512. if (opt_len != sizeof(nu64)) {
  513. return (-1);
  514. }
  515. memcpy(&nu64, opt_data, sizeof(nu64));
  516. *u64 = be64toh(nu64);
  517. return (0);
  518. }
  519. int
  520. tlv_iter_decode_reply_error_code(struct tlv_iterator *tlv_iter,
  521. enum tlv_reply_error_code *reply_error_code)
  522. {
  523. return (tlv_iter_decode_u16(tlv_iter, (uint16_t *)reply_error_code));
  524. }
  525. int
  526. tlv_iter_decode_tls_supported(struct tlv_iterator *tlv_iter, enum tlv_tls_supported *tls_supported)
  527. {
  528. uint8_t u8;
  529. enum tlv_tls_supported tmp_tls_supported;
  530. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  531. return (-1);
  532. }
  533. tmp_tls_supported = u8;
  534. if (tmp_tls_supported != TLV_TLS_UNSUPPORTED &&
  535. tmp_tls_supported != TLV_TLS_SUPPORTED &&
  536. tmp_tls_supported != TLV_TLS_REQUIRED) {
  537. return (-4);
  538. }
  539. *tls_supported = tmp_tls_supported;
  540. return (0);
  541. }
  542. int
  543. tlv_iter_decode_decision_algorithm(struct tlv_iterator *tlv_iter,
  544. enum tlv_decision_algorithm_type *decision_algorithm)
  545. {
  546. uint16_t u16;
  547. if (tlv_iter_decode_u16(tlv_iter, &u16) != 0) {
  548. return (-1);
  549. }
  550. *decision_algorithm = (enum tlv_decision_algorithm_type)u16;
  551. return (0);
  552. }
  553. int
  554. tlv_iter_decode_ring_id(struct tlv_iterator *tlv_iter, struct tlv_ring_id *ring_id)
  555. {
  556. const char *opt_data;
  557. uint16_t opt_len;
  558. uint32_t nu32;
  559. uint64_t nu64;
  560. char tmp_buf[12];
  561. opt_len = tlv_iter_get_len(tlv_iter);
  562. opt_data = tlv_iter_get_data(tlv_iter);
  563. if (opt_len != sizeof(tmp_buf)) {
  564. return (-1);
  565. }
  566. memcpy(&nu32, opt_data, sizeof(nu32));
  567. memcpy(&nu64, opt_data + sizeof(nu32), sizeof(nu64));
  568. ring_id->node_id = ntohl(nu32);
  569. ring_id->seq = be64toh(nu64);
  570. return (0);
  571. }
  572. int
  573. tlv_iter_decode_node_state(struct tlv_iterator *tlv_iter, enum tlv_node_state *node_state)
  574. {
  575. uint8_t u8;
  576. enum tlv_node_state tmp_node_state;
  577. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  578. return (-1);
  579. }
  580. tmp_node_state = u8;
  581. if (tmp_node_state != TLV_NODE_STATE_MEMBER &&
  582. tmp_node_state != TLV_NODE_STATE_DEAD &&
  583. tmp_node_state != TLV_NODE_STATE_LEAVING) {
  584. return (-4);
  585. }
  586. *node_state = tmp_node_state;
  587. return (0);
  588. }
  589. int
  590. tlv_iter_decode_node_info(struct tlv_iterator *tlv_iter, struct tlv_node_info *node_info)
  591. {
  592. struct tlv_iterator data_tlv_iter;
  593. int iter_res;
  594. int res;
  595. enum tlv_opt_type opt_type;
  596. struct tlv_node_info tmp_node_info;
  597. memset(&tmp_node_info, 0, sizeof(tmp_node_info));
  598. tlv_iter_init_str(tlv_iter_get_data(tlv_iter), tlv_iter_get_len(tlv_iter), 0,
  599. &data_tlv_iter);
  600. while ((iter_res = tlv_iter_next(&data_tlv_iter)) > 0) {
  601. opt_type = tlv_iter_get_type(&data_tlv_iter);
  602. switch (opt_type) {
  603. case TLV_OPT_NODE_ID:
  604. if ((res = tlv_iter_decode_u32(&data_tlv_iter,
  605. &tmp_node_info.node_id)) != 0) {
  606. return (res);
  607. }
  608. break;
  609. case TLV_OPT_DATA_CENTER_ID:
  610. if ((res = tlv_iter_decode_u32(&data_tlv_iter,
  611. &tmp_node_info.data_center_id)) != 0) {
  612. return (res);
  613. }
  614. break;
  615. case TLV_OPT_NODE_STATE:
  616. if ((res = tlv_iter_decode_node_state(&data_tlv_iter,
  617. &tmp_node_info.node_state)) != 0) {
  618. return (res);
  619. }
  620. break;
  621. default:
  622. /*
  623. * Other options are not processed
  624. */
  625. break;
  626. }
  627. }
  628. if (iter_res != 0) {
  629. return (-3);
  630. }
  631. if (tmp_node_info.node_id == 0) {
  632. return (-4);
  633. }
  634. memcpy(node_info, &tmp_node_info, sizeof(tmp_node_info));
  635. return (0);
  636. }
  637. int
  638. tlv_iter_decode_node_list_type(struct tlv_iterator *tlv_iter,
  639. enum tlv_node_list_type *node_list_type)
  640. {
  641. uint8_t u8;
  642. enum tlv_node_list_type tmp_node_list_type;
  643. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  644. return (-1);
  645. }
  646. tmp_node_list_type = u8;
  647. if (tmp_node_list_type != TLV_NODE_LIST_TYPE_INITIAL_CONFIG &&
  648. tmp_node_list_type != TLV_NODE_LIST_TYPE_CHANGED_CONFIG &&
  649. tmp_node_list_type != TLV_NODE_LIST_TYPE_MEMBERSHIP) {
  650. return (-4);
  651. }
  652. *node_list_type = tmp_node_list_type;
  653. return (0);
  654. }
  655. int
  656. tlv_iter_decode_vote(struct tlv_iterator *tlv_iter, enum tlv_vote *vote)
  657. {
  658. uint8_t u8;
  659. enum tlv_vote tmp_vote;
  660. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  661. return (-1);
  662. }
  663. tmp_vote = u8;
  664. if (tmp_vote != TLV_VOTE_ACK &&
  665. tmp_vote != TLV_VOTE_NACK &&
  666. tmp_vote != TLV_VOTE_ASK_LATER &&
  667. tmp_vote != TLV_VOTE_WAIT_FOR_REPLY) {
  668. return (-4);
  669. }
  670. *vote = tmp_vote;
  671. return (0);
  672. }
  673. int
  674. tlv_iter_decode_quorate(struct tlv_iterator *tlv_iter, enum tlv_quorate *quorate)
  675. {
  676. uint8_t u8;
  677. enum tlv_quorate tmp_quorate;
  678. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  679. return (-1);
  680. }
  681. tmp_quorate = u8;
  682. if (tmp_quorate != TLV_QUORATE_QUORATE &&
  683. tmp_quorate != TLV_QUORATE_INQUORATE) {
  684. return (-4);
  685. }
  686. *quorate = tmp_quorate;
  687. return (0);
  688. }
  689. void
  690. tlv_get_supported_options(enum tlv_opt_type **supported_options, size_t *no_supported_options)
  691. {
  692. *supported_options = tlv_static_supported_options;
  693. *no_supported_options = TLV_STATIC_SUPPORTED_OPTIONS_SIZE;
  694. }