Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl ToSocketAddrs for SocketAddress #2636

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll have to also feature-gate this import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure if I should use the existing [cfg(feature = "std")] or create a new feature flag. I imagine the former is the right approach but I want to make sure. Let me know

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah just using that one is good as we just want to ensure it's only imported when we have access to std :)

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 @@ -2676,7 +2709,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 @@ -4105,4 +4138,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());
}
}
Loading