diff options
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | talpid-net/Cargo.toml | 2 | ||||
| -rw-r--r-- | talpid-net/src/unix.rs | 18 | ||||
| -rw-r--r-- | talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs | 28 |
4 files changed, 22 insertions, 28 deletions
diff --git a/Cargo.lock b/Cargo.lock index 2bddb8bef9..aa087cf126 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4615,8 +4615,10 @@ version = "0.0.0" dependencies = [ "libc", "log", + "nix 0.29.0", "socket2", "talpid-types", + "thiserror 2.0.9", ] [[package]] diff --git a/talpid-net/Cargo.toml b/talpid-net/Cargo.toml index 861e1765cc..485eaa37d0 100644 --- a/talpid-net/Cargo.toml +++ b/talpid-net/Cargo.toml @@ -15,3 +15,5 @@ libc = "0.2" talpid-types = { path = "../talpid-types" } socket2 = { workspace = true, features = ["all"] } log = { workspace = true } +thiserror = { workspace = true } +nix = { version = "0.29", features = ["net"] } diff --git a/talpid-net/src/unix.rs b/talpid-net/src/unix.rs index 3843968157..48d65c45f0 100644 --- a/talpid-net/src/unix.rs +++ b/talpid-net/src/unix.rs @@ -1,10 +1,26 @@ #![cfg(any(target_os = "linux", target_os = "macos"))] -use std::{io, os::fd::AsRawFd}; +use std::{ffi::c_uint, io, os::fd::AsRawFd}; +use nix::{errno::Errno, net::if_::if_nametoindex}; use socket2::Domain; use talpid_types::ErrorExt; +/// Converts an interface name into the corresponding index. +pub fn iface_index(name: &str) -> Result<c_uint, IfaceIndexLookupError> { + if_nametoindex(name).map_err(|error| IfaceIndexLookupError { + interface_name: name.to_owned(), + error, + }) +} + +#[derive(Debug, thiserror::Error)] +#[error("Failed to get index for interface {interface_name}: {error}")] +pub struct IfaceIndexLookupError { + pub interface_name: String, + pub error: Errno, +} + #[cfg(target_os = "macos")] const SIOCSIFMTU: u64 = 0x80206934; #[cfg(target_os = "macos")] diff --git a/talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs b/talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs index ba3bca14be..46a394a59d 100644 --- a/talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs +++ b/talpid-wireguard/src/wireguard_kernel/nm_tunnel.rs @@ -13,6 +13,7 @@ use talpid_dbus::{ WireguardTunnel, }, }; +use talpid_net::unix::iface_index; use talpid_tunnel_config_client::DaitaSettings; #[derive(thiserror::Error, Debug)] @@ -209,30 +210,3 @@ fn convert_config_to_dbus(config: &Config) -> DeviceConfig { settings } - -/// Converts an interface name into the corresponding index. -#[cfg(target_os = "linux")] -fn iface_index(name: &str) -> std::result::Result<libc::c_uint, IfaceIndexLookupError> { - let c_name = std::ffi::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(), - std::io::Error::last_os_error(), - )) - } else { - Ok(index) - } -} - -/// Failure to lookup an interfaces index by its name. -#[derive(Debug, thiserror::Error)] -pub enum IfaceIndexLookupError { - /// The interface name is invalid - contains null bytes or is too long. - #[error("Invalid network interface name: {0}")] - InvalidInterfaceName(String, #[source] std::ffi::NulError), - /// Interface wasn't found by its name. - #[error("Failed to get index for interface {0}")] - InterfaceLookupError(String, #[source] std::io::Error), -} |
