Skip to content

Commit

Permalink
ipv6: add is_unique_local to IPv6 Address
Browse files Browse the repository at this point in the history
Unique Local Addresses (ULA) have a FC00::/7 prefix.
  • Loading branch information
thvdveld committed Nov 30, 2023
1 parent 2535160 commit 72ad79f
Showing 1 changed file with 23 additions and 4 deletions.
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

0 comments on commit 72ad79f

Please sign in to comment.