From 1b3249a1929929170bba9d2553d7a9c8670193e5 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 22 Jul 2021 16:05:48 +0000 Subject: [PATCH] Handle sending and receiving warning messages --- lightning/src/ln/msgs.rs | 11 ++++++++++- lightning/src/ln/peer_handler.rs | 26 ++++++++++++++++++++++++++ lightning/src/ln/wire.rs | 9 +++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 1335a04c4..6c04636a4 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -731,7 +731,16 @@ pub enum ErrorAction { /// The peer did something incorrect. Tell them. SendErrorMessage { /// The message to send. - msg: ErrorMessage + msg: ErrorMessage, + }, + /// The peer did something incorrect. Tell them without closing any channels. + SendWarningMessage { + /// The message to send. + msg: WarningMessage, + /// The peer may have done something harmless that we weren't able to meaningfully process, + /// though we should still tell them about it. + /// If this event is logged, log it at the given level. + log_level: logger::Level, }, } diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 9157dc87d..6607d986a 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -821,6 +821,11 @@ impl P self.enqueue_message(peer, &msg); continue; }, + msgs::ErrorAction::SendWarningMessage { msg, log_level } => { + log_given_level!(self.logger, log_level, "Error handling message{}; sending warning message with: {}", OptionalFromDebugger(&peer.their_node_id), e.err); + self.enqueue_message(peer, &msg); + continue; + }, } } } @@ -1022,6 +1027,21 @@ impl P return Err(PeerHandleError{ no_connection_possible: true }.into()); } }, + wire::Message::Warning(msg) => { + let mut data_is_printable = true; + for b in msg.data.bytes() { + if b < 32 || b > 126 { + data_is_printable = false; + break; + } + } + + if data_is_printable { + log_debug!(self.logger, "Got warning message from {}: {}", log_pubkey!(peer.their_node_id.unwrap()), msg.data); + } else { + log_debug!(self.logger, "Got warning message from {} with non-ASCII error message", log_pubkey!(peer.their_node_id.unwrap())); + } + }, wire::Message::Ping(msg) => { if msg.ponglen < 65532 { @@ -1419,6 +1439,12 @@ impl P msg.data); self.enqueue_message(get_peer_for_forwarding!(node_id), msg); }, + msgs::ErrorAction::SendWarningMessage { ref msg, ref log_level } => { + log_given_level!(self.logger, *log_level, "Handling SendWarningMessage HandleError event in peer_handler for node {} with message {}", + log_pubkey!(node_id), + msg.data); + self.enqueue_message(get_peer_for_forwarding!(node_id), msg); + }, } }, MessageSendEvent::SendChannelRangeQuery { ref node_id, ref msg } => { diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index d3cc257f5..2c77fd46b 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -35,6 +35,7 @@ pub trait CustomMessageReader { pub(crate) enum Message where T: core::fmt::Debug + Type { Init(msgs::Init), Error(msgs::ErrorMessage), + Warning(msgs::WarningMessage), Ping(msgs::Ping), Pong(msgs::Pong), OpenChannel(msgs::OpenChannel), @@ -74,6 +75,7 @@ impl Message where T: core::fmt::Debug + Type { match self { &Message::Init(ref msg) => msg.type_id(), &Message::Error(ref msg) => msg.type_id(), + &Message::Warning(ref msg) => msg.type_id(), &Message::Ping(ref msg) => msg.type_id(), &Message::Pong(ref msg) => msg.type_id(), &Message::OpenChannel(ref msg) => msg.type_id(), @@ -133,6 +135,9 @@ where msgs::ErrorMessage::TYPE => { Ok(Message::Error(Readable::read(buffer)?)) }, + msgs::WarningMessage::TYPE => { + Ok(Message::Warning(Readable::read(buffer)?)) + }, msgs::Ping::TYPE => { Ok(Message::Ping(Readable::read(buffer)?)) }, @@ -264,6 +269,10 @@ impl Encode for msgs::ErrorMessage { const TYPE: u16 = 17; } +impl Encode for msgs::WarningMessage { + const TYPE: u16 = 1; +} + impl Encode for msgs::Ping { const TYPE: u16 = 18; } -- 2.39.5