Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipv6: add is_unique_local to IPv6 Address #862

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/wire/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ impl Address {
self.0[0..8] == [0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
}

/// Query whether the IPv6 address is a [Unique Local Address] (ULA).
///
/// [Unique Local Address]: https://tools.ietf.org/html/rfc4193
pub fn is_unique_local(&self) -> bool {
(self.0[0] & 0b1111_1110) == 0xfc
}

/// Query whether the IPv6 address is the [loopback address].
///
/// [loopback address]: https://tools.ietf.org/html/rfc4291#section-2.5.3
Expand Down Expand Up @@ -831,20 +838,21 @@ mod test {
#[cfg(feature = "proto-ipv4")]
use crate::wire::ipv4::Address as Ipv4Address;

static LINK_LOCAL_ADDR: Address = Address([
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01,
]);
const LINK_LOCAL_ADDR: Address = Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
const UNIQUE_LOCAL_ADDR: Address = Address::new(0xfd00, 0, 0, 201, 1, 1, 1, 1);

#[test]
fn test_basic_multicast() {
assert!(!Address::LINK_LOCAL_ALL_ROUTERS.is_unspecified());
assert!(Address::LINK_LOCAL_ALL_ROUTERS.is_multicast());
assert!(!Address::LINK_LOCAL_ALL_ROUTERS.is_link_local());
assert!(!Address::LINK_LOCAL_ALL_ROUTERS.is_loopback());
assert!(!Address::LINK_LOCAL_ALL_ROUTERS.is_unique_local());
assert!(!Address::LINK_LOCAL_ALL_NODES.is_unspecified());
assert!(Address::LINK_LOCAL_ALL_NODES.is_multicast());
assert!(!Address::LINK_LOCAL_ALL_NODES.is_link_local());
assert!(!Address::LINK_LOCAL_ALL_NODES.is_loopback());
assert!(!Address::LINK_LOCAL_ALL_NODES.is_unique_local());
}

#[test]
Expand All @@ -853,6 +861,7 @@ mod test {
assert!(!LINK_LOCAL_ADDR.is_multicast());
assert!(LINK_LOCAL_ADDR.is_link_local());
assert!(!LINK_LOCAL_ADDR.is_loopback());
assert!(!LINK_LOCAL_ADDR.is_unique_local());
}

#[test]
Expand All @@ -861,6 +870,16 @@ mod test {
assert!(!Address::LOOPBACK.is_multicast());
assert!(!Address::LOOPBACK.is_link_local());
assert!(Address::LOOPBACK.is_loopback());
assert!(!Address::LOOPBACK.is_unique_local());
}

#[test]
fn test_unique_local() {
assert!(!UNIQUE_LOCAL_ADDR.is_unspecified());
assert!(!UNIQUE_LOCAL_ADDR.is_multicast());
assert!(!UNIQUE_LOCAL_ADDR.is_link_local());
assert!(!UNIQUE_LOCAL_ADDR.is_loopback());
assert!(UNIQUE_LOCAL_ADDR.is_unique_local());
}

#[test]
Expand Down