Skip to content

Commit 7ea4d39

Browse files
committed
Add new DisconnectPeerWithWarning variant to ErrorAction
1 parent 84072c7 commit 7ea4d39

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

lightning/src/ln/msgs.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,11 @@ pub enum ErrorAction {
11371137
/// An error message which we should make an effort to send before we disconnect.
11381138
msg: Option<ErrorMessage>
11391139
},
1140+
/// The peer did something incorrect. Tell them without closing any channels and disconnect them.
1141+
DisconnectPeerWithWarning {
1142+
/// A warning message which we should make an effort to send before we disconnect.
1143+
msg: WarningMessage,
1144+
},
11401145
/// The peer did something harmless that we weren't able to process, just log and ignore
11411146
// New code should *not* use this. New code must use IgnoreAndLog, below!
11421147
IgnoreError,

lightning/src/ln/peer_handler.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager
2626
use crate::util::ser::{VecWriter, Writeable, Writer};
2727
use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
2828
use crate::ln::wire;
29-
use crate::ln::wire::Encode;
29+
use crate::ln::wire::{Encode, Type};
3030
use crate::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
3131
use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, NodeAlias};
3232
use crate::util::atomic_counter::AtomicCounter;
@@ -1201,8 +1201,21 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
12011201
Ok(x) => x,
12021202
Err(e) => {
12031203
match e.action {
1204-
msgs::ErrorAction::DisconnectPeer { msg: _ } => {
1205-
//TODO: Try to push msg
1204+
msgs::ErrorAction::DisconnectPeer { .. } => {
1205+
// We may have an `ErrorMessage` to send to the peer,
1206+
// but writing to the socket while reading can lead to
1207+
// re-entrant code and possibly unexpected behavior. The
1208+
// message send is optimistic anyway, and in this case
1209+
// we immediately disconnect the peer.
1210+
log_debug!(self.logger, "Error handling message{}; disconnecting peer with: {}", OptionalFromDebugger(&peer_node_id), e.err);
1211+
return Err(PeerHandleError { });
1212+
},
1213+
msgs::ErrorAction::DisconnectPeerWithWarning { .. } => {
1214+
// We have a `WarningMessage` to send to the peer, but
1215+
// writing to the socket while reading can lead to
1216+
// re-entrant code and possibly unexpected behavior. The
1217+
// message send is optimistic anyway, and in this case
1218+
// we immediately disconnect the peer.
12061219
log_debug!(self.logger, "Error handling message{}; disconnecting peer with: {}", OptionalFromDebugger(&peer_node_id), e.err);
12071220
return Err(PeerHandleError { });
12081221
},
@@ -1337,7 +1350,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
13371350
Ok(x) => x,
13381351
Err(e) => {
13391352
match e {
1340-
// Note that to avoid recursion we never call
1353+
// Note that to avoid re-entrancy we never call
13411354
// `do_attempt_write_data` from here, causing
13421355
// the messages enqueued here to not actually
13431356
// be sent before the peer is disconnected.
@@ -2046,13 +2059,24 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20462059
log_pubkey!(node_id), msg.contents.short_channel_id);
20472060
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
20482061
},
2049-
MessageSendEvent::HandleError { ref node_id, ref action } => {
2050-
match *action {
2051-
msgs::ErrorAction::DisconnectPeer { ref msg } => {
2062+
MessageSendEvent::HandleError { ref node_id, action } => {
2063+
match action {
2064+
msgs::ErrorAction::DisconnectPeer { msg } => {
2065+
log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {}{}",
2066+
log_pubkey!(node_id), if let Some(msg) = msg.as_ref() { format!("with message {}", msg.data) } else { "".to_string() });
2067+
// We do not have the peers write lock, so we just store that we're
2068+
// about to disconenct the peer and do it after we finish
2069+
// processing most messages.
2070+
let msg = msg.map(|msg| wire::Message::<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>::Error(msg));
2071+
peers_to_disconnect.insert(*node_id, msg);
2072+
},
2073+
msgs::ErrorAction::DisconnectPeerWithWarning { msg } => {
2074+
log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
2075+
log_pubkey!(node_id), msg.data);
20522076
// We do not have the peers write lock, so we just store that we're
20532077
// about to disconenct the peer and do it after we finish
20542078
// processing most messages.
2055-
peers_to_disconnect.insert(*node_id, msg.clone());
2079+
peers_to_disconnect.insert(*node_id, Some(wire::Message::Warning(msg)));
20562080
},
20572081
msgs::ErrorAction::IgnoreAndLog(level) => {
20582082
log_given_level!(self.logger, level, "Received a HandleError event to be ignored for node {}", log_pubkey!(node_id));
@@ -2121,9 +2145,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
21212145
if let Some(peer_mutex) = peers.remove(&descriptor) {
21222146
let mut peer = peer_mutex.lock().unwrap();
21232147
if let Some(msg) = msg {
2124-
log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
2125-
log_pubkey!(node_id),
2126-
msg.data);
21272148
self.enqueue_message(&mut *peer, &msg);
21282149
// This isn't guaranteed to work, but if there is enough free
21292150
// room in the send buffer, put the error message there...

0 commit comments

Comments
 (0)