Skip to content

Commit

Permalink
Remove unnecessary cfgs, include Android, switch to socket fd as para…
Browse files Browse the repository at this point in the history
…meter
  • Loading branch information
arctic-alpaca committed Dec 19, 2023
1 parent d246e76 commit 93d2f4b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 44 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(linux_kernel)]
pub(crate) mod netdevice;
pub(crate) mod read_sockaddr;
pub(crate) mod send_recv;
Expand Down
10 changes: 3 additions & 7 deletions src/backend/libc/net/netdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ use crate::alloc::string::String;
use crate::backend::io::syscalls::ioctl;
use crate::fd::AsFd;
use crate::io;
use crate::net::netdevice::open_socket;
#[cfg(feature = "alloc")]
use libc::SIOCGIFNAME;
use libc::{__c_anonymous_ifr_ifru, c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX};

#[cfg(target_os = "linux")]
pub(crate) fn name_to_index(if_name: &str) -> io::Result<u32> {
pub(crate) fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
let if_name_bytes = if_name.as_bytes();
if if_name_bytes.len() >= IFNAMSIZ as usize {
return Err(io::Errno::NODEV);
Expand All @@ -25,22 +23,20 @@ pub(crate) fn name_to_index(if_name: &str) -> io::Result<u32> {
let mut if_name_c_char_iter = if_name_bytes.iter().map(|byte| *byte as c_char);
ifreq.ifr_name[..if_name_bytes.len()].fill_with(|| if_name_c_char_iter.next().unwrap());

let fd = open_socket()?;
unsafe { ioctl(fd.as_fd(), SIOCGIFINDEX as _, &mut ifreq as *mut ifreq as _) }?;
let index = unsafe { ifreq.ifr_ifru.ifru_ifindex };
Ok(index as u32)
}

#[cfg(all(target_os = "linux", feature = "alloc"))]
pub(crate) fn index_to_name(index: u32) -> io::Result<String> {
#[cfg(feature = "alloc")]
pub(crate) fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
let mut ifreq = ifreq {
ifr_name: [0; 16],
ifr_ifru: __c_anonymous_ifr_ifru {
ifru_ifindex: index as _,
},
};

let fd = open_socket()?;
unsafe { ioctl(fd.as_fd(), SIOCGIFNAME as _, &mut ifreq as *mut ifreq as _) }?;

if let Some(nul_byte) = ifreq.ifr_name.iter().position(|char| *char == 0) {
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(linux_kernel)]
pub(crate) mod netdevice;
pub(crate) mod read_sockaddr;
pub(crate) mod send_recv;
Expand Down
10 changes: 3 additions & 7 deletions src/backend/linux_raw/net/netdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ use crate::alloc::string::String;
use crate::backend::io::syscalls::ioctl;
use crate::fd::AsFd;
use crate::io;
use crate::net::netdevice::open_socket;
use linux_raw_sys::ioctl::SIOCGIFINDEX;
#[cfg(feature = "alloc")]
use linux_raw_sys::ioctl::SIOCGIFNAME;
use linux_raw_sys::net::{ifreq, ifreq__bindgen_ty_1, ifreq__bindgen_ty_2, IFNAMSIZ};

#[cfg(target_os = "linux")]
pub(crate) fn name_to_index(if_name: &str) -> io::Result<u32> {
pub(crate) fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
let if_name_bytes = if_name.as_bytes();
if if_name_bytes.len() >= IFNAMSIZ as usize {
return Err(io::Errno::NODEV);
Expand All @@ -24,22 +22,20 @@ pub(crate) fn name_to_index(if_name: &str) -> io::Result<u32> {
};
unsafe { ifreq.ifr_ifrn.ifrn_name[..if_name_bytes.len()].copy_from_slice(if_name_bytes) };

let fd = open_socket()?;
unsafe { ioctl(fd.as_fd(), SIOCGIFINDEX, &mut ifreq as *mut ifreq as _) }?;
let index = unsafe { ifreq.ifr_ifru.ifru_ivalue };
Ok(index as u32)
}

#[cfg(all(target_os = "linux", feature = "alloc"))]
pub(crate) fn index_to_name(index: u32) -> io::Result<String> {
#[cfg(feature = "alloc")]
pub(crate) fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
let mut ifreq = ifreq {
ifr_ifrn: ifreq__bindgen_ty_1 { ifrn_name: [0; 16] },
ifr_ifru: ifreq__bindgen_ty_2 {
ifru_ivalue: index as _,
},
};

let fd = open_socket()?;
unsafe { ioctl(fd.as_fd(), SIOCGIFNAME, &mut ifreq as *mut ifreq as _) }?;

if let Some(nul_byte) = unsafe { ifreq.ifr_ifrn.ifrn_name }
Expand Down
2 changes: 1 addition & 1 deletion src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod types;
#[cfg(windows)]
mod wsa;

#[cfg(target_os = "linux")]
#[cfg(linux_kernel)]
pub mod netdevice;
pub mod sockopt;

Expand Down
69 changes: 42 additions & 27 deletions src/net/netdevice.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,99 @@
//! Low-level Linux network device access
//!
//! The methods in this module take a socket's file descriptor to communicate with
//! the kernel in their ioctl call:
//! - glibc uses an `AF_UNIX`, `AF_INET`, or `AF_INET6` socket.
//! The address family itself does not matter and glibc tries the next address family if socket creation with one fails.
//! - Android (bionic) uses an `AF_INET` socket.
//! - Both create the socket with `SOCK_DGRAM|SOCK_CLOEXEC` type/flag.
//! - The [man-pages] specify, that the ioctl calls "can be used on any socket's file descriptor regardless of the
//! family or type".
//!
//! # References
//! - [Linux]
//!
//! [man-pages]: https://man7.org/linux/man-pages/man7/netdevice.7.html
//! [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
#[cfg(feature = "alloc")]
use crate::alloc::string::String;
use crate::fd::OwnedFd;
use crate::fd::AsFd;
use crate::io;
use crate::io::Errno;
use crate::net::{socket, AddressFamily, SocketType};

/// Creates a socket used to communicate with the kernel in the ioctl calls.
#[cfg(target_os = "linux")]
pub(crate) fn open_socket() -> io::Result<OwnedFd> {
if let Ok(fd) = socket(AddressFamily::UNIX, SocketType::DGRAM, None) {
Ok(fd)
} else if let Ok(fd) = socket(AddressFamily::INET, SocketType::DGRAM, None) {
Ok(fd)
} else if let Ok(fd) = socket(AddressFamily::INET6, SocketType::DGRAM, None) {
Ok(fd)
} else {
Err(Errno::NOENT)
}
}

/// `ioctl(fd, SIOCGIFINDEX, ifreq)`—Returns the interface index for a given name.
///
/// 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 = "SIOCGIFINDEX")]
#[cfg(target_os = "linux")]
pub fn name_to_index(if_name: &str) -> io::Result<u32> {
crate::backend::net::netdevice::name_to_index(if_name)
pub fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
crate::backend::net::netdevice::name_to_index(fd, if_name)
}

/// `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")]
#[cfg(all(target_os = "linux", feature = "alloc"))]
pub fn index_to_name(index: u32) -> io::Result<String> {
crate::backend::net::netdevice::index_to_name(index)
#[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]
#[cfg(target_os = "linux")]
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("lo"));
assert_eq!(Ok(loopback_index), name_to_index(fd, "lo"));
}

#[test]
#[cfg(all(target_os = "linux", feature = "alloc"))]
#[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(loopback_index));
assert_eq!(Ok("lo".to_owned()), index_to_name(fd, loopback_index));
}
}

0 comments on commit 93d2f4b

Please sign in to comment.