Skip to content

Commit bd2a678

Browse files
committed
Add (set_)so_bindany for target_os=openbsd
1 parent 3e718cc commit bd2a678

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/sys/unix.rs

+34
Original file line numberDiff line numberDiff line change
@@ -3309,6 +3309,40 @@ impl crate::Socket {
33093309
)
33103310
}
33113311
}
3312+
3313+
/// Set the value of the `SO_BINDANY` option on this socket.
3314+
///
3315+
/// SO_BINDANY allows the socket to be bound to addresses which are not
3316+
/// local to the machine, so it can be used to make a transparent proxy.
3317+
/// Note that this option is limited to the superuser. In order to
3318+
/// receive packets for these addresses, SO_BINDANY needs to be combined
3319+
/// with matching outgoing pf(4) rules with the divert-reply parameter.
3320+
#[cfg(all(feature = "all", target_os = "openbsd"))]
3321+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "openbsd"))))]
3322+
pub fn set_so_bindany(&self, bindany: bool) -> io::Result<()> {
3323+
unsafe {
3324+
setsockopt(
3325+
self.as_raw(),
3326+
libc::IPPROTO_IP,
3327+
libc::SO_BINDANY,
3328+
bindany as c_int,
3329+
)
3330+
}
3331+
}
3332+
3333+
/// Get the value of the `IP_BINDANY` option on this socket.
3334+
///
3335+
/// For more information about this option, see [`set_ip_bindany`].
3336+
///
3337+
/// [`set_ip_bindany`]: crate::Socket::set_ip_bindany
3338+
#[cfg(all(feature = "all", target_os = "freebsd"))]
3339+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "freebsd"))))]
3340+
pub fn ip_bindany(&self) -> io::Result<bool> {
3341+
unsafe {
3342+
getsockopt::<c_int>(self.as_raw(), libc::IPPROTO_IP, libc::IP_BINDANY)
3343+
.map(|bindany| bindany != 0)
3344+
}
3345+
}
33123346
}
33133347

33143348
/// See [`Socket::dccp_available_ccids`].

tests/socket.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,12 @@ test!(
13991399
ip_bindany_v6,
14001400
set_ip_bindany_v6(true)
14011401
);
1402+
#[cfg(all(feature = "all", target_os = "openbsd"))]
1403+
test!(
1404+
#[ignore = "setting `SO_BINDANY` is limited to the superuser"]
1405+
so_bindany,
1406+
set_so_bindany(true)
1407+
);
14021408
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
14031409
test!(
14041410
#[ignore = "setting `SO_MARK` requires the `CAP_NET_ADMIN` capability (works when running as root)"]

0 commit comments

Comments
 (0)