use crate::Archived;
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ArchivedIpv4Addr {
octets: [Archived<u8>; 4],
}
impl ArchivedIpv4Addr {
#[inline]
pub const fn octets(&self) -> [u8; 4] {
self.octets
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ArchivedIpv6Addr {
octets: [Archived<u8>; 16],
}
impl ArchivedIpv6Addr {
#[inline]
pub const fn segments(&self) -> [u16; 8] {
[
u16::from_be_bytes([self.octets[0], self.octets[1]]),
u16::from_be_bytes([self.octets[2], self.octets[3]]),
u16::from_be_bytes([self.octets[4], self.octets[5]]),
u16::from_be_bytes([self.octets[6], self.octets[7]]),
u16::from_be_bytes([self.octets[8], self.octets[9]]),
u16::from_be_bytes([self.octets[10], self.octets[11]]),
u16::from_be_bytes([self.octets[12], self.octets[13]]),
u16::from_be_bytes([self.octets[14], self.octets[15]]),
]
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum ArchivedIpAddr {
V4(ArchivedIpv4Addr),
V6(ArchivedIpv6Addr),
}
impl ArchivedIpAddr {
#[inline]
pub const fn is_ipv4(&self) -> bool {
matches!(self, ArchivedIpAddr::V4(_))
}
#[inline]
pub const fn is_ipv6(&self) -> bool {
matches!(self, ArchivedIpAddr::V6(_))
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "strict", repr(C))]
pub struct ArchivedSocketAddrV4 {
pub(crate) ip: ArchivedIpv4Addr,
pub(crate) port: Archived<u16>,
}
impl ArchivedSocketAddrV4 {
#[inline]
pub const fn ip(&self) -> &ArchivedIpv4Addr {
&self.ip
}
#[inline]
pub const fn port(&self) -> u16 {
from_archived!(self.port)
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "strict", repr(C))]
pub struct ArchivedSocketAddrV6 {
pub(crate) ip: ArchivedIpv6Addr,
pub(crate) port: Archived<u16>,
pub(crate) flowinfo: Archived<u32>,
pub(crate) scope_id: Archived<u32>,
}
impl ArchivedSocketAddrV6 {
#[inline]
pub const fn flowinfo(&self) -> u32 {
from_archived!(self.flowinfo)
}
#[inline]
pub const fn ip(&self) -> &ArchivedIpv6Addr {
&self.ip
}
#[inline]
pub const fn port(&self) -> u16 {
from_archived!(self.port)
}
#[inline]
pub const fn scope_id(&self) -> u32 {
from_archived!(self.scope_id)
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum ArchivedSocketAddr {
V4(ArchivedSocketAddrV4),
V6(ArchivedSocketAddrV6),
}
impl ArchivedSocketAddr {
#[inline]
pub fn port(&self) -> u16 {
match self {
ArchivedSocketAddr::V4(addr) => addr.port(),
ArchivedSocketAddr::V6(addr) => addr.port(),
}
}
#[inline]
pub fn is_ipv4(&self) -> bool {
matches!(self, ArchivedSocketAddr::V4(_))
}
#[inline]
pub fn is_ipv6(&self) -> bool {
matches!(self, ArchivedSocketAddr::V6(_))
}
}