tlv.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. #define be64toh(x) betoh64(x)
  48. #endif
  49. #include "tlv.h"
  50. #define TLV_TYPE_LENGTH 2
  51. #define TLV_LENGTH_LENGTH 2
  52. #define TLV_STATIC_SUPPORTED_OPTIONS_SIZE 22
  53. enum tlv_opt_type tlv_static_supported_options[TLV_STATIC_SUPPORTED_OPTIONS_SIZE] = {
  54. TLV_OPT_MSG_SEQ_NUMBER,
  55. TLV_OPT_CLUSTER_NAME,
  56. TLV_OPT_TLS_SUPPORTED,
  57. TLV_OPT_TLS_CLIENT_CERT_REQUIRED,
  58. TLV_OPT_SUPPORTED_MESSAGES,
  59. TLV_OPT_SUPPORTED_OPTIONS,
  60. TLV_OPT_REPLY_ERROR_CODE,
  61. TLV_OPT_SERVER_MAXIMUM_REQUEST_SIZE,
  62. TLV_OPT_SERVER_MAXIMUM_REPLY_SIZE,
  63. TLV_OPT_NODE_ID,
  64. TLV_OPT_SUPPORTED_DECISION_ALGORITHMS,
  65. TLV_OPT_DECISION_ALGORITHM,
  66. TLV_OPT_HEARTBEAT_INTERVAL,
  67. TLV_OPT_RING_ID,
  68. TLV_OPT_CONFIG_VERSION,
  69. TLV_OPT_DATA_CENTER_ID,
  70. TLV_OPT_NODE_STATE,
  71. TLV_OPT_NODE_INFO,
  72. TLV_OPT_NODE_LIST_TYPE,
  73. TLV_OPT_VOTE,
  74. TLV_OPT_QUORATE,
  75. TLV_OPT_TIE_BREAKER,
  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_tie_breaker(struct dynar *msg, const struct tlv_tie_breaker *tie_breaker)
  244. {
  245. uint32_t nu32;
  246. uint8_t u8;
  247. char tmp_buf[5];
  248. u8 = tie_breaker->mode;
  249. nu32 = (tie_breaker->mode == TLV_TIE_BREAKER_MODE_NODE_ID ?
  250. htonl(tie_breaker->node_id) : 0);
  251. memcpy(tmp_buf, &u8, sizeof(u8));
  252. memcpy(tmp_buf + sizeof(u8), &nu32, sizeof(nu32));
  253. return (tlv_add(msg, TLV_OPT_TIE_BREAKER, sizeof(tmp_buf), tmp_buf));
  254. }
  255. int
  256. tlv_add_config_version(struct dynar *msg, uint64_t config_version)
  257. {
  258. return (tlv_add_u64(msg, TLV_OPT_CONFIG_VERSION, config_version));
  259. }
  260. int
  261. tlv_add_data_center_id(struct dynar *msg, uint32_t data_center_id)
  262. {
  263. return (tlv_add_u32(msg, TLV_OPT_DATA_CENTER_ID, data_center_id));
  264. }
  265. int
  266. tlv_add_node_state(struct dynar *msg, enum tlv_node_state node_state)
  267. {
  268. return (tlv_add_u8(msg, TLV_OPT_NODE_STATE, node_state));
  269. }
  270. int
  271. tlv_add_node_info(struct dynar *msg, const struct tlv_node_info *node_info)
  272. {
  273. struct dynar opt_value;
  274. int res;
  275. res = 0;
  276. /*
  277. * Create sub message,
  278. */
  279. dynar_init(&opt_value, 1024);
  280. if ((res = tlv_add_node_id(&opt_value, node_info->node_id)) != 0) {
  281. goto exit_dynar_destroy;
  282. }
  283. if (node_info->data_center_id != 0) {
  284. if ((res = tlv_add_data_center_id(&opt_value, node_info->data_center_id)) != 0) {
  285. goto exit_dynar_destroy;
  286. }
  287. }
  288. if (node_info->node_state != TLV_NODE_STATE_NOT_SET) {
  289. if ((res = tlv_add_node_state(&opt_value, node_info->node_state)) != 0) {
  290. goto exit_dynar_destroy;
  291. }
  292. }
  293. res = tlv_add(msg, TLV_OPT_NODE_INFO, dynar_size(&opt_value), dynar_data(&opt_value));
  294. if (res != 0) {
  295. goto exit_dynar_destroy;
  296. }
  297. exit_dynar_destroy:
  298. dynar_destroy(&opt_value);
  299. return (res);
  300. }
  301. int
  302. tlv_add_node_list_type(struct dynar *msg, enum tlv_node_list_type node_list_type)
  303. {
  304. return (tlv_add_u8(msg, TLV_OPT_NODE_LIST_TYPE, node_list_type));
  305. }
  306. int
  307. tlv_add_vote(struct dynar *msg, enum tlv_vote vote)
  308. {
  309. return (tlv_add_u8(msg, TLV_OPT_VOTE, vote));
  310. }
  311. int
  312. tlv_add_quorate(struct dynar *msg, enum tlv_quorate quorate)
  313. {
  314. return (tlv_add_u8(msg, TLV_OPT_QUORATE, quorate));
  315. }
  316. void
  317. tlv_iter_init_str(const char *msg, size_t msg_len, size_t msg_header_len,
  318. struct tlv_iterator *tlv_iter)
  319. {
  320. tlv_iter->msg = msg;
  321. tlv_iter->msg_len = msg_len;
  322. tlv_iter->current_pos = 0;
  323. tlv_iter->msg_header_len = msg_header_len;
  324. tlv_iter->iter_next_called = 0;
  325. }
  326. void
  327. tlv_iter_init(const struct dynar *msg, size_t msg_header_len, struct tlv_iterator *tlv_iter)
  328. {
  329. tlv_iter_init_str(dynar_data(msg), dynar_size(msg), msg_header_len, tlv_iter);
  330. }
  331. enum tlv_opt_type
  332. tlv_iter_get_type(const struct tlv_iterator *tlv_iter)
  333. {
  334. uint16_t ntype;
  335. uint16_t type;
  336. memcpy(&ntype, tlv_iter->msg + tlv_iter->current_pos, sizeof(ntype));
  337. type = ntohs(ntype);
  338. return (type);
  339. }
  340. uint16_t
  341. tlv_iter_get_len(const struct tlv_iterator *tlv_iter)
  342. {
  343. uint16_t nlen;
  344. uint16_t len;
  345. memcpy(&nlen, tlv_iter->msg + tlv_iter->current_pos + TLV_TYPE_LENGTH, sizeof(nlen));
  346. len = ntohs(nlen);
  347. return (len);
  348. }
  349. const char *
  350. tlv_iter_get_data(const struct tlv_iterator *tlv_iter)
  351. {
  352. return (tlv_iter->msg + tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH);
  353. }
  354. int
  355. tlv_iter_next(struct tlv_iterator *tlv_iter)
  356. {
  357. uint16_t len;
  358. if (tlv_iter->iter_next_called == 0) {
  359. tlv_iter->iter_next_called = 1;
  360. tlv_iter->current_pos = tlv_iter->msg_header_len;
  361. goto check_tlv_validity;
  362. }
  363. len = tlv_iter_get_len(tlv_iter);
  364. if (tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len >=
  365. tlv_iter->msg_len) {
  366. return (0);
  367. }
  368. tlv_iter->current_pos += TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len;
  369. check_tlv_validity:
  370. /*
  371. * Check if tlv is valid = is not larger than whole message
  372. */
  373. len = tlv_iter_get_len(tlv_iter);
  374. if (tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len > tlv_iter->msg_len) {
  375. return (-1);
  376. }
  377. return (1);
  378. }
  379. int
  380. tlv_iter_decode_u32(struct tlv_iterator *tlv_iter, uint32_t *res)
  381. {
  382. const char *opt_data;
  383. uint16_t opt_len;
  384. uint32_t nu32;
  385. opt_len = tlv_iter_get_len(tlv_iter);
  386. opt_data = tlv_iter_get_data(tlv_iter);
  387. if (opt_len != sizeof(nu32)) {
  388. return (-1);
  389. }
  390. memcpy(&nu32, opt_data, sizeof(nu32));
  391. *res = ntohl(nu32);
  392. return (0);
  393. }
  394. int
  395. tlv_iter_decode_u8(struct tlv_iterator *tlv_iter, uint8_t *res)
  396. {
  397. const char *opt_data;
  398. uint16_t opt_len;
  399. opt_len = tlv_iter_get_len(tlv_iter);
  400. opt_data = tlv_iter_get_data(tlv_iter);
  401. if (opt_len != sizeof(*res)) {
  402. return (-1);
  403. }
  404. memcpy(res, opt_data, sizeof(*res));
  405. return (0);
  406. }
  407. int
  408. tlv_iter_decode_client_cert_required(struct tlv_iterator *tlv_iter, uint8_t *client_cert_required)
  409. {
  410. return (tlv_iter_decode_u8(tlv_iter, client_cert_required));
  411. }
  412. int
  413. tlv_iter_decode_str(struct tlv_iterator *tlv_iter, char **str, size_t *str_len)
  414. {
  415. const char *opt_data;
  416. uint16_t opt_len;
  417. char *tmp_str;
  418. opt_len = tlv_iter_get_len(tlv_iter);
  419. opt_data = tlv_iter_get_data(tlv_iter);
  420. tmp_str = malloc(opt_len + 1);
  421. if (tmp_str == NULL) {
  422. return (-1);
  423. }
  424. memcpy(tmp_str, opt_data, opt_len);
  425. tmp_str[opt_len] = '\0';
  426. *str = tmp_str;
  427. *str_len = opt_len;
  428. return (0);
  429. }
  430. int
  431. tlv_iter_decode_u16_array(struct tlv_iterator *tlv_iter, uint16_t **u16a, size_t *no_items)
  432. {
  433. uint16_t opt_len;
  434. uint16_t *u16a_res;
  435. size_t i;
  436. opt_len = tlv_iter_get_len(tlv_iter);
  437. if (opt_len % sizeof(uint16_t) != 0) {
  438. return (-1);
  439. }
  440. *no_items = opt_len / sizeof(uint16_t);
  441. u16a_res = malloc(sizeof(uint16_t) * *no_items);
  442. if (u16a_res == NULL) {
  443. return (-2);
  444. }
  445. memcpy(u16a_res, tlv_iter_get_data(tlv_iter), opt_len);
  446. for (i = 0; i < *no_items; i++) {
  447. u16a_res[i] = ntohs(u16a_res[i]);
  448. }
  449. *u16a = u16a_res;
  450. return (0);
  451. }
  452. int
  453. tlv_iter_decode_supported_options(struct tlv_iterator *tlv_iter,
  454. enum tlv_opt_type **supported_options, size_t *no_supported_options)
  455. {
  456. uint16_t *u16a;
  457. enum tlv_opt_type *tlv_opt_array;
  458. size_t i;
  459. int res;
  460. res = tlv_iter_decode_u16_array(tlv_iter, &u16a, no_supported_options);
  461. if (res != 0) {
  462. return (res);
  463. }
  464. tlv_opt_array = malloc(sizeof(enum tlv_opt_type) * *no_supported_options);
  465. if (tlv_opt_array == NULL) {
  466. free(u16a);
  467. return (-2);
  468. }
  469. for (i = 0; i < *no_supported_options; i++) {
  470. tlv_opt_array[i] = (enum tlv_opt_type)u16a[i];
  471. }
  472. free(u16a);
  473. *supported_options = tlv_opt_array;
  474. return (0);
  475. }
  476. int
  477. tlv_iter_decode_supported_decision_algorithms(struct tlv_iterator *tlv_iter,
  478. enum tlv_decision_algorithm_type **supported_decision_algorithms,
  479. size_t *no_supported_decision_algorithms)
  480. {
  481. uint16_t *u16a;
  482. enum tlv_decision_algorithm_type *tlv_decision_algorithm_type_array;
  483. size_t i;
  484. int res;
  485. res = tlv_iter_decode_u16_array(tlv_iter, &u16a, no_supported_decision_algorithms);
  486. if (res != 0) {
  487. return (res);
  488. }
  489. tlv_decision_algorithm_type_array = malloc(
  490. sizeof(enum tlv_decision_algorithm_type) * *no_supported_decision_algorithms);
  491. if (tlv_decision_algorithm_type_array == NULL) {
  492. free(u16a);
  493. return (-2);
  494. }
  495. for (i = 0; i < *no_supported_decision_algorithms; i++) {
  496. tlv_decision_algorithm_type_array[i] = (enum tlv_decision_algorithm_type)u16a[i];
  497. }
  498. free(u16a);
  499. *supported_decision_algorithms = tlv_decision_algorithm_type_array;
  500. return (0);
  501. }
  502. int
  503. tlv_iter_decode_u16(struct tlv_iterator *tlv_iter, uint16_t *u16)
  504. {
  505. const char *opt_data;
  506. uint16_t opt_len;
  507. uint16_t nu16;
  508. opt_len = tlv_iter_get_len(tlv_iter);
  509. opt_data = tlv_iter_get_data(tlv_iter);
  510. if (opt_len != sizeof(nu16)) {
  511. return (-1);
  512. }
  513. memcpy(&nu16, opt_data, sizeof(nu16));
  514. *u16 = ntohs(nu16);
  515. return (0);
  516. }
  517. int
  518. tlv_iter_decode_u64(struct tlv_iterator *tlv_iter, uint64_t *u64)
  519. {
  520. const char *opt_data;
  521. uint64_t opt_len;
  522. uint64_t nu64;
  523. opt_len = tlv_iter_get_len(tlv_iter);
  524. opt_data = tlv_iter_get_data(tlv_iter);
  525. if (opt_len != sizeof(nu64)) {
  526. return (-1);
  527. }
  528. memcpy(&nu64, opt_data, sizeof(nu64));
  529. *u64 = be64toh(nu64);
  530. return (0);
  531. }
  532. int
  533. tlv_iter_decode_reply_error_code(struct tlv_iterator *tlv_iter,
  534. enum tlv_reply_error_code *reply_error_code)
  535. {
  536. return (tlv_iter_decode_u16(tlv_iter, (uint16_t *)reply_error_code));
  537. }
  538. int
  539. tlv_iter_decode_tls_supported(struct tlv_iterator *tlv_iter, enum tlv_tls_supported *tls_supported)
  540. {
  541. uint8_t u8;
  542. enum tlv_tls_supported tmp_tls_supported;
  543. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  544. return (-1);
  545. }
  546. tmp_tls_supported = u8;
  547. if (tmp_tls_supported != TLV_TLS_UNSUPPORTED &&
  548. tmp_tls_supported != TLV_TLS_SUPPORTED &&
  549. tmp_tls_supported != TLV_TLS_REQUIRED) {
  550. return (-4);
  551. }
  552. *tls_supported = tmp_tls_supported;
  553. return (0);
  554. }
  555. int
  556. tlv_iter_decode_decision_algorithm(struct tlv_iterator *tlv_iter,
  557. enum tlv_decision_algorithm_type *decision_algorithm)
  558. {
  559. uint16_t u16;
  560. if (tlv_iter_decode_u16(tlv_iter, &u16) != 0) {
  561. return (-1);
  562. }
  563. *decision_algorithm = (enum tlv_decision_algorithm_type)u16;
  564. return (0);
  565. }
  566. int
  567. tlv_iter_decode_ring_id(struct tlv_iterator *tlv_iter, struct tlv_ring_id *ring_id)
  568. {
  569. const char *opt_data;
  570. uint16_t opt_len;
  571. uint32_t nu32;
  572. uint64_t nu64;
  573. char tmp_buf[12];
  574. opt_len = tlv_iter_get_len(tlv_iter);
  575. opt_data = tlv_iter_get_data(tlv_iter);
  576. if (opt_len != sizeof(tmp_buf)) {
  577. return (-1);
  578. }
  579. memcpy(&nu32, opt_data, sizeof(nu32));
  580. memcpy(&nu64, opt_data + sizeof(nu32), sizeof(nu64));
  581. ring_id->node_id = ntohl(nu32);
  582. ring_id->seq = be64toh(nu64);
  583. return (0);
  584. }
  585. int
  586. tlv_iter_decode_tie_breaker(struct tlv_iterator *tlv_iter, struct tlv_tie_breaker *tie_breaker)
  587. {
  588. const char *opt_data;
  589. uint16_t opt_len;
  590. uint32_t nu32;
  591. uint8_t u8;
  592. enum tlv_tie_breaker_mode tie_breaker_mode;
  593. char tmp_buf[5];
  594. opt_len = tlv_iter_get_len(tlv_iter);
  595. opt_data = tlv_iter_get_data(tlv_iter);
  596. if (opt_len != sizeof(tmp_buf)) {
  597. return (-1);
  598. }
  599. memcpy(&u8, opt_data, sizeof(u8));
  600. tie_breaker_mode = u8;
  601. if (tie_breaker_mode != TLV_TIE_BREAKER_MODE_LOWEST &&
  602. tie_breaker_mode != TLV_TIE_BREAKER_MODE_HIGHEST &&
  603. tie_breaker_mode != TLV_TIE_BREAKER_MODE_NODE_ID) {
  604. return (-4);
  605. }
  606. memcpy(&nu32, opt_data + sizeof(u8), sizeof(nu32));
  607. tie_breaker->mode = tie_breaker_mode;
  608. tie_breaker->node_id = (tie_breaker->mode == TLV_TIE_BREAKER_MODE_NODE_ID ?
  609. ntohl(nu32) : 0);
  610. return (0);
  611. }
  612. int
  613. tlv_iter_decode_node_state(struct tlv_iterator *tlv_iter, enum tlv_node_state *node_state)
  614. {
  615. uint8_t u8;
  616. enum tlv_node_state tmp_node_state;
  617. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  618. return (-1);
  619. }
  620. tmp_node_state = u8;
  621. if (tmp_node_state != TLV_NODE_STATE_MEMBER &&
  622. tmp_node_state != TLV_NODE_STATE_DEAD &&
  623. tmp_node_state != TLV_NODE_STATE_LEAVING) {
  624. return (-4);
  625. }
  626. *node_state = tmp_node_state;
  627. return (0);
  628. }
  629. int
  630. tlv_iter_decode_node_info(struct tlv_iterator *tlv_iter, struct tlv_node_info *node_info)
  631. {
  632. struct tlv_iterator data_tlv_iter;
  633. int iter_res;
  634. int res;
  635. enum tlv_opt_type opt_type;
  636. struct tlv_node_info tmp_node_info;
  637. memset(&tmp_node_info, 0, sizeof(tmp_node_info));
  638. tlv_iter_init_str(tlv_iter_get_data(tlv_iter), tlv_iter_get_len(tlv_iter), 0,
  639. &data_tlv_iter);
  640. while ((iter_res = tlv_iter_next(&data_tlv_iter)) > 0) {
  641. opt_type = tlv_iter_get_type(&data_tlv_iter);
  642. switch (opt_type) {
  643. case TLV_OPT_NODE_ID:
  644. if ((res = tlv_iter_decode_u32(&data_tlv_iter,
  645. &tmp_node_info.node_id)) != 0) {
  646. return (res);
  647. }
  648. break;
  649. case TLV_OPT_DATA_CENTER_ID:
  650. if ((res = tlv_iter_decode_u32(&data_tlv_iter,
  651. &tmp_node_info.data_center_id)) != 0) {
  652. return (res);
  653. }
  654. break;
  655. case TLV_OPT_NODE_STATE:
  656. if ((res = tlv_iter_decode_node_state(&data_tlv_iter,
  657. &tmp_node_info.node_state)) != 0) {
  658. return (res);
  659. }
  660. break;
  661. default:
  662. /*
  663. * Other options are not processed
  664. */
  665. break;
  666. }
  667. }
  668. if (iter_res != 0) {
  669. return (-3);
  670. }
  671. if (tmp_node_info.node_id == 0) {
  672. return (-4);
  673. }
  674. memcpy(node_info, &tmp_node_info, sizeof(tmp_node_info));
  675. return (0);
  676. }
  677. int
  678. tlv_iter_decode_node_list_type(struct tlv_iterator *tlv_iter,
  679. enum tlv_node_list_type *node_list_type)
  680. {
  681. uint8_t u8;
  682. enum tlv_node_list_type tmp_node_list_type;
  683. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  684. return (-1);
  685. }
  686. tmp_node_list_type = u8;
  687. if (tmp_node_list_type != TLV_NODE_LIST_TYPE_INITIAL_CONFIG &&
  688. tmp_node_list_type != TLV_NODE_LIST_TYPE_CHANGED_CONFIG &&
  689. tmp_node_list_type != TLV_NODE_LIST_TYPE_MEMBERSHIP &&
  690. tmp_node_list_type != TLV_NODE_LIST_TYPE_QUORUM) {
  691. return (-4);
  692. }
  693. *node_list_type = tmp_node_list_type;
  694. return (0);
  695. }
  696. int
  697. tlv_iter_decode_vote(struct tlv_iterator *tlv_iter, enum tlv_vote *vote)
  698. {
  699. uint8_t u8;
  700. enum tlv_vote tmp_vote;
  701. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  702. return (-1);
  703. }
  704. tmp_vote = u8;
  705. if (tmp_vote != TLV_VOTE_ACK &&
  706. tmp_vote != TLV_VOTE_NACK &&
  707. tmp_vote != TLV_VOTE_ASK_LATER &&
  708. tmp_vote != TLV_VOTE_WAIT_FOR_REPLY &&
  709. tmp_vote != TLV_VOTE_NO_CHANGE) {
  710. return (-4);
  711. }
  712. *vote = tmp_vote;
  713. return (0);
  714. }
  715. int
  716. tlv_iter_decode_quorate(struct tlv_iterator *tlv_iter, enum tlv_quorate *quorate)
  717. {
  718. uint8_t u8;
  719. enum tlv_quorate tmp_quorate;
  720. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  721. return (-1);
  722. }
  723. tmp_quorate = u8;
  724. if (tmp_quorate != TLV_QUORATE_QUORATE &&
  725. tmp_quorate != TLV_QUORATE_INQUORATE) {
  726. return (-4);
  727. }
  728. *quorate = tmp_quorate;
  729. return (0);
  730. }
  731. void
  732. tlv_get_supported_options(enum tlv_opt_type **supported_options, size_t *no_supported_options)
  733. {
  734. *supported_options = tlv_static_supported_options;
  735. *no_supported_options = TLV_STATIC_SUPPORTED_OPTIONS_SIZE;
  736. }
  737. int
  738. tlv_ring_id_eq(const struct tlv_ring_id *rid1, const struct tlv_ring_id *rid2)
  739. {
  740. return (rid1->node_id == rid2->node_id && rid1->seq == rid2->seq);
  741. }
  742. int
  743. tlv_tie_breaker_eq(const struct tlv_tie_breaker *tb1, const struct tlv_tie_breaker *tb2)
  744. {
  745. if (tb1->mode == tb2->mode && tb1->mode == TLV_TIE_BREAKER_MODE_NODE_ID) {
  746. return (tb1->node_id == tb2->node_id);
  747. }
  748. return (tb1->mode == tb2->mode);
  749. }
  750. const char *
  751. tlv_vote_to_str(enum tlv_vote vote)
  752. {
  753. switch (vote) {
  754. case TLV_VOTE_UNDEFINED: break;
  755. case TLV_VOTE_ACK: return ("ACK"); break;
  756. case TLV_VOTE_NACK: return ("NACK"); break;
  757. case TLV_VOTE_ASK_LATER: return ("Ask later"); break;
  758. case TLV_VOTE_WAIT_FOR_REPLY: return ("Wait for reply"); break;
  759. case TLV_VOTE_NO_CHANGE: return ("No change"); break;
  760. }
  761. return ("Unknown vote value");
  762. }
  763. const char *
  764. tlv_node_state_to_str(enum tlv_node_state state)
  765. {
  766. switch (state) {
  767. case TLV_NODE_STATE_NOT_SET: return ("not set"); break;
  768. case TLV_NODE_STATE_MEMBER: return ("member"); break;
  769. case TLV_NODE_STATE_DEAD: return ("dead"); break;
  770. case TLV_NODE_STATE_LEAVING: return ("leaving"); break;
  771. }
  772. return ("Unhandled node state");
  773. }
  774. const char *
  775. tlv_tls_supported_to_str(enum tlv_tls_supported tls_supported)
  776. {
  777. switch (tls_supported) {
  778. case TLV_TLS_UNSUPPORTED: return ("Unsupported"); break;
  779. case TLV_TLS_SUPPORTED: return ("Supported"); break;
  780. case TLV_TLS_REQUIRED: return ("Required"); break;
  781. }
  782. return ("Unhandled tls supported state");
  783. }
  784. const char *
  785. tlv_decision_algorithm_type_to_str(enum tlv_decision_algorithm_type algorithm)
  786. {
  787. switch (algorithm) {
  788. case TLV_DECISION_ALGORITHM_TYPE_TEST: return ("Test"); break;
  789. case TLV_DECISION_ALGORITHM_TYPE_FFSPLIT: return ("Fifty-Fifty split"); break;
  790. case TLV_DECISION_ALGORITHM_TYPE_2NODELMS: return ("2 Node LMS"); break;
  791. case TLV_DECISION_ALGORITHM_TYPE_LMS: return ("LMS"); break;
  792. }
  793. return ("Unknown algorithm");
  794. }