use lightning::util::logger::Logger;
use std::task;
-use std::net::IpAddr;
use std::net::SocketAddr;
use std::net::TcpStream as StdTcpStream;
use std::sync::{Arc, Mutex};
}
}
+fn get_addr_from_stream(stream: &StdTcpStream) -> Option<NetAddress> {
+ match stream.peer_addr() {
+ Ok(SocketAddr::V4(sockaddr)) => Some(NetAddress::IPv4 {
+ addr: sockaddr.ip().octets(),
+ port: sockaddr.port(),
+ }),
+ Ok(SocketAddr::V6(sockaddr)) => Some(NetAddress::IPv6 {
+ addr: sockaddr.ip().octets(),
+ port: sockaddr.port(),
+ }),
+ Err(_) => None,
+ }
+}
+
/// Process incoming messages and feed outgoing messages on the provided socket generated by
/// accepting an incoming connection.
///
RMH: RoutingMessageHandler + 'static + Send + Sync,
L: Logger + 'static + ?Sized + Send + Sync,
UMH: CustomMessageHandler + 'static + Send + Sync {
- let ip_addr = stream.peer_addr().unwrap();
+ let remote_addr = get_addr_from_stream(&stream);
let (reader, write_receiver, read_receiver, us) = Connection::new(stream);
#[cfg(debug_assertions)]
let last_us = Arc::clone(&us);
- let handle_opt = if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone()), match ip_addr.ip() {
- IpAddr::V4(ip) => Some(NetAddress::IPv4 {
- addr: ip.octets(),
- port: ip_addr.port(),
- }),
- IpAddr::V6(ip) => Some(NetAddress::IPv6 {
- addr: ip.octets(),
- port: ip_addr.port(),
- }),
- }) {
+ let handle_opt = if let Ok(_) = peer_manager.new_inbound_connection(SocketDescriptor::new(us.clone()), remote_addr) {
Some(tokio::spawn(Connection::schedule_read(peer_manager, us, reader, read_receiver, write_receiver)))
} else {
// Note that we will skip socket_disconnected here, in accordance with the PeerManager
RMH: RoutingMessageHandler + 'static + Send + Sync,
L: Logger + 'static + ?Sized + Send + Sync,
UMH: CustomMessageHandler + 'static + Send + Sync {
- let ip_addr = stream.peer_addr().unwrap();
+ let remote_addr = get_addr_from_stream(&stream);
let (reader, mut write_receiver, read_receiver, us) = Connection::new(stream);
#[cfg(debug_assertions)]
let last_us = Arc::clone(&us);
- let handle_opt = if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone()), match ip_addr.ip() {
- IpAddr::V4(ip) => Some(NetAddress::IPv4 {
- addr: ip.octets(),
- port: ip_addr.port(),
- }),
- IpAddr::V6(ip) => Some(NetAddress::IPv6 {
- addr: ip.octets(),
- port: ip_addr.port(),
- }),
- }) {
+ let handle_opt = if let Ok(initial_send) = peer_manager.new_outbound_connection(their_node_id, SocketDescriptor::new(us.clone()), remote_addr) {
Some(tokio::spawn(async move {
// We should essentially always have enough room in a TCP socket buffer to send the
// initial 10s of bytes. However, tokio running in single-threaded mode will always