summaryrefslogtreecommitdiffhomepage
path: root/talpid-core
diff options
context:
space:
mode:
authorJoakim Hulthe <joakim.hulthe@mullvad.net>2025-02-10 13:22:57 +0100
committerJoakim Hulthe <joakim.hulthe@mullvad.net>2025-02-25 13:37:28 +0100
commit9de617dfd667e13bf0cd2e3c213e0f6f656797a3 (patch)
tree4d975129e5f0c7ca14b1fa0751646a86ba22def7 /talpid-core
parentb908dc0f83f9e1def3104407a3d5aeefeb118fd1 (diff)
downloadmullvadvpn-9de617dfd667e13bf0cd2e3c213e0f6f656797a3.tar.xz
mullvadvpn-9de617dfd667e13bf0cd2e3c213e0f6f656797a3.zip
Replace libc::if_nametoindex with nix
Diffstat (limited to 'talpid-core')
-rw-r--r--talpid-core/src/linux/mod.rs29
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,
}