Skip to content

Commit

Permalink
Merge pull request #549 from greenbaum/illumos
Browse files Browse the repository at this point in the history
Add illumos support
  • Loading branch information
mrmonday committed Apr 25, 2022
2 parents a8cb175 + e175359 commit 44f17c8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 18 deletions.
33 changes: 28 additions & 5 deletions pnet_datalink/src/bindings/bpf.rs
Expand Up @@ -27,7 +27,12 @@ const IOCPARM_MASK: libc::c_ulong = (1 << (IOCPARM_SHIFT as usize)) - 1;
const SIZEOF_TIMEVAL: libc::c_ulong = 16;
const SIZEOF_IFREQ: libc::c_ulong = 32;
const SIZEOF_C_UINT: libc::c_ulong = 4;
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris"
))]
const SIZEOF_C_LONG: libc::c_int = 8;

pub const BIOCSETIF: libc::c_ulong =
Expand All @@ -46,7 +51,7 @@ pub const BIOCSHDRCMPLT: libc::c_ulong =
pub const BIOCSRTIMEOUT: libc::c_ulong =
IOC_IN | ((SIZEOF_TIMEVAL & IOCPARM_MASK) << 16) | (('B' as libc::c_ulong) << 8) | 109;

#[cfg(target_os = "freebsd")]
#[cfg(any(target_os = "freebsd", target_os = "illumos", target_os = "solaris"))]
pub const BIOCFEEDBACK: libc::c_ulong =
IOC_IN | ((SIZEOF_C_UINT & IOCPARM_MASK) << 16) | (('B' as libc::c_ulong) << 8) | 124;
#[cfg(target_os = "netbsd")]
Expand All @@ -56,7 +61,12 @@ pub const BIOCFEEDBACK: libc::c_ulong =

pub const DLT_NULL: libc::c_uint = 0;

#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris"
))]
const BPF_ALIGNMENT: libc::c_int = SIZEOF_C_LONG;
#[cfg(any(target_os = "openbsd", target_os = "macos", target_os = "ios", windows))]
const BPF_ALIGNMENT: libc::c_int = 4;
Expand All @@ -76,7 +86,15 @@ pub struct ifreq {
// sdl_data does not match if_dl.h on OS X, since the size of 12 is a minimum.
// Will be unsafe
// when sdl_nlen > 40.
#[cfg(any(target_os = "openbsd", target_os = "freebsd", target_os = "netbsd", target_os = "macos", target_os = "ios"))]
#[cfg(any(
target_os = "openbsd",
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios"
))]
pub struct sockaddr_dl {
pub sdl_len: libc::c_uchar,
pub sdl_family: libc::c_uchar,
Expand All @@ -92,7 +110,12 @@ pub struct sockaddr_dl {
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
all(any(target_os = "macos", target_os = "ios"), target_pointer_width = "32"),
target_os = "illumos",
target_os = "solaris",
all(
any(target_os = "macos", target_os = "ios"),
target_pointer_width = "32"
),
windows
))]
#[repr(C)]
Expand Down
2 changes: 2 additions & 0 deletions pnet_datalink/src/bindings/mod.rs
Expand Up @@ -10,6 +10,8 @@
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios",
windows
Expand Down
17 changes: 14 additions & 3 deletions pnet_datalink/src/bpf.rs
Expand Up @@ -75,11 +75,17 @@ impl Default for Config {
// NOTE buffer must be word aligned.
#[inline]
pub fn channel(network_interface: &NetworkInterface, config: Config) -> io::Result<super::Channel> {
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris"
))]
fn get_fd(_attempts: usize) -> libc::c_int {
let c_file_name = CString::new(&b"/dev/bpf"[..]).unwrap();
unsafe {
libc::open(
CString::new(&b"/dev/bpf"[..]).unwrap().as_ptr(),
c_file_name.as_ptr(),
libc::O_RDWR,
0,
)
Expand All @@ -106,7 +112,12 @@ pub fn channel(network_interface: &NetworkInterface, config: Config) -> io::Resu
-1
}

#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris"
))]
fn set_feedback(fd: libc::c_int) -> io::Result<()> {
if unsafe { bpf::ioctl(fd, bpf::BIOCFEEDBACK, &1) } == -1 {
let err = io::Error::last_os_error();
Expand Down
29 changes: 21 additions & 8 deletions pnet_datalink/src/lib.rs
Expand Up @@ -51,6 +51,8 @@ pub mod linux;
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios"
)
Expand All @@ -61,6 +63,8 @@ mod backend;
#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios"
))]
Expand Down Expand Up @@ -226,48 +230,57 @@ pub struct NetworkInterface {
/// IP addresses and netmasks for the interface.
pub ips: Vec<IpNetwork>,
/// Operating system specific flags for the interface.
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
pub flags: u32,
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
pub flags: u64,
}

/// Type alias for an `InterfaceType`.
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
pub type InterfaceType = u32;
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
pub type InterfaceType = u64;

impl NetworkInterface {
pub fn is_up(&self) -> bool {
self.flags & (pnet_sys::IFF_UP as u32) != 0
self.flags & (pnet_sys::IFF_UP as InterfaceType) != 0
}

pub fn is_broadcast(&self) -> bool {
self.flags & (pnet_sys::IFF_BROADCAST as u32) != 0
self.flags & (pnet_sys::IFF_BROADCAST as InterfaceType) != 0
}

/// Is the interface a loopback interface?
pub fn is_loopback(&self) -> bool {
self.flags & (pnet_sys::IFF_LOOPBACK as u32) != 0
self.flags & (pnet_sys::IFF_LOOPBACK as InterfaceType) != 0
}

pub fn is_point_to_point(&self) -> bool {
self.flags & (pnet_sys::IFF_POINTOPOINT as u32) != 0
self.flags & (pnet_sys::IFF_POINTOPOINT as InterfaceType) != 0
}

pub fn is_multicast(&self) -> bool {
self.flags & (pnet_sys::IFF_MULTICAST as u32) != 0
self.flags & (pnet_sys::IFF_MULTICAST as InterfaceType) != 0
}

/// Triggered when the driver has signated netif_carrier_on
/// Check <https://www.kernel.org/doc/html/latest/networking/operstates.html> for more information
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn is_lower_up(&self) -> bool {
self.flags & (pnet_sys::IFF_LOWER_UP as u32) != 0
self.flags & (pnet_sys::IFF_LOWER_UP as InterfaceType) != 0
}

/// Triggered when the driver has signated netif_dormant_on
/// Check <https://www.kernel.org/doc/html/latest/networking/operstates.html> for more information
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn is_dormant(&self) -> bool {
self.flags & (pnet_sys::IFF_DORMANT as u32) != 0
self.flags & (pnet_sys::IFF_DORMANT as InterfaceType) != 0
}

#[cfg(unix)]
pub fn is_running(&self) -> bool {
self.flags & (pnet_sys::IFF_RUNNING as u32) != 0
self.flags & (pnet_sys::IFF_RUNNING as InterfaceType) != 0
}
}

Expand Down
2 changes: 2 additions & 0 deletions pnet_datalink/src/unix_interfaces.rs
Expand Up @@ -142,6 +142,8 @@ fn sockaddr_to_network_addr(sa: *const libc::sockaddr) -> (Option<MacAddr>, Opti
target_os = "openbsd",
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios"
))]
Expand Down
8 changes: 6 additions & 2 deletions pnet_sys/src/unix.rs
Expand Up @@ -30,6 +30,10 @@ pub mod public {
pub type TvUsecType = libc::c_long;
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "netbsd"))]
pub type TvUsecType = libc::c_int;
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
pub type InAddrType = libc::c_uint;
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
pub type InAddrType = libc::c_ulonglong;

pub const AF_INET: libc::c_int = libc::AF_INET;
pub const AF_INET6: libc::c_int = libc::AF_INET6;
Expand Down Expand Up @@ -206,8 +210,8 @@ pub mod public {
use self::public::*;

#[inline(always)]
pub fn ipv4_addr(addr: InAddr) -> u32 {
(addr.s_addr as u32).to_be()
pub fn ipv4_addr(addr: InAddr) -> InAddrType {
(addr.s_addr as InAddrType).to_be()
}

#[inline(always)]
Expand Down

0 comments on commit 44f17c8

Please sign in to comment.