Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
larseggert committed Jan 14, 2025
1 parent 2d7fc27 commit f7755cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ unit_bindings = "warn"
unused_import_braces = "warn"
unused_lifetimes = "warn"
unused_macro_rules = "warn"
# unused_qualifications = "warn" // Try to re-enable when MSRV is > 1.76
# unused_qualifications = "warn" # Try to re-enable when MSRV is > 1.76

[lints.clippy]
cargo = { level = "warn", priority = -1 }
Expand Down
36 changes: 18 additions & 18 deletions src/bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
};

#[cfg(apple)]
const ALIGN: usize = std::mem::size_of::<libc::c_int>();
const ALIGN: usize = size_of::<libc::c_int>();

#[cfg(bsd)]
// See https://github.com/freebsd/freebsd-src/blob/524a425d30fce3d5e47614db796046830b1f6a83/sys/net/route.h#L362-L371
Expand All @@ -65,12 +65,12 @@ type AddressFamily = u16;
asserted_const_with_type!(AF_INET, AddressFamily, libc::AF_INET, i32);
asserted_const_with_type!(AF_INET6, AddressFamily, libc::AF_INET6, i32);
asserted_const_with_type!(AF_LINK, AddressFamily, libc::AF_LINK, i32);
asserted_const_with_type!(RTM_VERSION, u8, crate::bsd::bindings::RTM_VERSION, u32);
asserted_const_with_type!(RTM_GET, u8, crate::bsd::bindings::RTM_GET, u32);
asserted_const_with_type!(RTM_VERSION, u8, bindings::RTM_VERSION, u32);
asserted_const_with_type!(RTM_GET, u8, bindings::RTM_GET, u32);

const_assert!(std::mem::size_of::<sockaddr_in>() + ALIGN <= u8::MAX as usize);
const_assert!(std::mem::size_of::<sockaddr_in6>() + ALIGN <= u8::MAX as usize);
const_assert!(std::mem::size_of::<rt_msghdr>() <= u8::MAX as usize);
const_assert!(size_of::<sockaddr_in>() + ALIGN <= u8::MAX as usize);
const_assert!(size_of::<sockaddr_in6>() + ALIGN <= u8::MAX as usize);
const_assert!(size_of::<rt_msghdr>() <= u8::MAX as usize);

struct IfAddrs(*mut ifaddrs);

Expand Down Expand Up @@ -116,7 +116,7 @@ struct IfAddrPtr<'a> {
}

impl IfAddrPtr<'_> {
fn addr(&self) -> libc::sockaddr {
fn addr(&self) -> sockaddr {
unsafe { *self.ifa_addr }
}

Expand Down Expand Up @@ -183,8 +183,8 @@ union SockaddrStorage {

fn sockaddr_len(af: AddressFamily) -> Result<usize> {
let sa_len = match af {
AF_INET => std::mem::size_of::<sockaddr_in>(),
AF_INET6 => std::mem::size_of::<sockaddr_in6>(),
AF_INET => size_of::<sockaddr_in>(),
AF_INET6 => size_of::<sockaddr_in6>(),
_ => {
return Err(Error::new(
ErrorKind::InvalidInput,
Expand All @@ -203,7 +203,7 @@ impl From<IpAddr> for SockaddrStorage {
#[cfg(not(target_os = "solaris"))]
#[allow(clippy::cast_possible_truncation)]
// `sockaddr_in` len is <= u8::MAX per `const_assert!` above.
sin_len: std::mem::size_of::<sockaddr_in>() as u8,
sin_len: size_of::<sockaddr_in>() as u8,
sin_family: AF_INET,
sin_addr: in_addr {
s_addr: u32::from_ne_bytes(ip.octets()),
Expand All @@ -217,7 +217,7 @@ impl From<IpAddr> for SockaddrStorage {
#[cfg(not(target_os = "solaris"))]
#[allow(clippy::cast_possible_truncation)]
// `sockaddr_in6` len is <= u8::MAX per `const_assert!` above.
sin6_len: std::mem::size_of::<sockaddr_in6>() as u8,
sin6_len: size_of::<sockaddr_in6>() as u8,
sin6_family: AF_INET6,
sin6_addr: in6_addr {
s6_addr: ip.octets(),
Expand Down Expand Up @@ -250,7 +250,7 @@ impl RouteMessage {
rtm: rt_msghdr {
#[allow(clippy::cast_possible_truncation)]
// `rt_msghdr` len + `ALIGN` is <= u8::MAX per `const_assert!` above.
rtm_msglen: (std::mem::size_of::<rt_msghdr>() + sa_len) as u16,
rtm_msglen: (size_of::<rt_msghdr>() + sa_len) as u16,
rtm_version: RTM_VERSION,
rtm_type: RTM_GET,
rtm_seq: seq,
Expand All @@ -276,14 +276,14 @@ impl RouteMessage {

impl From<&RouteMessage> for &[u8] {
fn from(value: &RouteMessage) -> Self {
debug_assert!(value.len() >= std::mem::size_of::<Self>());
debug_assert!(value.len() >= size_of::<Self>());
unsafe { slice::from_raw_parts(ptr::from_ref(value).cast(), value.len()) }
}
}

impl From<&[u8]> for rt_msghdr {
fn from(value: &[u8]) -> Self {
debug_assert!(value.len() >= std::mem::size_of::<Self>());
debug_assert!(value.len() >= size_of::<Self>());
unsafe { ptr::read_unaligned(value.as_ptr().cast()) }
}
}
Expand All @@ -304,15 +304,15 @@ fn if_index_mtu(remote: IpAddr) -> Result<(u16, Option<usize>)> {
loop {
let mut buf = vec![
0u8;
std::mem::size_of::<rt_msghdr>() +
size_of::<rt_msghdr>() +
// There will never be `RTAX_MAX` sockaddrs attached, but it's a safe upper bound.
(RTAX_MAX as usize * std::mem::size_of::<sockaddr_storage>())
(RTAX_MAX as usize * size_of::<sockaddr_storage>())
];
let len = fd.read(&mut buf[..])?;
if len < std::mem::size_of::<rt_msghdr>() {
if len < size_of::<rt_msghdr>() {
return Err(default_err());
}
let (reply, mut sa) = buf.split_at(std::mem::size_of::<rt_msghdr>());
let (reply, mut sa) = buf.split_at(size_of::<rt_msghdr>());
let reply: rt_msghdr = reply.into();
if !(reply.rtm_version == query_version
&& reply.rtm_pid == pid
Expand Down
6 changes: 3 additions & 3 deletions src/routesocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ fn check_result(res: isize) -> Result<usize> {
}

impl Write for RouteSocket {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let res = unsafe { write(self.as_raw_fd(), buf.as_ptr().cast(), buf.len()) };
check_result(res)
}

fn flush(&mut self) -> std::io::Result<()> {
fn flush(&mut self) -> Result<()> {
let res = unsafe { fsync(self.as_raw_fd()) };
check_result(res as isize).and(Ok(()))
}
}

impl Read for RouteSocket {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
// If we've written a well-formed message into the kernel via `write`, we should be able to
// read a well-formed message back out, and not block.
let res = unsafe { read(self.as_raw_fd(), buf.as_mut_ptr().cast(), buf.len()) };
Expand Down

0 comments on commit f7755cd

Please sign in to comment.