tlv.c 24 KB

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