Skip to content

Commit

Permalink
feat(ipv4): add is_same_subnet to Ipv4Addr
Browse files Browse the repository at this point in the history
Signed-off-by: Anhad Singh <[email protected]>
  • Loading branch information
Andy-Python-Programmer committed Oct 4, 2023
1 parent 1a3f153 commit f3f8113
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,44 @@ impl Ipv4Addr {
pub fn new(addr: [u8; Self::ADDR_SIZE]) -> Self {
Self(addr)
}

/// Returns `true` if `self` and `other` belong to the same subnet.
///
/// ## Example
/// ```rust
/// use crabnet::network::Ipv4Addr;
///
/// let subnet_mask = Ipv4Addr::new([255, 255, 255, 0]);
///
/// let x = Ipv4Addr::new([192, 168, 1, 1]);
/// let y = Ipv4Addr::new([192, 168, 1, 2]);
/// let z = Ipv4Addr::new([192, 168, 2, 1]);
///
/// assert_eq!(x.is_same_subnet(y, subnet_mask), true);
/// assert_eq!(x.is_same_subnet(z, subnet_mask), false);
/// assert_eq!(y.is_same_subnet(z, subnet_mask), false);
/// ```
pub fn is_same_subnet(&self, other: Ipv4Addr, subnet_mask: Ipv4Addr) -> bool {
let subnet = u32::from_be_bytes(subnet_mask.octets());

let x = u32::from_be_bytes(other.octets());
let y = u32::from_be_bytes(self.octets());

(x & subnet) == (y & subnet)
}

/// Returns the four eight-bit integers that make up this address.
///
/// ## Examples
/// ```rust
/// use crabnet::network::Ipv4Addr;
///
/// let addr = Ipv4Addr::new([192, 168, 1, 1]);
/// assert_eq!(addr.octets(), [192, 168, 1, 1]);
#[inline]
pub fn octets(&self) -> [u8; Self::ADDR_SIZE] {
self.0
}
}

impl From<[u8; Ipv4Addr::ADDR_SIZE]> for Ipv4Addr {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/udp_no_parent.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ error[E0599]: the method `into_boxed_bytes` exists for struct `Udp`, but its tra
9 | let p = x.into_boxed_bytes();
| ^^^^^^^^^^^^^^^^ method cannot be called on `Udp` due to unsatisfied trait bounds
|
::: src/transport/mod.rs
::: src/transport/udp.rs
|
| pub struct Udp {
| --------------
Expand Down

0 comments on commit f3f8113

Please sign in to comment.