From 5410b8a48acb3d93fc6a1f7e025dd96c66663940 Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Thu, 21 Mar 2024 14:05:25 +0100 Subject: [PATCH 1/4] Added if_indextoname function. --- src/net/if_.rs | 22 +++++++++++++++++++--- test/test_net.rs | 11 +++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/net/if_.rs b/src/net/if_.rs index c66b5dc0b3..1c489b866b 100644 --- a/src/net/if_.rs +++ b/src/net/if_.rs @@ -3,9 +3,9 @@ //! Uses Linux and/or POSIX functions to resolve interface names like "eth0" //! or "socan1" into device numbers. -use std::fmt; +use std::{ffi::{CStr, CString}, fmt}; use crate::{Error, NixPath, Result}; -use libc::c_uint; +use libc::{c_uint, IF_NAMESIZE}; #[cfg(not(solarish))] /// type alias for InterfaceFlags @@ -14,7 +14,7 @@ pub type IflagsType = libc::c_int; /// type alias for InterfaceFlags pub type IflagsType = libc::c_longlong; -/// Resolve an interface into a interface number. +/// Resolve an interface into an interface number. pub fn if_nametoindex(name: &P) -> Result { let if_index = name .with_nix_path(|name| unsafe { libc::if_nametoindex(name.as_ptr()) })?; @@ -26,6 +26,22 @@ pub fn if_nametoindex(name: &P) -> Result { } } +/// Resolve an interface number into an interface. +pub fn if_indextoname(index: c_uint) -> Result { + // We need to allocate this anyway, so doing it directly is faster. + let mut buf = vec![0u8; IF_NAMESIZE]; + + let return_buf = unsafe { + libc::if_indextoname(index, buf.as_mut_ptr() as *mut i8) + }; + + if return_buf.is_null() { + Err(Error::last()) + } else { + Ok(CStr::from_bytes_until_nul(buf.as_slice()).unwrap().to_owned()) + } +} + libc_bitflags!( /// Standard interface flags, used by `getifaddrs` pub struct InterfaceFlags: IflagsType { diff --git a/test/test_net.rs b/test/test_net.rs index faba8503fe..46a3efd501 100644 --- a/test/test_net.rs +++ b/test/test_net.rs @@ -13,3 +13,14 @@ const LOOPBACK: &[u8] = b"loop"; fn test_if_nametoindex() { if_nametoindex(LOOPBACK).expect("assertion failed"); } + +#[test] +fn test_if_indextoname() { + let loopback_index = if_nametoindex(LOOPBACK).expect("assertion failed"); + assert_eq!( + if_indextoname(loopback_index) + .expect("assertion failed") + .as_bytes(), + LOOPBACK + ); +} From 8514c940809ac314343cdba3716729d2cd6a96c9 Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Thu, 21 Mar 2024 14:09:58 +0100 Subject: [PATCH 2/4] Added changelog. --- changelog/2340.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2340.added.md diff --git a/changelog/2340.added.md b/changelog/2340.added.md new file mode 100644 index 0000000000..9d120d4879 --- /dev/null +++ b/changelog/2340.added.md @@ -0,0 +1 @@ +Add if_indextoname function. From 404c0334d26006ee81d66e7eee58c2877e9a853b Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Thu, 21 Mar 2024 14:15:28 +0100 Subject: [PATCH 3/4] Fixed pointer cast. --- src/net/if_.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/if_.rs b/src/net/if_.rs index 1c489b866b..1ceb67b60d 100644 --- a/src/net/if_.rs +++ b/src/net/if_.rs @@ -32,7 +32,7 @@ pub fn if_indextoname(index: c_uint) -> Result { let mut buf = vec![0u8; IF_NAMESIZE]; let return_buf = unsafe { - libc::if_indextoname(index, buf.as_mut_ptr() as *mut i8) + libc::if_indextoname(index, buf.as_mut_ptr() as *mut _) }; if return_buf.is_null() { From a4eaae2cf9a16ab7f6cc13778b21ce1b9afad167 Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Sat, 23 Mar 2024 18:13:40 +0100 Subject: [PATCH 4/4] Applied suggestions. --- src/net/if_.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/net/if_.rs b/src/net/if_.rs index 1ceb67b60d..5b6c2e77a5 100644 --- a/src/net/if_.rs +++ b/src/net/if_.rs @@ -4,7 +4,7 @@ //! or "socan1" into device numbers. use std::{ffi::{CStr, CString}, fmt}; -use crate::{Error, NixPath, Result}; +use crate::{errno::Errno, Error, NixPath, Result}; use libc::{c_uint, IF_NAMESIZE}; #[cfg(not(solarish))] @@ -32,14 +32,11 @@ pub fn if_indextoname(index: c_uint) -> Result { let mut buf = vec![0u8; IF_NAMESIZE]; let return_buf = unsafe { - libc::if_indextoname(index, buf.as_mut_ptr() as *mut _) + libc::if_indextoname(index, buf.as_mut_ptr().cast()) }; - if return_buf.is_null() { - Err(Error::last()) - } else { - Ok(CStr::from_bytes_until_nul(buf.as_slice()).unwrap().to_owned()) - } + Errno::result(return_buf.cast())?; + Ok(CStr::from_bytes_until_nul(buf.as_slice()).unwrap().to_owned()) } libc_bitflags!(