1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
use super::Address;
use crate::{
any,
concrete::{Ipv4, Ipv6},
traits::{primitive::IntoIpv6Segments as _, Address as _, Afi},
};
// TODO: make methods `const fn`
impl Address<Ipv6> {
/// Returns [`true`] if the address is unicast link local.
///
/// This method is provided for compatibility with [`std::net::Ipv6Addr`],
/// and is just a wrapper around
/// [`Address::is_link_local()`][crate::traits::Address::is_link_local()].
#[must_use]
pub fn is_unicast_link_local(&self) -> bool {
self.is_link_local()
}
/// Returns the [`Ipv6MulticastScope`][MulticastScope] variant of the
/// address if the address is a multicast address, or [`None`]
/// otherwise.
///
/// # Examples
///
/// ``` rust
/// use ip::{concrete::Ipv6MulticastScope, Address, Ipv6};
///
/// let ipv6_site_local_multicast = "ff05::1".parse::<Address<Ipv6>>()?;
/// assert_eq!(
/// ipv6_site_local_multicast.multicast_scope(),
/// Some(Ipv6MulticastScope::SiteLocal),
/// );
///
/// let ipv6_global_multicast = "ff0e::1".parse::<Address<Ipv6>>()?;
/// assert_eq!(
/// ipv6_global_multicast.multicast_scope(),
/// Some(Ipv6MulticastScope::Global),
/// );
///
/// let ipv6_unicast = "2001:db8::1".parse::<Address<Ipv6>>()?;
/// assert_eq!(ipv6_unicast.multicast_scope(), None,);
/// # Ok::<(), ip::Error>(())
/// ```
#[allow(clippy::match_same_arms)]
#[must_use]
pub fn multicast_scope(&self) -> Option<MulticastScope> {
if self.is_multicast() {
match self.octets()[1] & 0x0f {
0x0 => Some(MulticastScope::Reserved),
0x1 => Some(MulticastScope::InterfaceLocal),
0x2 => Some(MulticastScope::LinkLocal),
0x3 => Some(MulticastScope::RealmLocal),
0x4 => Some(MulticastScope::AdminLocal),
0x5 => Some(MulticastScope::SiteLocal),
0x6..=0x7 => Some(MulticastScope::Unassigned),
0x8 => Some(MulticastScope::OrganizationLocal),
0x9..=0xd => Some(MulticastScope::Unassigned),
0xe => Some(MulticastScope::Global),
0xf => Some(MulticastScope::Reserved),
_ => unreachable!(),
}
} else {
None
}
}
/// Returns a big-endian [`[u16; 8]`] representing the segments of the
/// address.
///
/// # Examples
///
/// ``` rust
/// use ip::{Address, Ipv6};
///
/// assert_eq!(
/// "2001:db8:f00::1".parse::<Address<Ipv6>>()?.segments(),
/// [0x2001, 0xdb8, 0xf00, 0x0, 0x0, 0x0, 0x0, 0x1],
/// );
/// # Ok::<(), ip::Error>(())
/// ```
#[must_use]
pub fn segments(&self) -> [u16; 8] {
self.into_primitive().into_segments()
}
// TODO: move to `traits::Address`
/// Convert the address to its canonical representation as an
/// [`any::Address`], by converting an IPv4-mapped address to a
/// [`any::Address::Ipv4`], and returning an [`any::Address::Ipv6`]
/// otherwise.
///
/// # Examples
///
/// ``` rust
/// use ip::{Address, Any, Ipv6};
///
/// let ipv4_mapped = "::ffff:192.168.1.1".parse::<Address<Ipv6>>()?;
/// assert_eq!(
/// ipv4_mapped.to_canonical(),
/// "192.168.1.1".parse::<Address<Any>>()?,
/// );
///
/// let ipv6_unicast = "2001:db8::1".parse::<Address<Ipv6>>()?;
/// assert_eq!(
/// ipv6_unicast.to_canonical(),
/// Address::<Any>::Ipv6(ipv6_unicast),
/// );
/// # Ok::<(), ip::Error>(())
/// ```
#[allow(clippy::wrong_self_convention)]
#[allow(clippy::option_if_let_else)]
#[must_use]
pub fn to_canonical(&self) -> any::Address {
if let Some(ipv4_addr) = self.to_ipv4_mapped() {
any::Address::Ipv4(ipv4_addr)
} else {
any::Address::Ipv6(*self)
}
}
/// Returns the embedded [`Address<Ipv4>`] in an IPv4-compatible or
/// IPv4-mapped [`Address<Ipv6>`], or [`None`] otherwise.
///
/// # Examples
///
/// ``` rust
/// use ip::{Address, Ipv6};
///
/// assert_eq!(
/// "::192.168.1.1"
/// .parse::<Address<Ipv6>>()?
/// .to_ipv4()
/// .map(|ipv4| ipv4.octets()),
/// Some([192, 168, 1, 1]),
/// );
///
/// assert_eq!("2001:db8::1".parse::<Address<Ipv6>>()?.to_ipv4(), None,);
/// # Ok::<(), ip::Error>(())
/// ```
#[allow(clippy::wrong_self_convention)]
#[must_use]
pub fn to_ipv4(&self) -> Option<Address<Ipv4>> {
self.to_ipv4_mapped().or_else(|| match self.octets() {
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, octets @ ..] => Some(Address::new(
<Ipv4 as Afi>::Primitive::from_be_bytes(octets),
)),
_ => None,
})
}
/// Returns the embedded [`Address<Ipv4>`] in an IPv4-mapped
/// [`Address<Ipv6>`], or [`None`] otherwise.
///
/// # Examples
///
/// ``` rust
/// use ip::{Address, Ipv6};
///
/// assert_eq!(
/// "::ffff:172.16.1.1"
/// .parse::<Address<Ipv6>>()?
/// .to_ipv4_mapped()
/// .map(|ipv4| ipv4.octets()),
/// Some([172, 16, 1, 1]),
/// );
///
/// assert_eq!(
/// "::192.168.1.1".parse::<Address<Ipv6>>()?.to_ipv4_mapped(),
/// None,
/// );
///
/// assert_eq!(
/// "2001:db8::1".parse::<Address<Ipv6>>()?.to_ipv4_mapped(),
/// None,
/// );
/// # Ok::<(), ip::Error>(())
/// ```
#[allow(clippy::wrong_self_convention)]
#[must_use]
pub fn to_ipv4_mapped(&self) -> Option<Address<Ipv4>> {
match self.octets() {
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, octets @ ..] => Some(Address::new(
<Ipv4 as Afi>::Primitive::from_be_bytes(octets),
)),
_ => None,
}
}
}
// TODO: document omission of `non_exhaustive`
/// IPv6 multicast address scopes, as defined in [RFC 4291].
///
/// See also [`Address::multicast_scope()`].
///
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum MulticastScope {
/// Reserved.
Reserved,
/// Scope values not yet assigned to a multicast scope.
Unassigned,
/// Interface local scope.
InterfaceLocal,
/// Link local scope.
LinkLocal,
/// Realm local scope.
RealmLocal,
/// Locally administered scope.
AdminLocal,
/// Site local scope.
SiteLocal,
/// Organization local scope.
OrganizationLocal,
/// Global scope.
Global,
}