X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fpeer_handler.rs;h=173467dfce88bac83f6bb1593bc85fd62ccd39b3;hb=24fd566567320b4d78fa868cce873bd733e30ce5;hp=2b60bbf122bce3af47a16997afe48740ede9805a;hpb=f47ba769f544c4fd2e2d1aa0bd5b1fa74ae15daa;p=rust-lightning diff --git a/src/ln/peer_handler.rs b/src/ln/peer_handler.rs index 2b60bbf1..173467df 100644 --- a/src/ln/peer_handler.rs +++ b/src/ln/peer_handler.rs @@ -219,7 +219,6 @@ impl PeerManager { } fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: Vec) -> Result { - let mut upstream_events = Vec::new(); let pause_read = { let mut peers = self.peers.lock().unwrap(); let (should_insert_node_id, pause_read) = match peers.peers.get_mut(peer_descriptor) { @@ -280,6 +279,7 @@ impl PeerManager { let act_three = try_potential_handleerror!(peer.channel_encryptor.process_act_two(&peer.pending_read_buffer[..], &self.our_node_secret)).to_vec(); peer.pending_outbound_buffer.push_back(act_three); peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes + peer.pending_read_is_header = true; insert_node_id = Some(peer.their_node_id.unwrap()); encode_and_send_msg!(msgs::Init { @@ -318,7 +318,9 @@ impl PeerManager { if msg.local_features.requires_unknown_bits() { return Err(PeerHandleError{}); } - //TODO: Store features! + //TODO: Store features (and check that we've + //received Init prior to any other messages)! + //TODO: Respond to Init with Init if we're inbound. }, 17 => { // Error msg @@ -357,11 +359,20 @@ impl PeerManager { 38 => { let msg = try_potential_decodeerror!(msgs::Shutdown::decode(&msg_data[2..])); - try_potential_handleerror!(self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg)); + let resp_options = try_potential_handleerror!(self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg)); + if let Some(resp) = resp_options.0 { + encode_and_send_msg!(resp, 38); + } + if let Some(resp) = resp_options.1 { + encode_and_send_msg!(resp, 39); + } }, 39 => { let msg = try_potential_decodeerror!(msgs::ClosingSigned::decode(&msg_data[2..])); - try_potential_handleerror!(self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg)); + let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg)); + if let Some(resp) = resp_option { + encode_and_send_msg!(resp, 39); + } }, 128 => { @@ -370,42 +381,15 @@ impl PeerManager { }, 130 => { let msg = try_potential_decodeerror!(msgs::UpdateFulfillHTLC::decode(&msg_data[2..])); - let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg)); - match resp_option { - Some(resps) => { - for resp in resps.0 { - encode_and_send_msg!(resp, 128); - } - encode_and_send_msg!(resps.1, 132); - }, - None => {}, - } + try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg)); }, 131 => { let msg = try_potential_decodeerror!(msgs::UpdateFailHTLC::decode(&msg_data[2..])); - let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg)); - match resp_option { - Some(resps) => { - for resp in resps.0 { - encode_and_send_msg!(resp, 128); - } - encode_and_send_msg!(resps.1, 132); - }, - None => {}, - } + try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg)); }, 135 => { let msg = try_potential_decodeerror!(msgs::UpdateFailMalformedHTLC::decode(&msg_data[2..])); - let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg)); - match resp_option { - Some(resps) => { - for resp in resps.0 { - encode_and_send_msg!(resp, 128); - } - encode_and_send_msg!(resps.1, 132); - }, - None => {}, - } + try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg)); }, 132 => { @@ -415,9 +399,17 @@ impl PeerManager { }, 133 => { let msg = try_potential_decodeerror!(msgs::RevokeAndACK::decode(&msg_data[2..])); - try_potential_handleerror!(self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg)); + let resp_option = try_potential_handleerror!(self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg)); + match resp_option { + Some(resps) => { + for resp in resps.0 { + encode_and_send_msg!(resp, 128); + } + encode_and_send_msg!(resps.1, 132); + }, + None => {}, + } }, - 134 => { let msg = try_potential_decodeerror!(msgs::UpdateFee::decode(&msg_data[2..])); try_potential_handleerror!(self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg)); @@ -473,11 +465,25 @@ impl PeerManager { None => {} }; + pause_read + }; + + self.process_events(); + + Ok(pause_read) + } + + /// Checks for any events generated by our handlers and processes them. May be needed after eg + /// calls to ChannelManager::process_pending_htlc_forward. + pub fn process_events(&self) { + let mut upstream_events = Vec::new(); + { // TODO: There are some DoS attacks here where you can flood someone's outbound send // buffer by doing things like announcing channels on another node. We should be willing to // drop optional-ish messages when send buffers get full! let mut events_generated = self.message_handler.chan_handler.get_and_clear_pending_events(); + let mut peers = self.peers.lock().unwrap(); for event in events_generated.drain(..) { macro_rules! get_peer_for_forwarding { ($node_id: expr, $handle_no_such_peer: block) => { @@ -583,16 +589,12 @@ impl PeerManager { upstream_events.push(event); } - - pause_read - }; + } let mut pending_events = self.pending_events.lock().unwrap(); for event in upstream_events.drain(..) { pending_events.push(event); } - - Ok(pause_read) } /// Indicates that the given socket descriptor's connection is now closed.