MCPcopy Create free account
hub / github.com/ElementsProject/lightning / pad_encrypted_queue

Function pad_encrypted_queue

connectd/multiplex.c:539–596  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

write_to_peerFunction · 0.85

Calls 4

make_pingFunction · 0.85
encrypt_appendFunction · 0.85
abortFunction · 0.85

Tested by

no test coverage detected