Skip to content

Added bind_device_by_index_{v4,v6} for linux and android (#569) #572

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

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ targets = [
features = ["all"]

[target."cfg(unix)".dependencies]
libc = "0.2.171"
libc = "0.2.172"

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52"
Expand Down
92 changes: 88 additions & 4 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use std::net::{Ipv4Addr, Ipv6Addr};
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
target_os = "linux",
target_os = "android",
)
))]
use std::num::NonZeroU32;
Expand Down Expand Up @@ -1820,7 +1822,7 @@ impl crate::Socket {
.map(|_| ())
}

/// Sets the value for `IP_BOUND_IF` option on this socket.
/// Sets the value for `IP_BOUND_IF` or `SO_BINDTOIFINDEX` option on this socket.
///
/// If a socket is bound to an interface, only packets received from that
/// particular interface are processed by the socket.
Expand All @@ -1840,14 +1842,38 @@ impl crate::Socket {
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
target_os = "linux",
target_os = "android",
)
))]
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) }

#[cfg(any(
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
))]
unsafe {
setsockopt(self.as_raw(), IPPROTO_IP, libc::IP_BOUND_IF, index)
}

#[cfg(any(target_os = "linux", target_os = "android",))]
unsafe {
setsockopt(
self.as_raw(),
libc::SOL_SOCKET,
libc::SO_BINDTOIFINDEX,
index,
)
}
}

/// Sets the value for `IPV6_BOUND_IF` option on this socket.
/// Sets the value for `IPV6_BOUND_IF` or `SO_BINDTOIFINDEX` option on this socket.
///
/// If a socket is bound to an interface, only packets received from that
/// particular interface are processed by the socket.
Expand All @@ -1867,11 +1893,35 @@ impl crate::Socket {
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
target_os = "linux",
target_os = "android",
)
))]
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) }

#[cfg(any(
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
))]
unsafe {
setsockopt(self.as_raw(), IPPROTO_IPV6, libc::IPV6_BOUND_IF, index)
}

#[cfg(any(target_os = "linux", target_os = "android",))]
unsafe {
setsockopt(
self.as_raw(),
libc::SOL_SOCKET,
libc::SO_BINDTOIFINDEX,
index,
)
}
}

/// Gets the value for `IP_BOUND_IF` option on this socket, i.e. the index
Expand All @@ -1889,11 +1939,28 @@ impl crate::Socket {
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
target_os = "linux",
target_os = "android",
)
))]
pub fn device_index_v4(&self) -> io::Result<Option<NonZeroU32>> {
#[cfg(any(
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
))]
let index =
unsafe { getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IP, libc::IP_BOUND_IF)? };

#[cfg(any(target_os = "linux", target_os = "android",))]
let index = unsafe {
getsockopt::<libc::c_uint>(self.as_raw(), libc::SOL_SOCKET, libc::SO_BINDTOIFINDEX)?
};

Ok(NonZeroU32::new(index))
}

Expand All @@ -1912,12 +1979,29 @@ impl crate::Socket {
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
target_os = "linux",
target_os = "android",
)
))]
pub fn device_index_v6(&self) -> io::Result<Option<NonZeroU32>> {
#[cfg(any(
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "illumos",
target_os = "solaris",
))]
let index = unsafe {
getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IPV6, libc::IPV6_BOUND_IF)?
};

#[cfg(any(target_os = "linux", target_os = "android",))]
let index = unsafe {
getsockopt::<libc::c_uint>(self.as_raw(), libc::SOL_SOCKET, libc::SO_BINDTOIFINDEX)?
};

Ok(NonZeroU32::new(index))
}

Expand Down