Skip to content

Commit 3870acd

Browse files
committed
Argument added in lightning-net-tokio/src/lib.rs
1 parent e8cf22f commit 3870acd

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ 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::convert::TryInto;
89+
use std::net::IpAddr;
8890
use std::net::SocketAddr;
8991
use std::net::TcpStream as StdTcpStream;
9092
use std::sync::{Arc, Mutex};
@@ -222,11 +224,21 @@ pub fn setup_inbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerManag
222224
RMH: RoutingMessageHandler + 'static + Send + Sync,
223225
L: Logger + 'static + ?Sized + Send + Sync,
224226
UMH: CustomMessageHandler + 'static + Send + Sync {
227+
let ip_addr = stream.peer_addr().unwrap();
225228
let (reader, write_receiver, read_receiver, us) = Connection::new(stream);
226229
#[cfg(debug_assertions)]
227230
let last_us = Arc::clone(&us);
228231

229-
let handle_opt = if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone()), None) {
232+
let handle_opt = if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone()), match ip_addr.ip() {
233+
IpAddr::V4(ip) => Some(NetAddress::IPv4 {
234+
addr: ip.octets(),
235+
port: ip_addr.port(),
236+
}),
237+
IpAddr::V6(ip) => Some(NetAddress::IPv6 {
238+
addr: ip.octets(),
239+
port: ip_addr.port(),
240+
}),
241+
}) {
230242
Some(tokio::spawn(Connection::schedule_read(peer_manager, us, reader, read_receiver, write_receiver)))
231243
} else {
232244
// Note that we will skip socket_disconnected here, in accordance with the PeerManager
@@ -263,11 +275,20 @@ pub fn setup_outbound<CMH, RMH, L, UMH>(peer_manager: Arc<peer_handler::PeerMana
263275
RMH: RoutingMessageHandler + 'static + Send + Sync,
264276
L: Logger + 'static + ?Sized + Send + Sync,
265277
UMH: CustomMessageHandler + 'static + Send + Sync {
278+
let ip_addr = stream.peer_addr().unwrap();
266279
let (reader, mut write_receiver, read_receiver, us) = Connection::new(stream);
267280
#[cfg(debug_assertions)]
268281
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) {
282+
let handle_opt = if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone()), match ip_addr.ip() {
283+
IpAddr::V4(ip) => Some(NetAddress::IPv4 {
284+
addr: ip.octets(),
285+
port: ip_addr.port(),
286+
}),
287+
IpAddr::V6(ip) => Some(NetAddress::IPv6 {
288+
addr: ip.octets(),
289+
port: ip_addr.port(),
290+
}),
291+
}) {
271292
Some(tokio::spawn(async move {
272293
// We should essentially always have enough room in a TCP socket buffer to send the
273294
// initial 10s of bytes. However, tokio running in single-threaded mode will always

lightning/src/ln/msgs.rs

Lines changed: 3 additions & 3 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 containg the new address.
8282
pub remote_network_address: Option<NetAddress>,
8383
}
8484

@@ -2479,7 +2479,7 @@ 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);
24842484
assert_eq!(msgs::Init::read(&mut Cursor::new(&target_value)).unwrap(),init_msg);
24852485
}

lightning/src/ln/peer_handler.rs

Lines changed: 9 additions & 9 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
@@ -577,7 +577,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
577577
/// Indicates a new outbound connection has been established to a node with the given node_id
578578
/// and an optional remote network address.
579579
/// The remote network address adds the option to report a remote IP address back to a connecting
580-
/// peer using the init message.
580+
/// peer using the init message.
581581
/// The user should pass the remote network address to whatever host they are connected to.
582582
///
583583
/// Note that if an Err is returned here you MUST NOT call socket_disconnected for the new

0 commit comments

Comments
 (0)