Skip to content

Commit 72d1b37

Browse files
committed
Making message size limit an exportable constant
1 parent 59fe300 commit 72d1b37

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

lightning/src/ln/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ mod functional_tests;
3535
mod chanmon_update_fail_tests;
3636
#[cfg(test)]
3737
mod reorg_tests;
38+
39+
pub use self::wire::LN_MAX_MSG_LEN;

lightning/src/ln/peer_channel_encryptor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use ln::msgs::LightningError;
22
use ln::msgs;
3+
use ln::wire::LN_MAX_MSG_LEN;
34

45
use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine};
56
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -373,7 +374,7 @@ impl PeerChannelEncryptor {
373374
/// Encrypts the given message, returning the encrypted version
374375
/// panics if msg.len() > 65535 or Noise handshake has not finished.
375376
pub fn encrypt_message(&mut self, msg: &[u8]) -> Vec<u8> {
376-
if msg.len() > 65535 {
377+
if msg.len() > LN_MAX_MSG_LEN {
377378
panic!("Attempted to encrypt message longer than 65535 bytes!");
378379
}
379380

@@ -427,7 +428,7 @@ impl PeerChannelEncryptor {
427428
/// Decrypts the given message.
428429
/// panics if msg.len() > 65535 + 16
429430
pub fn decrypt_message(&mut self, msg: &[u8]) -> Result<Vec<u8>, LightningError> {
430-
if msg.len() > 65535 + 16 {
431+
if msg.len() > LN_MAX_MSG_LEN + 16 {
431432
panic!("Attempted to encrypt message longer than 65535 bytes!");
432433
}
433434

lightning/src/ln/wire.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
use ln::msgs;
1717
use util::ser::{Readable, Writeable, Writer};
1818

19+
/// Maximum Lightning message data length according to
20+
/// [BOLT-8](https://github.com/lightningnetwork/lightning-rfc/blob/v1.0/08-transport.md#lightning-message-specification):
21+
/// "The maximum size of any Lightning message MUST NOT exceed 65535 bytes.
22+
/// A maximum size of 65535 simplifies testing, makes memory management easier,
23+
/// and helps mitigate memory-exhaustion attacks."
24+
pub const LN_MAX_MSG_LEN: usize = 65535;
25+
1926
/// A Lightning message returned by [`read`] when decoding bytes received over the wire. Each
2027
/// variant contains a message from [`ln::msgs`] or otherwise the message type if unknown.
2128
///

0 commit comments

Comments
 (0)