cell_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* cell_common.c -- generated by Trunnel v1.5.
  2. * https://gitweb.torproject.org/trunnel.git
  3. * You probably shouldn't edit this file.
  4. */
  5. #include <stdlib.h>
  6. #include "trunnel-impl.h"
  7. #include "cell_common.h"
  8. #define TRUNNEL_SET_ERROR_CODE(obj) \
  9. do { \
  10. (obj)->trunnel_error_code_ = 1; \
  11. } while (0)
  12. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  13. /* If we're runnning a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int cellcommon_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || cellcommon_deadcode_dummy__
  17. #else
  18. #define OR_DEADCODE_DUMMY
  19. #endif
  20. #define CHECK_REMAINING(nbytes, label) \
  21. do { \
  22. if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
  23. goto label; \
  24. } \
  25. } while (0)
  26. cell_extension_fields_t *
  27. cell_extension_fields_new(void)
  28. {
  29. cell_extension_fields_t *val = trunnel_calloc(1, sizeof(cell_extension_fields_t));
  30. if (NULL == val)
  31. return NULL;
  32. return val;
  33. }
  34. /** Release all storage held inside 'obj', but do not free 'obj'.
  35. */
  36. static void
  37. cell_extension_fields_clear(cell_extension_fields_t *obj)
  38. {
  39. (void) obj;
  40. TRUNNEL_DYNARRAY_WIPE(&obj->field);
  41. TRUNNEL_DYNARRAY_CLEAR(&obj->field);
  42. }
  43. void
  44. cell_extension_fields_free(cell_extension_fields_t *obj)
  45. {
  46. if (obj == NULL)
  47. return;
  48. cell_extension_fields_clear(obj);
  49. trunnel_memwipe(obj, sizeof(cell_extension_fields_t));
  50. trunnel_free_(obj);
  51. }
  52. uint8_t
  53. cell_extension_fields_get_field_type(cell_extension_fields_t *inp)
  54. {
  55. return inp->field_type;
  56. }
  57. int
  58. cell_extension_fields_set_field_type(cell_extension_fields_t *inp, uint8_t val)
  59. {
  60. inp->field_type = val;
  61. return 0;
  62. }
  63. uint8_t
  64. cell_extension_fields_get_field_len(cell_extension_fields_t *inp)
  65. {
  66. return inp->field_len;
  67. }
  68. int
  69. cell_extension_fields_set_field_len(cell_extension_fields_t *inp, uint8_t val)
  70. {
  71. inp->field_len = val;
  72. return 0;
  73. }
  74. size_t
  75. cell_extension_fields_getlen_field(const cell_extension_fields_t *inp)
  76. {
  77. return TRUNNEL_DYNARRAY_LEN(&inp->field);
  78. }
  79. uint8_t
  80. cell_extension_fields_get_field(cell_extension_fields_t *inp, size_t idx)
  81. {
  82. return TRUNNEL_DYNARRAY_GET(&inp->field, idx);
  83. }
  84. uint8_t
  85. cell_extension_fields_getconst_field(const cell_extension_fields_t *inp, size_t idx)
  86. {
  87. return cell_extension_fields_get_field((cell_extension_fields_t*)inp, idx);
  88. }
  89. int
  90. cell_extension_fields_set_field(cell_extension_fields_t *inp, size_t idx, uint8_t elt)
  91. {
  92. TRUNNEL_DYNARRAY_SET(&inp->field, idx, elt);
  93. return 0;
  94. }
  95. int
  96. cell_extension_fields_add_field(cell_extension_fields_t *inp, uint8_t elt)
  97. {
  98. #if SIZE_MAX >= UINT8_MAX
  99. if (inp->field.n_ == UINT8_MAX)
  100. goto trunnel_alloc_failed;
  101. #endif
  102. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->field, elt, {});
  103. return 0;
  104. trunnel_alloc_failed:
  105. TRUNNEL_SET_ERROR_CODE(inp);
  106. return -1;
  107. }
  108. uint8_t *
  109. cell_extension_fields_getarray_field(cell_extension_fields_t *inp)
  110. {
  111. return inp->field.elts_;
  112. }
  113. const uint8_t *
  114. cell_extension_fields_getconstarray_field(const cell_extension_fields_t *inp)
  115. {
  116. return (const uint8_t *)cell_extension_fields_getarray_field((cell_extension_fields_t*)inp);
  117. }
  118. int
  119. cell_extension_fields_setlen_field(cell_extension_fields_t *inp, size_t newlen)
  120. {
  121. uint8_t *newptr;
  122. #if UINT8_MAX < SIZE_MAX
  123. if (newlen > UINT8_MAX)
  124. goto trunnel_alloc_failed;
  125. #endif
  126. newptr = trunnel_dynarray_setlen(&inp->field.allocated_,
  127. &inp->field.n_, inp->field.elts_, newlen,
  128. sizeof(inp->field.elts_[0]), (trunnel_free_fn_t) NULL,
  129. &inp->trunnel_error_code_);
  130. if (newlen != 0 && newptr == NULL)
  131. goto trunnel_alloc_failed;
  132. inp->field.elts_ = newptr;
  133. return 0;
  134. trunnel_alloc_failed:
  135. TRUNNEL_SET_ERROR_CODE(inp);
  136. return -1;
  137. }
  138. const char *
  139. cell_extension_fields_check(const cell_extension_fields_t *obj)
  140. {
  141. if (obj == NULL)
  142. return "Object was NULL";
  143. if (obj->trunnel_error_code_)
  144. return "A set function failed on this object";
  145. if (TRUNNEL_DYNARRAY_LEN(&obj->field) != obj->field_len)
  146. return "Length mismatch for field";
  147. return NULL;
  148. }
  149. ssize_t
  150. cell_extension_fields_encoded_len(const cell_extension_fields_t *obj)
  151. {
  152. ssize_t result = 0;
  153. if (NULL != cell_extension_fields_check(obj))
  154. return -1;
  155. /* Length of u8 field_type */
  156. result += 1;
  157. /* Length of u8 field_len */
  158. result += 1;
  159. /* Length of u8 field[field_len] */
  160. result += TRUNNEL_DYNARRAY_LEN(&obj->field);
  161. return result;
  162. }
  163. int
  164. cell_extension_fields_clear_errors(cell_extension_fields_t *obj)
  165. {
  166. int r = obj->trunnel_error_code_;
  167. obj->trunnel_error_code_ = 0;
  168. return r;
  169. }
  170. ssize_t
  171. cell_extension_fields_encode(uint8_t *output, const size_t avail, const cell_extension_fields_t *obj)
  172. {
  173. ssize_t result = 0;
  174. size_t written = 0;
  175. uint8_t *ptr = output;
  176. const char *msg;
  177. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  178. const ssize_t encoded_len = cell_extension_fields_encoded_len(obj);
  179. #endif
  180. if (NULL != (msg = cell_extension_fields_check(obj)))
  181. goto check_failed;
  182. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  183. trunnel_assert(encoded_len >= 0);
  184. #endif
  185. /* Encode u8 field_type */
  186. trunnel_assert(written <= avail);
  187. if (avail - written < 1)
  188. goto truncated;
  189. trunnel_set_uint8(ptr, (obj->field_type));
  190. written += 1; ptr += 1;
  191. /* Encode u8 field_len */
  192. trunnel_assert(written <= avail);
  193. if (avail - written < 1)
  194. goto truncated;
  195. trunnel_set_uint8(ptr, (obj->field_len));
  196. written += 1; ptr += 1;
  197. /* Encode u8 field[field_len] */
  198. {
  199. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->field);
  200. trunnel_assert(obj->field_len == elt_len);
  201. trunnel_assert(written <= avail);
  202. if (avail - written < elt_len)
  203. goto truncated;
  204. if (elt_len)
  205. memcpy(ptr, obj->field.elts_, elt_len);
  206. written += elt_len; ptr += elt_len;
  207. }
  208. trunnel_assert(ptr == output + written);
  209. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  210. {
  211. trunnel_assert(encoded_len >= 0);
  212. trunnel_assert((size_t)encoded_len == written);
  213. }
  214. #endif
  215. return written;
  216. truncated:
  217. result = -2;
  218. goto fail;
  219. check_failed:
  220. (void)msg;
  221. result = -1;
  222. goto fail;
  223. fail:
  224. trunnel_assert(result < 0);
  225. return result;
  226. }
  227. /** As cell_extension_fields_parse(), but do not allocate the output
  228. * object.
  229. */
  230. static ssize_t
  231. cell_extension_fields_parse_into(cell_extension_fields_t *obj, const uint8_t *input, const size_t len_in)
  232. {
  233. const uint8_t *ptr = input;
  234. size_t remaining = len_in;
  235. ssize_t result = 0;
  236. (void)result;
  237. /* Parse u8 field_type */
  238. CHECK_REMAINING(1, truncated);
  239. obj->field_type = (trunnel_get_uint8(ptr));
  240. remaining -= 1; ptr += 1;
  241. /* Parse u8 field_len */
  242. CHECK_REMAINING(1, truncated);
  243. obj->field_len = (trunnel_get_uint8(ptr));
  244. remaining -= 1; ptr += 1;
  245. /* Parse u8 field[field_len] */
  246. CHECK_REMAINING(obj->field_len, truncated);
  247. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->field, obj->field_len, {});
  248. obj->field.n_ = obj->field_len;
  249. if (obj->field_len)
  250. memcpy(obj->field.elts_, ptr, obj->field_len);
  251. ptr += obj->field_len; remaining -= obj->field_len;
  252. trunnel_assert(ptr + remaining == input + len_in);
  253. return len_in - remaining;
  254. truncated:
  255. return -2;
  256. trunnel_alloc_failed:
  257. return -1;
  258. }
  259. ssize_t
  260. cell_extension_fields_parse(cell_extension_fields_t **output, const uint8_t *input, const size_t len_in)
  261. {
  262. ssize_t result;
  263. *output = cell_extension_fields_new();
  264. if (NULL == *output)
  265. return -1;
  266. result = cell_extension_fields_parse_into(*output, input, len_in);
  267. if (result < 0) {
  268. cell_extension_fields_free(*output);
  269. *output = NULL;
  270. }
  271. return result;
  272. }
  273. cell_extension_t *
  274. cell_extension_new(void)
  275. {
  276. cell_extension_t *val = trunnel_calloc(1, sizeof(cell_extension_t));
  277. if (NULL == val)
  278. return NULL;
  279. return val;
  280. }
  281. /** Release all storage held inside 'obj', but do not free 'obj'.
  282. */
  283. static void
  284. cell_extension_clear(cell_extension_t *obj)
  285. {
  286. (void) obj;
  287. {
  288. unsigned idx;
  289. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
  290. cell_extension_fields_free(TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
  291. }
  292. }
  293. TRUNNEL_DYNARRAY_WIPE(&obj->fields);
  294. TRUNNEL_DYNARRAY_CLEAR(&obj->fields);
  295. }
  296. void
  297. cell_extension_free(cell_extension_t *obj)
  298. {
  299. if (obj == NULL)
  300. return;
  301. cell_extension_clear(obj);
  302. trunnel_memwipe(obj, sizeof(cell_extension_t));
  303. trunnel_free_(obj);
  304. }
  305. uint8_t
  306. cell_extension_get_num(cell_extension_t *inp)
  307. {
  308. return inp->num;
  309. }
  310. int
  311. cell_extension_set_num(cell_extension_t *inp, uint8_t val)
  312. {
  313. inp->num = val;
  314. return 0;
  315. }
  316. size_t
  317. cell_extension_getlen_fields(const cell_extension_t *inp)
  318. {
  319. return TRUNNEL_DYNARRAY_LEN(&inp->fields);
  320. }
  321. struct cell_extension_fields_st *
  322. cell_extension_get_fields(cell_extension_t *inp, size_t idx)
  323. {
  324. return TRUNNEL_DYNARRAY_GET(&inp->fields, idx);
  325. }
  326. const struct cell_extension_fields_st *
  327. cell_extension_getconst_fields(const cell_extension_t *inp, size_t idx)
  328. {
  329. return cell_extension_get_fields((cell_extension_t*)inp, idx);
  330. }
  331. int
  332. cell_extension_set_fields(cell_extension_t *inp, size_t idx, struct cell_extension_fields_st * elt)
  333. {
  334. cell_extension_fields_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->fields, idx);
  335. if (oldval && oldval != elt)
  336. cell_extension_fields_free(oldval);
  337. return cell_extension_set0_fields(inp, idx, elt);
  338. }
  339. int
  340. cell_extension_set0_fields(cell_extension_t *inp, size_t idx, struct cell_extension_fields_st * elt)
  341. {
  342. TRUNNEL_DYNARRAY_SET(&inp->fields, idx, elt);
  343. return 0;
  344. }
  345. int
  346. cell_extension_add_fields(cell_extension_t *inp, struct cell_extension_fields_st * elt)
  347. {
  348. #if SIZE_MAX >= UINT8_MAX
  349. if (inp->fields.n_ == UINT8_MAX)
  350. goto trunnel_alloc_failed;
  351. #endif
  352. TRUNNEL_DYNARRAY_ADD(struct cell_extension_fields_st *, &inp->fields, elt, {});
  353. return 0;
  354. trunnel_alloc_failed:
  355. TRUNNEL_SET_ERROR_CODE(inp);
  356. return -1;
  357. }
  358. struct cell_extension_fields_st * *
  359. cell_extension_getarray_fields(cell_extension_t *inp)
  360. {
  361. return inp->fields.elts_;
  362. }
  363. const struct cell_extension_fields_st * const *
  364. cell_extension_getconstarray_fields(const cell_extension_t *inp)
  365. {
  366. return (const struct cell_extension_fields_st * const *)cell_extension_getarray_fields((cell_extension_t*)inp);
  367. }
  368. int
  369. cell_extension_setlen_fields(cell_extension_t *inp, size_t newlen)
  370. {
  371. struct cell_extension_fields_st * *newptr;
  372. #if UINT8_MAX < SIZE_MAX
  373. if (newlen > UINT8_MAX)
  374. goto trunnel_alloc_failed;
  375. #endif
  376. newptr = trunnel_dynarray_setlen(&inp->fields.allocated_,
  377. &inp->fields.n_, inp->fields.elts_, newlen,
  378. sizeof(inp->fields.elts_[0]), (trunnel_free_fn_t) cell_extension_fields_free,
  379. &inp->trunnel_error_code_);
  380. if (newlen != 0 && newptr == NULL)
  381. goto trunnel_alloc_failed;
  382. inp->fields.elts_ = newptr;
  383. return 0;
  384. trunnel_alloc_failed:
  385. TRUNNEL_SET_ERROR_CODE(inp);
  386. return -1;
  387. }
  388. const char *
  389. cell_extension_check(const cell_extension_t *obj)
  390. {
  391. if (obj == NULL)
  392. return "Object was NULL";
  393. if (obj->trunnel_error_code_)
  394. return "A set function failed on this object";
  395. {
  396. const char *msg;
  397. unsigned idx;
  398. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
  399. if (NULL != (msg = cell_extension_fields_check(TRUNNEL_DYNARRAY_GET(&obj->fields, idx))))
  400. return msg;
  401. }
  402. }
  403. if (TRUNNEL_DYNARRAY_LEN(&obj->fields) != obj->num)
  404. return "Length mismatch for fields";
  405. return NULL;
  406. }
  407. ssize_t
  408. cell_extension_encoded_len(const cell_extension_t *obj)
  409. {
  410. ssize_t result = 0;
  411. if (NULL != cell_extension_check(obj))
  412. return -1;
  413. /* Length of u8 num */
  414. result += 1;
  415. /* Length of struct cell_extension_fields fields[num] */
  416. {
  417. unsigned idx;
  418. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
  419. result += cell_extension_fields_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
  420. }
  421. }
  422. return result;
  423. }
  424. int
  425. cell_extension_clear_errors(cell_extension_t *obj)
  426. {
  427. int r = obj->trunnel_error_code_;
  428. obj->trunnel_error_code_ = 0;
  429. return r;
  430. }
  431. ssize_t
  432. cell_extension_encode(uint8_t *output, const size_t avail, const cell_extension_t *obj)
  433. {
  434. ssize_t result = 0;
  435. size_t written = 0;
  436. uint8_t *ptr = output;
  437. const char *msg;
  438. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  439. const ssize_t encoded_len = cell_extension_encoded_len(obj);
  440. #endif
  441. if (NULL != (msg = cell_extension_check(obj)))
  442. goto check_failed;
  443. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  444. trunnel_assert(encoded_len >= 0);
  445. #endif
  446. /* Encode u8 num */
  447. trunnel_assert(written <= avail);
  448. if (avail - written < 1)
  449. goto truncated;
  450. trunnel_set_uint8(ptr, (obj->num));
  451. written += 1; ptr += 1;
  452. /* Encode struct cell_extension_fields fields[num] */
  453. {
  454. unsigned idx;
  455. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
  456. trunnel_assert(written <= avail);
  457. result = cell_extension_fields_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
  458. if (result < 0)
  459. goto fail; /* XXXXXXX !*/
  460. written += result; ptr += result;
  461. }
  462. }
  463. trunnel_assert(ptr == output + written);
  464. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  465. {
  466. trunnel_assert(encoded_len >= 0);
  467. trunnel_assert((size_t)encoded_len == written);
  468. }
  469. #endif
  470. return written;
  471. truncated:
  472. result = -2;
  473. goto fail;
  474. check_failed:
  475. (void)msg;
  476. result = -1;
  477. goto fail;
  478. fail:
  479. trunnel_assert(result < 0);
  480. return result;
  481. }
  482. /** As cell_extension_parse(), but do not allocate the output object.
  483. */
  484. static ssize_t
  485. cell_extension_parse_into(cell_extension_t *obj, const uint8_t *input, const size_t len_in)
  486. {
  487. const uint8_t *ptr = input;
  488. size_t remaining = len_in;
  489. ssize_t result = 0;
  490. (void)result;
  491. /* Parse u8 num */
  492. CHECK_REMAINING(1, truncated);
  493. obj->num = (trunnel_get_uint8(ptr));
  494. remaining -= 1; ptr += 1;
  495. /* Parse struct cell_extension_fields fields[num] */
  496. TRUNNEL_DYNARRAY_EXPAND(cell_extension_fields_t *, &obj->fields, obj->num, {});
  497. {
  498. cell_extension_fields_t * elt;
  499. unsigned idx;
  500. for (idx = 0; idx < obj->num; ++idx) {
  501. result = cell_extension_fields_parse(&elt, ptr, remaining);
  502. if (result < 0)
  503. goto relay_fail;
  504. trunnel_assert((size_t)result <= remaining);
  505. remaining -= result; ptr += result;
  506. TRUNNEL_DYNARRAY_ADD(cell_extension_fields_t *, &obj->fields, elt, {cell_extension_fields_free(elt);});
  507. }
  508. }
  509. trunnel_assert(ptr + remaining == input + len_in);
  510. return len_in - remaining;
  511. truncated:
  512. return -2;
  513. relay_fail:
  514. trunnel_assert(result < 0);
  515. return result;
  516. trunnel_alloc_failed:
  517. return -1;
  518. }
  519. ssize_t
  520. cell_extension_parse(cell_extension_t **output, const uint8_t *input, const size_t len_in)
  521. {
  522. ssize_t result;
  523. *output = cell_extension_new();
  524. if (NULL == *output)
  525. return -1;
  526. result = cell_extension_parse_into(*output, input, len_in);
  527. if (result < 0) {
  528. cell_extension_free(*output);
  529. *output = NULL;
  530. }
  531. return result;
  532. }