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

Enable IP_BOUND_IF on illumos and Solaris #561

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
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;
Comment on lines +1394 to +1396
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a bummer; I have an upstream PR rust-lang/libc#4287 to add these constants in libc, but wanted to test the change without that. I don't have a strong opinion on whether it's important to wait to pick these up from libc or if it's okay to just merge with a local definition and use them from libc in the future, but I imagine the maintainers will have an opinion on this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The libc pr is already merged, so let's wait for a release. I really don't want to maintain constants :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm happy to wait --- this was just intended to allow testing the change temporarily. Once there's a new libc release, I'll pick it up and update this.

#[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