From 4c70397a85f2a7760d0c719de2d20c20fbd90048 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Sun, 8 Nov 2020 13:48:42 +0000 Subject: [PATCH] network: Add socket_addr method to AddrV2Message --- src/network/address.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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;