tlv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. #include "tlv.h"
  40. #define TLV_TYPE_LENGTH 2
  41. #define TLV_LENGTH_LENGTH 2
  42. #define TLV_STATIC_SUPPORTED_OPTIONS_SIZE 13
  43. enum tlv_opt_type tlv_static_supported_options[TLV_STATIC_SUPPORTED_OPTIONS_SIZE] = {
  44. TLV_OPT_MSG_SEQ_NUMBER,
  45. TLV_OPT_CLUSTER_NAME,
  46. TLV_OPT_TLS_SUPPORTED,
  47. TLV_OPT_TLS_CLIENT_CERT_REQUIRED,
  48. TLV_OPT_SUPPORTED_MESSAGES,
  49. TLV_OPT_SUPPORTED_OPTIONS,
  50. TLV_OPT_REPLY_ERROR_CODE,
  51. TLV_OPT_SERVER_MAXIMUM_REQUEST_SIZE,
  52. TLV_OPT_SERVER_MAXIMUM_REPLY_SIZE,
  53. TLV_OPT_NODE_ID,
  54. TLV_OPT_SUPPORTED_DECISION_ALGORITHMS,
  55. TLV_OPT_DECISION_ALGORITHM,
  56. TLV_OPT_HEARTBEAT_INTERVAL,
  57. };
  58. int
  59. tlv_add(struct dynar *msg, enum tlv_opt_type opt_type, uint16_t opt_len, const void *value)
  60. {
  61. uint16_t nlen;
  62. uint16_t nopt_type;
  63. if (dynar_size(msg) + sizeof(nopt_type) + sizeof(nlen) + opt_len > dynar_max_size(msg)) {
  64. return (-1);
  65. }
  66. nopt_type = htons((uint16_t)opt_type);
  67. nlen = htons(opt_len);
  68. dynar_cat(msg, &nopt_type, sizeof(nopt_type));
  69. dynar_cat(msg, &nlen, sizeof(nlen));
  70. dynar_cat(msg, value, opt_len);
  71. return (0);
  72. }
  73. int
  74. tlv_add_u32(struct dynar *msg, enum tlv_opt_type opt_type, uint32_t u32)
  75. {
  76. uint32_t nu32;
  77. nu32 = htonl(u32);
  78. return (tlv_add(msg, opt_type, sizeof(nu32), &nu32));
  79. }
  80. int
  81. tlv_add_u8(struct dynar *msg, enum tlv_opt_type opt_type, uint8_t u8)
  82. {
  83. return (tlv_add(msg, opt_type, sizeof(u8), &u8));
  84. }
  85. int
  86. tlv_add_u16(struct dynar *msg, enum tlv_opt_type opt_type, uint16_t u16)
  87. {
  88. uint16_t nu16;
  89. nu16 = htons(u16);
  90. return (tlv_add(msg, opt_type, sizeof(nu16), &nu16));
  91. }
  92. int
  93. tlv_add_string(struct dynar *msg, enum tlv_opt_type opt_type, const char *str)
  94. {
  95. return (tlv_add(msg, opt_type, strlen(str), str));
  96. }
  97. int
  98. tlv_add_msg_seq_number(struct dynar *msg, uint32_t msg_seq_number)
  99. {
  100. return (tlv_add_u32(msg, TLV_OPT_MSG_SEQ_NUMBER, msg_seq_number));
  101. }
  102. int
  103. tlv_add_cluster_name(struct dynar *msg, const char *cluster_name)
  104. {
  105. return (tlv_add_string(msg, TLV_OPT_CLUSTER_NAME, cluster_name));
  106. }
  107. int
  108. tlv_add_tls_supported(struct dynar *msg, enum tlv_tls_supported tls_supported)
  109. {
  110. return (tlv_add_u8(msg, TLV_OPT_TLS_SUPPORTED, tls_supported));
  111. }
  112. int
  113. tlv_add_tls_client_cert_required(struct dynar *msg, int tls_client_cert_required)
  114. {
  115. return (tlv_add_u8(msg, TLV_OPT_TLS_CLIENT_CERT_REQUIRED, tls_client_cert_required));
  116. }
  117. int
  118. tlv_add_u16_array(struct dynar *msg, enum tlv_opt_type opt_type, const uint16_t *array, size_t array_size)
  119. {
  120. size_t i;
  121. uint16_t *nu16a;
  122. uint16_t opt_len;
  123. int res;
  124. nu16a = malloc(sizeof(uint16_t) * array_size);
  125. if (nu16a == NULL) {
  126. return (-1);
  127. }
  128. for (i = 0; i < array_size; i++) {
  129. nu16a[i] = htons(array[i]);
  130. }
  131. opt_len = sizeof(uint16_t) * array_size;
  132. res = tlv_add(msg, opt_type, opt_len, nu16a);
  133. free(nu16a);
  134. return (res);
  135. }
  136. int
  137. tlv_add_supported_options(struct dynar *msg, const enum tlv_opt_type *supported_options,
  138. size_t no_supported_options)
  139. {
  140. uint16_t *u16a;
  141. size_t i;
  142. int res;
  143. u16a = malloc(sizeof(*u16a) * no_supported_options);
  144. if (u16a == NULL) {
  145. return (-1);
  146. }
  147. for (i = 0; i < no_supported_options; i++) {
  148. u16a[i] = (uint16_t)supported_options[i];
  149. }
  150. res = (tlv_add_u16_array(msg, TLV_OPT_SUPPORTED_OPTIONS, u16a, no_supported_options));
  151. free(u16a);
  152. return (res);
  153. }
  154. int
  155. tlv_add_supported_decision_algorithms(struct dynar *msg, const enum tlv_decision_algorithm_type *supported_algorithms,
  156. size_t no_supported_algorithms)
  157. {
  158. uint16_t *u16a;
  159. size_t i;
  160. int res;
  161. u16a = malloc(sizeof(*u16a) * no_supported_algorithms);
  162. if (u16a == NULL) {
  163. return (-1);
  164. }
  165. for (i = 0; i < no_supported_algorithms; i++) {
  166. u16a[i] = (uint16_t)supported_algorithms[i];
  167. }
  168. res = (tlv_add_u16_array(msg, TLV_OPT_SUPPORTED_DECISION_ALGORITHMS, u16a, no_supported_algorithms));
  169. free(u16a);
  170. return (res);
  171. }
  172. int
  173. tlv_add_reply_error_code(struct dynar *msg, enum tlv_reply_error_code error_code)
  174. {
  175. return (tlv_add_u16(msg, TLV_OPT_REPLY_ERROR_CODE, (uint16_t)error_code));
  176. }
  177. int
  178. tlv_add_server_maximum_request_size(struct dynar *msg, size_t server_maximum_request_size)
  179. {
  180. return (tlv_add_u32(msg, TLV_OPT_SERVER_MAXIMUM_REQUEST_SIZE, server_maximum_request_size));
  181. }
  182. int
  183. tlv_add_server_maximum_reply_size(struct dynar *msg, size_t server_maximum_reply_size)
  184. {
  185. return (tlv_add_u32(msg, TLV_OPT_SERVER_MAXIMUM_REPLY_SIZE, server_maximum_reply_size));
  186. }
  187. int
  188. tlv_add_node_id(struct dynar *msg, uint32_t node_id)
  189. {
  190. return (tlv_add_u32(msg, TLV_OPT_NODE_ID, node_id));
  191. }
  192. int
  193. tlv_add_decision_algorithm(struct dynar *msg, enum tlv_decision_algorithm_type decision_algorithm)
  194. {
  195. return (tlv_add_u16(msg, TLV_OPT_DECISION_ALGORITHM, (uint16_t)decision_algorithm));
  196. }
  197. int
  198. tlv_add_heartbeat_interval(struct dynar *msg, uint32_t heartbeat_interval)
  199. {
  200. return (tlv_add_u32(msg, TLV_OPT_HEARTBEAT_INTERVAL, heartbeat_interval));
  201. }
  202. void
  203. tlv_iter_init(const struct dynar *msg, size_t msg_header_len, struct tlv_iterator *tlv_iter)
  204. {
  205. tlv_iter->msg = msg;
  206. tlv_iter->current_pos = 0;
  207. tlv_iter->msg_header_len = msg_header_len;
  208. }
  209. enum tlv_opt_type
  210. tlv_iter_get_type(const struct tlv_iterator *tlv_iter)
  211. {
  212. uint16_t ntype;
  213. uint16_t type;
  214. memcpy(&ntype, dynar_data(tlv_iter->msg) + tlv_iter->current_pos, sizeof(ntype));
  215. type = ntohs(ntype);
  216. return (type);
  217. }
  218. uint16_t
  219. tlv_iter_get_len(const struct tlv_iterator *tlv_iter)
  220. {
  221. uint16_t nlen;
  222. uint16_t len;
  223. memcpy(&nlen, dynar_data(tlv_iter->msg) + tlv_iter->current_pos + TLV_TYPE_LENGTH, sizeof(nlen));
  224. len = ntohs(nlen);
  225. return (len);
  226. }
  227. const char *
  228. tlv_iter_get_data(const struct tlv_iterator *tlv_iter)
  229. {
  230. return (dynar_data(tlv_iter->msg) + tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH);
  231. }
  232. int
  233. tlv_iter_next(struct tlv_iterator *tlv_iter)
  234. {
  235. uint16_t len;
  236. if (tlv_iter->current_pos == 0) {
  237. tlv_iter->current_pos = tlv_iter->msg_header_len;
  238. goto check_tlv_validity;
  239. }
  240. len = tlv_iter_get_len(tlv_iter);
  241. if (tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len >= dynar_size(tlv_iter->msg)) {
  242. return (0);
  243. }
  244. tlv_iter->current_pos += TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len;
  245. check_tlv_validity:
  246. /*
  247. * Check if tlv is valid = is not larger than whole message
  248. */
  249. len = tlv_iter_get_len(tlv_iter);
  250. if (tlv_iter->current_pos + TLV_TYPE_LENGTH + TLV_LENGTH_LENGTH + len > dynar_size(tlv_iter->msg)) {
  251. return (-1);
  252. }
  253. return (1);
  254. }
  255. int
  256. tlv_iter_decode_u32(struct tlv_iterator *tlv_iter, uint32_t *res)
  257. {
  258. const char *opt_data;
  259. uint16_t opt_len;
  260. uint32_t nu32;
  261. opt_len = tlv_iter_get_len(tlv_iter);
  262. opt_data = tlv_iter_get_data(tlv_iter);
  263. if (opt_len != sizeof(nu32)) {
  264. return (-1);
  265. }
  266. memcpy(&nu32, opt_data, sizeof(nu32));
  267. *res = ntohl(nu32);
  268. return (0);
  269. }
  270. int
  271. tlv_iter_decode_u8(struct tlv_iterator *tlv_iter, uint8_t *res)
  272. {
  273. const char *opt_data;
  274. uint16_t opt_len;
  275. opt_len = tlv_iter_get_len(tlv_iter);
  276. opt_data = tlv_iter_get_data(tlv_iter);
  277. if (opt_len != sizeof(*res)) {
  278. return (-1);
  279. }
  280. memcpy(res, opt_data, sizeof(*res));
  281. return (0);
  282. }
  283. int
  284. tlv_iter_decode_client_cert_required(struct tlv_iterator *tlv_iter, uint8_t *client_cert_required)
  285. {
  286. return (tlv_iter_decode_u8(tlv_iter, client_cert_required));
  287. }
  288. int
  289. tlv_iter_decode_str(struct tlv_iterator *tlv_iter, char **str, size_t *str_len)
  290. {
  291. const char *opt_data;
  292. uint16_t opt_len;
  293. char *tmp_str;
  294. opt_len = tlv_iter_get_len(tlv_iter);
  295. opt_data = tlv_iter_get_data(tlv_iter);
  296. tmp_str = malloc(opt_len + 1);
  297. if (tmp_str == NULL) {
  298. return (-1);
  299. }
  300. memcpy(tmp_str, opt_data, opt_len);
  301. tmp_str[opt_len] = '\0';
  302. *str = tmp_str;
  303. *str_len = opt_len;
  304. return (0);
  305. }
  306. int
  307. tlv_iter_decode_u16_array(struct tlv_iterator *tlv_iter, uint16_t **u16a, size_t *no_items)
  308. {
  309. uint16_t opt_len;
  310. uint16_t *u16a_res;
  311. size_t i;
  312. opt_len = tlv_iter_get_len(tlv_iter);
  313. if (opt_len % sizeof(uint16_t) != 0) {
  314. return (-1);
  315. }
  316. *no_items = opt_len / sizeof(uint16_t);
  317. u16a_res = malloc(sizeof(uint16_t) * *no_items);
  318. if (u16a_res == NULL) {
  319. return (-2);
  320. }
  321. memcpy(u16a_res, tlv_iter_get_data(tlv_iter), opt_len);
  322. for (i = 0; i < *no_items; i++) {
  323. u16a_res[i] = ntohs(u16a_res[i]);
  324. }
  325. *u16a = u16a_res;
  326. return (0);
  327. }
  328. int
  329. tlv_iter_decode_supported_options(struct tlv_iterator *tlv_iter, enum tlv_opt_type **supported_options,
  330. size_t *no_supported_options)
  331. {
  332. uint16_t *u16a;
  333. enum tlv_opt_type *tlv_opt_array;
  334. size_t i;
  335. int res;
  336. res = tlv_iter_decode_u16_array(tlv_iter, &u16a, no_supported_options);
  337. if (res != 0) {
  338. return (res);
  339. }
  340. tlv_opt_array = malloc(sizeof(enum tlv_opt_type) * *no_supported_options);
  341. if (tlv_opt_array == NULL) {
  342. free(u16a);
  343. return (-2);
  344. }
  345. for (i = 0; i < *no_supported_options; i++) {
  346. tlv_opt_array[i] = (enum tlv_opt_type)u16a[i];
  347. }
  348. free(u16a);
  349. *supported_options = tlv_opt_array;
  350. return (0);
  351. }
  352. int
  353. tlv_iter_decode_supported_decision_algorithms(struct tlv_iterator *tlv_iter,
  354. enum tlv_decision_algorithm_type **supported_decision_algorithms, size_t *no_supported_decision_algorithms)
  355. {
  356. uint16_t *u16a;
  357. enum tlv_decision_algorithm_type *tlv_decision_algorithm_type_array;
  358. size_t i;
  359. int res;
  360. res = tlv_iter_decode_u16_array(tlv_iter, &u16a, no_supported_decision_algorithms);
  361. if (res != 0) {
  362. return (res);
  363. }
  364. tlv_decision_algorithm_type_array = malloc(
  365. sizeof(enum tlv_decision_algorithm_type) * *no_supported_decision_algorithms);
  366. if (tlv_decision_algorithm_type_array == NULL) {
  367. free(u16a);
  368. return (-2);
  369. }
  370. for (i = 0; i < *no_supported_decision_algorithms; i++) {
  371. tlv_decision_algorithm_type_array[i] = (enum tlv_decision_algorithm_type)u16a[i];
  372. }
  373. free(u16a);
  374. *supported_decision_algorithms = tlv_decision_algorithm_type_array;
  375. return (0);
  376. }
  377. int
  378. tlv_iter_decode_u16(struct tlv_iterator *tlv_iter, uint16_t *u16)
  379. {
  380. const char *opt_data;
  381. uint16_t opt_len;
  382. uint16_t nu16;
  383. opt_len = tlv_iter_get_len(tlv_iter);
  384. opt_data = tlv_iter_get_data(tlv_iter);
  385. if (opt_len != sizeof(nu16)) {
  386. return (-1);
  387. }
  388. memcpy(&nu16, opt_data, sizeof(nu16));
  389. *u16 = ntohs(nu16);
  390. return (0);
  391. }
  392. int
  393. tlv_iter_decode_reply_error_code(struct tlv_iterator *tlv_iter, enum tlv_reply_error_code *reply_error_code)
  394. {
  395. return (tlv_iter_decode_u16(tlv_iter, (uint16_t *)reply_error_code));
  396. }
  397. int
  398. tlv_iter_decode_tls_supported(struct tlv_iterator *tlv_iter, enum tlv_tls_supported *tls_supported)
  399. {
  400. uint8_t u8;
  401. if (tlv_iter_decode_u8(tlv_iter, &u8) != 0) {
  402. return (-1);
  403. }
  404. *tls_supported = u8;
  405. if (*tls_supported != TLV_TLS_UNSUPPORTED &&
  406. *tls_supported != TLV_TLS_SUPPORTED &&
  407. *tls_supported != TLV_TLS_REQUIRED) {
  408. return (-4);
  409. }
  410. return (0);
  411. }
  412. int
  413. tlv_iter_decode_decision_algorithm(struct tlv_iterator *tlv_iter, enum tlv_decision_algorithm_type *decision_algorithm)
  414. {
  415. uint16_t u16;
  416. if (tlv_iter_decode_u16(tlv_iter, &u16) != 0) {
  417. return (-1);
  418. }
  419. *decision_algorithm = (enum tlv_decision_algorithm_type)u16;
  420. return (0);
  421. }
  422. void
  423. tlv_get_supported_options(enum tlv_opt_type **supported_options, size_t *no_supported_options)
  424. {
  425. *supported_options = tlv_static_supported_options;
  426. *no_supported_options = TLV_STATIC_SUPPORTED_OPTIONS_SIZE;
  427. }