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

wire: use core::net types for IP addresses. #994

Merged
merged 6 commits into from
Oct 7, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wire: move prefix_len impl down to v4 and v6 addrs.
  • Loading branch information
Dirbaio committed Oct 6, 2024
commit 23be2beab39d436c2443642c47d3e66604bd0da9
25 changes: 5 additions & 20 deletions src/wire/ip.rs
Original file line number Diff line number Diff line change
@@ -180,27 +180,12 @@ impl Address {
/// If `self` is a CIDR-compatible subnet mask, return `Some(prefix_len)`,
/// where `prefix_len` is the number of leading zeroes. Return `None` otherwise.
pub fn prefix_len(&self) -> Option<u8> {
let mut ones = true;
let mut prefix_len = 0;
for byte in self.as_bytes() {
let mut mask = 0x80;
for _ in 0..8 {
let one = *byte & mask != 0;
if ones {
// Expect 1s until first 0
if one {
prefix_len += 1;
} else {
ones = false;
}
} else if one {
// 1 where 0 was expected
return None;
}
mask >>= 1;
}
match self {
#[cfg(feature = "proto-ipv4")]
Address::Ipv4(addr) => addr.prefix_len(),
#[cfg(feature = "proto-ipv6")]
Address::Ipv6(addr) => addr.prefix_len(),
}
Some(prefix_len)
}
}

26 changes: 26 additions & 0 deletions src/wire/ipv4.rs
Original file line number Diff line number Diff line change
@@ -108,6 +108,32 @@ impl Address {
pub const fn into_address(self) -> super::IpAddress {
super::IpAddress::Ipv4(self)
}

/// If `self` is a CIDR-compatible subnet mask, return `Some(prefix_len)`,
/// where `prefix_len` is the number of leading zeroes. Return `None` otherwise.
pub fn prefix_len(&self) -> Option<u8> {
let mut ones = true;
let mut prefix_len = 0;
for byte in self.as_bytes() {
let mut mask = 0x80;
for _ in 0..8 {
let one = *byte & mask != 0;
if ones {
// Expect 1s until first 0
if one {
prefix_len += 1;
} else {
ones = false;
}
} else if one {
// 1 where 0 was expected
return None;
}
mask >>= 1;
}
}
Some(prefix_len)
}
}

impl From<::core::net::Ipv4Addr> for Address {
26 changes: 26 additions & 0 deletions src/wire/ipv6.rs
Original file line number Diff line number Diff line change
@@ -310,6 +310,32 @@ impl Address {
pub const fn into_address(self) -> super::IpAddress {
super::IpAddress::Ipv6(self)
}

/// If `self` is a CIDR-compatible subnet mask, return `Some(prefix_len)`,
/// where `prefix_len` is the number of leading zeroes. Return `None` otherwise.
pub fn prefix_len(&self) -> Option<u8> {
let mut ones = true;
let mut prefix_len = 0;
for byte in self.as_bytes() {
let mut mask = 0x80;
for _ in 0..8 {
let one = *byte & mask != 0;
if ones {
// Expect 1s until first 0
if one {
prefix_len += 1;
} else {
ones = false;
}
} else if one {
// 1 where 0 was expected
return None;
}
mask >>= 1;
}
}
Some(prefix_len)
}
}

impl From<::core::net::Ipv6Addr> for Address {