//! 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 [manual pages] specify that the ioctl calls “can be used on any //! socket's file descriptor regardless of the family or type”. //! //! # References //! - [Linux] //! //! [manual pages]: https://man7.org/linux/man-pages/man7/netdevice.7.html //! [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html
usecrate::fd::AsFd; usecrate::io; #[cfg(feature = "alloc")] use alloc::{borrow::ToOwned, string::String};
/// `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")] pubfn name_to_index<Fd: AsFd>(fd: Fd, if_name: &str) -> io::Result<u32> { crate::backend::net::netdevice::name_to_index(fd.as_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. /// /// See also [`index_to_name_inlined`] which does not require `alloc` feature. /// /// # References /// - [Linux] /// /// [module-level documentation]: self /// [Linux]: https://man7.org/linux/man-pages/man7/netdevice.7.html #[inline] #[doc(alias = "SIOCGIFNAME")] #[cfg(feature = "alloc")] #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] pubfn index_to_name<Fd: AsFd>(fd: Fd, index: u32) -> io::Result<String> { let (len, ifrn_name) = crate::backend::net::netdevice::index_to_name(fd.as_fd(), index)?;
/// `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")] pubfn 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,
})
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.