Skip to content

Commit 4fbc0b3

Browse files
committed
Require Init as first message, send our own Init for outbound
1 parent df9367a commit 4fbc0b3

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/ln/peer_handler.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,21 @@ pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
4040
/// generate no further read/write_events for the descriptor, only triggering a single
4141
/// disconnect_event (unless it was provided in response to a new_*_connection event, in which case
4242
/// no such disconnect_event must be generated and the socket be silently disconencted).
43-
pub struct PeerHandleError {}
43+
pub struct PeerHandleError {
44+
no_connection_possible: bool,
45+
}
4446
impl fmt::Debug for PeerHandleError {
4547
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
46-
formatter.write_str("Peer Send Invalid Data")
48+
formatter.write_str("Peer Sent Invalid Data")
4749
}
4850
}
4951

5052
struct Peer {
5153
channel_encryptor: PeerChannelEncryptor,
54+
outbound: bool,
5255
their_node_id: Option<PublicKey>,
56+
their_global_features: Option<msgs::GlobalFeatures>,
57+
their_local_features: Option<msgs::LocalFeatures>,
5358

5459
pending_outbound_buffer: LinkedList<Vec<u8>>,
5560
pending_outbound_buffer_first_msg_offset: usize,
@@ -112,7 +117,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
112117
let mut peers = self.peers.lock().unwrap();
113118
if peers.peers.insert(descriptor, Peer {
114119
channel_encryptor: peer_encryptor,
120+
outbound: true,
115121
their_node_id: Some(their_node_id),
122+
their_global_features: None,
123+
their_local_features: None,
116124

117125
pending_outbound_buffer: LinkedList::new(),
118126
pending_outbound_buffer_first_msg_offset: 0,
@@ -141,7 +149,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
141149
let mut peers = self.peers.lock().unwrap();
142150
if peers.peers.insert(descriptor, Peer {
143151
channel_encryptor: peer_encryptor,
152+
outbound: false,
144153
their_node_id: None,
154+
their_global_features: None,
155+
their_local_features: None,
145156

146157
pending_outbound_buffer: LinkedList::new(),
147158
pending_outbound_buffer_first_msg_offset: 0,
@@ -308,6 +319,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
308319
assert!(msg_data.len() >= 2);
309320

310321
let msg_type = byte_utils::slice_to_be16(&msg_data[0..2]);
322+
if msg_type != 16 && peer.their_global_features.is_none() {
323+
// Need an init message as first message
324+
return Err(PeerHandleError{});
325+
}
311326
match msg_type {
312327
// Connection control:
313328
16 => {
@@ -318,9 +333,15 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
318333
if msg.local_features.requires_unknown_bits() {
319334
return Err(PeerHandleError{});
320335
}
321-
//TODO: Store features (and check that we've
322-
//received Init prior to any other messages)!
323-
//TODO: Respond to Init with Init if we're inbound.
336+
peer.their_global_features = Some(msg.global_features);
337+
peer.their_local_features = Some(msg.local_features);
338+
339+
if !peer.outbound {
340+
encode_and_send_msg!(msgs::Init {
341+
global_features: msgs::GlobalFeatures::new(),
342+
local_features: msgs::LocalFeatures::new(),
343+
}, 16);
344+
}
324345
},
325346
17 => {
326347
// Error msg

0 commit comments

Comments
 (0)