Skip to content

Commit fc2f793

Browse files
committed
Argument added in lightning-net-tokio/src/lib.rs and comments updated
1 parent 20a81e5 commit fc2f793

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
8181
use lightning::ln::peer_handler;
8282
use lightning::ln::peer_handler::SocketDescriptor as LnSocketTrait;
8383
use lightning::ln::peer_handler::CustomMessageHandler;
84-
use lightning::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler};
84+
use lightning::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, NetAddress};
8585
use lightning::util::logger::Logger;
8686

8787
use std::task;
88+
use std::net::IpAddr;
8889
use std::net::SocketAddr;
8990
use std::net::TcpStream as StdTcpStream;
9091
use std::sync::{Arc, Mutex};
@@ -222,11 +223,21 @@ pub fn setup_inbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManag
222223
RMH: RoutingMessageHandler + 'static + Send + Sync,
223224
L: Logger + 'static + ?Sized + Send + Sync,
224225
UMH: CustomMessageHandler + 'static + Send + Sync {
226+
let ip_addr = stream.peer_addr().unwrap();
225227
let (reader, write_receiver, read_receiver, us) = Connection::new(stream);
226228
#[cfg(debug_assertions)]
227229
let last_us = Arc::clone(&us);
228230

229-
let handle_opt = if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone()), None) {
231+
let handle_opt = if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone()), match ip_addr.ip() {
232+
IpAddr::V4(ip) => Some(NetAddress::IPv4 {
233+
addr: ip.octets(),
234+
port: ip_addr.port(),
235+
}),
236+
IpAddr::V6(ip) => Some(NetAddress::IPv6 {
237+
addr: ip.octets(),
238+
port: ip_addr.port(),
239+
}),
240+
}) {
230241
Some(tokio::spawn(Connection::schedule_read(peer_manager, us, reader, read_receiver, write_receiver)))
231242
} else {
232243
// Note that we will skip socket_disconnected here, in accordance with the PeerManager
@@ -263,11 +274,20 @@ pub fn setup_outbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerMana
263274
RMH: RoutingMessageHandler + 'static + Send + Sync,
264275
L: Logger + 'static + ?Sized + Send + Sync,
265276
UMH: CustomMessageHandler + 'static + Send + Sync {
277+
let ip_addr = stream.peer_addr().unwrap();
266278
let (reader, mut write_receiver, read_receiver, us) = Connection::new(stream);
267279
#[cfg(debug_assertions)]
268280
let last_us = Arc::clone(&us);
269-
270-
let handle_opt = if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone()), None) {
281+
let handle_opt = if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone()), match ip_addr.ip() {
282+
IpAddr::V4(ip) => Some(NetAddress::IPv4 {
283+
addr: ip.octets(),
284+
port: ip_addr.port(),
285+
}),
286+
IpAddr::V6(ip) => Some(NetAddress::IPv6 {
287+
addr: ip.octets(),
288+
port: ip_addr.port(),
289+
}),
290+
}) {
271291
Some(tokio::spawn(async move {
272292
// We should essentially always have enough room in a TCP socket buffer to send the
273293
// initial 10s of bytes. However, tokio running in single-threaded mode will always

lightning/src/ln/msgs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ pub enum DecodeError {
7575
pub struct Init {
7676
/// The relevant features which the sender supports
7777
pub features: InitFeatures,
78-
/// The receipient's network address. This adds the option to report a remote IP address
78+
/// The receipient's network address. This adds the option to report a remote IP address
7979
/// back to a connecting peer using the init message. A node can decide to use that information
8080
/// to discover a potential update to its public IPv4 address (NAT) and use
81-
/// that for a node_announcement update message containg the new address.
81+
/// that for a node_announcement update message containing the new address.
8282
pub remote_network_address: Option<NetAddress>,
8383
}
8484

@@ -2479,9 +2479,9 @@ mod tests {
24792479
}),
24802480
};
24812481
let encoded_value = init_msg.encode();
2482-
let target_value = hex::decode("000000000307017f00000103e8").unwrap();
2482+
let target_value = hex::decode("000000000307017f00000103e8").unwrap();
24832483
assert_eq!(encoded_value, target_value);
2484-
assert_eq!(msgs::Init::read(&mut Cursor::new(&target_value)).unwrap(),init_msg);
2484+
assert_eq!(msgs::Init::read(&mut Cursor::new(&target_value)).unwrap(), init_msg);
24852485
}
24862486

24872487
#[test]

lightning/src/ln/peer_handler.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -502,21 +502,21 @@ impl core::fmt::Display for OptionalFromDebugger<'_> {
502502
fn filter_addresses(ip_address: Option<NetAddress>) -> Option<NetAddress> {
503503
match ip_address{
504504
// For IPv4 range 10.0.0.0 - 10.255.255.255 (10/8)
505-
Some(NetAddress::IPv4{addr: [0xA, 0x00..=0xFF, _, _], port: _}) => None,
505+
Some(NetAddress::IPv4{addr: [10, _, _, _], port: _}) => None,
506506
// For IPv4 range 0.0.0.0 - 0.255.255.255 (0/8)
507-
Some(NetAddress::IPv4{addr: [0x0, 0x0..=0xFF, _, _], port: _}) => None,
507+
Some(NetAddress::IPv4{addr: [0, _, _, _], port: _}) => None,
508508
// For IPv4 range 100.64.0.0 - 100.127.255.255 (100.64/10)
509-
Some(NetAddress::IPv4{addr: [0x64, 0x40..=0x7F, _, _], port: _}) => None,
509+
Some(NetAddress::IPv4{addr: [100, 64..=127, _, _], port: _}) => None,
510510
// For IPv4 range 127.0.0.0 - 127.255.255.255 (127/8)
511-
Some(NetAddress::IPv4{addr: [0x7F, 0x0..=0xFF, _, _], port: _}) => None,
511+
Some(NetAddress::IPv4{addr: [127, _, _, _], port: _}) => None,
512512
// For IPv4 range 169.254.0.0 - 169.254.255.255 (169.254/16)
513-
Some(NetAddress::IPv4{addr: [0xA9, 0xFE, _, _], port: _}) => None,
513+
Some(NetAddress::IPv4{addr: [169, 254, _, _], port: _}) => None,
514514
// For IPv4 range 172.16.0.0 - 172.31.255.255 (172.16/12)
515-
Some(NetAddress::IPv4{addr: [0xAC, 0x10..=0x1F, _, _], port: _}) => None,
515+
Some(NetAddress::IPv4{addr: [172, 16..=31, _, _], port: _}) => None,
516516
// For IPv4 range 192.168.0.0 - 192.168.255.255 (192.168/16)
517-
Some(NetAddress::IPv4{addr: [0xC0, 0xA8, _, _], port: _}) => None,
517+
Some(NetAddress::IPv4{addr: [192, 168, _, _], port: _}) => None,
518518
// For IPv4 range 192.88.99.0 - 192.88.99.255 (192.88.99/24)
519-
Some(NetAddress::IPv4{addr: [0xC0, 0x58, 0x63, _], port: _}) => None,
519+
Some(NetAddress::IPv4{addr: [192, 88, 99, _], port: _}) => None,
520520
// For IPv6 range 2000:0000:0000:0000:0000:0000:0000:0000 - 3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff (2000::/3)
521521
Some(NetAddress::IPv6{addr: [0x20..=0x3F, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _], port: _}) => ip_address,
522522
// For remaining addresses
@@ -576,9 +576,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
576576

577577
/// Indicates a new outbound connection has been established to a node with the given node_id
578578
/// and an optional remote network address.
579+
///
579580
/// The remote network address adds the option to report a remote IP address back to a connecting
580-
/// peer using the init message.
581-
/// The user should pass the remote network address to whatever host they are connected to.
581+
/// peer using the init message.
582+
/// The user should pass the remote network address of the host they are connected to.
582583
///
583584
/// Note that if an Err is returned here you MUST NOT call socket_disconnected for the new
584585
/// descriptor but must disconnect the connection immediately.
@@ -623,6 +624,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
623624
/// Indicates a new inbound connection has been established to a node with an optional remote
624625
/// network address.
625626
///
627+
/// The remote network address adds the option to report a remote IP address back to a connecting
628+
/// peer using the init message.
629+
/// The user should pass the remote network address of the host they are connected to.
630+
///
626631
/// May refuse the connection by returning an Err, but will never write bytes to the remote end
627632
/// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
628633
/// call socket_disconnected for the new descriptor but must disconnect the connection
@@ -903,7 +908,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
903908
peer.their_node_id = Some(their_node_id);
904909
insert_node_id!();
905910
let features = InitFeatures::known();
906-
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone())};
911+
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone())};
907912
self.enqueue_message(peer, &resp);
908913
peer.awaiting_pong_timer_tick_intervals = 0;
909914
},
@@ -914,7 +919,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
914919
peer.their_node_id = Some(their_node_id);
915920
insert_node_id!();
916921
let features = InitFeatures::known();
917-
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone())};
922+
let resp = msgs::Init { features, remote_network_address: filter_addresses(peer.their_net_address.clone())};
918923
self.enqueue_message(peer, &resp);
919924
peer.awaiting_pong_timer_tick_intervals = 0;
920925
},

0 commit comments

Comments
 (0)