diff --git a/src/network/address.rs b/src/network/address.rs index e05dd576..c4d58b3c 100644 --- a/src/network/address.rs +++ b/src/network/address.rs @@ -241,6 +241,19 @@ pub struct AddrV2Message { pub port: u16 } +impl AddrV2Message { + /// Extract socket address from an [AddrV2Message] message. + /// This will return [io::Error] [ErrorKind::AddrNotAvailable] + /// if the address type can't be converted into a [SocketAddr]. + pub fn socket_addr(&self) -> Result { + match self.addr { + AddrV2::Ipv4(addr) => Ok(SocketAddr::V4(SocketAddrV4::new(addr, self.port))), + AddrV2::Ipv6(addr) => Ok(SocketAddr::V6(SocketAddrV6::new(addr, self.port, 0, 0))), + _ => return Err(io::Error::from(io::ErrorKind::AddrNotAvailable)), + } + } +} + impl Encodable for AddrV2Message { fn consensus_encode(&self, mut e: W) -> Result { let mut len = 0;