| 540 | } |
| 541 | |
| 542 | static void pad_encrypted_queue(struct peer *peer) |
| 543 | { |
| 544 | size_t needed, pingpad; |
| 545 | u8 *ping; |
| 546 | |
| 547 | /* BOLT #8: |
| 548 | * |
| 549 | * ``` |
| 550 | * +------------------------------- |
| 551 | * |2-byte encrypted message length| |
| 552 | * +------------------------------- |
| 553 | * | 16-byte MAC of the encrypted | |
| 554 | * | message length | |
| 555 | * +------------------------------- |
| 556 | * | | |
| 557 | * | | |
| 558 | * | encrypted Lightning | |
| 559 | * | message | |
| 560 | * | | |
| 561 | * +------------------------------- |
| 562 | * | 16-byte MAC of the | |
| 563 | * | Lightning message | |
| 564 | * +------------------------------- |
| 565 | * ``` |
| 566 | * |
| 567 | * The prefixed message length is encoded as a 2-byte big-endian integer, |
| 568 | * for a total maximum packet length of `2 + 16 + 65535 + 16` = `65569` bytes. |
| 569 | */ |
| 570 | assert(membuf_num_elems(&peer->encrypted_peer_out) < UNIFORM_MESSAGE_SIZE); |
| 571 | needed = UNIFORM_MESSAGE_SIZE - membuf_num_elems(&peer->encrypted_peer_out); |
| 572 | |
| 573 | /* BOLT #1: |
| 574 | * 1. type: 18 (`ping`) |
| 575 | * 2. data: |
| 576 | * * [`u16`:`num_pong_bytes`] |
| 577 | * * [`u16`:`byteslen`] |
| 578 | * * [`byteslen*byte`:`ignored`] |
| 579 | */ |
| 580 | /* So smallest possible ping is 6 bytes (2 byte type field) */ |
| 581 | if (needed < 2 + 16 + 16 + 6) |
| 582 | needed += UNIFORM_MESSAGE_SIZE; |
| 583 | |
| 584 | pingpad = needed - (2 + 16 + 16 + 6); |
| 585 | /* Note: we don't bother --dev-disconnect here */ |
| 586 | /* BOLT #1: |
| 587 | * A node receiving a `ping` message: |
| 588 | * - if `num_pong_bytes` is less than 65532: |
| 589 | * - MUST respond by sending a `pong` message, with `byteslen` equal to `num_pong_bytes`. |
| 590 | * - otherwise (`num_pong_bytes` is **not** less than 65532): |
| 591 | * - MUST ignore the `ping`. |
| 592 | */ |
| 593 | ping = make_ping(NULL, 65535, pingpad); |
| 594 | if (!encrypt_append(peer, take(ping))) |
| 595 | abort(); |
| 596 | |
| 597 | assert(have_full_encrypted_queue(peer)); |
| 598 | assert(membuf_num_elems(&peer->encrypted_peer_out) % UNIFORM_MESSAGE_SIZE == 0); |
| 599 | } |
no test coverage detected