#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[derive(Serialize, Deserialize)]
#[repr(C, packed)]
pub struct InternetProtocolVersion4HostAddress(pub [u8; InternetProtocolVersion4HostAddress::Size]);
impl Display for InternetProtocolVersion4HostAddress
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result
{
write!(f, "{}.{}.{}.{}", self.get_first_byte(), self.get_second_byte(), self.get_third_byte(), self.get_fourth_byte())
}
}
impl Debug for InternetProtocolVersion4HostAddress
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result
{
write!(f, "{}.{}.{}.{}", self.get_first_byte(), self.get_second_byte(), self.get_third_byte(), self.get_fourth_byte())
}
}
impl Default for InternetProtocolVersion4HostAddress
{
#[inline(always)]
fn default() -> Self
{
Self::Unspecified
}
}
impl InternetProtocolHostAddress for InternetProtocolVersion4HostAddress
{
type BigEndianValue = u32;
type RustAddress = Ipv4Addr;
type LibCAddress = in_addr;
type MaskBits = InternetProtocolVersion4MaskBits;
type Octets = [u8; 4];
const Size: usize = 4;
const SizeU8: u8 = 4;
#[inline(always)]
fn from_network_endian(big_endian_value: Self::BigEndianValue) -> Self
{
Self::from_octets(unsafe { transmute(big_endian_value) })
}
#[inline(always)]
fn from_octets(octets: Self::Octets) -> Self
{
InternetProtocolVersion4HostAddress(octets)
}
#[inline(always)]
fn from_rust_address_to_libc_address(rust_address: &Self::RustAddress) -> Self::LibCAddress
{
unsafe { transmute_copy(rust_address) }
}
#[inline(always)]
fn from_rust_address(rust_address: &Self::RustAddress) -> Self
{
unsafe { transmute_copy(rust_address) }
}
#[inline(always)]
fn to_rust_address(&self) -> Self::RustAddress
{
unsafe { transmute_copy(self) }
}
#[inline(always)]
fn to_libc_address(self) -> Self::LibCAddress
{
in_addr
{
s_addr: unsafe { transmute(self.0) },
}
}
#[inline(always)]
fn as_native_endian(&self) -> Self::BigEndianValue
{
u32::from_be(self.as_network_endian())
}
#[inline(always)]
fn as_network_endian(&self) -> Self::BigEndianValue
{
unsafe { transmute(self.0) }
}
#[inline(always)]
fn to_media_access_control_address(&self) -> Result<MediaAccessControlAddress, ()>
{
MediaAccessControlAddress::from_private_internet_protocol_version_4_host_address(self)
}
}
impl InternetProtocolVersion4HostAddress
{
pub const Unspecified: Self = InternetProtocolVersion4HostAddress([0, 0, 0, 0]);
pub const Broadcast: Self = InternetProtocolVersion4HostAddress([255, 255, 255, 255]);
#[inline(always)]
pub fn to_embedded_rfc8215_internet_protocol_version_6_host_address(self) -> InternetProtocolVersion6HostAddress
{
InternetProtocolVersion6HostAddress([0x00, 0x64, 0xFF, 0x9B, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, self.get_first_byte(), self.get_second_byte(), self.get_third_byte(), self.get_fourth_byte()])
}
#[inline(always)]
pub fn to_embedded_rfc6052_internet_protocol_version_6_host_address(self) -> InternetProtocolVersion6HostAddress
{
InternetProtocolVersion6HostAddress([0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, self.get_first_byte(), self.get_second_byte(), self.get_third_byte(), self.get_fourth_byte()])
}
#[inline(always)]
pub fn to_mapped_internet_protocol_version_6_host_address(self) -> InternetProtocolVersion6HostAddress
{
InternetProtocolVersion6HostAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, self.get_first_byte(), self.get_second_byte(), self.get_third_byte(), self.get_fourth_byte()])
}
#[inline(always)]
pub fn to_deprecated_compatible_internet_protocol_version_6_host_address(self) -> InternetProtocolVersion6HostAddress
{
InternetProtocolVersion6HostAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, self.get_first_byte(), self.get_second_byte(), self.get_third_byte(), self.get_fourth_byte()])
}
#[inline(always)]
pub fn is_valid_unicast(self) -> bool
{
!self.is_not_valid_unicast()
}
#[inline(always)]
pub fn is_not_valid_unicast(self) -> bool
{
self.is_unspecified() ||
self.is_loopback() ||
self.is_multicast() ||
self.is_documentation() ||
self.is_broadcast()
}
#[inline(always)]
pub fn is_not_globally_unicast_unique(self) -> bool
{
self.is_not_valid_unicast() || self.is_link_local() || self.is_private()
}
#[inline(always)]
pub fn is_private(self) -> bool
{
InternetProtocolVersion4NetworkAddress::Private1.contains(self) || InternetProtocolVersion4NetworkAddress::Private2.contains(self) || InternetProtocolVersion4NetworkAddress::Private3.contains(self)
}
#[inline(always)]
pub fn is_link_local(self) -> bool
{
InternetProtocolVersion4NetworkAddress::LinkLocal.contains(self)
}
#[inline(always)]
pub fn is_unspecified(self) -> bool
{
self == Self::Unspecified
}
#[inline(always)]
pub fn is_broadcast(self) -> bool
{
self == Self::Broadcast
}
#[inline(always)]
pub fn is_not_broadcast(self) -> bool
{
self != Self::Broadcast
}
#[inline(always)]
pub fn is_loopback(self) -> bool
{
InternetProtocolVersion4NetworkAddress::Loopback.contains(self)
}
#[inline(always)]
pub fn is_multicast(self) -> bool
{
InternetProtocolVersion4NetworkAddress::Multicast.contains(self)
}
#[inline(always)]
pub fn is_not_multicast(self) -> bool
{
!self.is_multicast()
}
#[inline(always)]
pub fn is_documentation(self) -> bool
{
InternetProtocolVersion4NetworkAddress::TestNet1.contains(self) || InternetProtocolVersion4NetworkAddress::TestNet2.contains(self) || InternetProtocolVersion4NetworkAddress::TestNet3.contains(self)
}
#[inline(always)]
pub fn has_lower_23_bits(self, lower_23_bits: &[u8; 3]) -> bool
{
self.get_second_byte() & 0b0111_1111 == lower_23_bits[0] && self.get_third_byte() == lower_23_bits[1] && self.get_fourth_byte() == lower_23_bits[2]
}
#[inline(always)]
pub fn does_not_have_lower_23_bits(self, lower_23_bits: &[u8; 3]) -> bool
{
!self.has_lower_23_bits(lower_23_bits)
}
#[inline(always)]
fn get_first_byte(&self) -> u8
{
unsafe { * self.0.get_unchecked(0) }
}
#[inline(always)]
fn get_second_byte(&self) -> u8
{
unsafe { * self.0.get_unchecked(1) }
}
#[inline(always)]
fn get_third_byte(&self) -> u8
{
unsafe { * self.0.get_unchecked(2) }
}
#[inline(always)]
fn get_fourth_byte(&self) -> u8
{
unsafe { * self.0.get_unchecked(3) }
}
}