Making message size limit an exportable constant

This commit is contained in:
Dr. Maxim Orlovsky 2020-05-17 16:02:56 +02:00 committed by Dr Maxim Orlovsky
parent d6d01bab84
commit eb8bcaae87
3 changed files with 12 additions and 2 deletions

View file

@ -35,3 +35,5 @@ mod functional_tests;
mod chanmon_update_fail_tests; mod chanmon_update_fail_tests;
#[cfg(test)] #[cfg(test)]
mod reorg_tests; mod reorg_tests;
pub use self::wire::LN_MAX_MSG_LEN;

View file

@ -1,5 +1,6 @@
use ln::msgs::LightningError; use ln::msgs::LightningError;
use ln::msgs; use ln::msgs;
use ln::wire::LN_MAX_MSG_LEN;
use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine}; use bitcoin::hashes::{Hash, HashEngine, Hmac, HmacEngine};
use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::sha256::Hash as Sha256;
@ -373,7 +374,7 @@ impl PeerChannelEncryptor {
/// Encrypts the given message, returning the encrypted version /// Encrypts the given message, returning the encrypted version
/// panics if msg.len() > 65535 or Noise handshake has not finished. /// panics if msg.len() > 65535 or Noise handshake has not finished.
pub fn encrypt_message(&mut self, msg: &[u8]) -> Vec<u8> { pub fn encrypt_message(&mut self, msg: &[u8]) -> Vec<u8> {
if msg.len() > 65535 { if msg.len() > LN_MAX_MSG_LEN {
panic!("Attempted to encrypt message longer than 65535 bytes!"); panic!("Attempted to encrypt message longer than 65535 bytes!");
} }
@ -427,7 +428,7 @@ impl PeerChannelEncryptor {
/// Decrypts the given message. /// Decrypts the given message.
/// panics if msg.len() > 65535 + 16 /// panics if msg.len() > 65535 + 16
pub fn decrypt_message(&mut self, msg: &[u8]) -> Result<Vec<u8>, LightningError> { pub fn decrypt_message(&mut self, msg: &[u8]) -> Result<Vec<u8>, LightningError> {
if msg.len() > 65535 + 16 { if msg.len() > LN_MAX_MSG_LEN + 16 {
panic!("Attempted to encrypt message longer than 65535 bytes!"); panic!("Attempted to encrypt message longer than 65535 bytes!");
} }

View file

@ -16,6 +16,13 @@
use ln::msgs; use ln::msgs;
use util::ser::{Readable, Writeable, Writer}; use util::ser::{Readable, Writeable, Writer};
/// Maximum Lightning message data length according to
/// [BOLT-8](https://github.com/lightningnetwork/lightning-rfc/blob/v1.0/08-transport.md#lightning-message-specification):
/// "The maximum size of any Lightning message MUST NOT exceed 65535 bytes.
/// A maximum size of 65535 simplifies testing, makes memory management easier,
/// and helps mitigate memory-exhaustion attacks."
pub const LN_MAX_MSG_LEN: usize = 65535;
/// A Lightning message returned by [`read`] when decoding bytes received over the wire. Each /// A Lightning message returned by [`read`] when decoding bytes received over the wire. Each
/// variant contains a message from [`ln::msgs`] or otherwise the message type if unknown. /// variant contains a message from [`ln::msgs`] or otherwise the message type if unknown.
/// ///