Skip to content
Merged
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
100 changes: 92 additions & 8 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,15 +1893,39 @@ 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
/// for the interface to which the socket is bound.
/// Gets the value for `IP_BOUND_IF` or `SO_BINDTOIFINDEX` option on this
/// socket, i.e. the index for the interface to which the socket is bound.
///
/// Returns `None` if the socket is not bound to any interface, otherwise
/// returns an interface index.
Expand All @@ -1889,16 +1939,33 @@ 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))
}

/// Gets the value for `IPV6_BOUND_IF` option on this socket, i.e. the index
/// for the interface to which the socket is bound.
/// Gets the value for `IPV6_BOUND_IF` or `SO_BINDTOIFINDEX` option on this
/// socket, i.e. the index for the interface to which the socket is bound.
///
/// Returns `None` if the socket is not bound to any interface, otherwise
/// returns an interface 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
Loading