Skip to content

Commit bcb8739

Browse files
committed
Remove unnecessary cfgs, include Android, switch to socket fd as parameter
1 parent e87c108 commit bcb8739

File tree

6 files changed

+51
-44
lines changed

6 files changed

+51
-44
lines changed

src/backend/libc/net/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) mod ext;
88
target_os = "wasi"
99
)))]
1010
pub(crate) mod msghdr;
11-
#[cfg(target_os = "linux")]
11+
#[cfg(linux_kernel)]
1212
pub(crate) mod netdevice;
1313
pub(crate) mod read_sockaddr;
1414
pub(crate) mod send_recv;

src/backend/libc/net/netdevice.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ use crate::alloc::string::String;
55
use crate::backend::io::syscalls::ioctl;
66
use crate::fd::AsFd;
77
use crate::io;
8-
use crate::net::netdevice::open_socket;
98
#[cfg(feature = "alloc")]
109
use libc::SIOCGIFNAME;
1110
use libc::{__c_anonymous_ifr_ifru, c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX};
1211

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

28-
let fd = open_socket()?;
2926
unsafe { ioctl(fd.as_fd(), SIOCGIFINDEX as _, &mut ifreq as *mut ifreq as _) }?;
3027
let index = unsafe { ifreq.ifr_ifru.ifru_ifindex };
3128
Ok(index as u32)
3229
}
3330

34-
#[cfg(all(target_os = "linux", feature = "alloc"))]
35-
pub(crate) fn index_to_name(index: u32) -> io::Result<String> {
31+
#[cfg(feature = "alloc")]
32+
pub(crate) fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
3633
let mut ifreq = ifreq {
3734
ifr_name: [0; 16],
3835
ifr_ifru: __c_anonymous_ifr_ifru {
3936
ifru_ifindex: index as _,
4037
},
4138
};
4239

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

4642
if let Some(nul_byte) = ifreq.ifr_name.iter().position(|char| *char == 0) {

src/backend/linux_raw/net/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub(crate) mod addr;
22
pub(crate) mod msghdr;
3-
#[cfg(target_os = "linux")]
3+
#[cfg(linux_kernel)]
44
pub(crate) mod netdevice;
55
pub(crate) mod read_sockaddr;
66
pub(crate) mod send_recv;

src/backend/linux_raw/net/netdevice.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ use crate::alloc::string::String;
55
use crate::backend::io::syscalls::ioctl;
66
use crate::fd::AsFd;
77
use crate::io;
8-
use crate::net::netdevice::open_socket;
98
use linux_raw_sys::ioctl::SIOCGIFINDEX;
109
#[cfg(feature = "alloc")]
1110
use linux_raw_sys::ioctl::SIOCGIFNAME;
1211
use linux_raw_sys::net::{ifreq, ifreq__bindgen_ty_1, ifreq__bindgen_ty_2, IFNAMSIZ};
1312

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

27-
let fd = open_socket()?;
2825
unsafe { ioctl(fd.as_fd(), SIOCGIFINDEX, &mut ifreq as *mut ifreq as _) }?;
2926
let index = unsafe { ifreq.ifr_ifru.ifru_ivalue };
3027
Ok(index as u32)
3128
}
3229

33-
#[cfg(all(target_os = "linux", feature = "alloc"))]
34-
pub(crate) fn index_to_name(index: u32) -> io::Result<String> {
30+
#[cfg(feature = "alloc")]
31+
pub(crate) fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
3532
let mut ifreq = ifreq {
3633
ifr_ifrn: ifreq__bindgen_ty_1 { ifrn_name: [0; 16] },
3734
ifr_ifru: ifreq__bindgen_ty_2 {
3835
ifru_ivalue: index as _,
3936
},
4037
};
4138

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

4541
if let Some(nul_byte) = unsafe { ifreq.ifr_ifrn.ifrn_name }

src/net/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod types;
1616
#[cfg(windows)]
1717
mod wsa;
1818

19-
#[cfg(target_os = "linux")]
19+
#[cfg(linux_kernel)]
2020
pub mod netdevice;
2121
pub mod sockopt;
2222

src/net/netdevice.rs

+42-27
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,99 @@
11
//! Low-level Linux network device access
22
//!
3+
//! The methods in this module take a socket's file descriptor to communicate with
4+
//! the kernel in their ioctl call:
5+
//! - glibc uses an `AF_UNIX`, `AF_INET`, or `AF_INET6` socket.
6+
//! The address family itself does not matter and glibc tries the next address family if socket creation with one fails.
7+
//! - Android (bionic) uses an `AF_INET` socket.
8+
//! - Both create the socket with `SOCK_DGRAM|SOCK_CLOEXEC` type/flag.
9+
//! - The [man-pages] specify, that the ioctl calls "can be used on any socket's file descriptor regardless of the
10+
//! family or type".
11+
//!
312
//! # References
413
//! - [Linux]
514
//!
15+
//! [man-pages]: https://man7.org/linux/man-pages/man7/netdevice.7.html
616
//! [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
717
818
#[cfg(feature = "alloc")]
919
use crate::alloc::string::String;
10-
use crate::fd::OwnedFd;
20+
use crate::fd::AsFd;
1121
use crate::io;
12-
use crate::io::Errno;
13-
use crate::net::{socket, AddressFamily, SocketType};
14-
15-
/// Creates a socket used to communicate with the kernel in the ioctl calls.
16-
#[cfg(target_os = "linux")]
17-
pub(crate) fn open_socket() -> io::Result<OwnedFd> {
18-
if let Ok(fd) = socket(AddressFamily::UNIX, SocketType::DGRAM, None) {
19-
Ok(fd)
20-
} else if let Ok(fd) = socket(AddressFamily::INET, SocketType::DGRAM, None) {
21-
Ok(fd)
22-
} else if let Ok(fd) = socket(AddressFamily::INET6, SocketType::DGRAM, None) {
23-
Ok(fd)
24-
} else {
25-
Err(Errno::NOENT)
26-
}
27-
}
2822

2923
/// `ioctl(fd, SIOCGIFINDEX, ifreq)`—Returns the interface index for a given name.
3024
///
25+
/// See the [module-level documentation] for information about `fd` usage.
26+
///
3127
/// # References
3228
/// - [Linux]
3329
///
30+
/// [module-level documentation]: self
3431
/// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
3532
#[inline]
3633
#[doc(alias = "SIOCGIFINDEX")]
37-
#[cfg(target_os = "linux")]
38-
pub fn name_to_index(if_name: &str) -> io::Result<u32> {
39-
crate::backend::net::netdevice::name_to_index(if_name)
34+
pub fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
35+
crate::backend::net::netdevice::name_to_index(fd, if_name)
4036
}
4137

4238
/// `ioctl(fd, SIOCGIFNAME, ifreq)`—Returns the interface name for a given index.
4339
///
40+
/// See the [module-level documentation] for information about `fd` usage.
41+
///
4442
/// # References
4543
/// - [Linux]
4644
///
45+
/// [module-level documentation]: self
4746
/// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
4847
#[inline]
4948
#[doc(alias = "SIOCGIFNAME")]
50-
#[cfg(all(target_os = "linux", feature = "alloc"))]
51-
pub fn index_to_name(index: u32) -> io::Result<String> {
52-
crate::backend::net::netdevice::index_to_name(index)
49+
#[cfg(feature = "alloc")]
50+
pub fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
51+
crate::backend::net::netdevice::index_to_name(fd, index)
5352
}
5453

5554
#[cfg(test)]
5655
mod tests {
5756
use crate::backend::net::netdevice::{index_to_name, name_to_index};
57+
use crate::net::{AddressFamily, SocketFlags, SocketType};
5858

5959
#[test]
60-
#[cfg(target_os = "linux")]
6160
fn test_name_to_index() {
61+
let fd = crate::net::socket_with(
62+
AddressFamily::INET,
63+
SocketType::DGRAM,
64+
SocketFlags::CLOEXEC,
65+
None,
66+
)
67+
.unwrap();
68+
6269
let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex")
6370
.unwrap()
6471
.as_str()
6572
.split_at(1)
6673
.0
6774
.parse::<u32>()
6875
.unwrap();
69-
assert_eq!(Ok(loopback_index), name_to_index("lo"));
76+
assert_eq!(Ok(loopback_index), name_to_index(fd, "lo"));
7077
}
7178

7279
#[test]
73-
#[cfg(all(target_os = "linux", feature = "alloc"))]
80+
#[cfg(feature = "alloc")]
7481
fn test_index_to_name() {
82+
let fd = crate::net::socket_with(
83+
AddressFamily::INET,
84+
SocketType::DGRAM,
85+
SocketFlags::CLOEXEC,
86+
None,
87+
)
88+
.unwrap();
89+
7590
let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex")
7691
.unwrap()
7792
.as_str()
7893
.split_at(1)
7994
.0
8095
.parse::<u32>()
8196
.unwrap();
82-
assert_eq!(Ok("lo".to_owned()), index_to_name(loopback_index));
97+
assert_eq!(Ok("lo".to_owned()), index_to_name(fd, loopback_index));
8398
}
8499
}

0 commit comments

Comments
 (0)