From: psycho-pirate Date: Sat, 12 Mar 2022 19:10:35 +0000 (+0530) Subject: added network address in methods, filter_address function with tests and updated... X-Git-Tag: v0.0.106~14^2~1 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=20a81e5c1484dfef0cdf636438889344052cb2b1;p=rust-lightning added network address in methods, filter_address function with tests and updated documentation --- diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index 79faba901..8c4f5adcb 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -411,8 +411,8 @@ pub fn do_test(data: &[u8], underlying_out: Out) { let mut channel_txn = Vec::new(); macro_rules! make_channel { ($source: expr, $dest: expr, $chan_id: expr) => { { - $source.peer_connected(&$dest.get_our_node_id(), &Init { features: InitFeatures::known() }); - $dest.peer_connected(&$source.get_our_node_id(), &Init { features: InitFeatures::known() }); + $source.peer_connected(&$dest.get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); + $dest.peer_connected(&$source.get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); $source.create_channel($dest.get_our_node_id(), 100_000, 42, 0, None).unwrap(); let open_channel = { @@ -921,15 +921,15 @@ pub fn do_test(data: &[u8], underlying_out: Out) { }, 0x0e => { if chan_a_disconnected { - nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known() }); - nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::known() }); + nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); + nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); chan_a_disconnected = false; } }, 0x0f => { if chan_b_disconnected { - nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::known() }); - nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known() }); + nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); + nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); chan_b_disconnected = false; } }, @@ -1124,13 +1124,13 @@ pub fn do_test(data: &[u8], underlying_out: Out) { // Next, make sure peers are all connected to each other if chan_a_disconnected { - nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known() }); - nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::known() }); + nodes[0].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); + nodes[1].peer_connected(&nodes[0].get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); chan_a_disconnected = false; } if chan_b_disconnected { - nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::known() }); - nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known() }); + nodes[1].peer_connected(&nodes[2].get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); + nodes[2].peer_connected(&nodes[1].get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); chan_b_disconnected = false; } diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index a4e1a0864..0412547a0 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -422,7 +422,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { } } if new_id == 0 { return; } - loss_detector.handler.new_outbound_connection(get_pubkey!(), Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap(); + loss_detector.handler.new_outbound_connection(get_pubkey!(), Peer{id: (new_id - 1) as u8, peers_connected: &peers}, None).unwrap(); peers.borrow_mut()[new_id - 1] = true; }, 1 => { @@ -434,7 +434,7 @@ pub fn do_test(data: &[u8], logger: &Arc) { } } if new_id == 0 { return; } - loss_detector.handler.new_inbound_connection(Peer{id: (new_id - 1) as u8, peers_connected: &peers}).unwrap(); + loss_detector.handler.new_inbound_connection(Peer{id: (new_id - 1) as u8, peers_connected: &peers}, None).unwrap(); peers.borrow_mut()[new_id - 1] = true; }, 2 => { diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 084ea62ef..22fef2661 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -435,8 +435,8 @@ mod tests { for i in 0..num_nodes { for j in (i+1)..num_nodes { - nodes[i].node.peer_connected(&nodes[j].node.get_our_node_id(), &Init { features: InitFeatures::known() }); - nodes[j].node.peer_connected(&nodes[i].node.get_our_node_id(), &Init { features: InitFeatures::known() }); + nodes[i].node.peer_connected(&nodes[j].node.get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); + nodes[j].node.peer_connected(&nodes[i].node.get_our_node_id(), &Init { features: InitFeatures::known(), remote_network_address: None }); } } diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index c5c18b5df..af2f57d3b 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -226,7 +226,7 @@ pub fn setup_inbound(peer_manager: Arc(peer_manager: Arc(node_count: usize, cfgs: &'b Vec(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>, send_funding_locked: (bool, bool), pending_htlc_adds: (i64, i64), pending_htlc_claims: (usize, usize), pending_htlc_fails: (usize, usize), pending_cell_htlc_claims: (usize, usize), pending_cell_htlc_fails: (usize, usize), pending_raa: (bool, bool)) { - node_a.node.peer_connected(&node_b.node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + node_a.node.peer_connected(&node_b.node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_1 = get_chan_reestablish_msgs!(node_a, node_b); - node_b.node.peer_connected(&node_a.node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + node_b.node.peer_connected(&node_a.node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_2 = get_chan_reestablish_msgs!(node_b, node_a); if send_funding_locked.0 { diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 85007e0e5..05d42fba2 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -3779,9 +3779,9 @@ fn test_funding_peer_disconnect() { let events_2 = nodes[1].node.get_and_clear_pending_msg_events(); assert!(events_2.is_empty()); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()); // nodes[0] hasn't yet received a funding_locked, so it only sends that on reconnect. @@ -4026,10 +4026,10 @@ fn test_drop_messages_peer_disconnect_dual_htlc() { nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); assert_eq!(reestablish_1.len(), 1); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); assert_eq!(reestablish_2.len(), 1); @@ -4305,9 +4305,9 @@ fn test_no_txn_manager_serialize_deserialize() { assert_eq!(nodes[0].node.list_channels().len(), 1); check_added_monitors!(nodes[0], 1); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]); @@ -4425,9 +4425,9 @@ fn test_manager_serialize_deserialize_events() { assert_eq!(nodes[0].node.list_channels().len(), 1); check_added_monitors!(nodes[0], 1); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &reestablish_1[0]); @@ -4620,9 +4620,9 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() { //... and we can even still claim the payment! claim_payment(&nodes[2], &[&nodes[0], &nodes[1]], our_payment_preimage); - nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[3].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish = get_event_msg!(nodes[3], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()); - nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[3].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); nodes[0].node.handle_channel_reestablish(&nodes[3].node.get_our_node_id(), &reestablish); let msg_events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(msg_events.len(), 1); @@ -6681,10 +6681,10 @@ fn test_update_add_htlc_bolt2_receiver_check_repeated_id_ignore() { //Disconnect and Reconnect nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); assert_eq!(reestablish_1.len(), 1); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); assert_eq!(reestablish_2.len(), 1); nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_2[0]); @@ -7329,8 +7329,8 @@ fn test_data_loss_protect() { check_added_monitors!(nodes[0], 1); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_0 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); @@ -7478,10 +7478,10 @@ fn test_announce_disable_channels() { } } // Reconnect peers - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_1 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); assert_eq!(reestablish_1.len(), 3); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let reestablish_2 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); assert_eq!(reestablish_2.len(), 3); @@ -9514,8 +9514,8 @@ fn test_keysend_payments_to_private_node() { let payer_pubkey = nodes[0].node.get_our_node_id(); let payee_pubkey = nodes[1].node.get_our_node_id(); - nodes[0].node.peer_connected(&payee_pubkey, &msgs::Init { features: InitFeatures::known() }); - nodes[1].node.peer_connected(&payer_pubkey, &msgs::Init { features: InitFeatures::known() }); + nodes[0].node.peer_connected(&payee_pubkey, &msgs::Init { features: InitFeatures::known(), remote_network_address: None }); + nodes[1].node.peer_connected(&payer_pubkey, &msgs::Init { features: InitFeatures::known(), remote_network_address: None }); let _chan = create_chan_between_nodes(&nodes[0], &nodes[1], InitFeatures::known(), InitFeatures::known()); let route_params = RouteParameters { diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 769d648fc..132e6c203 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -75,6 +75,11 @@ pub enum DecodeError { pub struct Init { /// The relevant features which the sender supports pub features: InitFeatures, + /// The receipient's network address. This adds the option to report a remote IP address + /// back to a connecting peer using the init message. A node can decide to use that information + /// to discover a potential update to its public IPv4 address (NAT) and use + /// that for a node_announcement update message containg the new address. + pub remote_network_address: Option, } /// An error message to be sent or received from a peer @@ -1167,7 +1172,11 @@ impl Writeable for Init { // global_features gets the bottom 13 bits of our features, and local_features gets all of // our relevant feature bits. This keeps us compatible with old nodes. self.features.write_up_to_13(w)?; - self.features.write(w) + self.features.write(w)?; + encode_tlv_stream!(w, { + (3, self.remote_network_address, option) + }); + Ok(()) } } @@ -1175,8 +1184,13 @@ impl Readable for Init { fn read(r: &mut R) -> Result { let global_features: InitFeatures = Readable::read(r)?; let features: InitFeatures = Readable::read(r)?; + let mut remote_network_address: Option = None; + decode_tlv_stream!(r, { + (3, remote_network_address, option) + }); Ok(Init { features: features.or(global_features), + remote_network_address, }) } } @@ -2447,13 +2461,27 @@ mod tests { fn encoding_init() { assert_eq!(msgs::Init { features: InitFeatures::from_le_bytes(vec![0xFF, 0xFF, 0xFF]), + remote_network_address: None, }.encode(), hex::decode("00023fff0003ffffff").unwrap()); assert_eq!(msgs::Init { features: InitFeatures::from_le_bytes(vec![0xFF]), + remote_network_address: None, }.encode(), hex::decode("0001ff0001ff").unwrap()); assert_eq!(msgs::Init { features: InitFeatures::from_le_bytes(vec![]), + remote_network_address: None, }.encode(), hex::decode("00000000").unwrap()); + + let init_msg = msgs::Init { features: InitFeatures::from_le_bytes(vec![]), + remote_network_address: Some(msgs::NetAddress::IPv4 { + addr: [127, 0, 0, 1], + port: 1000, + }), + }; + let encoded_value = init_msg.encode(); + let target_value = hex::decode("000000000307017f00000103e8").unwrap(); + assert_eq!(encoded_value, target_value); + assert_eq!(msgs::Init::read(&mut Cursor::new(&target_value)).unwrap(),init_msg); } #[test] diff --git a/lightning/src/ln/payment_tests.rs b/lightning/src/ln/payment_tests.rs index 26a0c1938..346fb98b4 100644 --- a/lightning/src/ln/payment_tests.rs +++ b/lightning/src/ln/payment_tests.rs @@ -449,12 +449,12 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) { assert_eq!(as_broadcasted_txn[0], as_commitment_tx); nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known()}); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None }); assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); // Now nodes[1] should send a channel reestablish, which nodes[0] will respond to with an // error, as the channel has hit the chain. - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known()}); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None }); let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()); nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish); let as_err = nodes[0].node.get_and_clear_pending_msg_events(); diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 4c4e22c0c..120a7535a 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -19,7 +19,7 @@ use bitcoin::secp256k1::key::{SecretKey,PublicKey}; use ln::features::InitFeatures; use ln::msgs; -use ln::msgs::{ChannelMessageHandler, LightningError, RoutingMessageHandler}; +use ln::msgs::{ChannelMessageHandler, LightningError, NetAddress, RoutingMessageHandler}; use ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager}; use util::ser::{VecWriter, Writeable, Writer}; use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep}; @@ -324,6 +324,7 @@ struct Peer { channel_encryptor: PeerChannelEncryptor, their_node_id: Option, their_features: Option, + their_net_address: Option, pending_outbound_buffer: LinkedList>, pending_outbound_buffer_first_msg_offset: usize, @@ -495,6 +496,36 @@ impl core::fmt::Display for OptionalFromDebugger<'_> { } } +/// A function used to filter out local or private addresses +/// https://www.iana.org./assignments/ipv4-address-space/ipv4-address-space.xhtml +/// https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml +fn filter_addresses(ip_address: Option) -> Option { + match ip_address{ + // For IPv4 range 10.0.0.0 - 10.255.255.255 (10/8) + Some(NetAddress::IPv4{addr: [0xA, 0x00..=0xFF, _, _], port: _}) => None, + // For IPv4 range 0.0.0.0 - 0.255.255.255 (0/8) + Some(NetAddress::IPv4{addr: [0x0, 0x0..=0xFF, _, _], port: _}) => None, + // For IPv4 range 100.64.0.0 - 100.127.255.255 (100.64/10) + Some(NetAddress::IPv4{addr: [0x64, 0x40..=0x7F, _, _], port: _}) => None, + // For IPv4 range 127.0.0.0 - 127.255.255.255 (127/8) + Some(NetAddress::IPv4{addr: [0x7F, 0x0..=0xFF, _, _], port: _}) => None, + // For IPv4 range 169.254.0.0 - 169.254.255.255 (169.254/16) + Some(NetAddress::IPv4{addr: [0xA9, 0xFE, _, _], port: _}) => None, + // For IPv4 range 172.16.0.0 - 172.31.255.255 (172.16/12) + Some(NetAddress::IPv4{addr: [0xAC, 0x10..=0x1F, _, _], port: _}) => None, + // For IPv4 range 192.168.0.0 - 192.168.255.255 (192.168/16) + Some(NetAddress::IPv4{addr: [0xC0, 0xA8, _, _], port: _}) => None, + // For IPv4 range 192.88.99.0 - 192.88.99.255 (192.88.99/24) + Some(NetAddress::IPv4{addr: [0xC0, 0x58, 0x63, _], port: _}) => None, + // For IPv6 range 2000:0000:0000:0000:0000:0000:0000:0000 - 3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff (2000::/3) + Some(NetAddress::IPv6{addr: [0x20..=0x3F, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _], port: _}) => ip_address, + // For remaining addresses + Some(NetAddress::IPv6{addr: _, port: _}) => None, + Some(..) => ip_address, + None => None, + } +} + impl PeerManager where CM::Target: ChannelMessageHandler, RM::Target: RoutingMessageHandler, @@ -543,7 +574,12 @@ impl P SecretKey::from_slice(&Sha256::from_engine(ephemeral_hash).into_inner()).expect("You broke SHA-256!") } - /// Indicates a new outbound connection has been established to a node with the given node_id. + /// Indicates a new outbound connection has been established to a node with the given node_id + /// and an optional remote network address. + /// The remote network address adds the option to report a remote IP address back to a connecting + /// peer using the init message. + /// The user should pass the remote network address to whatever host they are connected to. + /// /// Note that if an Err is returned here you MUST NOT call socket_disconnected for the new /// descriptor but must disconnect the connection immediately. /// @@ -553,7 +589,7 @@ impl P /// [`socket_disconnected()`]. /// /// [`socket_disconnected()`]: PeerManager::socket_disconnected - pub fn new_outbound_connection(&self, their_node_id: PublicKey, descriptor: Descriptor) -> Result, PeerHandleError> { + pub fn new_outbound_connection(&self, their_node_id: PublicKey, descriptor: Descriptor, remote_network_address: Option) -> Result, PeerHandleError> { let mut peer_encryptor = PeerChannelEncryptor::new_outbound(their_node_id.clone(), self.get_ephemeral_key()); let res = peer_encryptor.get_act_one().to_vec(); let pending_read_buffer = [0; 50].to_vec(); // Noise act two is 50 bytes @@ -563,6 +599,7 @@ impl P channel_encryptor: peer_encryptor, their_node_id: None, their_features: None, + their_net_address: remote_network_address, pending_outbound_buffer: LinkedList::new(), pending_outbound_buffer_first_msg_offset: 0, @@ -583,7 +620,8 @@ impl P Ok(res) } - /// Indicates a new inbound connection has been established. + /// Indicates a new inbound connection has been established to a node with an optional remote + /// network address. /// /// May refuse the connection by returning an Err, but will never write bytes to the remote end /// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT @@ -594,7 +632,7 @@ impl P /// [`socket_disconnected()`]. /// /// [`socket_disconnected()`]: PeerManager::socket_disconnected - pub fn new_inbound_connection(&self, descriptor: Descriptor) -> Result<(), PeerHandleError> { + pub fn new_inbound_connection(&self, descriptor: Descriptor, remote_network_address: Option) -> Result<(), PeerHandleError> { let peer_encryptor = PeerChannelEncryptor::new_inbound(&self.our_node_secret); let pending_read_buffer = [0; 50].to_vec(); // Noise act one is 50 bytes @@ -603,6 +641,7 @@ impl P channel_encryptor: peer_encryptor, their_node_id: None, their_features: None, + their_net_address: remote_network_address, pending_outbound_buffer: LinkedList::new(), pending_outbound_buffer_first_msg_offset: 0, @@ -864,7 +903,7 @@ impl P peer.their_node_id = Some(their_node_id); insert_node_id!(); let features = InitFeatures::known(); - let resp = msgs::Init { features }; + let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone())}; self.enqueue_message(peer, &resp); peer.awaiting_pong_timer_tick_intervals = 0; }, @@ -875,7 +914,7 @@ impl P peer.their_node_id = Some(their_node_id); insert_node_id!(); let features = InitFeatures::known(); - let resp = msgs::Init { features }; + let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone())}; self.enqueue_message(peer, &resp); peer.awaiting_pong_timer_tick_intervals = 0; }, @@ -1666,8 +1705,9 @@ fn is_gossip_msg(type_id: u16) -> bool { #[cfg(test)] mod tests { - use ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler}; + use ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler, filter_addresses}; use ln::msgs; + use ln::msgs::NetAddress; use util::events; use util::test_utils; @@ -1743,8 +1783,8 @@ mod tests { let a_id = PublicKey::from_secret_key(&secp_ctx, &peer_a.our_node_secret); let mut fd_a = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) }; let mut fd_b = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) }; - let initial_data = peer_b.new_outbound_connection(a_id, fd_b.clone()).unwrap(); - peer_a.new_inbound_connection(fd_a.clone()).unwrap(); + let initial_data = peer_b.new_outbound_connection(a_id, fd_b.clone(), None).unwrap(); + peer_a.new_inbound_connection(fd_a.clone(), None).unwrap(); assert_eq!(peer_a.read_event(&mut fd_a, &initial_data).unwrap(), false); peer_a.process_events(); assert_eq!(peer_b.read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false); @@ -1851,8 +1891,8 @@ mod tests { let a_id = PublicKey::from_secret_key(&secp_ctx, &peers[0].our_node_secret); let mut fd_a = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) }; let mut fd_b = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) }; - let initial_data = peers[1].new_outbound_connection(a_id, fd_b.clone()).unwrap(); - peers[0].new_inbound_connection(fd_a.clone()).unwrap(); + let initial_data = peers[1].new_outbound_connection(a_id, fd_b.clone(), None).unwrap(); + peers[0].new_inbound_connection(fd_a.clone(), None).unwrap(); // If we get a single timer tick before completion, that's fine assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 1); @@ -1870,4 +1910,100 @@ mod tests { assert!(peers[0].read_event(&mut fd_a, &fd_b.outbound_data.lock().unwrap().split_off(0)).is_err()); } + + #[test] + fn test_filter_addresses(){ + // Tests the filter_addresses function. + + // For (10/8) + let ip_address = NetAddress::IPv4{addr: [10, 0, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [10, 0, 255, 201], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [10, 255, 255, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For (0/8) + let ip_address = NetAddress::IPv4{addr: [0, 0, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [0, 0, 255, 187], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [0, 255, 255, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For (100.64/10) + let ip_address = NetAddress::IPv4{addr: [100, 64, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [100, 78, 255, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [100, 127, 255, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For (127/8) + let ip_address = NetAddress::IPv4{addr: [127, 0, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [127, 65, 73, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [127, 255, 255, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For (169.254/16) + let ip_address = NetAddress::IPv4{addr: [169, 254, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [169, 254, 221, 101], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [169, 254, 255, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For (172.16/12) + let ip_address = NetAddress::IPv4{addr: [172, 16, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [172, 27, 101, 23], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [172, 31, 255, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For (192.168/16) + let ip_address = NetAddress::IPv4{addr: [192, 168, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [192, 168, 205, 159], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [192, 168, 255, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For (192.88.99/24) + let ip_address = NetAddress::IPv4{addr: [192, 88, 99, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [192, 88, 99, 140], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv4{addr: [192, 88, 99, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For other IPv4 addresses + let ip_address = NetAddress::IPv4{addr: [188, 255, 99, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), Some(ip_address.clone())); + let ip_address = NetAddress::IPv4{addr: [123, 8, 129, 14], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), Some(ip_address.clone())); + let ip_address = NetAddress::IPv4{addr: [2, 88, 9, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), Some(ip_address.clone())); + + // For (2000::/3) + let ip_address = NetAddress::IPv6{addr: [32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), Some(ip_address.clone())); + let ip_address = NetAddress::IPv6{addr: [45, 34, 209, 190, 0, 123, 55, 34, 0, 0, 3, 27, 201, 0, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), Some(ip_address.clone())); + let ip_address = NetAddress::IPv6{addr: [63, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), Some(ip_address.clone())); + + // For other IPv6 addresses + let ip_address = NetAddress::IPv6{addr: [24, 240, 12, 32, 0, 0, 0, 0, 20, 97, 0, 32, 121, 254, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv6{addr: [68, 23, 56, 63, 0, 0, 2, 7, 75, 109, 0, 39, 0, 0, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + let ip_address = NetAddress::IPv6{addr: [101, 38, 140, 230, 100, 0, 30, 98, 0, 26, 0, 0, 57, 96, 0, 0], port: 1000}; + assert_eq!(filter_addresses(Some(ip_address.clone())), None); + + // For (None) + assert_eq!(filter_addresses(None), None); + } } diff --git a/lightning/src/ln/priv_short_conf_tests.rs b/lightning/src/ln/priv_short_conf_tests.rs index a6f5a1b2e..66c1b20d2 100644 --- a/lightning/src/ln/priv_short_conf_tests.rs +++ b/lightning/src/ln/priv_short_conf_tests.rs @@ -132,8 +132,8 @@ fn test_priv_forwarding_rejection() { check_added_monitors!(nodes[1], 2); nodes[1].node = &nodes_1_deserialized; - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known() }); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()); let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()); nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish); @@ -141,8 +141,8 @@ fn test_priv_forwarding_rejection() { get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); - nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known() }); - nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None }); + nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[2].node.get_our_node_id()); let cs_reestablish = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()); nodes[2].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish); diff --git a/lightning/src/ln/shutdown_tests.rs b/lightning/src/ln/shutdown_tests.rs index cd0f77448..557aff84c 100644 --- a/lightning/src/ln/shutdown_tests.rs +++ b/lightning/src/ln/shutdown_tests.rs @@ -245,9 +245,9 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) { nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let node_0_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let node_1_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()); nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &node_0_reestablish); @@ -305,9 +305,9 @@ fn do_test_shutdown_rebroadcast(recv_count: u8) { nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id(), false); nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); let node_1_2nd_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()); - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty() }); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); if recv_count == 0 { // If all closing_signeds weren't delivered we can just resume where we left off... let node_0_2nd_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()); diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index d113acbfc..e7db446a8 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -505,7 +505,7 @@ mod tests { let mut reader = io::Cursor::new(buffer); let decoded_msg = read(&mut reader, &IgnoringMessageHandler{}).unwrap(); match decoded_msg { - Message::Init(msgs::Init { features }) => { + Message::Init(msgs::Init { features, .. }) => { assert!(features.supports_variable_length_onion()); assert!(features.supports_upfront_shutdown_script()); assert!(features.supports_gossip_queries()); diff --git a/lightning/src/routing/network_graph.rs b/lightning/src/routing/network_graph.rs index e7c8271bf..012357df3 100644 --- a/lightning/src/routing/network_graph.rs +++ b/lightning/src/routing/network_graph.rs @@ -2284,7 +2284,7 @@ mod tests { // It should ignore if gossip_queries feature is not enabled { - let init_msg = Init { features: InitFeatures::known().clear_gossip_queries() }; + let init_msg = Init { features: InitFeatures::known().clear_gossip_queries(), remote_network_address: None }; net_graph_msg_handler.peer_connected(&node_id_1, &init_msg); let events = net_graph_msg_handler.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 0); @@ -2292,7 +2292,7 @@ mod tests { // It should send a query_channel_message with the correct information { - let init_msg = Init { features: InitFeatures::known() }; + let init_msg = Init { features: InitFeatures::known(), remote_network_address: None }; net_graph_msg_handler.peer_connected(&node_id_1, &init_msg); let events = net_graph_msg_handler.get_and_clear_pending_msg_events(); assert_eq!(events.len(), 2); @@ -2322,7 +2322,7 @@ mod tests { { let network_graph = create_network_graph(); let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler(&network_graph); - let init_msg = Init { features: InitFeatures::known() }; + let init_msg = Init { features: InitFeatures::known(), remote_network_address: None }; for n in 1..7 { let node_privkey = &SecretKey::from_slice(&[n; 32]).unwrap(); let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);