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

Fix is_global special address handling #119006

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,11 @@ impl Ipv4Addr {
|| self.is_loopback()
|| self.is_link_local()
// addresses reserved for future protocols (`192.0.0.0/24`)
||(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0)
// .9 and .10 are documented as globally reachable so they're excluded
|| (
self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0
&& self.octets()[3] != 9 && self.octets()[3] != 10
)
|| self.is_documentation()
|| self.is_benchmarking()
|| self.is_reserved()
Expand Down Expand Up @@ -1515,8 +1519,12 @@ impl Ipv6Addr {
// AS112-v6 (`2001:4:112::/48`)
|| matches!(self.segments(), [0x2001, 4, 0x112, _, _, _, _, _])
// ORCHIDv2 (`2001:20::/28`)
|| matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x2F)
// Drone Remote ID Protocol Entity Tags (DETs) Prefix (`2001:30::/28`)`
|| matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x3F)
))
// 6to4 (`2002::/16`) – it's not explicitly documented as globally reachable,
// IANA says N/A.
|| matches!(self.segments(), [0x2002, _, _, _, _, _, _, _])
|| self.is_documentation()
|| self.is_unique_local()
|| self.is_unicast_link_local())
Expand Down
14 changes: 12 additions & 2 deletions library/core/tests/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ fn ipv4_properties() {
check!("198.18.54.2", benchmarking);
check!("198.19.255.255", benchmarking);
check!("192.0.0.0");
check!("192.0.0.8");
check!("192.0.0.9", global);
check!("192.0.0.10", global);
check!("192.0.0.11");
check!("192.0.0.255");
check!("192.0.0.100");
check!("240.0.0.0", reserved);
Expand All @@ -480,6 +484,10 @@ fn ipv6_properties() {
}

macro_rules! check {
($s:expr, &[$($octet:expr),*]) => {
check!($s, &[$($octet),*], 0);
};

($s:expr, &[$($octet:expr),*], $mask:expr) => {
assert_eq!($s, ip!($s).to_string());
let octets = &[$($octet),*];
Expand Down Expand Up @@ -656,15 +664,17 @@ fn ipv6_properties() {
&[0x20, 1, 0, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
global | unicast_global
);

check!("2001:30::", &[0x20, 1, 0, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);
check!("2001:30::", &[0x20, 1, 0, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global | unicast_global);
check!("2001:40::", &[0x20, 1, 0, 0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);

check!(
"2001:200::",
&[0x20, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
global | unicast_global
);

check!("2002::", &[0x20, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);

check!("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unique_local);

check!(
Expand Down
Loading