conflux.c (29857B)
1 /* conflux.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 "conflux.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 conflux_deadcode_dummy__ = 0; 19 #define OR_DEADCODE_DUMMY || conflux_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_conflux_link_t * 32 trn_cell_conflux_link_new(void) 33 { 34 trn_cell_conflux_link_t *val = trunnel_calloc(1, sizeof(trn_cell_conflux_link_t)); 35 if (NULL == val) 36 return NULL; 37 val->version = 1; 38 return val; 39 } 40 41 /** Release all storage held inside 'obj', but do not free 'obj'. 42 */ 43 static void 44 trn_cell_conflux_link_clear(trn_cell_conflux_link_t *obj) 45 { 46 (void) obj; 47 TRUNNEL_DYNARRAY_WIPE(&obj->payload); 48 TRUNNEL_DYNARRAY_CLEAR(&obj->payload); 49 } 50 51 void 52 trn_cell_conflux_link_free(trn_cell_conflux_link_t *obj) 53 { 54 if (obj == NULL) 55 return; 56 trn_cell_conflux_link_clear(obj); 57 trunnel_memwipe(obj, sizeof(trn_cell_conflux_link_t)); 58 trunnel_free_(obj); 59 } 60 61 uint8_t 62 trn_cell_conflux_link_get_version(const trn_cell_conflux_link_t *inp) 63 { 64 return inp->version; 65 } 66 int 67 trn_cell_conflux_link_set_version(trn_cell_conflux_link_t *inp, uint8_t val) 68 { 69 if (! ((val == 1))) { 70 TRUNNEL_SET_ERROR_CODE(inp); 71 return -1; 72 } 73 inp->version = val; 74 return 0; 75 } 76 size_t 77 trn_cell_conflux_link_getlen_payload(const trn_cell_conflux_link_t *inp) 78 { 79 return TRUNNEL_DYNARRAY_LEN(&inp->payload); 80 } 81 82 uint8_t 83 trn_cell_conflux_link_get_payload(trn_cell_conflux_link_t *inp, size_t idx) 84 { 85 return TRUNNEL_DYNARRAY_GET(&inp->payload, idx); 86 } 87 88 uint8_t 89 trn_cell_conflux_link_getconst_payload(const trn_cell_conflux_link_t *inp, size_t idx) 90 { 91 return trn_cell_conflux_link_get_payload((trn_cell_conflux_link_t*)inp, idx); 92 } 93 int 94 trn_cell_conflux_link_set_payload(trn_cell_conflux_link_t *inp, size_t idx, uint8_t elt) 95 { 96 TRUNNEL_DYNARRAY_SET(&inp->payload, idx, elt); 97 return 0; 98 } 99 int 100 trn_cell_conflux_link_add_payload(trn_cell_conflux_link_t *inp, uint8_t elt) 101 { 102 TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->payload, elt, {}); 103 return 0; 104 trunnel_alloc_failed: 105 TRUNNEL_SET_ERROR_CODE(inp); 106 return -1; 107 } 108 109 uint8_t * 110 trn_cell_conflux_link_getarray_payload(trn_cell_conflux_link_t *inp) 111 { 112 return inp->payload.elts_; 113 } 114 const uint8_t * 115 trn_cell_conflux_link_getconstarray_payload(const trn_cell_conflux_link_t *inp) 116 { 117 return (const uint8_t *)trn_cell_conflux_link_getarray_payload((trn_cell_conflux_link_t*)inp); 118 } 119 int 120 trn_cell_conflux_link_setlen_payload(trn_cell_conflux_link_t *inp, size_t newlen) 121 { 122 uint8_t *newptr; 123 newptr = trunnel_dynarray_setlen(&inp->payload.allocated_, 124 &inp->payload.n_, inp->payload.elts_, newlen, 125 sizeof(inp->payload.elts_[0]), (trunnel_free_fn_t) NULL, 126 &inp->trunnel_error_code_); 127 if (newlen != 0 && newptr == NULL) 128 goto trunnel_alloc_failed; 129 inp->payload.elts_ = newptr; 130 return 0; 131 trunnel_alloc_failed: 132 TRUNNEL_SET_ERROR_CODE(inp); 133 return -1; 134 } 135 const char * 136 trn_cell_conflux_link_check(const trn_cell_conflux_link_t *obj) 137 { 138 if (obj == NULL) 139 return "Object was NULL"; 140 if (obj->trunnel_error_code_) 141 return "A set function failed on this object"; 142 if (! (obj->version == 1)) 143 return "Integer out of bounds"; 144 return NULL; 145 } 146 147 ssize_t 148 trn_cell_conflux_link_encoded_len(const trn_cell_conflux_link_t *obj) 149 { 150 ssize_t result = 0; 151 152 if (NULL != trn_cell_conflux_link_check(obj)) 153 return -1; 154 155 156 /* Length of u8 version IN [1] */ 157 result += 1; 158 159 /* Length of u8 payload[] */ 160 result += TRUNNEL_DYNARRAY_LEN(&obj->payload); 161 return result; 162 } 163 int 164 trn_cell_conflux_link_clear_errors(trn_cell_conflux_link_t *obj) 165 { 166 int r = obj->trunnel_error_code_; 167 obj->trunnel_error_code_ = 0; 168 return r; 169 } 170 ssize_t 171 trn_cell_conflux_link_encode(uint8_t *output, const size_t avail, const trn_cell_conflux_link_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 = trn_cell_conflux_link_encoded_len(obj); 179 #endif 180 181 if (NULL != (msg = trn_cell_conflux_link_check(obj))) 182 goto check_failed; 183 184 #ifdef TRUNNEL_CHECK_ENCODED_LEN 185 trunnel_assert(encoded_len >= 0); 186 #endif 187 188 /* Encode u8 version IN [1] */ 189 trunnel_assert(written <= avail); 190 if (avail - written < 1) 191 goto truncated; 192 trunnel_set_uint8(ptr, (obj->version)); 193 written += 1; ptr += 1; 194 195 /* Encode u8 payload[] */ 196 { 197 size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->payload); 198 trunnel_assert(written <= avail); 199 if (avail - written < elt_len) 200 goto truncated; 201 if (elt_len) 202 memcpy(ptr, obj->payload.elts_, elt_len); 203 written += elt_len; ptr += elt_len; 204 } 205 206 207 trunnel_assert(ptr == output + written); 208 #ifdef TRUNNEL_CHECK_ENCODED_LEN 209 { 210 trunnel_assert(encoded_len >= 0); 211 trunnel_assert((size_t)encoded_len == written); 212 } 213 214 #endif 215 216 return written; 217 218 truncated: 219 result = -2; 220 goto fail; 221 check_failed: 222 (void)msg; 223 result = -1; 224 goto fail; 225 fail: 226 trunnel_assert(result < 0); 227 return result; 228 } 229 230 /** As trn_cell_conflux_link_parse(), but do not allocate the output 231 * object. 232 */ 233 static ssize_t 234 trn_cell_conflux_link_parse_into(trn_cell_conflux_link_t *obj, const uint8_t *input, const size_t len_in) 235 { 236 const uint8_t *ptr = input; 237 size_t remaining = len_in; 238 ssize_t result = 0; 239 (void)result; 240 241 /* Parse u8 version IN [1] */ 242 CHECK_REMAINING(1, truncated); 243 obj->version = (trunnel_get_uint8(ptr)); 244 remaining -= 1; ptr += 1; 245 if (! (obj->version == 1)) 246 goto fail; 247 248 /* Parse u8 payload[] */ 249 TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->payload, remaining, {}); 250 obj->payload.n_ = remaining; 251 if (remaining) 252 memcpy(obj->payload.elts_, ptr, remaining); 253 ptr += remaining; remaining -= remaining; 254 trunnel_assert(ptr + remaining == input + len_in); 255 return len_in - remaining; 256 257 truncated: 258 return -2; 259 trunnel_alloc_failed: 260 return -1; 261 fail: 262 result = -1; 263 return result; 264 } 265 266 ssize_t 267 trn_cell_conflux_link_parse(trn_cell_conflux_link_t **output, const uint8_t *input, const size_t len_in) 268 { 269 ssize_t result; 270 *output = trn_cell_conflux_link_new(); 271 if (NULL == *output) 272 return -1; 273 result = trn_cell_conflux_link_parse_into(*output, input, len_in); 274 if (result < 0) { 275 trn_cell_conflux_link_free(*output); 276 *output = NULL; 277 } 278 return result; 279 } 280 trn_cell_conflux_link_payload_v1_t * 281 trn_cell_conflux_link_payload_v1_new(void) 282 { 283 trn_cell_conflux_link_payload_v1_t *val = trunnel_calloc(1, sizeof(trn_cell_conflux_link_payload_v1_t)); 284 if (NULL == val) 285 return NULL; 286 val->desired_ux = CONFLUX_UX_HIGH_THROUGHPUT; 287 return val; 288 } 289 290 /** Release all storage held inside 'obj', but do not free 'obj'. 291 */ 292 static void 293 trn_cell_conflux_link_payload_v1_clear(trn_cell_conflux_link_payload_v1_t *obj) 294 { 295 (void) obj; 296 } 297 298 void 299 trn_cell_conflux_link_payload_v1_free(trn_cell_conflux_link_payload_v1_t *obj) 300 { 301 if (obj == NULL) 302 return; 303 trn_cell_conflux_link_payload_v1_clear(obj); 304 trunnel_memwipe(obj, sizeof(trn_cell_conflux_link_payload_v1_t)); 305 trunnel_free_(obj); 306 } 307 308 size_t 309 trn_cell_conflux_link_payload_v1_getlen_nonce(const trn_cell_conflux_link_payload_v1_t *inp) 310 { 311 (void)inp; return 32; 312 } 313 314 uint8_t 315 trn_cell_conflux_link_payload_v1_get_nonce(trn_cell_conflux_link_payload_v1_t *inp, size_t idx) 316 { 317 trunnel_assert(idx < 32); 318 return inp->nonce[idx]; 319 } 320 321 uint8_t 322 trn_cell_conflux_link_payload_v1_getconst_nonce(const trn_cell_conflux_link_payload_v1_t *inp, size_t idx) 323 { 324 return trn_cell_conflux_link_payload_v1_get_nonce((trn_cell_conflux_link_payload_v1_t*)inp, idx); 325 } 326 int 327 trn_cell_conflux_link_payload_v1_set_nonce(trn_cell_conflux_link_payload_v1_t *inp, size_t idx, uint8_t elt) 328 { 329 trunnel_assert(idx < 32); 330 inp->nonce[idx] = elt; 331 return 0; 332 } 333 334 uint8_t * 335 trn_cell_conflux_link_payload_v1_getarray_nonce(trn_cell_conflux_link_payload_v1_t *inp) 336 { 337 return inp->nonce; 338 } 339 const uint8_t * 340 trn_cell_conflux_link_payload_v1_getconstarray_nonce(const trn_cell_conflux_link_payload_v1_t *inp) 341 { 342 return (const uint8_t *)trn_cell_conflux_link_payload_v1_getarray_nonce((trn_cell_conflux_link_payload_v1_t*)inp); 343 } 344 uint64_t 345 trn_cell_conflux_link_payload_v1_get_last_seqno_sent(const trn_cell_conflux_link_payload_v1_t *inp) 346 { 347 return inp->last_seqno_sent; 348 } 349 int 350 trn_cell_conflux_link_payload_v1_set_last_seqno_sent(trn_cell_conflux_link_payload_v1_t *inp, uint64_t val) 351 { 352 inp->last_seqno_sent = val; 353 return 0; 354 } 355 uint64_t 356 trn_cell_conflux_link_payload_v1_get_last_seqno_recv(const trn_cell_conflux_link_payload_v1_t *inp) 357 { 358 return inp->last_seqno_recv; 359 } 360 int 361 trn_cell_conflux_link_payload_v1_set_last_seqno_recv(trn_cell_conflux_link_payload_v1_t *inp, uint64_t val) 362 { 363 inp->last_seqno_recv = val; 364 return 0; 365 } 366 uint8_t 367 trn_cell_conflux_link_payload_v1_get_desired_ux(const trn_cell_conflux_link_payload_v1_t *inp) 368 { 369 return inp->desired_ux; 370 } 371 int 372 trn_cell_conflux_link_payload_v1_set_desired_ux(trn_cell_conflux_link_payload_v1_t *inp, uint8_t val) 373 { 374 if (! ((val == CONFLUX_UX_HIGH_THROUGHPUT || val == CONFLUX_UX_LOW_MEM_LATENCY || val == CONFLUX_UX_LOW_MEM_THROUGHPUT || val == CONFLUX_UX_MIN_LATENCY || val == CONFLUX_UX_NO_OPINION))) { 375 TRUNNEL_SET_ERROR_CODE(inp); 376 return -1; 377 } 378 inp->desired_ux = val; 379 return 0; 380 } 381 const char * 382 trn_cell_conflux_link_payload_v1_check(const trn_cell_conflux_link_payload_v1_t *obj) 383 { 384 if (obj == NULL) 385 return "Object was NULL"; 386 if (obj->trunnel_error_code_) 387 return "A set function failed on this object"; 388 if (! (obj->desired_ux == CONFLUX_UX_HIGH_THROUGHPUT || obj->desired_ux == CONFLUX_UX_LOW_MEM_LATENCY || obj->desired_ux == CONFLUX_UX_LOW_MEM_THROUGHPUT || obj->desired_ux == CONFLUX_UX_MIN_LATENCY || obj->desired_ux == CONFLUX_UX_NO_OPINION)) 389 return "Integer out of bounds"; 390 return NULL; 391 } 392 393 ssize_t 394 trn_cell_conflux_link_payload_v1_encoded_len(const trn_cell_conflux_link_payload_v1_t *obj) 395 { 396 ssize_t result = 0; 397 398 if (NULL != trn_cell_conflux_link_payload_v1_check(obj)) 399 return -1; 400 401 402 /* Length of u8 nonce[32] */ 403 result += 32; 404 405 /* Length of u64 last_seqno_sent */ 406 result += 8; 407 408 /* Length of u64 last_seqno_recv */ 409 result += 8; 410 411 /* Length of u8 desired_ux IN [CONFLUX_UX_HIGH_THROUGHPUT, CONFLUX_UX_LOW_MEM_LATENCY, CONFLUX_UX_LOW_MEM_THROUGHPUT, CONFLUX_UX_MIN_LATENCY, CONFLUX_UX_NO_OPINION] */ 412 result += 1; 413 return result; 414 } 415 int 416 trn_cell_conflux_link_payload_v1_clear_errors(trn_cell_conflux_link_payload_v1_t *obj) 417 { 418 int r = obj->trunnel_error_code_; 419 obj->trunnel_error_code_ = 0; 420 return r; 421 } 422 ssize_t 423 trn_cell_conflux_link_payload_v1_encode(uint8_t *output, const size_t avail, const trn_cell_conflux_link_payload_v1_t *obj) 424 { 425 ssize_t result = 0; 426 size_t written = 0; 427 uint8_t *ptr = output; 428 const char *msg; 429 #ifdef TRUNNEL_CHECK_ENCODED_LEN 430 const ssize_t encoded_len = trn_cell_conflux_link_payload_v1_encoded_len(obj); 431 #endif 432 433 if (NULL != (msg = trn_cell_conflux_link_payload_v1_check(obj))) 434 goto check_failed; 435 436 #ifdef TRUNNEL_CHECK_ENCODED_LEN 437 trunnel_assert(encoded_len >= 0); 438 #endif 439 440 /* Encode u8 nonce[32] */ 441 trunnel_assert(written <= avail); 442 if (avail - written < 32) 443 goto truncated; 444 memcpy(ptr, obj->nonce, 32); 445 written += 32; ptr += 32; 446 447 /* Encode u64 last_seqno_sent */ 448 trunnel_assert(written <= avail); 449 if (avail - written < 8) 450 goto truncated; 451 trunnel_set_uint64(ptr, trunnel_htonll(obj->last_seqno_sent)); 452 written += 8; ptr += 8; 453 454 /* Encode u64 last_seqno_recv */ 455 trunnel_assert(written <= avail); 456 if (avail - written < 8) 457 goto truncated; 458 trunnel_set_uint64(ptr, trunnel_htonll(obj->last_seqno_recv)); 459 written += 8; ptr += 8; 460 461 /* Encode u8 desired_ux IN [CONFLUX_UX_HIGH_THROUGHPUT, CONFLUX_UX_LOW_MEM_LATENCY, CONFLUX_UX_LOW_MEM_THROUGHPUT, CONFLUX_UX_MIN_LATENCY, CONFLUX_UX_NO_OPINION] */ 462 trunnel_assert(written <= avail); 463 if (avail - written < 1) 464 goto truncated; 465 trunnel_set_uint8(ptr, (obj->desired_ux)); 466 written += 1; ptr += 1; 467 468 469 trunnel_assert(ptr == output + written); 470 #ifdef TRUNNEL_CHECK_ENCODED_LEN 471 { 472 trunnel_assert(encoded_len >= 0); 473 trunnel_assert((size_t)encoded_len == written); 474 } 475 476 #endif 477 478 return written; 479 480 truncated: 481 result = -2; 482 goto fail; 483 check_failed: 484 (void)msg; 485 result = -1; 486 goto fail; 487 fail: 488 trunnel_assert(result < 0); 489 return result; 490 } 491 492 /** As trn_cell_conflux_link_payload_v1_parse(), but do not allocate 493 * the output object. 494 */ 495 static ssize_t 496 trn_cell_conflux_link_payload_v1_parse_into(trn_cell_conflux_link_payload_v1_t *obj, const uint8_t *input, const size_t len_in) 497 { 498 const uint8_t *ptr = input; 499 size_t remaining = len_in; 500 ssize_t result = 0; 501 (void)result; 502 503 /* Parse u8 nonce[32] */ 504 CHECK_REMAINING(32, truncated); 505 memcpy(obj->nonce, ptr, 32); 506 remaining -= 32; ptr += 32; 507 508 /* Parse u64 last_seqno_sent */ 509 CHECK_REMAINING(8, truncated); 510 obj->last_seqno_sent = trunnel_ntohll(trunnel_get_uint64(ptr)); 511 remaining -= 8; ptr += 8; 512 513 /* Parse u64 last_seqno_recv */ 514 CHECK_REMAINING(8, truncated); 515 obj->last_seqno_recv = trunnel_ntohll(trunnel_get_uint64(ptr)); 516 remaining -= 8; ptr += 8; 517 518 /* Parse u8 desired_ux IN [CONFLUX_UX_HIGH_THROUGHPUT, CONFLUX_UX_LOW_MEM_LATENCY, CONFLUX_UX_LOW_MEM_THROUGHPUT, CONFLUX_UX_MIN_LATENCY, CONFLUX_UX_NO_OPINION] */ 519 CHECK_REMAINING(1, truncated); 520 obj->desired_ux = (trunnel_get_uint8(ptr)); 521 remaining -= 1; ptr += 1; 522 if (! (obj->desired_ux == CONFLUX_UX_HIGH_THROUGHPUT || obj->desired_ux == CONFLUX_UX_LOW_MEM_LATENCY || obj->desired_ux == CONFLUX_UX_LOW_MEM_THROUGHPUT || obj->desired_ux == CONFLUX_UX_MIN_LATENCY || obj->desired_ux == CONFLUX_UX_NO_OPINION)) 523 goto fail; 524 trunnel_assert(ptr + remaining == input + len_in); 525 return len_in - remaining; 526 527 truncated: 528 return -2; 529 fail: 530 result = -1; 531 return result; 532 } 533 534 ssize_t 535 trn_cell_conflux_link_payload_v1_parse(trn_cell_conflux_link_payload_v1_t **output, const uint8_t *input, const size_t len_in) 536 { 537 ssize_t result; 538 *output = trn_cell_conflux_link_payload_v1_new(); 539 if (NULL == *output) 540 return -1; 541 result = trn_cell_conflux_link_payload_v1_parse_into(*output, input, len_in); 542 if (result < 0) { 543 trn_cell_conflux_link_payload_v1_free(*output); 544 *output = NULL; 545 } 546 return result; 547 } 548 trn_cell_conflux_linked_t * 549 trn_cell_conflux_linked_new(void) 550 { 551 trn_cell_conflux_linked_t *val = trunnel_calloc(1, sizeof(trn_cell_conflux_linked_t)); 552 if (NULL == val) 553 return NULL; 554 val->version = 1; 555 return val; 556 } 557 558 /** Release all storage held inside 'obj', but do not free 'obj'. 559 */ 560 static void 561 trn_cell_conflux_linked_clear(trn_cell_conflux_linked_t *obj) 562 { 563 (void) obj; 564 TRUNNEL_DYNARRAY_WIPE(&obj->payload); 565 TRUNNEL_DYNARRAY_CLEAR(&obj->payload); 566 } 567 568 void 569 trn_cell_conflux_linked_free(trn_cell_conflux_linked_t *obj) 570 { 571 if (obj == NULL) 572 return; 573 trn_cell_conflux_linked_clear(obj); 574 trunnel_memwipe(obj, sizeof(trn_cell_conflux_linked_t)); 575 trunnel_free_(obj); 576 } 577 578 uint8_t 579 trn_cell_conflux_linked_get_version(const trn_cell_conflux_linked_t *inp) 580 { 581 return inp->version; 582 } 583 int 584 trn_cell_conflux_linked_set_version(trn_cell_conflux_linked_t *inp, uint8_t val) 585 { 586 if (! ((val == 1))) { 587 TRUNNEL_SET_ERROR_CODE(inp); 588 return -1; 589 } 590 inp->version = val; 591 return 0; 592 } 593 size_t 594 trn_cell_conflux_linked_getlen_payload(const trn_cell_conflux_linked_t *inp) 595 { 596 return TRUNNEL_DYNARRAY_LEN(&inp->payload); 597 } 598 599 uint8_t 600 trn_cell_conflux_linked_get_payload(trn_cell_conflux_linked_t *inp, size_t idx) 601 { 602 return TRUNNEL_DYNARRAY_GET(&inp->payload, idx); 603 } 604 605 uint8_t 606 trn_cell_conflux_linked_getconst_payload(const trn_cell_conflux_linked_t *inp, size_t idx) 607 { 608 return trn_cell_conflux_linked_get_payload((trn_cell_conflux_linked_t*)inp, idx); 609 } 610 int 611 trn_cell_conflux_linked_set_payload(trn_cell_conflux_linked_t *inp, size_t idx, uint8_t elt) 612 { 613 TRUNNEL_DYNARRAY_SET(&inp->payload, idx, elt); 614 return 0; 615 } 616 int 617 trn_cell_conflux_linked_add_payload(trn_cell_conflux_linked_t *inp, uint8_t elt) 618 { 619 TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->payload, elt, {}); 620 return 0; 621 trunnel_alloc_failed: 622 TRUNNEL_SET_ERROR_CODE(inp); 623 return -1; 624 } 625 626 uint8_t * 627 trn_cell_conflux_linked_getarray_payload(trn_cell_conflux_linked_t *inp) 628 { 629 return inp->payload.elts_; 630 } 631 const uint8_t * 632 trn_cell_conflux_linked_getconstarray_payload(const trn_cell_conflux_linked_t *inp) 633 { 634 return (const uint8_t *)trn_cell_conflux_linked_getarray_payload((trn_cell_conflux_linked_t*)inp); 635 } 636 int 637 trn_cell_conflux_linked_setlen_payload(trn_cell_conflux_linked_t *inp, size_t newlen) 638 { 639 uint8_t *newptr; 640 newptr = trunnel_dynarray_setlen(&inp->payload.allocated_, 641 &inp->payload.n_, inp->payload.elts_, newlen, 642 sizeof(inp->payload.elts_[0]), (trunnel_free_fn_t) NULL, 643 &inp->trunnel_error_code_); 644 if (newlen != 0 && newptr == NULL) 645 goto trunnel_alloc_failed; 646 inp->payload.elts_ = newptr; 647 return 0; 648 trunnel_alloc_failed: 649 TRUNNEL_SET_ERROR_CODE(inp); 650 return -1; 651 } 652 const char * 653 trn_cell_conflux_linked_check(const trn_cell_conflux_linked_t *obj) 654 { 655 if (obj == NULL) 656 return "Object was NULL"; 657 if (obj->trunnel_error_code_) 658 return "A set function failed on this object"; 659 if (! (obj->version == 1)) 660 return "Integer out of bounds"; 661 return NULL; 662 } 663 664 ssize_t 665 trn_cell_conflux_linked_encoded_len(const trn_cell_conflux_linked_t *obj) 666 { 667 ssize_t result = 0; 668 669 if (NULL != trn_cell_conflux_linked_check(obj)) 670 return -1; 671 672 673 /* Length of u8 version IN [1] */ 674 result += 1; 675 676 /* Length of u8 payload[] */ 677 result += TRUNNEL_DYNARRAY_LEN(&obj->payload); 678 return result; 679 } 680 int 681 trn_cell_conflux_linked_clear_errors(trn_cell_conflux_linked_t *obj) 682 { 683 int r = obj->trunnel_error_code_; 684 obj->trunnel_error_code_ = 0; 685 return r; 686 } 687 ssize_t 688 trn_cell_conflux_linked_encode(uint8_t *output, const size_t avail, const trn_cell_conflux_linked_t *obj) 689 { 690 ssize_t result = 0; 691 size_t written = 0; 692 uint8_t *ptr = output; 693 const char *msg; 694 #ifdef TRUNNEL_CHECK_ENCODED_LEN 695 const ssize_t encoded_len = trn_cell_conflux_linked_encoded_len(obj); 696 #endif 697 698 if (NULL != (msg = trn_cell_conflux_linked_check(obj))) 699 goto check_failed; 700 701 #ifdef TRUNNEL_CHECK_ENCODED_LEN 702 trunnel_assert(encoded_len >= 0); 703 #endif 704 705 /* Encode u8 version IN [1] */ 706 trunnel_assert(written <= avail); 707 if (avail - written < 1) 708 goto truncated; 709 trunnel_set_uint8(ptr, (obj->version)); 710 written += 1; ptr += 1; 711 712 /* Encode u8 payload[] */ 713 { 714 size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->payload); 715 trunnel_assert(written <= avail); 716 if (avail - written < elt_len) 717 goto truncated; 718 if (elt_len) 719 memcpy(ptr, obj->payload.elts_, elt_len); 720 written += elt_len; ptr += elt_len; 721 } 722 723 724 trunnel_assert(ptr == output + written); 725 #ifdef TRUNNEL_CHECK_ENCODED_LEN 726 { 727 trunnel_assert(encoded_len >= 0); 728 trunnel_assert((size_t)encoded_len == written); 729 } 730 731 #endif 732 733 return written; 734 735 truncated: 736 result = -2; 737 goto fail; 738 check_failed: 739 (void)msg; 740 result = -1; 741 goto fail; 742 fail: 743 trunnel_assert(result < 0); 744 return result; 745 } 746 747 /** As trn_cell_conflux_linked_parse(), but do not allocate the output 748 * object. 749 */ 750 static ssize_t 751 trn_cell_conflux_linked_parse_into(trn_cell_conflux_linked_t *obj, const uint8_t *input, const size_t len_in) 752 { 753 const uint8_t *ptr = input; 754 size_t remaining = len_in; 755 ssize_t result = 0; 756 (void)result; 757 758 /* Parse u8 version IN [1] */ 759 CHECK_REMAINING(1, truncated); 760 obj->version = (trunnel_get_uint8(ptr)); 761 remaining -= 1; ptr += 1; 762 if (! (obj->version == 1)) 763 goto fail; 764 765 /* Parse u8 payload[] */ 766 TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->payload, remaining, {}); 767 obj->payload.n_ = remaining; 768 if (remaining) 769 memcpy(obj->payload.elts_, ptr, remaining); 770 ptr += remaining; remaining -= remaining; 771 trunnel_assert(ptr + remaining == input + len_in); 772 return len_in - remaining; 773 774 truncated: 775 return -2; 776 trunnel_alloc_failed: 777 return -1; 778 fail: 779 result = -1; 780 return result; 781 } 782 783 ssize_t 784 trn_cell_conflux_linked_parse(trn_cell_conflux_linked_t **output, const uint8_t *input, const size_t len_in) 785 { 786 ssize_t result; 787 *output = trn_cell_conflux_linked_new(); 788 if (NULL == *output) 789 return -1; 790 result = trn_cell_conflux_linked_parse_into(*output, input, len_in); 791 if (result < 0) { 792 trn_cell_conflux_linked_free(*output); 793 *output = NULL; 794 } 795 return result; 796 } 797 trn_cell_conflux_linked_ack_t * 798 trn_cell_conflux_linked_ack_new(void) 799 { 800 trn_cell_conflux_linked_ack_t *val = trunnel_calloc(1, sizeof(trn_cell_conflux_linked_ack_t)); 801 if (NULL == val) 802 return NULL; 803 return val; 804 } 805 806 /** Release all storage held inside 'obj', but do not free 'obj'. 807 */ 808 static void 809 trn_cell_conflux_linked_ack_clear(trn_cell_conflux_linked_ack_t *obj) 810 { 811 (void) obj; 812 TRUNNEL_DYNARRAY_WIPE(&obj->payload); 813 TRUNNEL_DYNARRAY_CLEAR(&obj->payload); 814 } 815 816 void 817 trn_cell_conflux_linked_ack_free(trn_cell_conflux_linked_ack_t *obj) 818 { 819 if (obj == NULL) 820 return; 821 trn_cell_conflux_linked_ack_clear(obj); 822 trunnel_memwipe(obj, sizeof(trn_cell_conflux_linked_ack_t)); 823 trunnel_free_(obj); 824 } 825 826 size_t 827 trn_cell_conflux_linked_ack_getlen_payload(const trn_cell_conflux_linked_ack_t *inp) 828 { 829 return TRUNNEL_DYNARRAY_LEN(&inp->payload); 830 } 831 832 uint8_t 833 trn_cell_conflux_linked_ack_get_payload(trn_cell_conflux_linked_ack_t *inp, size_t idx) 834 { 835 return TRUNNEL_DYNARRAY_GET(&inp->payload, idx); 836 } 837 838 uint8_t 839 trn_cell_conflux_linked_ack_getconst_payload(const trn_cell_conflux_linked_ack_t *inp, size_t idx) 840 { 841 return trn_cell_conflux_linked_ack_get_payload((trn_cell_conflux_linked_ack_t*)inp, idx); 842 } 843 int 844 trn_cell_conflux_linked_ack_set_payload(trn_cell_conflux_linked_ack_t *inp, size_t idx, uint8_t elt) 845 { 846 TRUNNEL_DYNARRAY_SET(&inp->payload, idx, elt); 847 return 0; 848 } 849 int 850 trn_cell_conflux_linked_ack_add_payload(trn_cell_conflux_linked_ack_t *inp, uint8_t elt) 851 { 852 TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->payload, elt, {}); 853 return 0; 854 trunnel_alloc_failed: 855 TRUNNEL_SET_ERROR_CODE(inp); 856 return -1; 857 } 858 859 uint8_t * 860 trn_cell_conflux_linked_ack_getarray_payload(trn_cell_conflux_linked_ack_t *inp) 861 { 862 return inp->payload.elts_; 863 } 864 const uint8_t * 865 trn_cell_conflux_linked_ack_getconstarray_payload(const trn_cell_conflux_linked_ack_t *inp) 866 { 867 return (const uint8_t *)trn_cell_conflux_linked_ack_getarray_payload((trn_cell_conflux_linked_ack_t*)inp); 868 } 869 int 870 trn_cell_conflux_linked_ack_setlen_payload(trn_cell_conflux_linked_ack_t *inp, size_t newlen) 871 { 872 uint8_t *newptr; 873 newptr = trunnel_dynarray_setlen(&inp->payload.allocated_, 874 &inp->payload.n_, inp->payload.elts_, newlen, 875 sizeof(inp->payload.elts_[0]), (trunnel_free_fn_t) NULL, 876 &inp->trunnel_error_code_); 877 if (newlen != 0 && newptr == NULL) 878 goto trunnel_alloc_failed; 879 inp->payload.elts_ = newptr; 880 return 0; 881 trunnel_alloc_failed: 882 TRUNNEL_SET_ERROR_CODE(inp); 883 return -1; 884 } 885 const char * 886 trn_cell_conflux_linked_ack_check(const trn_cell_conflux_linked_ack_t *obj) 887 { 888 if (obj == NULL) 889 return "Object was NULL"; 890 if (obj->trunnel_error_code_) 891 return "A set function failed on this object"; 892 return NULL; 893 } 894 895 ssize_t 896 trn_cell_conflux_linked_ack_encoded_len(const trn_cell_conflux_linked_ack_t *obj) 897 { 898 ssize_t result = 0; 899 900 if (NULL != trn_cell_conflux_linked_ack_check(obj)) 901 return -1; 902 903 904 /* Length of u8 payload[] */ 905 result += TRUNNEL_DYNARRAY_LEN(&obj->payload); 906 return result; 907 } 908 int 909 trn_cell_conflux_linked_ack_clear_errors(trn_cell_conflux_linked_ack_t *obj) 910 { 911 int r = obj->trunnel_error_code_; 912 obj->trunnel_error_code_ = 0; 913 return r; 914 } 915 ssize_t 916 trn_cell_conflux_linked_ack_encode(uint8_t *output, const size_t avail, const trn_cell_conflux_linked_ack_t *obj) 917 { 918 ssize_t result = 0; 919 size_t written = 0; 920 uint8_t *ptr = output; 921 const char *msg; 922 #ifdef TRUNNEL_CHECK_ENCODED_LEN 923 const ssize_t encoded_len = trn_cell_conflux_linked_ack_encoded_len(obj); 924 #endif 925 926 if (NULL != (msg = trn_cell_conflux_linked_ack_check(obj))) 927 goto check_failed; 928 929 #ifdef TRUNNEL_CHECK_ENCODED_LEN 930 trunnel_assert(encoded_len >= 0); 931 #endif 932 933 /* Encode u8 payload[] */ 934 { 935 size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->payload); 936 trunnel_assert(written <= avail); 937 if (avail - written < elt_len) 938 goto truncated; 939 if (elt_len) 940 memcpy(ptr, obj->payload.elts_, elt_len); 941 written += elt_len; ptr += elt_len; 942 } 943 944 945 trunnel_assert(ptr == output + written); 946 #ifdef TRUNNEL_CHECK_ENCODED_LEN 947 { 948 trunnel_assert(encoded_len >= 0); 949 trunnel_assert((size_t)encoded_len == written); 950 } 951 952 #endif 953 954 return written; 955 956 truncated: 957 result = -2; 958 goto fail; 959 check_failed: 960 (void)msg; 961 result = -1; 962 goto fail; 963 fail: 964 trunnel_assert(result < 0); 965 return result; 966 } 967 968 /** As trn_cell_conflux_linked_ack_parse(), but do not allocate the 969 * output object. 970 */ 971 static ssize_t 972 trn_cell_conflux_linked_ack_parse_into(trn_cell_conflux_linked_ack_t *obj, const uint8_t *input, const size_t len_in) 973 { 974 const uint8_t *ptr = input; 975 size_t remaining = len_in; 976 ssize_t result = 0; 977 (void)result; 978 979 /* Parse u8 payload[] */ 980 TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->payload, remaining, {}); 981 obj->payload.n_ = remaining; 982 if (remaining) 983 memcpy(obj->payload.elts_, ptr, remaining); 984 ptr += remaining; remaining -= remaining; 985 trunnel_assert(ptr + remaining == input + len_in); 986 return len_in - remaining; 987 988 trunnel_alloc_failed: 989 return -1; 990 } 991 992 ssize_t 993 trn_cell_conflux_linked_ack_parse(trn_cell_conflux_linked_ack_t **output, const uint8_t *input, const size_t len_in) 994 { 995 ssize_t result; 996 *output = trn_cell_conflux_linked_ack_new(); 997 if (NULL == *output) 998 return -1; 999 result = trn_cell_conflux_linked_ack_parse_into(*output, input, len_in); 1000 if (result < 0) { 1001 trn_cell_conflux_linked_ack_free(*output); 1002 *output = NULL; 1003 } 1004 return result; 1005 } 1006 trn_cell_conflux_switch_t * 1007 trn_cell_conflux_switch_new(void) 1008 { 1009 trn_cell_conflux_switch_t *val = trunnel_calloc(1, sizeof(trn_cell_conflux_switch_t)); 1010 if (NULL == val) 1011 return NULL; 1012 return val; 1013 } 1014 1015 /** Release all storage held inside 'obj', but do not free 'obj'. 1016 */ 1017 static void 1018 trn_cell_conflux_switch_clear(trn_cell_conflux_switch_t *obj) 1019 { 1020 (void) obj; 1021 } 1022 1023 void 1024 trn_cell_conflux_switch_free(trn_cell_conflux_switch_t *obj) 1025 { 1026 if (obj == NULL) 1027 return; 1028 trn_cell_conflux_switch_clear(obj); 1029 trunnel_memwipe(obj, sizeof(trn_cell_conflux_switch_t)); 1030 trunnel_free_(obj); 1031 } 1032 1033 uint32_t 1034 trn_cell_conflux_switch_get_seqnum(const trn_cell_conflux_switch_t *inp) 1035 { 1036 return inp->seqnum; 1037 } 1038 int 1039 trn_cell_conflux_switch_set_seqnum(trn_cell_conflux_switch_t *inp, uint32_t val) 1040 { 1041 inp->seqnum = val; 1042 return 0; 1043 } 1044 const char * 1045 trn_cell_conflux_switch_check(const trn_cell_conflux_switch_t *obj) 1046 { 1047 if (obj == NULL) 1048 return "Object was NULL"; 1049 if (obj->trunnel_error_code_) 1050 return "A set function failed on this object"; 1051 return NULL; 1052 } 1053 1054 ssize_t 1055 trn_cell_conflux_switch_encoded_len(const trn_cell_conflux_switch_t *obj) 1056 { 1057 ssize_t result = 0; 1058 1059 if (NULL != trn_cell_conflux_switch_check(obj)) 1060 return -1; 1061 1062 1063 /* Length of u32 seqnum */ 1064 result += 4; 1065 return result; 1066 } 1067 int 1068 trn_cell_conflux_switch_clear_errors(trn_cell_conflux_switch_t *obj) 1069 { 1070 int r = obj->trunnel_error_code_; 1071 obj->trunnel_error_code_ = 0; 1072 return r; 1073 } 1074 ssize_t 1075 trn_cell_conflux_switch_encode(uint8_t *output, const size_t avail, const trn_cell_conflux_switch_t *obj) 1076 { 1077 ssize_t result = 0; 1078 size_t written = 0; 1079 uint8_t *ptr = output; 1080 const char *msg; 1081 #ifdef TRUNNEL_CHECK_ENCODED_LEN 1082 const ssize_t encoded_len = trn_cell_conflux_switch_encoded_len(obj); 1083 #endif 1084 1085 if (NULL != (msg = trn_cell_conflux_switch_check(obj))) 1086 goto check_failed; 1087 1088 #ifdef TRUNNEL_CHECK_ENCODED_LEN 1089 trunnel_assert(encoded_len >= 0); 1090 #endif 1091 1092 /* Encode u32 seqnum */ 1093 trunnel_assert(written <= avail); 1094 if (avail - written < 4) 1095 goto truncated; 1096 trunnel_set_uint32(ptr, trunnel_htonl(obj->seqnum)); 1097 written += 4; ptr += 4; 1098 1099 1100 trunnel_assert(ptr == output + written); 1101 #ifdef TRUNNEL_CHECK_ENCODED_LEN 1102 { 1103 trunnel_assert(encoded_len >= 0); 1104 trunnel_assert((size_t)encoded_len == written); 1105 } 1106 1107 #endif 1108 1109 return written; 1110 1111 truncated: 1112 result = -2; 1113 goto fail; 1114 check_failed: 1115 (void)msg; 1116 result = -1; 1117 goto fail; 1118 fail: 1119 trunnel_assert(result < 0); 1120 return result; 1121 } 1122 1123 /** As trn_cell_conflux_switch_parse(), but do not allocate the output 1124 * object. 1125 */ 1126 static ssize_t 1127 trn_cell_conflux_switch_parse_into(trn_cell_conflux_switch_t *obj, const uint8_t *input, const size_t len_in) 1128 { 1129 const uint8_t *ptr = input; 1130 size_t remaining = len_in; 1131 ssize_t result = 0; 1132 (void)result; 1133 1134 /* Parse u32 seqnum */ 1135 CHECK_REMAINING(4, truncated); 1136 obj->seqnum = trunnel_ntohl(trunnel_get_uint32(ptr)); 1137 remaining -= 4; ptr += 4; 1138 trunnel_assert(ptr + remaining == input + len_in); 1139 return len_in - remaining; 1140 1141 truncated: 1142 return -2; 1143 } 1144 1145 ssize_t 1146 trn_cell_conflux_switch_parse(trn_cell_conflux_switch_t **output, const uint8_t *input, const size_t len_in) 1147 { 1148 ssize_t result; 1149 *output = trn_cell_conflux_switch_new(); 1150 if (NULL == *output) 1151 return -1; 1152 result = trn_cell_conflux_switch_parse_into(*output, input, len_in); 1153 if (result < 0) { 1154 trn_cell_conflux_switch_free(*output); 1155 *output = NULL; 1156 } 1157 return result; 1158 }