Skip to content

Commit

Permalink
Fix cfg, take &str instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-alpaca committed Dec 10, 2023
1 parent e9c0a89 commit 5d9c99c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/backend/libc/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/backend/libc/net/netdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> {
#[cfg(all(target_os = "linux", feature = "alloc"))]
pub(crate) fn index_to_name(if_name: &str) -> io::Result<u32> {
if if_name.len() >= IFNAMSIZ as usize {
return Err(io::Errno::NODEV);
}
Expand All @@ -36,7 +38,7 @@ pub(crate) fn index_to_name(if_name: String) -> io::Result<u32> {
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<String> {
let mut uninit_ifreq = MaybeUninit::<ifreq>::uninit();
let uninit_ifreq_ptr = uninit_ifreq.as_mut_ptr();
Expand Down
2 changes: 1 addition & 1 deletion src/backend/linux_raw/net/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 7 additions & 4 deletions src/backend/linux_raw/net/netdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> {
#[cfg(all(target_os = "linux", feature = "alloc"))]
pub(crate) fn index_to_name(if_name: &str) -> io::Result<u32> {
if if_name.len() >= IFNAMSIZ as usize {
return Err(io::Errno::NODEV);
}
Expand All @@ -38,7 +41,7 @@ pub(crate) fn index_to_name(if_name: String) -> io::Result<u32> {
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<String> {
let mut uninit_ifreq = MaybeUninit::<ifreq>::uninit();
let uninit_ifreq_ptr = uninit_ifreq.as_mut_ptr();
Expand Down
3 changes: 2 additions & 1 deletion src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
32 changes: 17 additions & 15 deletions src/net/netdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OwnedFd> {
if let Ok(fd) = socket(AddressFamily::UNIX, SocketType::DGRAM, None) {
Ok(fd)
Expand All @@ -24,38 +26,38 @@ pub(crate) fn open_socket() -> io::Result<OwnedFd> {
}
}

/// `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<String> {
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<u32> {
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<u32> {
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<String> {
crate::backend::net::netdevice::name_to_index(index)
}

#[cfg(test)]
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()
Expand All @@ -64,11 +66,11 @@ mod tests {
.0
.parse::<u32>()
.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()
Expand Down

0 comments on commit 5d9c99c

Please sign in to comment.