Skip to content

Commit

Permalink
Added if_indextoname function. (nix-rust#2340)
Browse files Browse the repository at this point in the history
* Added if_indextoname function.

* Added changelog.

* Fixed pointer cast.

* Applied suggestions.
  • Loading branch information
Frostie314159 authored Mar 24, 2024
1 parent 6c11f02 commit 945910d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
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.
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
);
}

0 comments on commit 945910d

Please sign in to comment.