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

add prefixlen to ifaddr #37

Merged
merged 2 commits into from
Apr 7, 2024
Merged
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
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
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,
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,
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
Loading