Skip to content

Commit

Permalink
Add (set_)ip_bindany_v6 for target_os=freebsd
Browse files Browse the repository at this point in the history
  • Loading branch information
cavivie committed Aug 2, 2024
1 parent 8274773 commit 9242341
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3274,6 +3274,41 @@ impl crate::Socket {
)
}
}

/// Get the value of the `IPV6_BINDANY` option on this socket.
///
/// For more information about this option, see [`set_ip_bindany_v6`].
///
/// [`set_ip_bindany_v6`]: crate::Socket::set_ip_bindany_v6
#[cfg(all(feature = "all", target_os = "freebsd"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "freebsd"))))]
pub fn ip_bindany_v6(&self) -> io::Result<bool> {
unsafe {
getsockopt::<c_int>(self.as_raw(), libc::IPPROTO_IPV6, libc::IPV6_BINDANY)
.map(|bindany| bindany != 0)
}
}

/// Set the value of the `IPV6_BINDANY` option on this socket.
///
/// If the IPV6_BINDANY option is enabled on a SOCK_STREAM, SOCK_DGRAM or a
/// SOCK_RAW socket, one can bind(2) to any address, even one not bound to
/// any available network interface in the system. This functionality (in
/// conjunction with special firewall rules) can be used for implementing a
/// transparent proxy. The PRIV_NETINET_BINDANY privilege is needed to set
/// this option.
#[cfg(all(feature = "all", target_os = "freebsd"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "freebsd"))))]
pub fn set_ip_bindany_v6(&self, bindany: bool) -> io::Result<()> {
unsafe {
setsockopt(
self.as_raw(),
libc::IPPROTO_IPV6,
libc::IPV6_BINDANY,
bindany as c_int,
)
}
}
}

/// See [`Socket::dccp_available_ccids`].
Expand Down
6 changes: 6 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,12 @@ test!(
ip_bindany,
set_ip_bindany_v4(true)
);
#[cfg(all(feature = "all", target_os = "freebsd"))]
test!(
#[ignore = "setting `IPV6_BINDANY` requires the `PRIV_NETINET_BINDANY` privilege (works when running as root)"]
ip_bindany_v6,
set_ip_bindany_v6(true)
);
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
test!(
#[ignore = "setting `SO_MARK` requires the `CAP_NET_ADMIN` capability (works when running as root)"]
Expand Down

0 comments on commit 9242341

Please sign in to comment.