trunnel-impl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* trunnel-impl.h -- Implementation helpers for trunnel, included by
  2. * generated trunnel files
  3. *
  4. * Copyright 2014, The Tor Project, Inc.
  5. * See license at the end of this file for copying information.
  6. */
  7. #ifndef TRUNNEL_IMPL_H_INCLUDED_
  8. #define TRUNNEL_IMPL_H_INCLUDED_
  9. #include "trunnel.h"
  10. #include <assert.h>
  11. #include <string.h>
  12. #ifdef TRUNNEL_LOCAL_H
  13. #include "trunnel-local.h"
  14. #endif
  15. #ifdef _MSC_VER
  16. #define uint8_t unsigned char
  17. #define uint16_t unsigned short
  18. #define uint32_t unsigned int
  19. #define uint64_t unsigned __int64
  20. #define inline __inline
  21. #else
  22. #include <stdint.h>
  23. #endif
  24. #ifdef _WIN32
  25. uint32_t trunnel_htonl(uint32_t a);
  26. uint32_t trunnel_ntohl(uint32_t a);
  27. uint16_t trunnel_htons(uint16_t a);
  28. uint16_t trunnel_ntohs(uint16_t a);
  29. #else
  30. #include <arpa/inet.h>
  31. #define trunnel_htonl(x) htonl(x)
  32. #define trunnel_htons(x) htons(x)
  33. #define trunnel_ntohl(x) ntohl(x)
  34. #define trunnel_ntohs(x) ntohs(x)
  35. #endif
  36. uint64_t trunnel_htonll(uint64_t a);
  37. uint64_t trunnel_ntohll(uint64_t a);
  38. #ifndef trunnel_assert
  39. #define trunnel_assert(x) assert(x)
  40. #endif
  41. static inline void
  42. trunnel_set_uint64(void *p, uint64_t v) {
  43. memcpy(p, &v, 8);
  44. }
  45. static inline void
  46. trunnel_set_uint32(void *p, uint32_t v) {
  47. memcpy(p, &v, 4);
  48. }
  49. static inline void
  50. trunnel_set_uint16(void *p, uint16_t v) {
  51. memcpy(p, &v, 2);
  52. }
  53. static inline void
  54. trunnel_set_uint8(void *p, uint8_t v) {
  55. memcpy(p, &v, 1);
  56. }
  57. static inline uint64_t
  58. trunnel_get_uint64(const void *p) {
  59. uint64_t x;
  60. memcpy(&x, p, 8);
  61. return x;
  62. }
  63. static inline uint32_t
  64. trunnel_get_uint32(const void *p) {
  65. uint32_t x;
  66. memcpy(&x, p, 4);
  67. return x;
  68. }
  69. static inline uint16_t
  70. trunnel_get_uint16(const void *p) {
  71. uint16_t x;
  72. memcpy(&x, p, 2);
  73. return x;
  74. }
  75. static inline uint8_t
  76. trunnel_get_uint8(const void *p) {
  77. return *(const uint8_t*)p;
  78. }
  79. #ifdef TRUNNEL_DEBUG_FAILING_ALLOC
  80. extern int trunnel_provoke_alloc_failure;
  81. static inline void *
  82. trunnel_malloc(size_t n)
  83. {
  84. if (trunnel_provoke_alloc_failure) {
  85. if (--trunnel_provoke_alloc_failure == 0)
  86. return NULL;
  87. }
  88. return malloc(n);
  89. }
  90. static inline void *
  91. trunnel_calloc(size_t a, size_t b)
  92. {
  93. if (trunnel_provoke_alloc_failure) {
  94. if (--trunnel_provoke_alloc_failure == 0)
  95. return NULL;
  96. }
  97. return calloc(a,b);
  98. }
  99. static inline char *
  100. trunnel_strdup(const char *s)
  101. {
  102. if (trunnel_provoke_alloc_failure) {
  103. if (--trunnel_provoke_alloc_failure == 0)
  104. return NULL;
  105. }
  106. return strdup(s);
  107. }
  108. #else
  109. #ifndef trunnel_malloc
  110. #define trunnel_malloc(x) (malloc((x)))
  111. #endif
  112. #ifndef trunnel_calloc
  113. #define trunnel_calloc(a,b) (calloc((a),(b)))
  114. #endif
  115. #ifndef trunnel_strdup
  116. #define trunnel_strdup(s) (strdup((s)))
  117. #endif
  118. #endif
  119. #ifndef trunnel_realloc
  120. #define trunnel_realloc(a,b) realloc((a),(b))
  121. #endif
  122. #ifndef trunnel_free_
  123. #define trunnel_free_(x) (free(x))
  124. #endif
  125. #define trunnel_free(x) ((x) ? (trunnel_free_(x),0) : (0))
  126. #ifndef trunnel_abort
  127. #define trunnel_abort() abort()
  128. #endif
  129. #ifndef trunnel_memwipe
  130. #define trunnel_memwipe(mem, len) ((void)0)
  131. #define trunnel_wipestr(s) ((void)0)
  132. #else
  133. #define trunnel_wipestr(s) do { \
  134. if (s) \
  135. trunnel_memwipe(s, strlen(s)); \
  136. } while (0)
  137. #endif
  138. /* ====== dynamic arrays ======== */
  139. #ifdef NDEBUG
  140. #define TRUNNEL_DYNARRAY_GET(da, n) \
  141. ((da)->elts_[(n)])
  142. #else
  143. /** Return the 'n'th element of 'da'. */
  144. #define TRUNNEL_DYNARRAY_GET(da, n) \
  145. (((n) >= (da)->n_ ? (trunnel_abort(),0) : 0), (da)->elts_[(n)])
  146. #endif
  147. /** Change the 'n'th element of 'da' to 'v'. */
  148. #define TRUNNEL_DYNARRAY_SET(da, n, v) do { \
  149. trunnel_assert((n) < (da)->n_); \
  150. (da)->elts_[(n)] = (v); \
  151. } while (0)
  152. /** Expand the dynamic array 'da' of 'elttype' so that it can hold at least
  153. * 'howmanymore' elements than its current capacity. Always tries to increase
  154. * the length of the array. On failure, run the code in 'on_fail' and goto
  155. * trunnel_alloc_failed. */
  156. #define TRUNNEL_DYNARRAY_EXPAND(elttype, da, howmanymore, on_fail) do { \
  157. elttype *newarray; \
  158. newarray = trunnel_dynarray_expand(&(da)->allocated_, \
  159. (da)->elts_, (howmanymore), \
  160. sizeof(elttype)); \
  161. if (newarray == NULL) { \
  162. on_fail; \
  163. goto trunnel_alloc_failed; \
  164. } \
  165. (da)->elts_ = newarray; \
  166. } while (0)
  167. /** Add 'v' to the end of the dynamic array 'da' of 'elttype', expanding it if
  168. * necessary. code in 'on_fail' and goto trunnel_alloc_failed. */
  169. #define TRUNNEL_DYNARRAY_ADD(elttype, da, v, on_fail) do { \
  170. if ((da)->n_ == (da)->allocated_) { \
  171. TRUNNEL_DYNARRAY_EXPAND(elttype, da, 1, on_fail); \
  172. } \
  173. (da)->elts_[(da)->n_++] = (v); \
  174. } while (0)
  175. /** Return the number of elements in 'da'. */
  176. #define TRUNNEL_DYNARRAY_LEN(da) ((da)->n_)
  177. /** Remove all storage held by 'da' and set it to be empty. Does not free
  178. * storage held by the elements themselves. */
  179. #define TRUNNEL_DYNARRAY_CLEAR(da) do { \
  180. trunnel_free((da)->elts_); \
  181. (da)->elts_ = NULL; \
  182. (da)->n_ = (da)->allocated_ = 0; \
  183. } while (0)
  184. /** Remove all storage held by 'da' and set it to be empty. Does not free
  185. * storage held by the elements themselves. */
  186. #define TRUNNEL_DYNARRAY_WIPE(da) do { \
  187. trunnel_memwipe((da)->elts_, (da)->allocated_ * sizeof((da)->elts_[0])); \
  188. } while (0)
  189. /** Helper: wraps or implements an OpenBSD-style reallocarray. Behaves
  190. * as realloc(a, x*y), but verifies that no overflow will occur in the
  191. * multiplication. Returns NULL on failure. */
  192. #ifndef trunnel_reallocarray
  193. void *trunnel_reallocarray(void *a, size_t x, size_t y);
  194. #endif
  195. /** Helper to expand a dynamic array. Behaves as TRUNNEL_DYNARRAY_EXPAND(),
  196. * taking the array of elements in 'ptr', a pointer to thethe current number
  197. * of allocated elements in allocated_p, the minimum numbeer of elements to
  198. * add in 'howmanymore', and the size of a single element in 'eltsize'.
  199. *
  200. * On success, adjust *allocated_p, and return the new value for the array of
  201. * elements. On failure, adjust nothing and return NULL.
  202. */
  203. void *trunnel_dynarray_expand(size_t *allocated_p, void *ptr,
  204. size_t howmanymore, size_t eltsize);
  205. /** Type for a function to free members of a dynarray of pointers. */
  206. typedef void (*trunnel_free_fn_t)(void *);
  207. /**
  208. * Helper to change the length of a dynamic array. Takes pointers to the
  209. * current allocated and n fields of the array in 'allocated_p' and 'len_p',
  210. * and the current array of elements in 'ptr'; takes the length of a single
  211. * element in 'eltsize'. Changes the length to 'newlen'. If 'newlen' is
  212. * greater than the current length, pads the new elements with 0. If newlen
  213. * is less than the current length, and free_fn is non-NULL, treat the
  214. * array as an array of void *, and invoke free_fn() on each removed element.
  215. *
  216. * On success, adjust *allocated_p and *len_p, and return the new value for
  217. * the array of elements. On failure, adjust nothing, set *errcode_ptr to 1,
  218. * and return NULL.
  219. */
  220. void *trunnel_dynarray_setlen(size_t *allocated_p, size_t *len_p,
  221. void *ptr, size_t newlen,
  222. size_t eltsize, trunnel_free_fn_t free_fn,
  223. uint8_t *errcode_ptr);
  224. /**
  225. * Helper: return a pointer to the value of 'str' as a NUL-terminated string.
  226. * Might have to reallocate the storage for 'str' in order to fit in the final
  227. * NUL character. On allocation failure, return NULL.
  228. */
  229. const char *trunnel_string_getstr(trunnel_string_t *str);
  230. /**
  231. * Helper: change the contents of 'str' to hold the 'len'-byte string in
  232. * 'inp'. Adjusts the storage to have a terminating NUL that doesn't count
  233. * towards the length of the string. On success, return 0. On failure, set
  234. * *errcode_ptr to 1 and return -1.
  235. */
  236. int trunnel_string_setstr0(trunnel_string_t *str, const char *inp, size_t len,
  237. uint8_t *errcode_ptr);
  238. /**
  239. * As trunnel_dynarray_setlen, but adjusts a string rather than a dynamic
  240. * array, and ensures that the new string is NUL-terminated.
  241. */
  242. int trunnel_string_setlen(trunnel_string_t *str, size_t newlen,
  243. uint8_t *errcode_ptr);
  244. #endif
  245. /*
  246. Copyright 2014 The Tor Project, Inc.
  247. Redistribution and use in source and binary forms, with or without
  248. modification, are permitted provided that the following conditions are
  249. met:
  250. * Redistributions of source code must retain the above copyright
  251. notice, this list of conditions and the following disclaimer.
  252. * Redistributions in binary form must reproduce the above
  253. copyright notice, this list of conditions and the following disclaimer
  254. in the documentation and/or other materials provided with the
  255. distribution.
  256. * Neither the names of the copyright owners nor the names of its
  257. contributors may be used to endorse or promote products derived from
  258. this software without specific prior written permission.
  259. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  260. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  261. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  262. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  263. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  264. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  265. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  266. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  267. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  268. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  269. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  270. */