|
| 1 | +//! Low-level Linux network device access |
| 2 | +//! |
| 3 | +//! # References |
| 4 | +//! - [Linux] |
| 5 | +//! |
| 6 | +//! [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html |
| 7 | +
|
| 8 | +#[cfg(feature = "alloc")] |
| 9 | +use crate::alloc::string::String; |
| 10 | +use crate::fd::OwnedFd; |
| 11 | +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 | +} |
| 28 | + |
| 29 | +/// `ioctl(fd, SIOCGIFINDEX, ifreq)`—Returns the interface index for a given name. |
| 30 | +/// |
| 31 | +/// # References |
| 32 | +/// - [Linux] |
| 33 | +/// |
| 34 | +/// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html |
| 35 | +#[inline] |
| 36 | +#[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) |
| 40 | +} |
| 41 | + |
| 42 | +/// `ioctl(fd, SIOCGIFNAME, ifreq)`—Returns the interface name for a given index. |
| 43 | +/// |
| 44 | +/// # References |
| 45 | +/// - [Linux] |
| 46 | +/// |
| 47 | +/// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html |
| 48 | +#[inline] |
| 49 | +#[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) |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use crate::backend::net::netdevice::{index_to_name, name_to_index}; |
| 58 | + |
| 59 | + #[test] |
| 60 | + #[cfg(target_os = "linux")] |
| 61 | + fn test_name_to_index() { |
| 62 | + let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex") |
| 63 | + .unwrap() |
| 64 | + .as_str() |
| 65 | + .split_at(1) |
| 66 | + .0 |
| 67 | + .parse::<u32>() |
| 68 | + .unwrap(); |
| 69 | + assert_eq!(Ok(loopback_index), name_to_index("lo")); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + #[cfg(all(target_os = "linux", feature = "alloc"))] |
| 74 | + fn test_index_to_name() { |
| 75 | + let loopback_index = std::fs::read_to_string("/sys/class/net/lo/ifindex") |
| 76 | + .unwrap() |
| 77 | + .as_str() |
| 78 | + .split_at(1) |
| 79 | + .0 |
| 80 | + .parse::<u32>() |
| 81 | + .unwrap(); |
| 82 | + assert_eq!(Ok("lo".to_owned()), index_to_name(loopback_index)); |
| 83 | + } |
| 84 | +} |
0 commit comments