Skip to content

Commit 4f22a6b

Browse files
committed
Add (set_)ip_bindany_v4 for target_os=freebsd
1 parent 309d17b commit 4f22a6b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/sys/unix.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,6 +3239,41 @@ impl crate::Socket {
32393239
)
32403240
}
32413241
}
3242+
3243+
/// Get the value of the `IP_BINDANY` option on this socket.
3244+
///
3245+
/// For more information about this option, see [`set_ip_bindany`].
3246+
///
3247+
/// [`set_ip_bindany`]: crate::Socket::set_ip_bindany
3248+
#[cfg(all(feature = "all", target_os = "freebsd"))]
3249+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "freebsd"))))]
3250+
pub fn ip_bindany_v4(&self) -> io::Result<bool> {
3251+
unsafe {
3252+
getsockopt::<c_int>(self.as_raw(), libc::IPPROTO_IP, libc::IP_BINDANY)
3253+
.map(|bindany| bindany != 0)
3254+
}
3255+
}
3256+
3257+
/// Set the value of the `IP_BINDANY` option on this socket.
3258+
///
3259+
/// If the IP_BINDANY option is enabled on a SOCK_STREAM, SOCK_DGRAM or a
3260+
/// SOCK_RAW socket, one can bind(2) to any address, even one not bound to
3261+
/// any available network interface in the system. This functionality (in
3262+
/// conjunction with special firewall rules) can be used for implementing a
3263+
/// transparent proxy. The PRIV_NETINET_BINDANY privilege is needed to set
3264+
/// this option.
3265+
#[cfg(all(feature = "all", target_os = "freebsd"))]
3266+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "freebsd"))))]
3267+
pub fn set_ip_bindany_v4(&self, bindany: bool) -> io::Result<()> {
3268+
unsafe {
3269+
setsockopt(
3270+
self.as_raw(),
3271+
libc::IPPROTO_IP,
3272+
libc::IP_BINDANY,
3273+
bindany as c_int,
3274+
)
3275+
}
3276+
}
32423277
}
32433278

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

tests/socket.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,12 @@ test!(
13871387
ip_transparent_v6,
13881388
set_ip_transparent_v6(true)
13891389
);
1390+
#[cfg(all(feature = "all", target_os = "freebsd"))]
1391+
test!(
1392+
#[ignore = "setting `IP_BINDANY` requires the `PRIV_NETINET_BINDANY` privilege (works when running as root)"]
1393+
ip_bindany_v4,
1394+
set_ip_bindany_v4(true)
1395+
);
13901396
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
13911397
test!(
13921398
#[ignore = "setting `SO_MARK` requires the `CAP_NET_ADMIN` capability (works when running as root)"]

0 commit comments

Comments
 (0)