Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
52 changes: 45 additions & 7 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::net::{Ipv4Addr, Ipv6Addr};
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
)
))]
use std::num::NonZeroU32;
Expand Down Expand Up @@ -1389,6 +1391,36 @@ pub(crate) fn original_dst_ipv6(fd: Socket) -> io::Result<SockAddr> {
.map(|(_, addr)| addr)
}

// TODO: remove this once https://github.com/rust-lang/libc/pull/4287 merges
#[cfg(all(feature = "all", any(target_os = "illumos", target_os = "solaris")))]
const IP_BOUND_IF: libc::c_int = 0x41;
#[cfg(all(
feature = "all",
any(
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
)
))]
use libc::IP_BOUND_IF;

// TODO: remove this once https://github.com/rust-lang/libc/pull/4287 merges
#[cfg(all(feature = "all", any(target_os = "illumos", target_os = "solaris")))]
const IPV6_BOUND_IF: libc::c_int = 0x41;
#[cfg(all(
feature = "all",
any(
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
)
))]
use libc::IPV6_BOUND_IF;

/// Unix only API.
impl crate::Socket {
/// Accept a new incoming connection from this listener.
Expand Down Expand Up @@ -1834,11 +1866,13 @@ impl crate::Socket {
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
)
))]
pub fn bind_device_by_index_v4(&self, interface: Option<NonZeroU32>) -> io::Result<()> {
let index = interface.map_or(0, NonZeroU32::get);
unsafe { setsockopt(self.as_raw(), IPPROTO_IP, libc::IP_BOUND_IF, index) }
unsafe { setsockopt(self.as_raw(), IPPROTO_IP, IP_BOUND_IF, index) }
}

/// Sets the value for `IPV6_BOUND_IF` option on this socket.
Expand All @@ -1859,11 +1893,13 @@ impl crate::Socket {
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
)
))]
pub fn bind_device_by_index_v6(&self, interface: Option<NonZeroU32>) -> io::Result<()> {
let index = interface.map_or(0, NonZeroU32::get);
unsafe { setsockopt(self.as_raw(), IPPROTO_IPV6, libc::IPV6_BOUND_IF, index) }
unsafe { setsockopt(self.as_raw(), IPPROTO_IPV6, IPV6_BOUND_IF, index) }
}

/// Gets the value for `IP_BOUND_IF` option on this socket, i.e. the index
Expand All @@ -1879,11 +1915,12 @@ impl crate::Socket {
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
)
))]
pub fn device_index_v4(&self) -> io::Result<Option<NonZeroU32>> {
let index =
unsafe { getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IP, libc::IP_BOUND_IF)? };
let index = unsafe { getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IP, IP_BOUND_IF)? };
Ok(NonZeroU32::new(index))
}

Expand All @@ -1900,12 +1937,13 @@ impl crate::Socket {
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
)
))]
pub fn device_index_v6(&self) -> io::Result<Option<NonZeroU32>> {
let index = unsafe {
getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IPV6, libc::IPV6_BOUND_IF)?
};
let index =
unsafe { getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IPV6, IPV6_BOUND_IF)? };
Ok(NonZeroU32::new(index))
}

Expand Down
4 changes: 4 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ fn device() {
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "solaris",
target_os = "illumos",
)
))]
#[test]
Expand Down Expand Up @@ -1036,6 +1038,8 @@ fn device() {
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "solaris",
target_os = "illumos",
)
))]
#[test]
Expand Down