Struct net_types::ip::Ipv4Addr

source ·
pub struct Ipv4Addr(/* private fields */);
Expand description

An IPv4 address.

§Layout

Ipv4Addr has the same layout as [u8; 4], which is the layout that most protocols use to represent an IPv4 address in their packet formats. This can be useful when parsing an IPv4 address from a packet. For example:

/// An ICMPv4 Redirect Message header.
///
/// `Icmpv4RedirectHeader` has the same layout as the header of an ICMPv4
/// Redirect Message.
#[repr(C)]
struct Icmpv4RedirectHeader {
    typ: u8,
    code: u8,
    checksum: [u8; 2],
    gateway: Ipv4Addr,
}

Implementations§

source§

impl Ipv4Addr

source

pub const fn new(bytes: [u8; 4]) -> Self

Creates a new IPv4 address.

source

pub const fn ipv4_bytes(self) -> [u8; 4]

Gets the bytes of the IPv4 address.

source

pub fn is_limited_broadcast(self) -> bool

Is this the limited broadcast address?

is_limited_broadcast is a shorthand for comparing against Ipv4::LIMITED_BROADCAST_ADDRESS.

source

pub fn is_class_e(self) -> bool

Is this a Class E address?

is_class_e is a shorthand for checking membership in Ipv4::CLASS_E_SUBNET.

source

pub fn to_ipv6_compatible(self) -> Ipv6Addr

Converts the address to an IPv4-compatible IPv6 address according to RFC 4291 Section 2.5.5.1.

IPv4-compatible IPv6 addresses were defined to assist in the IPv6 transition, and are now specified in RFC 4291 Section 2.5.5.1. The lowest-order 32 bits carry an IPv4 address, while the highest-order 96 bits are all set to 0 as in this diagram from the RFC:

|                80 bits               | 16 |      32 bits        |
+--------------------------------------+--------------------------+
|0000..............................0000|0000|    IPv4 address     |
+--------------------------------------+----+---------------------+

Per RFC 4291:

The ‘IPv4-Compatible IPv6 address’ is now deprecated because the current IPv6 transition mechanisms no longer use these addresses. New or updated implementations are not required to support this address type.

The more modern embedding format is IPv4-mapped IPv6 addressing - see to_ipv6_mapped.

source

pub fn to_ipv6_mapped(self) -> SpecifiedAddr<Ipv6Addr>

Converts the address to an IPv4-mapped IPv6 address according to RFC 4291 Section 2.5.5.2.

IPv4-mapped IPv6 addresses are used to represent the addresses of IPv4 nodes as IPv6 addresses, and are defined in RFC 4291 Section 2.5.5.2. The lowest-order 32 bits carry an IPv4 address, the middle order 16 bits carry the literal 0xFFFF, and the highest order 80 bits are set to 0 as in this diagram from the RFC:

|                80 bits               | 16 |      32 bits        |
+--------------------------------------+--------------------------+
|0000..............................0000|FFFF|    IPv4 address     |
+--------------------------------------+----+---------------------+
source

pub fn class(&self) -> Ipv4AddressClass

Returns the address’s class according to the obsoleted classful addressing architecture.

Trait Implementations§

source§

impl Clone for Ipv4Addr

source§

fn clone(&self) -> Ipv4Addr

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Ipv4Addr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Ipv4Addr

source§

fn default() -> Ipv4Addr

Returns the “default value” for a type. Read more
source§

impl Display for Ipv4Addr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl From<[u8; 4]> for Ipv4Addr

source§

fn from(bytes: [u8; 4]) -> Ipv4Addr

Converts to this type from the input type.
source§

impl From<Ipv4Addr> for Ipv4Addr

source§

fn from(ip: Ipv4Addr) -> Ipv4Addr

Converts to this type from the input type.
source§

impl From<Ipv4Addr> for Ipv4Addr

source§

fn from(ip: Ipv4Addr) -> Ipv4Addr

Converts to this type from the input type.
source§

impl From<Ipv4Addr> for Ipv6Addr

Ipv4Addr is convertible into Ipv6Addr through Ipv4Addr::to_ipv6_mapped.

source§

fn from(addr: Ipv4Addr) -> Ipv6Addr

Converts to this type from the input type.
source§

impl<A: Into<Ipv4Addr> + LinkLocalAddress + Copy> From<LinkLocalAddr<A>> for Ipv4Addr

source§

fn from(addr: LinkLocalAddr<A>) -> Ipv4Addr

Converts to this type from the input type.
source§

impl<A: Into<Ipv4Addr> + MulticastAddress + Copy> From<MulticastAddr<A>> for Ipv4Addr

source§

fn from(addr: MulticastAddr<A>) -> Ipv4Addr

Converts to this type from the input type.
source§

impl<A: Into<Ipv4Addr> + MappedAddress + Copy> From<NonMappedAddr<A>> for Ipv4Addr

source§

fn from(addr: NonMappedAddr<A>) -> Ipv4Addr

Converts to this type from the input type.
source§

impl<A: Into<Ipv4Addr> + SpecifiedAddress + Copy> From<SpecifiedAddr<A>> for Ipv4Addr

source§

fn from(addr: SpecifiedAddr<A>) -> Ipv4Addr

Converts to this type from the input type.
source§

impl FromBytes for Ipv4Addr
where [u8; 4]: FromBytes,

§

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
§

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
§

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
§

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
§

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
§

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
§

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
§

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
§

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
source§

impl FromZeros for Ipv4Addr
where [u8; 4]: FromZeros,

§

fn zero(&mut self)

Overwrites self with zeros. Read more
§

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
source§

impl<I: Ip> GenericOverIp<I> for Ipv4Addr

§

type Type = <I as Ip>::Addr

The type of Self when its IP-generic parameter is replaced with the type NewIp.
source§

impl Hash for Ipv4Addr

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl IntoBytes for Ipv4Addr
where [u8; 4]: IntoBytes,

§

fn as_bytes(&self) -> &[u8]
where Self: Immutable,

Gets the bytes of this value. Read more
§

fn as_mut_bytes(&mut self) -> &mut [u8]
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
§

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
§

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
§

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
source§

impl IpAddress for Ipv4Addr

source§

const BYTES: u8 = 4u8

The number of bytes in an address of this type. Read more
§

type Version = Ipv4

The IP version type of this address. Read more
source§

fn mask(&self, bits: u8) -> Self

Masks off the top bits of the address. Read more
source§

fn bytes(&self) -> &[u8]

Gets the underlying bytes of the address.
source§

fn to_ip_addr(&self) -> IpAddr

Converts a statically-typed IP address into a dynamically-typed one.
source§

fn common_prefix_len(&self, other: &Ipv4Addr) -> u8

Calculates the common prefix length between this address and other.
source§

fn is_unicast_in_subnet(&self, subnet: &Subnet<Self>) -> bool

Is this a unicast address contained in the given subnet? Read more
source§

fn is_loopback(&self) -> bool

Is this a loopback address? Read more
source§

impl KnownLayout for Ipv4Addr
where Self: Sized,

§

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
source§

impl LinkLocalAddress for Ipv4Addr

Is this address in the link-local subnet?

is_link_local returns true if self is in Ipv4::LINK_LOCAL_UNICAST_SUBNET or Ipv4::LINK_LOCAL_MULTICAST_SUBNET.

source§

impl Ord for Ipv4Addr

source§

fn cmp(&self, other: &Ipv4Addr) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Ipv4Addr

source§

fn eq(&self, other: &Ipv4Addr) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Ipv4Addr

source§

fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl ScopeableAddress for Ipv4Addr

source§

fn scope(&self)

The scope of this address.

Although IPv4 defines a link local subnet, IPv4 addresses are always considered to be in the global scope.

§

type Scope = ()

The type of all non-global scopes.
source§

impl TryFrom<Ipv4Addr> for LinkLocalAddr<Ipv4Addr>

§

type Error = ()

The type returned in the event of a conversion error.
source§

fn try_from(addr: Ipv4Addr) -> Result<LinkLocalAddr<Ipv4Addr>, ()>

Performs the conversion.
source§

impl TryFrom<Ipv4Addr> for MulticastAddr<Ipv4Addr>

§

type Error = ()

The type returned in the event of a conversion error.
source§

fn try_from(addr: Ipv4Addr) -> Result<MulticastAddr<Ipv4Addr>, ()>

Performs the conversion.
source§

impl TryFrom<Ipv4Addr> for NonMappedAddr<Ipv4Addr>

§

type Error = ()

The type returned in the event of a conversion error.
source§

fn try_from(addr: Ipv4Addr) -> Result<NonMappedAddr<Ipv4Addr>, ()>

Performs the conversion.
source§

impl TryFrom<Ipv4Addr> for SpecifiedAddr<Ipv4Addr>

§

type Error = ()

The type returned in the event of a conversion error.
source§

fn try_from(addr: Ipv4Addr) -> Result<SpecifiedAddr<Ipv4Addr>, ()>

Performs the conversion.
source§

impl TryFromBytes for Ipv4Addr
where [u8; 4]: TryFromBytes,

§

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
§

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
§

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
§

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout,

Attempts to interpret the given source as a &mut Self without copying. Read more
§

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
§

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
§

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
§

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
§

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
source§

impl Copy for Ipv4Addr

source§

impl Eq for Ipv4Addr

source§

impl Immutable for Ipv4Addr
where [u8; 4]: Immutable,

source§

impl StructuralPartialEq for Ipv4Addr

source§

impl Unaligned for Ipv4Addr
where [u8; 4]: Unaligned,

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<A> MappedAddress for A
where A: IpAddress,

source§

fn is_non_mapped(&self) -> bool

Is this address non-mapped?

For IPv4 addresses, this always returns true because they do not have a mapped address space.

For Ipv6 addresses, this returns true if self is outside of the IPv4 mapped Ipv6 address subnet, as defined in RFC 4291 Section 2.5.5.2 (e.g. ::FFFF:0:0/96).

source§

impl<A> MulticastAddress for A
where A: IpAddress,

source§

fn is_multicast(&self) -> bool

Is this address in the multicast subnet?

is_multicast returns true if self is in A::Version::MULTICAST_SUBNET.

source§

fn is_non_multicast(&self) -> bool

Is this a non-multicast address? The inverse of is_multicast().
source§

impl<A> SpecifiedAddress for A
where A: IpAddress,

source§

fn is_specified(&self) -> bool

Is this an address other than the unspecified address?

is_specified returns true if self is not equal to A::Version::UNSPECIFIED_ADDRESS.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<A> LinkLocalMulticastAddress for A