Skip to content

Commit 0aa04ea

Browse files
committed
review: Catch duplicate connection after NOISE before INIT
Catch a duplication connection from a peer with the same node_id at the completion of the NOISE handshake instead of the processing of the INIT message.
1 parent c711502 commit 0aa04ea

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

lightning/src/ln/peers/handler.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
740740
//
741741
// For an inbound connection, this will respond with an INIT message.
742742
//
743-
// In the event an INIT has already been seen from this node_id, the current peer connection
744-
// will be disconnected, but the first connection will remain available.
745-
//
746743
// In the event this message is not an INIT the peer will be disconnected.
747744
//
748745
// On successful processing of the INIT message, the peer_connected() callback on the
@@ -753,10 +750,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
753750
match message {
754751
Message::Init(ref init_message) => {
755752
log_trace!(self.logger, "Received Init message from {}", log_pubkey!(&their_node_id));
756-
if node_id_to_descriptor.contains_key(&their_node_id) {
757-
log_trace!(self.logger, "Got second connection with {}, closing", log_pubkey!(&their_node_id));
758-
return Err(PeerHandleError { no_connection_possible: false });
759-
}
753+
760754

761755
let new_post_init_state = self.post_init_state_from_init_message(init_message, &their_node_id)?;
762756

@@ -788,7 +782,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, TransportImpl
788782
},
789783
Ok(newly_connected) => {
790784
if newly_connected {
791-
log_trace!(self.logger, "Finished noise handshake for connection with {}", log_pubkey!(&peer.transport.get_their_node_id()));
785+
let their_node_id = peer.transport.get_their_node_id();
786+
log_trace!(self.logger, "Finished noise handshake for connection with {}", log_pubkey!(&their_node_id));
787+
788+
// Check for a duplicate connection at the completion of the NOISE handshake
789+
if node_id_to_descriptor.contains_key(&their_node_id) {
790+
log_trace!(self.logger, "Got second connection with {}, closing", log_pubkey!(&their_node_id));
791+
return Err(PeerHandleError { no_connection_possible: false });
792+
}
792793
}
793794

794795
if newly_connected && peer.outbound {

0 commit comments

Comments
 (0)