From 5d9c99c70be3974f445b26e47bfc08e00a04442a Mon Sep 17 00:00:00 2001 From: arctic-alpaca <67190338+arctic-alpaca@users.noreply.github.com> Date: Sun, 10 Dec 2023 15:26:26 +0100 Subject: [PATCH] Fix cfg, take &str instead of String --- src/backend/libc/net/mod.rs | 2 +- src/backend/libc/net/netdevice.rs | 10 ++++---- src/backend/linux_raw/net/mod.rs | 2 +- src/backend/linux_raw/net/netdevice.rs | 11 +++++---- src/net/mod.rs | 3 ++- src/net/netdevice.rs | 32 ++++++++++++++------------ 6 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/backend/libc/net/mod.rs b/src/backend/libc/net/mod.rs index ff017d904..07121f647 100644 --- a/src/backend/libc/net/mod.rs +++ b/src/backend/libc/net/mod.rs @@ -8,7 +8,7 @@ pub(crate) mod ext; target_os = "wasi" )))] pub(crate) mod msghdr; -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", feature = "alloc"))] pub(crate) mod netdevice; pub(crate) mod read_sockaddr; pub(crate) mod send_recv; diff --git a/src/backend/libc/net/netdevice.rs b/src/backend/libc/net/netdevice.rs index 8ba9063a4..d1f22e000 100644 --- a/src/backend/libc/net/netdevice.rs +++ b/src/backend/libc/net/netdevice.rs @@ -2,15 +2,17 @@ use crate::backend::io::syscalls::ioctl; use crate::fd::AsFd; -use crate::ffi::{CStr, CString}; +use crate::ffi::CStr; +#[cfg(all(target_os = "linux", feature = "alloc"))] +use crate::ffi::CString; use crate::io; use crate::net::netdevice::open_socket; use core::mem::MaybeUninit; use core::ptr::{addr_of, addr_of_mut}; use libc::{c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX, SIOCGIFNAME}; -#[cfg(target_os = "linux")] -pub(crate) fn index_to_name(if_name: String) -> io::Result { +#[cfg(all(target_os = "linux", feature = "alloc"))] +pub(crate) fn index_to_name(if_name: &str) -> io::Result { if if_name.len() >= IFNAMSIZ as usize { return Err(io::Errno::NODEV); } @@ -36,7 +38,7 @@ pub(crate) fn index_to_name(if_name: String) -> io::Result { Ok(index as u32) } -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", feature = "alloc"))] pub(crate) fn name_to_index(index: u32) -> io::Result { let mut uninit_ifreq = MaybeUninit::::uninit(); let uninit_ifreq_ptr = uninit_ifreq.as_mut_ptr(); diff --git a/src/backend/linux_raw/net/mod.rs b/src/backend/linux_raw/net/mod.rs index 11fdb7404..384eeb6cc 100644 --- a/src/backend/linux_raw/net/mod.rs +++ b/src/backend/linux_raw/net/mod.rs @@ -1,6 +1,6 @@ pub(crate) mod addr; pub(crate) mod msghdr; -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", feature = "alloc"))] pub(crate) mod netdevice; pub(crate) mod read_sockaddr; pub(crate) mod send_recv; diff --git a/src/backend/linux_raw/net/netdevice.rs b/src/backend/linux_raw/net/netdevice.rs index 05f9ba0c6..7a20a2342 100644 --- a/src/backend/linux_raw/net/netdevice.rs +++ b/src/backend/linux_raw/net/netdevice.rs @@ -2,17 +2,20 @@ use crate::backend::io::syscalls::ioctl; use crate::fd::AsFd; -use crate::ffi::{CStr, CString}; +use crate::ffi::CStr; +#[cfg(all(target_os = "linux", feature = "alloc"))] +use crate::ffi::CString; use crate::io; use crate::net::netdevice::open_socket; +use crate::path::Arg; use core::mem::MaybeUninit; use core::ptr::{addr_of, addr_of_mut}; use linux_raw_sys::ctypes::c_char; use linux_raw_sys::ioctl::{SIOCGIFINDEX, SIOCGIFNAME}; use linux_raw_sys::net::{ifreq, IFNAMSIZ}; -#[cfg(target_os = "linux")] -pub(crate) fn index_to_name(if_name: String) -> io::Result { +#[cfg(all(target_os = "linux", feature = "alloc"))] +pub(crate) fn index_to_name(if_name: &str) -> io::Result { if if_name.len() >= IFNAMSIZ as usize { return Err(io::Errno::NODEV); } @@ -38,7 +41,7 @@ pub(crate) fn index_to_name(if_name: String) -> io::Result { Ok(index as u32) } -#[cfg(target_os = "linux")] +#[cfg(all(target_os = "linux", feature = "alloc"))] pub(crate) fn name_to_index(index: u32) -> io::Result { let mut uninit_ifreq = MaybeUninit::::uninit(); let uninit_ifreq_ptr = uninit_ifreq.as_mut_ptr(); diff --git a/src/net/mod.rs b/src/net/mod.rs index d23139a65..34078bdbe 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -16,7 +16,8 @@ mod types; #[cfg(windows)] mod wsa; -#[cfg(target_os = "linux")] +// Currently limited to feature = "alloc" as it only contains methods requiring alloc at this time. +#[cfg(all(target_os = "linux", feature = "alloc"))] pub mod netdevice; pub mod sockopt; diff --git a/src/net/netdevice.rs b/src/net/netdevice.rs index 2e9598f54..626533f12 100644 --- a/src/net/netdevice.rs +++ b/src/net/netdevice.rs @@ -5,13 +5,15 @@ //! //! [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html +use crate::fd::OwnedFd; use crate::io; use crate::io::Errno; use crate::net::{socket, AddressFamily, SocketType}; -use std::os::fd::OwnedFd; /// Creates a socket used to communicate with the kernel in the ioctl calls. -#[cfg(target_os = "linux")] +// Currently limited to feature = "alloc" as it is only used by methods +// requiring alloc at this time. +#[cfg(all(target_os = "linux", feature = "alloc"))] pub(crate) fn open_socket() -> io::Result { if let Ok(fd) = socket(AddressFamily::UNIX, SocketType::DGRAM, None) { Ok(fd) @@ -24,30 +26,30 @@ pub(crate) fn open_socket() -> io::Result { } } -/// `ioctl(fd, SIOCGIFNAME, ifreq)`—Returns the interface name for a given index. +/// `ioctl(fd, SIOCGIFINDEX, ifreq)`—Returns the interface index for a given name. /// /// # References /// - [Linux] /// /// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html #[inline] -#[doc(alias = "SIOCGIFNAME")] -#[cfg(target_os = "linux")] -pub fn name_to_index(index: u32) -> io::Result { - crate::backend::net::netdevice::name_to_index(index) +#[doc(alias = "SIOCGIFINDEX")] +#[cfg(all(target_os = "linux", feature = "alloc"))] +pub fn index_to_name(if_name: &str) -> io::Result { + crate::backend::net::netdevice::index_to_name(if_name) } -/// `ioctl(fd, SIOCGIFINDEX, ifreq)`—Returns the interface index for a given name. +/// `ioctl(fd, SIOCGIFNAME, ifreq)`—Returns the interface name for a given index. /// /// # References /// - [Linux] /// /// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html #[inline] -#[doc(alias = "SIOCGIFINDEX")] -#[cfg(target_os = "linux")] -pub fn index_to_name(if_name: String) -> io::Result { - crate::backend::net::netdevice::index_to_name(if_name) +#[doc(alias = "SIOCGIFNAME")] +#[cfg(all(target_os = "linux", feature = "alloc"))] +pub fn name_to_index(index: u32) -> io::Result { + crate::backend::net::netdevice::name_to_index(index) } #[cfg(test)] @@ -55,7 +57,7 @@ mod tests { use crate::backend::net::netdevice::{index_to_name, name_to_index}; #[test] - #[cfg(target_os = "linux")] + #[cfg(all(target_os = "linux", feature = "alloc"))] fn test_index_to_name() { let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex") .unwrap() @@ -64,11 +66,11 @@ mod tests { .0 .parse::() .unwrap(); - assert_eq!(Ok(loopback_index), index_to_name("lo".to_owned())); + assert_eq!(Ok(loopback_index), index_to_name("lo")); } #[test] - #[cfg(target_os = "linux")] + #[cfg(all(target_os = "linux", feature = "alloc"))] fn test_name_to_index() { let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex") .unwrap()