network: Add socket_addr method to AddrV2Message

This commit is contained in:
Steven Roose 2020-11-08 13:48:42 +00:00
parent c7ec4f171f
commit 4c70397a85
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
1 changed files with 13 additions and 0 deletions

View File

@ -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<SocketAddr, io::Error> {
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<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
let mut len = 0;