Skip to content

Add index_to_name_inlined #1397

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

Merged
merged 6 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
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
19 changes: 7 additions & 12 deletions src/backend/libc/net/netdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

#![allow(unsafe_code)]

#[cfg(feature = "alloc")]
use crate::alloc::string::String;
use crate::backend::c;
use crate::backend::io::syscalls::ioctl;
use crate::fd::BorrowedFd;
use crate::io;
#[cfg(feature = "alloc")]
use c::SIOCGIFNAME;
use c::{__c_anonymous_ifr_ifru, c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX};
use c::{__c_anonymous_ifr_ifru, c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX, SIOCGIFNAME};

pub(crate) fn name_to_index(fd: BorrowedFd<'_>, if_name: &str) -> io::Result<u32> {
let if_name_bytes = if_name.as_bytes();
Expand All @@ -31,8 +27,7 @@ pub(crate) fn name_to_index(fd: BorrowedFd<'_>, if_name: &str) -> io::Result<u32
Ok(index as u32)
}

#[cfg(feature = "alloc")]
pub(crate) fn index_to_name(fd: BorrowedFd<'_>, index: u32) -> io::Result<String> {
pub(crate) fn index_to_name(fd: BorrowedFd<'_>, index: u32) -> io::Result<(usize, [u8; 16])> {
let mut ifreq = ifreq {
ifr_name: [0; 16],
ifr_ifru: __c_anonymous_ifr_ifru {
Expand All @@ -43,12 +38,12 @@ pub(crate) fn index_to_name(fd: BorrowedFd<'_>, index: u32) -> io::Result<String
unsafe { ioctl(fd, SIOCGIFNAME as _, &mut ifreq as *mut ifreq as _) }?;

if let Some(nul_byte) = ifreq.ifr_name.iter().position(|char| *char == 0) {
let name: String = ifreq.ifr_name[..nul_byte]
.iter()
.map(|v| *v as u8 as char)
.collect();
let mut buf = [0u8; 16];
ifreq.ifr_name.iter().enumerate().for_each(|(idx, c)| {
buf[idx] = *c as u8;
});

Ok(name)
Ok((nul_byte, buf))
} else {
Err(io::Errno::INVAL)
}
Expand Down
16 changes: 6 additions & 10 deletions src/backend/linux_raw/net/netdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ use crate::io;
use core::ptr::addr_of_mut;
use core::{slice, str};
use linux_raw_sys::ctypes::c_char;
use linux_raw_sys::ioctl::SIOCGIFINDEX;
#[cfg(feature = "alloc")]
use linux_raw_sys::ioctl::SIOCGIFNAME;
use linux_raw_sys::ioctl::{SIOCGIFINDEX, SIOCGIFNAME};
use linux_raw_sys::net::{ifreq, ifreq__bindgen_ty_1, ifreq__bindgen_ty_2, IFNAMSIZ};
#[cfg(feature = "alloc")]
use {alloc::borrow::ToOwned, alloc::string::String};

pub(crate) fn name_to_index(fd: BorrowedFd<'_>, if_name: &str) -> io::Result<u32> {
let if_name_bytes = if_name.as_bytes();
Expand All @@ -40,8 +36,7 @@ pub(crate) fn name_to_index(fd: BorrowedFd<'_>, if_name: &str) -> io::Result<u32
Ok(index as u32)
}

#[cfg(feature = "alloc")]
pub(crate) fn index_to_name(fd: BorrowedFd<'_>, index: u32) -> io::Result<String> {
pub(crate) fn index_to_name(fd: BorrowedFd<'_>, index: u32) -> io::Result<(usize, [u8; 16])> {
let mut ifreq = ifreq {
ifr_ifrn: ifreq__bindgen_ty_1 { ifrn_name: [0; 16] },
ifr_ifru: ifreq__bindgen_ty_2 {
Expand All @@ -61,9 +56,10 @@ pub(crate) fn index_to_name(fd: BorrowedFd<'_>, index: u32) -> io::Result<String
let ifrn_name =
unsafe { slice::from_raw_parts(ifrn_name.as_ptr().cast::<u8>(), ifrn_name.len()) };

str::from_utf8(ifrn_name)
.map_err(|_| io::Errno::ILSEQ)
.map(ToOwned::to_owned)
let mut name_buf = [0; 16];
name_buf[..ifrn_name.len()].copy_from_slice(ifrn_name);

Ok((nul_byte, name_buf))
} else {
Err(io::Errno::INVAL)
}
Expand Down
105 changes: 102 additions & 3 deletions src/net/netdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::fd::AsFd;
use crate::io;
#[cfg(feature = "alloc")]
use alloc::string::String;
use alloc::{borrow::ToOwned, string::String};

/// `ioctl(fd, SIOCGIFINDEX, ifreq)`—Returns the interface index for a given
/// name.
Expand All @@ -42,6 +42,8 @@ pub fn name_to_index<Fd: AsFd>(fd: Fd, if_name: &str) -> io::Result<u32> {
///
/// See the [module-level documentation] for information about `fd` usage.
///
/// See also [`index_to_name_inlined`] which does not require `alloc` feature.
///
/// # References
/// - [Linux]
///
Expand All @@ -52,12 +54,84 @@ pub fn name_to_index<Fd: AsFd>(fd: Fd, if_name: &str) -> io::Result<u32> {
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn index_to_name<Fd: AsFd>(fd: Fd, index: u32) -> io::Result<String> {
crate::backend::net::netdevice::index_to_name(fd.as_fd(), index)
let (len, ifrn_name) = crate::backend::net::netdevice::index_to_name(fd.as_fd(), index)?;

core::str::from_utf8(&ifrn_name[..len])
.map_err(|_| io::Errno::ILSEQ)
.map(ToOwned::to_owned)
}

/// `ioctl(fd, SIOCGIFNAME, ifreq)`—Returns the interface name for a given
/// index.
///
/// See the [module-level documentation] for information about `fd` usage.
///
/// # References
/// - [Linux]
///
/// [module-level documentation]: self
/// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
#[inline]
#[doc(alias = "SIOCGIFNAME")]
pub fn index_to_name_inlined<Fd: AsFd>(fd: Fd, index: u32) -> io::Result<InlinedName> {
let (len, ifrn_name) = crate::backend::net::netdevice::index_to_name(fd.as_fd(), index)?;

// Check if the name is valid UTF-8.
core::str::from_utf8(&ifrn_name[..len])
.map_err(|_| io::Errno::ILSEQ)
.map(|_| InlinedName {
len,
name: ifrn_name,
})
}

/// The inlined interface name.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct InlinedName {
len: usize,
name: [u8; 16],
}

impl InlinedName {
/// Returns the str representation of the inlined name.
pub fn as_str(&self) -> &str {
self.as_ref()
}

/// Returns the bytes representation of the inlined name.
pub fn as_bytes(&self) -> &[u8] {
self.as_ref()
}
}

impl AsRef<[u8]> for InlinedName {
fn as_ref(&self) -> &[u8] {
&self.name[..self.len]
}
}

impl AsRef<str> for InlinedName {
fn as_ref(&self) -> &str {
// SAFETY: `InlinedName` is constructed with valid UTF-8.
core::str::from_utf8(&self.name[..self.len]).unwrap()
}
}

impl core::borrow::Borrow<str> for InlinedName {
fn borrow(&self) -> &str {
self.as_ref()
}
}

impl core::fmt::Display for InlinedName {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.as_str().fmt(f)
}
}

#[cfg(test)]
mod tests {
use crate::backend::net::netdevice::{index_to_name, name_to_index};
use super::{index_to_name, index_to_name_inlined, name_to_index};
use crate::fd::AsFd;
use crate::net::{AddressFamily, SocketFlags, SocketType};

Expand All @@ -81,6 +155,31 @@ mod tests {
assert_eq!(Ok(loopback_index), name_to_index(fd.as_fd(), "lo"));
}

#[test]
fn test_index_to_name_inlined() {
let fd = crate::net::socket_with(
AddressFamily::INET,
SocketType::DGRAM,
SocketFlags::CLOEXEC,
None,
)
.unwrap();

let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex")
.unwrap()
.as_str()
.split_at(1)
.0
.parse::<u32>()
.unwrap();
assert_eq!(
"lo",
index_to_name_inlined(fd.as_fd(), loopback_index)
.unwrap()
.as_str(),
);
}

#[test]
#[cfg(feature = "alloc")]
fn test_index_to_name() {
Expand Down
Loading