fix(p2p): remove `AddrV2` <> `SocketAddr` conversions
This commit is contained in:
parent
5e0b86d2b1
commit
6335c623f6
|
@ -138,56 +138,6 @@ pub enum AddrV2 {
|
|||
Unknown(u8, Vec<u8>),
|
||||
}
|
||||
|
||||
/// Error types for [`AddrV2`] to [`SocketAddr`] conversion.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum AddrV2ToSocketAddrError {
|
||||
/// A [`AddrV2::TorV3`] address cannot be converted to a [`SocketAddr`].
|
||||
TorV3NotSupported,
|
||||
/// A [`AddrV2::I2p`] address cannot be converted to a [`SocketAddr`].
|
||||
I2pNotSupported,
|
||||
/// A [`AddrV2::Cjdns`] address can be converted to a [`SocketAddr`],
|
||||
/// but it won't work with a tradicional socket API.
|
||||
CjdnsNotRecommended,
|
||||
/// A [`AddrV2::Unknown`] address cannot be converted to a [`SocketAddr`].
|
||||
UnknownNotSupported,
|
||||
}
|
||||
|
||||
impl fmt::Display for AddrV2ToSocketAddrError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::TorV3NotSupported => write!(f, "TorV3 addresses cannot be converted to SocketAddr"),
|
||||
Self::I2pNotSupported => write!(f, "I2P addresses cannot be converted to SocketAddr"),
|
||||
Self::CjdnsNotRecommended => write!(f, "CJDNS addresses can be converted to SocketAddr, but won't work with a traditional socket API"),
|
||||
Self::UnknownNotSupported => write!(f, "Unknown address type cannot be converted to SocketAddr"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for AddrV2ToSocketAddrError {}
|
||||
|
||||
impl From<SocketAddr> for AddrV2 {
|
||||
fn from(addr: SocketAddr) -> Self {
|
||||
match addr {
|
||||
SocketAddr::V4(sock) => AddrV2::Ipv4(*sock.ip()),
|
||||
SocketAddr::V6(sock) => AddrV2::Ipv6(*sock.ip()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<AddrV2> for SocketAddr {
|
||||
type Error = AddrV2ToSocketAddrError;
|
||||
|
||||
fn try_from(addr: AddrV2) -> Result<SocketAddr, Self::Error> {
|
||||
match addr {
|
||||
AddrV2::Ipv4(ip) => Ok(SocketAddr::V4(SocketAddrV4::new(ip, 0))),
|
||||
AddrV2::Ipv6(ip) => Ok(SocketAddr::V6(SocketAddrV6::new(ip, 0, 0, 0))),
|
||||
AddrV2::Cjdns(_) => Err(AddrV2ToSocketAddrError::CjdnsNotRecommended),
|
||||
AddrV2::TorV3(_) => Err(AddrV2ToSocketAddrError::TorV3NotSupported),
|
||||
AddrV2::I2p(_) => Err(AddrV2ToSocketAddrError::I2pNotSupported),
|
||||
AddrV2::Unknown(_, _) => Err(AddrV2ToSocketAddrError::UnknownNotSupported),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<AddrV2> for IpAddr {
|
||||
type Error = AddrV2ToIpAddrError;
|
||||
|
@ -731,87 +681,6 @@ mod test {
|
|||
assert_eq!(serialize(&addresses), raw);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn socketaddr_to_addrv2_ipv4() {
|
||||
let socket = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 1, 1), 8333));
|
||||
let addr = AddrV2::from(socket);
|
||||
|
||||
assert_eq!(addr, AddrV2::Ipv4(Ipv4Addr::new(192, 168, 1, 1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn socketaddr_to_addrv2_ipv6() {
|
||||
let socket = SocketAddr::V6(SocketAddrV6::new(
|
||||
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
|
||||
8333,
|
||||
0,
|
||||
0,
|
||||
));
|
||||
let addr = AddrV2::from(socket);
|
||||
|
||||
assert_eq!(addr, AddrV2::Ipv6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addrv2_to_socketaddr_ipv4() {
|
||||
let addr = AddrV2::Ipv4(Ipv4Addr::new(192, 168, 1, 1));
|
||||
let socket = SocketAddr::try_from(addr).unwrap();
|
||||
|
||||
assert_eq!(socket, SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 1, 1), 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addrv2_to_socketaddr_ipv6() {
|
||||
let addr = AddrV2::Ipv6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
|
||||
let socket = SocketAddr::try_from(addr).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
socket,
|
||||
SocketAddr::V6(SocketAddrV6::new(
|
||||
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
|
||||
0,
|
||||
0,
|
||||
0
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addrv2_to_socketaddr_cjdns() {
|
||||
let addr = AddrV2::Cjdns(Ipv6Addr::new(0xfc00, 0, 0, 0, 0, 0, 0, 1));
|
||||
let result = SocketAddr::try_from(addr);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.unwrap_err(), AddrV2ToSocketAddrError::CjdnsNotRecommended);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addrv2_to_socketaddr_torv3() {
|
||||
let addr = AddrV2::TorV3([0; 32]);
|
||||
let result = SocketAddr::try_from(addr);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.unwrap_err(), AddrV2ToSocketAddrError::TorV3NotSupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addrv2_to_socketaddr_i2p() {
|
||||
let addr = AddrV2::I2p([0; 32]);
|
||||
let result = SocketAddr::try_from(addr);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.unwrap_err(), AddrV2ToSocketAddrError::I2pNotSupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addrv2_to_socketaddr_unknown() {
|
||||
let addr = AddrV2::Unknown(42, vec![1, 2, 3, 4]);
|
||||
let result = SocketAddr::try_from(addr);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert_eq!(result.unwrap_err(), AddrV2ToSocketAddrError::UnknownNotSupported);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addrv2_to_ipaddr_ipv4() {
|
||||
let addr = AddrV2::Ipv4(Ipv4Addr::new(192, 168, 1, 1));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use std::convert::TryFrom;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
|
||||
use bitcoin::consensus::Decodable;
|
||||
use bitcoin::p2p::address::AddrV2;
|
||||
|
@ -31,14 +31,6 @@ fn do_test(data: &[u8]) {
|
|||
let round_trip: AddrV2 = AddrV2::from(ip_addr);
|
||||
assert_eq!(addr_v2, round_trip, "AddrV2 -> Ipv6Addr -> AddrV2 should round-trip correctly");
|
||||
}
|
||||
|
||||
if let Ok(socket_addr) = SocketAddr::try_from(addr_v2.clone()) {
|
||||
let round_trip: AddrV2 = AddrV2::from(socket_addr);
|
||||
assert_eq!(
|
||||
addr_v2, round_trip,
|
||||
"AddrV2 -> SocketAddr -> AddrV2 should round-trip correctly"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
Loading…
Reference in New Issue