Skip to content

Commit

Permalink
Merge pull request #37 from nvandamme/master
Browse files Browse the repository at this point in the history
add prefixlen to ifaddr
  • Loading branch information
messense committed Apr 7, 2024
2 parents d60d72f + 193761f commit bfbdb05
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/lib.rs
Expand Up @@ -92,6 +92,8 @@ pub struct Ifv4Addr {
pub ip: Ipv4Addr,
/// The netmask of the interface.
pub netmask: Ipv4Addr,
/// The CIDR prefix of the interface.
pub prefixlen: u8,
/// The broadcast address of the interface.
pub broadcast: Option<Ipv4Addr>,
}
Expand All @@ -115,6 +117,8 @@ pub struct Ifv6Addr {
pub ip: Ipv6Addr,
/// The netmask of the interface.
pub netmask: Ipv6Addr,
/// The CIDR prefix of the interface.
pub prefixlen: u8,
/// The broadcast address of the interface.
pub broadcast: Option<Ipv6Addr>,
}
Expand Down Expand Up @@ -166,10 +170,15 @@ mod getifaddrs_posix {
} else {
None
};

let prefixlen = if cfg!(target_endian = "little") {
u32::from_le_bytes(netmask.octets()).count_ones() as u8
} else {
u32::from_be_bytes(netmask.octets()).count_ones() as u8
};
IfAddr::V4(Ifv4Addr {
ip: ipv4_addr,
netmask,
prefixlen: prefixlen,

Check warning on line 181 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant field names in struct initialization

warning: redundant field names in struct initialization --> src/lib.rs:181:25 | 181 | prefixlen: prefixlen, | ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `prefixlen` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names = note: `#[warn(clippy::redundant_field_names)]` on by default
broadcast,
})
}
Expand All @@ -186,10 +195,15 @@ mod getifaddrs_posix {
} else {
None
};

let prefixlen = if cfg!(target_endian = "little") {
u128::from_le_bytes(netmask.octets()).count_ones() as u8
} else {
u128::from_be_bytes(netmask.octets()).count_ones() as u8
};
IfAddr::V6(Ifv6Addr {
ip: ipv6_addr,
netmask,
prefixlen: prefixlen,

Check warning on line 206 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant field names in struct initialization

warning: redundant field names in struct initialization --> src/lib.rs:206:25 | 206 | prefixlen: prefixlen, | ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `prefixlen` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
broadcast,
})
}
Expand Down Expand Up @@ -247,6 +261,7 @@ mod getifaddrs_windows {
Some(IpAddr::V4(ipv4_addr)) => {
let mut item_netmask = Ipv4Addr::new(0, 0, 0, 0);
let mut item_broadcast = None;
let item_prefix = addr.OnLinkPrefixLength;

// Search prefixes for a prefix matching addr
'prefixloopv4: for prefix in ifaddr.prefixes() {
Expand Down Expand Up @@ -294,11 +309,13 @@ mod getifaddrs_windows {
IfAddr::V4(Ifv4Addr {
ip: ipv4_addr,
netmask: item_netmask,
prefixlen: item_prefix,
broadcast: item_broadcast,
})
}
Some(IpAddr::V6(ipv6_addr)) => {
let mut item_netmask = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
let item_prefix = addr.OnLinkPrefixLength;
// Search prefixes for a prefix matching addr
'prefixloopv6: for prefix in ifaddr.prefixes() {
let ipprefix = sockaddr::to_ipaddr(prefix.Address.lpSockaddr);
Expand Down Expand Up @@ -338,6 +355,7 @@ mod getifaddrs_windows {
IfAddr::V6(Ifv6Addr {
ip: ipv6_addr,
netmask: item_netmask,
prefixlen: item_prefix,
broadcast: None,
})
}
Expand Down

0 comments on commit bfbdb05

Please sign in to comment.