Skip to content

Commit 9ec630d

Browse files
committed
Add (set_)so_bindany for target_os=openbsd
1 parent 75e4138 commit 9ec630d

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

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

tests/socket.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,12 @@ test!(
13901390
ip_bindany_v6,
13911391
set_ip_bindany_v6(true)
13921392
);
1393+
#[cfg(all(feature = "all", target_os = "openbsd"))]
1394+
test!(
1395+
#[ignore = "setting `SO_BINDANY` is limited to the superuser"]
1396+
so_bindany,
1397+
set_so_bindany(true)
1398+
);
13931399
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
13941400
test!(
13951401
#[ignore = "setting `SO_MARK` requires the `CAP_NET_ADMIN` capability (works when running as root)"]

0 commit comments

Comments
 (0)