veilid/veilid-tools/src/ip_addr_port.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2021-11-22 16:28:30 +00:00
use super::*;
2022-11-27 02:37:23 +00:00
2021-11-22 16:28:30 +00:00
use core::fmt;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub struct IpAddrPort {
addr: IpAddr,
port: u16,
}
impl IpAddrPort {
pub fn new(addr: IpAddr, port: u16) -> Self {
2021-11-27 17:44:21 +00:00
Self { addr, port }
2021-11-22 16:28:30 +00:00
}
pub fn addr(&self) -> &IpAddr {
&self.addr
}
pub fn port(&self) -> u16 {
self.port
}
pub fn set_addr(&mut self, new_addr: IpAddr) {
self.addr = new_addr;
}
pub fn set_port(&mut self, new_port: u16) {
self.port = new_port;
}
pub fn from_socket_addr(sa: &SocketAddr) -> Self {
match sa {
SocketAddr::V4(v) => Self {
addr: IpAddr::V4(*v.ip()),
port: v.port(),
},
SocketAddr::V6(v) => Self {
addr: IpAddr::V6(*v.ip()),
port: v.port(),
},
}
}
pub fn to_socket_addr(&self) -> SocketAddr {
match self.addr {
IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, self.port)),
IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, self.port, 0, 0)),
}
}
}
impl fmt::Display for IpAddrPort {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.addr {
IpAddr::V4(a) => write!(f, "{}:{}", a, self.port()),
IpAddr::V6(a) => write!(f, "[{}]:{}", a, self.port()),
}
}
}
impl From<SocketAddrV4> for IpAddrPort {
fn from(sock4: SocketAddrV4) -> IpAddrPort {
Self::from_socket_addr(&SocketAddr::V4(sock4))
}
}
impl From<SocketAddrV6> for IpAddrPort {
fn from(sock6: SocketAddrV6) -> IpAddrPort {
Self::from_socket_addr(&SocketAddr::V6(sock6))
}
}