cell_rendezvous.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* cell_rendezvous.c -- generated by Trunnel v1.5.1.
  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_rendezvous.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 cellrendezvous_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || cellrendezvous_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. trn_cell_rendezvous1_t *
  27. trn_cell_rendezvous1_new(void)
  28. {
  29. trn_cell_rendezvous1_t *val = trunnel_calloc(1, sizeof(trn_cell_rendezvous1_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. trn_cell_rendezvous1_clear(trn_cell_rendezvous1_t *obj)
  38. {
  39. (void) obj;
  40. TRUNNEL_DYNARRAY_WIPE(&obj->handshake_info);
  41. TRUNNEL_DYNARRAY_CLEAR(&obj->handshake_info);
  42. }
  43. void
  44. trn_cell_rendezvous1_free(trn_cell_rendezvous1_t *obj)
  45. {
  46. if (obj == NULL)
  47. return;
  48. trn_cell_rendezvous1_clear(obj);
  49. trunnel_memwipe(obj, sizeof(trn_cell_rendezvous1_t));
  50. trunnel_free_(obj);
  51. }
  52. size_t
  53. trn_cell_rendezvous1_getlen_rendezvous_cookie(const trn_cell_rendezvous1_t *inp)
  54. {
  55. (void)inp; return TRUNNEL_REND_COOKIE_LEN;
  56. }
  57. uint8_t
  58. trn_cell_rendezvous1_get_rendezvous_cookie(trn_cell_rendezvous1_t *inp, size_t idx)
  59. {
  60. trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  61. return inp->rendezvous_cookie[idx];
  62. }
  63. uint8_t
  64. trn_cell_rendezvous1_getconst_rendezvous_cookie(const trn_cell_rendezvous1_t *inp, size_t idx)
  65. {
  66. return trn_cell_rendezvous1_get_rendezvous_cookie((trn_cell_rendezvous1_t*)inp, idx);
  67. }
  68. int
  69. trn_cell_rendezvous1_set_rendezvous_cookie(trn_cell_rendezvous1_t *inp, size_t idx, uint8_t elt)
  70. {
  71. trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN);
  72. inp->rendezvous_cookie[idx] = elt;
  73. return 0;
  74. }
  75. uint8_t *
  76. trn_cell_rendezvous1_getarray_rendezvous_cookie(trn_cell_rendezvous1_t *inp)
  77. {
  78. return inp->rendezvous_cookie;
  79. }
  80. const uint8_t *
  81. trn_cell_rendezvous1_getconstarray_rendezvous_cookie(const trn_cell_rendezvous1_t *inp)
  82. {
  83. return (const uint8_t *)trn_cell_rendezvous1_getarray_rendezvous_cookie((trn_cell_rendezvous1_t*)inp);
  84. }
  85. size_t
  86. trn_cell_rendezvous1_getlen_handshake_info(const trn_cell_rendezvous1_t *inp)
  87. {
  88. return TRUNNEL_DYNARRAY_LEN(&inp->handshake_info);
  89. }
  90. uint8_t
  91. trn_cell_rendezvous1_get_handshake_info(trn_cell_rendezvous1_t *inp, size_t idx)
  92. {
  93. return TRUNNEL_DYNARRAY_GET(&inp->handshake_info, idx);
  94. }
  95. uint8_t
  96. trn_cell_rendezvous1_getconst_handshake_info(const trn_cell_rendezvous1_t *inp, size_t idx)
  97. {
  98. return trn_cell_rendezvous1_get_handshake_info((trn_cell_rendezvous1_t*)inp, idx);
  99. }
  100. int
  101. trn_cell_rendezvous1_set_handshake_info(trn_cell_rendezvous1_t *inp, size_t idx, uint8_t elt)
  102. {
  103. TRUNNEL_DYNARRAY_SET(&inp->handshake_info, idx, elt);
  104. return 0;
  105. }
  106. int
  107. trn_cell_rendezvous1_add_handshake_info(trn_cell_rendezvous1_t *inp, uint8_t elt)
  108. {
  109. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->handshake_info, elt, {});
  110. return 0;
  111. trunnel_alloc_failed:
  112. TRUNNEL_SET_ERROR_CODE(inp);
  113. return -1;
  114. }
  115. uint8_t *
  116. trn_cell_rendezvous1_getarray_handshake_info(trn_cell_rendezvous1_t *inp)
  117. {
  118. return inp->handshake_info.elts_;
  119. }
  120. const uint8_t *
  121. trn_cell_rendezvous1_getconstarray_handshake_info(const trn_cell_rendezvous1_t *inp)
  122. {
  123. return (const uint8_t *)trn_cell_rendezvous1_getarray_handshake_info((trn_cell_rendezvous1_t*)inp);
  124. }
  125. int
  126. trn_cell_rendezvous1_setlen_handshake_info(trn_cell_rendezvous1_t *inp, size_t newlen)
  127. {
  128. uint8_t *newptr;
  129. newptr = trunnel_dynarray_setlen(&inp->handshake_info.allocated_,
  130. &inp->handshake_info.n_, inp->handshake_info.elts_, newlen,
  131. sizeof(inp->handshake_info.elts_[0]), (trunnel_free_fn_t) NULL,
  132. &inp->trunnel_error_code_);
  133. if (newlen != 0 && newptr == NULL)
  134. goto trunnel_alloc_failed;
  135. inp->handshake_info.elts_ = newptr;
  136. return 0;
  137. trunnel_alloc_failed:
  138. TRUNNEL_SET_ERROR_CODE(inp);
  139. return -1;
  140. }
  141. const char *
  142. trn_cell_rendezvous1_check(const trn_cell_rendezvous1_t *obj)
  143. {
  144. if (obj == NULL)
  145. return "Object was NULL";
  146. if (obj->trunnel_error_code_)
  147. return "A set function failed on this object";
  148. return NULL;
  149. }
  150. ssize_t
  151. trn_cell_rendezvous1_encoded_len(const trn_cell_rendezvous1_t *obj)
  152. {
  153. ssize_t result = 0;
  154. if (NULL != trn_cell_rendezvous1_check(obj))
  155. return -1;
  156. /* Length of u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */
  157. result += TRUNNEL_REND_COOKIE_LEN;
  158. /* Length of u8 handshake_info[] */
  159. result += TRUNNEL_DYNARRAY_LEN(&obj->handshake_info);
  160. return result;
  161. }
  162. int
  163. trn_cell_rendezvous1_clear_errors(trn_cell_rendezvous1_t *obj)
  164. {
  165. int r = obj->trunnel_error_code_;
  166. obj->trunnel_error_code_ = 0;
  167. return r;
  168. }
  169. ssize_t
  170. trn_cell_rendezvous1_encode(uint8_t *output, const size_t avail, const trn_cell_rendezvous1_t *obj)
  171. {
  172. ssize_t result = 0;
  173. size_t written = 0;
  174. uint8_t *ptr = output;
  175. const char *msg;
  176. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  177. const ssize_t encoded_len = trn_cell_rendezvous1_encoded_len(obj);
  178. #endif
  179. if (NULL != (msg = trn_cell_rendezvous1_check(obj)))
  180. goto check_failed;
  181. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  182. trunnel_assert(encoded_len >= 0);
  183. #endif
  184. /* Encode u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */
  185. trunnel_assert(written <= avail);
  186. if (avail - written < TRUNNEL_REND_COOKIE_LEN)
  187. goto truncated;
  188. memcpy(ptr, obj->rendezvous_cookie, TRUNNEL_REND_COOKIE_LEN);
  189. written += TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;
  190. /* Encode u8 handshake_info[] */
  191. {
  192. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->handshake_info);
  193. trunnel_assert(written <= avail);
  194. if (avail - written < elt_len)
  195. goto truncated;
  196. if (elt_len)
  197. memcpy(ptr, obj->handshake_info.elts_, elt_len);
  198. written += elt_len; ptr += elt_len;
  199. }
  200. trunnel_assert(ptr == output + written);
  201. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  202. {
  203. trunnel_assert(encoded_len >= 0);
  204. trunnel_assert((size_t)encoded_len == written);
  205. }
  206. #endif
  207. return written;
  208. truncated:
  209. result = -2;
  210. goto fail;
  211. check_failed:
  212. (void)msg;
  213. result = -1;
  214. goto fail;
  215. fail:
  216. trunnel_assert(result < 0);
  217. return result;
  218. }
  219. /** As trn_cell_rendezvous1_parse(), but do not allocate the output
  220. * object.
  221. */
  222. static ssize_t
  223. trn_cell_rendezvous1_parse_into(trn_cell_rendezvous1_t *obj, const uint8_t *input, const size_t len_in)
  224. {
  225. const uint8_t *ptr = input;
  226. size_t remaining = len_in;
  227. ssize_t result = 0;
  228. (void)result;
  229. /* Parse u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */
  230. CHECK_REMAINING(TRUNNEL_REND_COOKIE_LEN, truncated);
  231. memcpy(obj->rendezvous_cookie, ptr, TRUNNEL_REND_COOKIE_LEN);
  232. remaining -= TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN;
  233. /* Parse u8 handshake_info[] */
  234. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->handshake_info, remaining, {});
  235. obj->handshake_info.n_ = remaining;
  236. if (remaining)
  237. memcpy(obj->handshake_info.elts_, ptr, remaining);
  238. ptr += remaining; remaining -= remaining;
  239. trunnel_assert(ptr + remaining == input + len_in);
  240. return len_in - remaining;
  241. truncated:
  242. return -2;
  243. trunnel_alloc_failed:
  244. return -1;
  245. }
  246. ssize_t
  247. trn_cell_rendezvous1_parse(trn_cell_rendezvous1_t **output, const uint8_t *input, const size_t len_in)
  248. {
  249. ssize_t result;
  250. *output = trn_cell_rendezvous1_new();
  251. if (NULL == *output)
  252. return -1;
  253. result = trn_cell_rendezvous1_parse_into(*output, input, len_in);
  254. if (result < 0) {
  255. trn_cell_rendezvous1_free(*output);
  256. *output = NULL;
  257. }
  258. return result;
  259. }