diff options
Diffstat (limited to 'talpid-core/src')
| -rw-r--r-- | talpid-core/src/linux/mod.rs | 29 |
1 files changed, 9 insertions, 20 deletions
diff --git a/talpid-core/src/linux/mod.rs b/talpid-core/src/linux/mod.rs index 4b5321e4f0..225534ea12 100644 --- a/talpid-core/src/linux/mod.rs +++ b/talpid-core/src/linux/mod.rs @@ -1,27 +1,16 @@ -use std::{ - ffi::{self, CString}, - io, -}; +use nix::{errno::Errno, net::if_::if_nametoindex}; /// Converts an interface name into the corresponding index. pub fn iface_index(name: &str) -> Result<libc::c_uint, IfaceIndexLookupError> { - let c_name = CString::new(name) - .map_err(|e| IfaceIndexLookupError::InvalidInterfaceName(name.to_owned(), e))?; - let index = unsafe { libc::if_nametoindex(c_name.as_ptr()) }; - if index == 0 { - Err(IfaceIndexLookupError::InterfaceLookupError( - name.to_owned(), - io::Error::last_os_error(), - )) - } else { - Ok(index) - } + if_nametoindex(name).map_err(|error| IfaceIndexLookupError { + interface_name: name.to_owned(), + error, + }) } #[derive(Debug, thiserror::Error)] -pub enum IfaceIndexLookupError { - #[error("Invalid network interface name: {0}")] - InvalidInterfaceName(String, #[source] ffi::NulError), - #[error("Failed to get index for interface {0}")] - InterfaceLookupError(String, #[source] io::Error), +#[error("Failed to get index for interface {interface_name}: {error}")] +pub struct IfaceIndexLookupError { + pub interface_name: String, + pub error: Errno, } |
