use crate::fd::AsFd;
use crate::io;
#[cfg(feature = "alloc")]
use alloc::string::String;
#[inline]
#[doc(alias = "SIOCGIFINDEX")]
pub fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
crate::backend::net::netdevice::name_to_index(fd, if_name)
}
#[inline]
#[doc(alias = "SIOCGIFNAME")]
#[cfg(feature = "alloc")]
pub fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
crate::backend::net::netdevice::index_to_name(fd, index)
}
#[cfg(test)]
mod tests {
use crate::backend::net::netdevice::{index_to_name, name_to_index};
use crate::net::{AddressFamily, SocketFlags, SocketType};
#[test]
fn test_name_to_index() {
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!(Ok(loopback_index), name_to_index(fd, "lo"));
}
#[test]
#[cfg(feature = "alloc")]
fn test_index_to_name() {
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!(Ok("lo".to_owned()), index_to_name(fd, loopback_index));
}
}