use std::{ffi::{CStr, CString}, fmt};
use crate::{errno::Errno, Error, NixPath, Result};
use libc::{c_uint, IF_NAMESIZE};
#[cfg(not(solarish))]
pub type IflagsType = libc::c_int;
#[cfg(solarish)]
pub type IflagsType = libc::c_longlong;
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()) })?;
if if_index == 0 {
Err(Error::last())
} else {
Ok(if_index)
}
}
pub fn if_indextoname(index: c_uint) -> Result<CString> {
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!(
pub struct InterfaceFlags: IflagsType {
IFF_UP as IflagsType;
IFF_BROADCAST as IflagsType;
#[cfg(not(any(target_os = "haiku", target_os = "cygwin")))]
IFF_DEBUG as IflagsType;
IFF_LOOPBACK as IflagsType;
IFF_POINTOPOINT as IflagsType;
#[cfg(any(
linux_android,
solarish,
apple_targets,
target_os = "fuchsia",
target_os = "netbsd",
target_os = "cygwin"))]
IFF_NOTRAILERS as IflagsType;
#[cfg(any(target_os = "dragonfly"))]
IFF_SMART as IflagsType;
#[cfg(any(
linux_android,
bsd,
solarish,
target_os = "fuchsia",
target_os = "cygwin"))]
IFF_RUNNING as IflagsType;
IFF_NOARP as IflagsType;
IFF_PROMISC as IflagsType;
#[cfg(not(target_os = "cygwin"))]
IFF_ALLMULTI as IflagsType;
#[cfg(any(linux_android, target_os = "fuchsia"))]
IFF_MASTER;
#[cfg(any(target_os = "freebsd", apple_targets, netbsdlike))]
IFF_OACTIVE;
#[cfg(solarish)]
IFF_INTELLIGENT as IflagsType;
#[cfg(any(linux_android, target_os = "fuchsia"))]
IFF_SLAVE;
#[cfg(bsd)]
IFF_SIMPLEX;
IFF_MULTICAST as IflagsType;
#[cfg(bsd)]
IFF_LINK0;
#[cfg(solarish)]
IFF_MULTI_BCAST as IflagsType;
#[cfg(any(linux_android, target_os = "fuchsia"))]
IFF_PORTSEL;
#[cfg(bsd)]
IFF_LINK1;
#[cfg(solarish)]
IFF_UNNUMBERED as IflagsType;
#[cfg(any(linux_android, target_os = "fuchsia"))]
IFF_AUTOMEDIA;
#[cfg(bsd)]
IFF_LINK2;
#[cfg(any(freebsdlike, apple_targets))]
IFF_ALTPHYS;
#[cfg(solarish)]
IFF_DHCPRUNNING as IflagsType;
#[cfg(any(linux_android, target_os = "fuchsia"))]
IFF_DYNAMIC;
#[cfg(solarish)]
IFF_PRIVATE as IflagsType;
#[cfg(any(target_os = "fuchsia", target_os = "linux", target_os = "cygwin"))]
IFF_LOWER_UP;
#[cfg(any(target_os = "dragonfly"))]
IFF_POLLING_COMPAT;
#[cfg(any(target_os = "freebsd"))]
IFF_CANTCONFIG;
#[cfg(solarish)]
IFF_NOXMIT as IflagsType;
#[cfg(any(target_os = "fuchsia", target_os = "linux", target_os = "cygwin"))]
IFF_DORMANT;
#[cfg(freebsdlike)]
IFF_PPROMISC;
#[cfg(solarish)]
IFF_NOLOCAL as IflagsType;
#[cfg(any(target_os = "fuchsia", target_os = "linux"))]
IFF_ECHO;
#[cfg(freebsdlike)]
IFF_MONITOR;
#[cfg(solarish)]
IFF_DEPRECATED as IflagsType;
#[cfg(freebsdlike)]
IFF_STATICARP;
#[cfg(solarish)]
IFF_ADDRCONF as IflagsType;
#[cfg(any(target_os = "dragonfly"))]
IFF_NPOLLING;
#[cfg(solarish)]
IFF_ROUTER as IflagsType;
#[cfg(any(target_os = "dragonfly"))]
IFF_IDIRECT;
#[cfg(any(target_os = "freebsd"))]
IFF_DYING;
#[cfg(solarish)]
IFF_NONUD as IflagsType;
#[cfg(any(target_os = "freebsd"))]
IFF_RENAMING;
#[cfg(solarish)]
IFF_ANYCAST as IflagsType;
#[cfg(solarish)]
IFF_NORTEXCH as IflagsType;
#[cfg(any(linux_android, target_os = "fuchsia"))]
IFF_NO_PI as IflagsType;
#[cfg(any(linux_android, target_os = "fuchsia"))]
IFF_TUN as IflagsType;
#[cfg(any(linux_android, target_os = "fuchsia"))]
IFF_TAP as IflagsType;
#[cfg(solarish)]
IFF_IPV4 as IflagsType;
#[cfg(solarish)]
IFF_IPV6 as IflagsType;
#[cfg(solarish)]
IFF_NOFAILOVER as IflagsType;
#[cfg(solarish)]
IFF_FAILED as IflagsType;
#[cfg(solarish)]
IFF_STANDBY as IflagsType;
#[cfg(solarish)]
IFF_INACTIVE as IflagsType;
#[cfg(solarish)]
IFF_OFFLINE as IflagsType;
#[cfg(solarish)]
IFF_COS_ENABLED as IflagsType;
#[cfg(solarish)]
IFF_PREFERRED as IflagsType;
#[cfg(solarish)]
IFF_TEMPORARY as IflagsType;
#[cfg(solarish)]
IFF_FIXEDMTU as IflagsType;
#[cfg(solarish)]
IFF_VIRTUAL as IflagsType;
#[cfg(solarish)]
IFF_DUPLICATE as IflagsType;
#[cfg(solarish)]
IFF_IPMP as IflagsType;
}
);
impl fmt::Display for InterfaceFlags {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
bitflags::parser::to_writer(self, f)
}
}
#[cfg(any(
bsd,
target_os = "fuchsia",
target_os = "linux",
solarish,
))]
mod if_nameindex {
use super::*;
use std::ffi::CStr;
use std::fmt;
use std::marker::PhantomData;
use std::ptr::NonNull;
#[allow(missing_copy_implementations)]
#[repr(transparent)]
pub struct Interface(libc::if_nameindex);
impl Interface {
pub fn index(&self) -> c_uint {
self.0.if_index
}
pub fn name(&self) -> &CStr {
unsafe { CStr::from_ptr(self.0.if_name) }
}
}
impl fmt::Debug for Interface {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Interface")
.field("index", &self.index())
.field("name", &self.name())
.finish()
}
}
#[repr(transparent)]
pub struct Interfaces {
ptr: NonNull<libc::if_nameindex>,
}
impl Interfaces {
#[inline]
pub fn iter(&self) -> InterfacesIter<'_> {
self.into_iter()
}
pub fn to_slice(&self) -> &[Interface] {
let ifs = self.ptr.as_ptr().cast();
let len = self.iter().count();
unsafe { std::slice::from_raw_parts(ifs, len) }
}
}
impl Drop for Interfaces {
fn drop(&mut self) {
unsafe { libc::if_freenameindex(self.ptr.as_ptr()) };
}
}
impl fmt::Debug for Interfaces {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_slice().fmt(f)
}
}
impl<'a> IntoIterator for &'a Interfaces {
type IntoIter = InterfacesIter<'a>;
type Item = &'a Interface;
#[inline]
fn into_iter(self) -> Self::IntoIter {
InterfacesIter {
ptr: self.ptr.as_ptr(),
_marker: PhantomData,
}
}
}
#[derive(Debug)]
pub struct InterfacesIter<'a> {
ptr: *const libc::if_nameindex,
_marker: PhantomData<&'a Interfaces>,
}
impl<'a> Iterator for InterfacesIter<'a> {
type Item = &'a Interface;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
unsafe {
if (*self.ptr).if_index == 0 {
None
} else {
let ret = &*(self.ptr as *const Interface);
self.ptr = self.ptr.add(1);
Some(ret)
}
}
}
}
pub fn if_nameindex() -> Result<Interfaces> {
unsafe {
let ifs = libc::if_nameindex();
let ptr = NonNull::new(ifs).ok_or_else(Error::last)?;
Ok(Interfaces { ptr })
}
}
}
#[cfg(any(
bsd,
target_os = "fuchsia",
target_os = "linux",
solarish,
))]
pub use if_nameindex::*;