Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added if_indextoname function. #2340

Merged
merged 5 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2340.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add if_indextoname function.
21 changes: 17 additions & 4 deletions src/net/if_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 crate::{Error, NixPath, Result};
use libc::c_uint;
use std::{ffi::{CStr, CString}, fmt};
use crate::{errno::Errno, Error, NixPath, Result};
use libc::{c_uint, IF_NAMESIZE};

#[cfg(not(solarish))]
/// type alias for InterfaceFlags
Expand All @@ -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.
Frostie314159 marked this conversation as resolved.
Show resolved Hide resolved
pub fn if_nametoindex<P: ?Sized + NixPath>(name: &P) -> Result<c_uint> {
let if_index = name
.with_nix_path(|name| unsafe { libc::if_nametoindex(name.as_ptr()) })?;
Expand All @@ -26,6 +26,19 @@ pub fn if_nametoindex<P: ?Sized + NixPath>(name: &P) -> Result<c_uint> {
}
}

/// Resolve an interface number into an interface.
pub fn if_indextoname(index: c_uint) -> Result<CString> {
// 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().cast())
};

Errno::result(return_buf.cast())?;
Ok(CStr::from_bytes_until_nul(buf.as_slice()).unwrap().to_owned())
}

libc_bitflags!(
/// Standard interface flags, used by `getifaddrs`
pub struct InterfaceFlags: IflagsType {
Expand Down
11 changes: 11 additions & 0 deletions test/test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}