@@ -40,16 +40,21 @@ pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
40
40
/// generate no further read/write_events for the descriptor, only triggering a single
41
41
/// disconnect_event (unless it was provided in response to a new_*_connection event, in which case
42
42
/// 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
+ }
44
46
impl fmt:: Debug for PeerHandleError {
45
47
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" )
47
49
}
48
50
}
49
51
50
52
struct Peer {
51
53
channel_encryptor : PeerChannelEncryptor ,
54
+ outbound : bool ,
52
55
their_node_id : Option < PublicKey > ,
56
+ their_global_features : Option < msgs:: GlobalFeatures > ,
57
+ their_local_features : Option < msgs:: LocalFeatures > ,
53
58
54
59
pending_outbound_buffer : LinkedList < Vec < u8 > > ,
55
60
pending_outbound_buffer_first_msg_offset : usize ,
@@ -112,7 +117,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
112
117
let mut peers = self . peers . lock ( ) . unwrap ( ) ;
113
118
if peers. peers . insert ( descriptor, Peer {
114
119
channel_encryptor : peer_encryptor,
120
+ outbound : true ,
115
121
their_node_id : Some ( their_node_id) ,
122
+ their_global_features : None ,
123
+ their_local_features : None ,
116
124
117
125
pending_outbound_buffer : LinkedList :: new ( ) ,
118
126
pending_outbound_buffer_first_msg_offset : 0 ,
@@ -141,7 +149,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
141
149
let mut peers = self . peers . lock ( ) . unwrap ( ) ;
142
150
if peers. peers . insert ( descriptor, Peer {
143
151
channel_encryptor : peer_encryptor,
152
+ outbound : false ,
144
153
their_node_id : None ,
154
+ their_global_features : None ,
155
+ their_local_features : None ,
145
156
146
157
pending_outbound_buffer : LinkedList :: new ( ) ,
147
158
pending_outbound_buffer_first_msg_offset : 0 ,
@@ -308,6 +319,10 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
308
319
assert ! ( msg_data. len( ) >= 2 ) ;
309
320
310
321
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
+ }
311
326
match msg_type {
312
327
// Connection control:
313
328
16 => {
@@ -318,9 +333,15 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
318
333
if msg. local_features . requires_unknown_bits ( ) {
319
334
return Err ( PeerHandleError { } ) ;
320
335
}
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
+ }
324
345
} ,
325
346
17 => {
326
347
// Error msg
0 commit comments