Skip to content

Commit 6335c62

Browse files
committed
fix(p2p): remove AddrV2 <> SocketAddr conversions
1 parent 5e0b86d commit 6335c62

File tree

2 files changed

+1
-140
lines changed

2 files changed

+1
-140
lines changed

bitcoin/src/p2p/address.rs

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -138,56 +138,6 @@ pub enum AddrV2 {
138138
Unknown(u8, Vec<u8>),
139139
}
140140

141-
/// Error types for [`AddrV2`] to [`SocketAddr`] conversion.
142-
#[derive(Debug, PartialEq, Eq)]
143-
pub enum AddrV2ToSocketAddrError {
144-
/// A [`AddrV2::TorV3`] address cannot be converted to a [`SocketAddr`].
145-
TorV3NotSupported,
146-
/// A [`AddrV2::I2p`] address cannot be converted to a [`SocketAddr`].
147-
I2pNotSupported,
148-
/// A [`AddrV2::Cjdns`] address can be converted to a [`SocketAddr`],
149-
/// but it won't work with a tradicional socket API.
150-
CjdnsNotRecommended,
151-
/// A [`AddrV2::Unknown`] address cannot be converted to a [`SocketAddr`].
152-
UnknownNotSupported,
153-
}
154-
155-
impl fmt::Display for AddrV2ToSocketAddrError {
156-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
157-
match self {
158-
Self::TorV3NotSupported => write!(f, "TorV3 addresses cannot be converted to SocketAddr"),
159-
Self::I2pNotSupported => write!(f, "I2P addresses cannot be converted to SocketAddr"),
160-
Self::CjdnsNotRecommended => write!(f, "CJDNS addresses can be converted to SocketAddr, but won't work with a traditional socket API"),
161-
Self::UnknownNotSupported => write!(f, "Unknown address type cannot be converted to SocketAddr"),
162-
}
163-
}
164-
}
165-
166-
impl std::error::Error for AddrV2ToSocketAddrError {}
167-
168-
impl From<SocketAddr> for AddrV2 {
169-
fn from(addr: SocketAddr) -> Self {
170-
match addr {
171-
SocketAddr::V4(sock) => AddrV2::Ipv4(*sock.ip()),
172-
SocketAddr::V6(sock) => AddrV2::Ipv6(*sock.ip()),
173-
}
174-
}
175-
}
176-
177-
impl TryFrom<AddrV2> for SocketAddr {
178-
type Error = AddrV2ToSocketAddrError;
179-
180-
fn try_from(addr: AddrV2) -> Result<SocketAddr, Self::Error> {
181-
match addr {
182-
AddrV2::Ipv4(ip) => Ok(SocketAddr::V4(SocketAddrV4::new(ip, 0))),
183-
AddrV2::Ipv6(ip) => Ok(SocketAddr::V6(SocketAddrV6::new(ip, 0, 0, 0))),
184-
AddrV2::Cjdns(_) => Err(AddrV2ToSocketAddrError::CjdnsNotRecommended),
185-
AddrV2::TorV3(_) => Err(AddrV2ToSocketAddrError::TorV3NotSupported),
186-
AddrV2::I2p(_) => Err(AddrV2ToSocketAddrError::I2pNotSupported),
187-
AddrV2::Unknown(_, _) => Err(AddrV2ToSocketAddrError::UnknownNotSupported),
188-
}
189-
}
190-
}
191141

192142
impl TryFrom<AddrV2> for IpAddr {
193143
type Error = AddrV2ToIpAddrError;
@@ -731,87 +681,6 @@ mod test {
731681
assert_eq!(serialize(&addresses), raw);
732682
}
733683

734-
#[test]
735-
fn socketaddr_to_addrv2_ipv4() {
736-
let socket = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 1, 1), 8333));
737-
let addr = AddrV2::from(socket);
738-
739-
assert_eq!(addr, AddrV2::Ipv4(Ipv4Addr::new(192, 168, 1, 1)));
740-
}
741-
742-
#[test]
743-
fn socketaddr_to_addrv2_ipv6() {
744-
let socket = SocketAddr::V6(SocketAddrV6::new(
745-
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
746-
8333,
747-
0,
748-
0,
749-
));
750-
let addr = AddrV2::from(socket);
751-
752-
assert_eq!(addr, AddrV2::Ipv6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)));
753-
}
754-
755-
#[test]
756-
fn addrv2_to_socketaddr_ipv4() {
757-
let addr = AddrV2::Ipv4(Ipv4Addr::new(192, 168, 1, 1));
758-
let socket = SocketAddr::try_from(addr).unwrap();
759-
760-
assert_eq!(socket, SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 1, 1), 0)));
761-
}
762-
763-
#[test]
764-
fn addrv2_to_socketaddr_ipv6() {
765-
let addr = AddrV2::Ipv6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
766-
let socket = SocketAddr::try_from(addr).unwrap();
767-
768-
assert_eq!(
769-
socket,
770-
SocketAddr::V6(SocketAddrV6::new(
771-
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
772-
0,
773-
0,
774-
0
775-
))
776-
);
777-
}
778-
779-
#[test]
780-
fn addrv2_to_socketaddr_cjdns() {
781-
let addr = AddrV2::Cjdns(Ipv6Addr::new(0xfc00, 0, 0, 0, 0, 0, 0, 1));
782-
let result = SocketAddr::try_from(addr);
783-
784-
assert!(result.is_err());
785-
assert_eq!(result.unwrap_err(), AddrV2ToSocketAddrError::CjdnsNotRecommended);
786-
}
787-
788-
#[test]
789-
fn addrv2_to_socketaddr_torv3() {
790-
let addr = AddrV2::TorV3([0; 32]);
791-
let result = SocketAddr::try_from(addr);
792-
793-
assert!(result.is_err());
794-
assert_eq!(result.unwrap_err(), AddrV2ToSocketAddrError::TorV3NotSupported);
795-
}
796-
797-
#[test]
798-
fn addrv2_to_socketaddr_i2p() {
799-
let addr = AddrV2::I2p([0; 32]);
800-
let result = SocketAddr::try_from(addr);
801-
802-
assert!(result.is_err());
803-
assert_eq!(result.unwrap_err(), AddrV2ToSocketAddrError::I2pNotSupported);
804-
}
805-
806-
#[test]
807-
fn addrv2_to_socketaddr_unknown() {
808-
let addr = AddrV2::Unknown(42, vec![1, 2, 3, 4]);
809-
let result = SocketAddr::try_from(addr);
810-
811-
assert!(result.is_err());
812-
assert_eq!(result.unwrap_err(), AddrV2ToSocketAddrError::UnknownNotSupported);
813-
}
814-
815684
#[test]
816685
fn addrv2_to_ipaddr_ipv4() {
817686
let addr = AddrV2::Ipv4(Ipv4Addr::new(192, 168, 1, 1));

fuzz/fuzz_targets/bitcoin/p2p_address_roundtrip.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::convert::TryFrom;
2-
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
2+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
33

44
use bitcoin::consensus::Decodable;
55
use bitcoin::p2p::address::AddrV2;
@@ -31,14 +31,6 @@ fn do_test(data: &[u8]) {
3131
let round_trip: AddrV2 = AddrV2::from(ip_addr);
3232
assert_eq!(addr_v2, round_trip, "AddrV2 -> Ipv6Addr -> AddrV2 should round-trip correctly");
3333
}
34-
35-
if let Ok(socket_addr) = SocketAddr::try_from(addr_v2.clone()) {
36-
let round_trip: AddrV2 = AddrV2::from(socket_addr);
37-
assert_eq!(
38-
addr_v2, round_trip,
39-
"AddrV2 -> SocketAddr -> AddrV2 should round-trip correctly"
40-
);
41-
}
4234
}
4335

4436
fn main() {

0 commit comments

Comments
 (0)