X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fmsgs.rs;h=ef07d4c918f8597f28a6b3df90aeaded15b06bb9;hb=aeeafed7d531618edc54effbdeed5e35ee3eaa16;hp=fab396e3c7112af934021041abfcf2e1f4d609af;hpb=996d3d8058f2342b9e23d53f9dd81ae472822120;p=rust-lightning diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index fab396e3..ef07d4c9 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 containing the new address. + pub remote_network_address: Option, } /// An error message to be sent or received from a peer @@ -886,7 +891,7 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider { /// Called when a connection is established with a peer. This can be used to /// perform routing table synchronization using a strategy defined by the /// implementor. - fn sync_routing_table(&self, their_node_id: &PublicKey, init: &Init); + fn peer_connected(&self, their_node_id: &PublicKey, init: &Init); /// Handles the reply of a query we initiated to learn about channels /// for a given range of blocks. We can expect to receive one or more /// replies to a single query. @@ -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]