cell_rendezvous.c (12525B)
1 /* cell_rendezvous.c -- generated by Trunnel v1.5.3. 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 8 #include "cell_rendezvous.h" 9 10 #define TRUNNEL_SET_ERROR_CODE(obj) \ 11 do { \ 12 (obj)->trunnel_error_code_ = 1; \ 13 } while (0) 14 15 #if defined(__COVERITY__) || defined(__clang_analyzer__) 16 /* If we're running a static analysis tool, we don't want it to complain 17 * that some of our remaining-bytes checks are dead-code. */ 18 int cellrendezvous_deadcode_dummy__ = 0; 19 #define OR_DEADCODE_DUMMY || cellrendezvous_deadcode_dummy__ 20 #else 21 #define OR_DEADCODE_DUMMY 22 #endif 23 24 #define CHECK_REMAINING(nbytes, label) \ 25 do { \ 26 if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \ 27 goto label; \ 28 } \ 29 } while (0) 30 31 trn_cell_rendezvous1_t * 32 trn_cell_rendezvous1_new(void) 33 { 34 trn_cell_rendezvous1_t *val = trunnel_calloc(1, sizeof(trn_cell_rendezvous1_t)); 35 if (NULL == val) 36 return NULL; 37 return val; 38 } 39 40 /** Release all storage held inside 'obj', but do not free 'obj'. 41 */ 42 static void 43 trn_cell_rendezvous1_clear(trn_cell_rendezvous1_t *obj) 44 { 45 (void) obj; 46 TRUNNEL_DYNARRAY_WIPE(&obj->handshake_info); 47 TRUNNEL_DYNARRAY_CLEAR(&obj->handshake_info); 48 } 49 50 void 51 trn_cell_rendezvous1_free(trn_cell_rendezvous1_t *obj) 52 { 53 if (obj == NULL) 54 return; 55 trn_cell_rendezvous1_clear(obj); 56 trunnel_memwipe(obj, sizeof(trn_cell_rendezvous1_t)); 57 trunnel_free_(obj); 58 } 59 60 size_t 61 trn_cell_rendezvous1_getlen_rendezvous_cookie(const trn_cell_rendezvous1_t *inp) 62 { 63 (void)inp; return TRUNNEL_REND_COOKIE_LEN; 64 } 65 66 uint8_t 67 trn_cell_rendezvous1_get_rendezvous_cookie(trn_cell_rendezvous1_t *inp, size_t idx) 68 { 69 trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN); 70 return inp->rendezvous_cookie[idx]; 71 } 72 73 uint8_t 74 trn_cell_rendezvous1_getconst_rendezvous_cookie(const trn_cell_rendezvous1_t *inp, size_t idx) 75 { 76 return trn_cell_rendezvous1_get_rendezvous_cookie((trn_cell_rendezvous1_t*)inp, idx); 77 } 78 int 79 trn_cell_rendezvous1_set_rendezvous_cookie(trn_cell_rendezvous1_t *inp, size_t idx, uint8_t elt) 80 { 81 trunnel_assert(idx < TRUNNEL_REND_COOKIE_LEN); 82 inp->rendezvous_cookie[idx] = elt; 83 return 0; 84 } 85 86 uint8_t * 87 trn_cell_rendezvous1_getarray_rendezvous_cookie(trn_cell_rendezvous1_t *inp) 88 { 89 return inp->rendezvous_cookie; 90 } 91 const uint8_t * 92 trn_cell_rendezvous1_getconstarray_rendezvous_cookie(const trn_cell_rendezvous1_t *inp) 93 { 94 return (const uint8_t *)trn_cell_rendezvous1_getarray_rendezvous_cookie((trn_cell_rendezvous1_t*)inp); 95 } 96 size_t 97 trn_cell_rendezvous1_getlen_handshake_info(const trn_cell_rendezvous1_t *inp) 98 { 99 return TRUNNEL_DYNARRAY_LEN(&inp->handshake_info); 100 } 101 102 uint8_t 103 trn_cell_rendezvous1_get_handshake_info(trn_cell_rendezvous1_t *inp, size_t idx) 104 { 105 return TRUNNEL_DYNARRAY_GET(&inp->handshake_info, idx); 106 } 107 108 uint8_t 109 trn_cell_rendezvous1_getconst_handshake_info(const trn_cell_rendezvous1_t *inp, size_t idx) 110 { 111 return trn_cell_rendezvous1_get_handshake_info((trn_cell_rendezvous1_t*)inp, idx); 112 } 113 int 114 trn_cell_rendezvous1_set_handshake_info(trn_cell_rendezvous1_t *inp, size_t idx, uint8_t elt) 115 { 116 TRUNNEL_DYNARRAY_SET(&inp->handshake_info, idx, elt); 117 return 0; 118 } 119 int 120 trn_cell_rendezvous1_add_handshake_info(trn_cell_rendezvous1_t *inp, uint8_t elt) 121 { 122 TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->handshake_info, elt, {}); 123 return 0; 124 trunnel_alloc_failed: 125 TRUNNEL_SET_ERROR_CODE(inp); 126 return -1; 127 } 128 129 uint8_t * 130 trn_cell_rendezvous1_getarray_handshake_info(trn_cell_rendezvous1_t *inp) 131 { 132 return inp->handshake_info.elts_; 133 } 134 const uint8_t * 135 trn_cell_rendezvous1_getconstarray_handshake_info(const trn_cell_rendezvous1_t *inp) 136 { 137 return (const uint8_t *)trn_cell_rendezvous1_getarray_handshake_info((trn_cell_rendezvous1_t*)inp); 138 } 139 int 140 trn_cell_rendezvous1_setlen_handshake_info(trn_cell_rendezvous1_t *inp, size_t newlen) 141 { 142 uint8_t *newptr; 143 newptr = trunnel_dynarray_setlen(&inp->handshake_info.allocated_, 144 &inp->handshake_info.n_, inp->handshake_info.elts_, newlen, 145 sizeof(inp->handshake_info.elts_[0]), (trunnel_free_fn_t) NULL, 146 &inp->trunnel_error_code_); 147 if (newlen != 0 && newptr == NULL) 148 goto trunnel_alloc_failed; 149 inp->handshake_info.elts_ = newptr; 150 return 0; 151 trunnel_alloc_failed: 152 TRUNNEL_SET_ERROR_CODE(inp); 153 return -1; 154 } 155 const char * 156 trn_cell_rendezvous1_check(const trn_cell_rendezvous1_t *obj) 157 { 158 if (obj == NULL) 159 return "Object was NULL"; 160 if (obj->trunnel_error_code_) 161 return "A set function failed on this object"; 162 return NULL; 163 } 164 165 ssize_t 166 trn_cell_rendezvous1_encoded_len(const trn_cell_rendezvous1_t *obj) 167 { 168 ssize_t result = 0; 169 170 if (NULL != trn_cell_rendezvous1_check(obj)) 171 return -1; 172 173 174 /* Length of u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */ 175 result += TRUNNEL_REND_COOKIE_LEN; 176 177 /* Length of u8 handshake_info[] */ 178 result += TRUNNEL_DYNARRAY_LEN(&obj->handshake_info); 179 return result; 180 } 181 int 182 trn_cell_rendezvous1_clear_errors(trn_cell_rendezvous1_t *obj) 183 { 184 int r = obj->trunnel_error_code_; 185 obj->trunnel_error_code_ = 0; 186 return r; 187 } 188 ssize_t 189 trn_cell_rendezvous1_encode(uint8_t *output, const size_t avail, const trn_cell_rendezvous1_t *obj) 190 { 191 ssize_t result = 0; 192 size_t written = 0; 193 uint8_t *ptr = output; 194 const char *msg; 195 #ifdef TRUNNEL_CHECK_ENCODED_LEN 196 const ssize_t encoded_len = trn_cell_rendezvous1_encoded_len(obj); 197 #endif 198 199 if (NULL != (msg = trn_cell_rendezvous1_check(obj))) 200 goto check_failed; 201 202 #ifdef TRUNNEL_CHECK_ENCODED_LEN 203 trunnel_assert(encoded_len >= 0); 204 #endif 205 206 /* Encode u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */ 207 trunnel_assert(written <= avail); 208 if (avail - written < TRUNNEL_REND_COOKIE_LEN) 209 goto truncated; 210 memcpy(ptr, obj->rendezvous_cookie, TRUNNEL_REND_COOKIE_LEN); 211 written += TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN; 212 213 /* Encode u8 handshake_info[] */ 214 { 215 size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->handshake_info); 216 trunnel_assert(written <= avail); 217 if (avail - written < elt_len) 218 goto truncated; 219 if (elt_len) 220 memcpy(ptr, obj->handshake_info.elts_, elt_len); 221 written += elt_len; ptr += elt_len; 222 } 223 224 225 trunnel_assert(ptr == output + written); 226 #ifdef TRUNNEL_CHECK_ENCODED_LEN 227 { 228 trunnel_assert(encoded_len >= 0); 229 trunnel_assert((size_t)encoded_len == written); 230 } 231 232 #endif 233 234 return written; 235 236 truncated: 237 result = -2; 238 goto fail; 239 check_failed: 240 (void)msg; 241 result = -1; 242 goto fail; 243 fail: 244 trunnel_assert(result < 0); 245 return result; 246 } 247 248 /** As trn_cell_rendezvous1_parse(), but do not allocate the output 249 * object. 250 */ 251 static ssize_t 252 trn_cell_rendezvous1_parse_into(trn_cell_rendezvous1_t *obj, const uint8_t *input, const size_t len_in) 253 { 254 const uint8_t *ptr = input; 255 size_t remaining = len_in; 256 ssize_t result = 0; 257 (void)result; 258 259 /* Parse u8 rendezvous_cookie[TRUNNEL_REND_COOKIE_LEN] */ 260 CHECK_REMAINING(TRUNNEL_REND_COOKIE_LEN, truncated); 261 memcpy(obj->rendezvous_cookie, ptr, TRUNNEL_REND_COOKIE_LEN); 262 remaining -= TRUNNEL_REND_COOKIE_LEN; ptr += TRUNNEL_REND_COOKIE_LEN; 263 264 /* Parse u8 handshake_info[] */ 265 TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->handshake_info, remaining, {}); 266 obj->handshake_info.n_ = remaining; 267 if (remaining) 268 memcpy(obj->handshake_info.elts_, ptr, remaining); 269 ptr += remaining; remaining -= remaining; 270 trunnel_assert(ptr + remaining == input + len_in); 271 return len_in - remaining; 272 273 truncated: 274 return -2; 275 trunnel_alloc_failed: 276 return -1; 277 } 278 279 ssize_t 280 trn_cell_rendezvous1_parse(trn_cell_rendezvous1_t **output, const uint8_t *input, const size_t len_in) 281 { 282 ssize_t result; 283 *output = trn_cell_rendezvous1_new(); 284 if (NULL == *output) 285 return -1; 286 result = trn_cell_rendezvous1_parse_into(*output, input, len_in); 287 if (result < 0) { 288 trn_cell_rendezvous1_free(*output); 289 *output = NULL; 290 } 291 return result; 292 } 293 trn_cell_rendezvous2_t * 294 trn_cell_rendezvous2_new(void) 295 { 296 trn_cell_rendezvous2_t *val = trunnel_calloc(1, sizeof(trn_cell_rendezvous2_t)); 297 if (NULL == val) 298 return NULL; 299 return val; 300 } 301 302 /** Release all storage held inside 'obj', but do not free 'obj'. 303 */ 304 static void 305 trn_cell_rendezvous2_clear(trn_cell_rendezvous2_t *obj) 306 { 307 (void) obj; 308 } 309 310 void 311 trn_cell_rendezvous2_free(trn_cell_rendezvous2_t *obj) 312 { 313 if (obj == NULL) 314 return; 315 trn_cell_rendezvous2_clear(obj); 316 trunnel_memwipe(obj, sizeof(trn_cell_rendezvous2_t)); 317 trunnel_free_(obj); 318 } 319 320 size_t 321 trn_cell_rendezvous2_getlen_handshake_info(const trn_cell_rendezvous2_t *inp) 322 { 323 (void)inp; return TRUNNEL_HANDSHAKE_INFO_LEN; 324 } 325 326 uint8_t 327 trn_cell_rendezvous2_get_handshake_info(trn_cell_rendezvous2_t *inp, size_t idx) 328 { 329 trunnel_assert(idx < TRUNNEL_HANDSHAKE_INFO_LEN); 330 return inp->handshake_info[idx]; 331 } 332 333 uint8_t 334 trn_cell_rendezvous2_getconst_handshake_info(const trn_cell_rendezvous2_t *inp, size_t idx) 335 { 336 return trn_cell_rendezvous2_get_handshake_info((trn_cell_rendezvous2_t*)inp, idx); 337 } 338 int 339 trn_cell_rendezvous2_set_handshake_info(trn_cell_rendezvous2_t *inp, size_t idx, uint8_t elt) 340 { 341 trunnel_assert(idx < TRUNNEL_HANDSHAKE_INFO_LEN); 342 inp->handshake_info[idx] = elt; 343 return 0; 344 } 345 346 uint8_t * 347 trn_cell_rendezvous2_getarray_handshake_info(trn_cell_rendezvous2_t *inp) 348 { 349 return inp->handshake_info; 350 } 351 const uint8_t * 352 trn_cell_rendezvous2_getconstarray_handshake_info(const trn_cell_rendezvous2_t *inp) 353 { 354 return (const uint8_t *)trn_cell_rendezvous2_getarray_handshake_info((trn_cell_rendezvous2_t*)inp); 355 } 356 const char * 357 trn_cell_rendezvous2_check(const trn_cell_rendezvous2_t *obj) 358 { 359 if (obj == NULL) 360 return "Object was NULL"; 361 if (obj->trunnel_error_code_) 362 return "A set function failed on this object"; 363 return NULL; 364 } 365 366 ssize_t 367 trn_cell_rendezvous2_encoded_len(const trn_cell_rendezvous2_t *obj) 368 { 369 ssize_t result = 0; 370 371 if (NULL != trn_cell_rendezvous2_check(obj)) 372 return -1; 373 374 375 /* Length of u8 handshake_info[TRUNNEL_HANDSHAKE_INFO_LEN] */ 376 result += TRUNNEL_HANDSHAKE_INFO_LEN; 377 return result; 378 } 379 int 380 trn_cell_rendezvous2_clear_errors(trn_cell_rendezvous2_t *obj) 381 { 382 int r = obj->trunnel_error_code_; 383 obj->trunnel_error_code_ = 0; 384 return r; 385 } 386 ssize_t 387 trn_cell_rendezvous2_encode(uint8_t *output, const size_t avail, const trn_cell_rendezvous2_t *obj) 388 { 389 ssize_t result = 0; 390 size_t written = 0; 391 uint8_t *ptr = output; 392 const char *msg; 393 #ifdef TRUNNEL_CHECK_ENCODED_LEN 394 const ssize_t encoded_len = trn_cell_rendezvous2_encoded_len(obj); 395 #endif 396 397 if (NULL != (msg = trn_cell_rendezvous2_check(obj))) 398 goto check_failed; 399 400 #ifdef TRUNNEL_CHECK_ENCODED_LEN 401 trunnel_assert(encoded_len >= 0); 402 #endif 403 404 /* Encode u8 handshake_info[TRUNNEL_HANDSHAKE_INFO_LEN] */ 405 trunnel_assert(written <= avail); 406 if (avail - written < TRUNNEL_HANDSHAKE_INFO_LEN) 407 goto truncated; 408 memcpy(ptr, obj->handshake_info, TRUNNEL_HANDSHAKE_INFO_LEN); 409 written += TRUNNEL_HANDSHAKE_INFO_LEN; ptr += TRUNNEL_HANDSHAKE_INFO_LEN; 410 411 412 trunnel_assert(ptr == output + written); 413 #ifdef TRUNNEL_CHECK_ENCODED_LEN 414 { 415 trunnel_assert(encoded_len >= 0); 416 trunnel_assert((size_t)encoded_len == written); 417 } 418 419 #endif 420 421 return written; 422 423 truncated: 424 result = -2; 425 goto fail; 426 check_failed: 427 (void)msg; 428 result = -1; 429 goto fail; 430 fail: 431 trunnel_assert(result < 0); 432 return result; 433 } 434 435 /** As trn_cell_rendezvous2_parse(), but do not allocate the output 436 * object. 437 */ 438 static ssize_t 439 trn_cell_rendezvous2_parse_into(trn_cell_rendezvous2_t *obj, const uint8_t *input, const size_t len_in) 440 { 441 const uint8_t *ptr = input; 442 size_t remaining = len_in; 443 ssize_t result = 0; 444 (void)result; 445 446 /* Parse u8 handshake_info[TRUNNEL_HANDSHAKE_INFO_LEN] */ 447 CHECK_REMAINING(TRUNNEL_HANDSHAKE_INFO_LEN, truncated); 448 memcpy(obj->handshake_info, ptr, TRUNNEL_HANDSHAKE_INFO_LEN); 449 remaining -= TRUNNEL_HANDSHAKE_INFO_LEN; ptr += TRUNNEL_HANDSHAKE_INFO_LEN; 450 trunnel_assert(ptr + remaining == input + len_in); 451 return len_in - remaining; 452 453 truncated: 454 return -2; 455 } 456 457 ssize_t 458 trn_cell_rendezvous2_parse(trn_cell_rendezvous2_t **output, const uint8_t *input, const size_t len_in) 459 { 460 ssize_t result; 461 *output = trn_cell_rendezvous2_new(); 462 if (NULL == *output) 463 return -1; 464 result = trn_cell_rendezvous2_parse_into(*output, input, len_in); 465 if (result < 0) { 466 trn_cell_rendezvous2_free(*output); 467 *output = NULL; 468 } 469 return result; 470 }