Skip to content

Commit

Permalink
Merge pull request #2636 from slanesuke/impl-ToSocketAddrs-for-Hostname
Browse files Browse the repository at this point in the history
Impl ToSocketAddrs for SocketAddress
  • Loading branch information
tnull authored Oct 20, 2023
2 parents 6fff3e5 + e9ff38f commit 62fd36d
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ use core::fmt::Debug;
use core::ops::Deref;
#[cfg(feature = "std")]
use core::str::FromStr;
#[cfg(feature = "std")]
use std::net::SocketAddr;
use crate::io::{self, Cursor, Read};
use crate::io_extras::read_to_end;

Expand Down Expand Up @@ -958,6 +960,37 @@ impl From<std::net::SocketAddr> for SocketAddress {
}
}

#[cfg(feature = "std")]
impl std::net::ToSocketAddrs for SocketAddress {
type Iter = std::vec::IntoIter<std::net::SocketAddr>;

fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
match self {
SocketAddress::TcpIpV4 { addr, port } => {
let ip_addr = std::net::Ipv4Addr::from(*addr);
let socket_addr = SocketAddr::new(ip_addr.into(), *port);
Ok(vec![socket_addr].into_iter())
}
SocketAddress::TcpIpV6 { addr, port } => {
let ip_addr = std::net::Ipv6Addr::from(*addr);
let socket_addr = SocketAddr::new(ip_addr.into(), *port);
Ok(vec![socket_addr].into_iter())
}
SocketAddress::Hostname { ref hostname, port } => {
(hostname.as_str(), *port).to_socket_addrs()
}
SocketAddress::OnionV2(..) => {
Err(std::io::Error::new(std::io::ErrorKind::Other, "Resolution of OnionV2 \
addresses is currently unsupported."))
}
SocketAddress::OnionV3 { .. } => {
Err(std::io::Error::new(std::io::ErrorKind::Other, "Resolution of OnionV3 \
addresses is currently unsupported."))
}
}
}
}

/// Parses an OnionV3 host and port into a [`SocketAddress::OnionV3`].
///
/// The host part must end with ".onion".
Expand Down Expand Up @@ -2681,7 +2714,7 @@ mod tests {
use crate::chain::transaction::OutPoint;

#[cfg(feature = "std")]
use std::net::{Ipv4Addr, Ipv6Addr};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
use crate::ln::msgs::SocketAddressParseError;

#[test]
Expand Down Expand Up @@ -4110,4 +4143,22 @@ mod tests {
assert!("invalid-address".parse::<SocketAddress>().is_err());
assert!(SocketAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion.onion:1234").is_err());
}

#[test]
#[cfg(feature = "std")]
fn test_socket_address_to_socket_addrs() {
assert_eq!(SocketAddress::TcpIpV4 {addr:[0u8; 4], port: 1337,}.to_socket_addrs().unwrap().next().unwrap(),
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0,0,0,0), 1337)));
assert_eq!(SocketAddress::TcpIpV6 {addr:[0u8; 16], port: 1337,}.to_socket_addrs().unwrap().next().unwrap(),
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::from([0u8; 16]), 1337, 0, 0)));
assert_eq!(SocketAddress::Hostname { hostname: Hostname::try_from("0.0.0.0".to_string()).unwrap(), port: 0 }
.to_socket_addrs().unwrap().next().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::from([0u8; 4]),0)));
assert!(SocketAddress::OnionV2([0u8; 12]).to_socket_addrs().is_err());
assert!(SocketAddress::OnionV3{ ed25519_pubkey: [37, 24, 75, 5, 25, 73, 117, 194, 139, 102,
182, 107, 4, 105, 247, 246, 85, 111, 177, 172, 49, 137, 167, 155, 64, 221, 163, 47, 31,
33, 71, 3],
checksum: 48326,
version: 121,
port: 1234 }.to_socket_addrs().is_err());
}
}

0 comments on commit 62fd36d

Please sign in to comment.