Skip to content

Commit adc2b3a

Browse files
committed
Deprecate (set_)ip_freebind(_ipv6) in favour of (set_)ip_bindany_(v4|v6)
1 parent 99d9851 commit adc2b3a

File tree

2 files changed

+119
-77
lines changed

2 files changed

+119
-77
lines changed

src/sys/unix.rs

Lines changed: 102 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,11 +2285,7 @@ impl crate::Socket {
22852285
}
22862286
}
22872287

2288-
/// Get the value of the `IP_FREEBIND` option on this socket.
2289-
///
2290-
/// For more information about this option, see [`set_freebind`].
2291-
///
2292-
/// [`set_freebind`]: crate::Socket::set_freebind
2288+
/// This method is deprecated, use [`crate::Socket::ip_bindany_v4`].
22932289
#[cfg(all(
22942290
feature = "all",
22952291
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
@@ -2301,20 +2297,12 @@ impl crate::Socket {
23012297
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
23022298
)))
23032299
)]
2300+
#[deprecated = "Use `Socket::ip_bindany_v4` instead"]
23042301
pub fn freebind(&self) -> io::Result<bool> {
2305-
unsafe {
2306-
getsockopt::<c_int>(self.as_raw(), libc::SOL_IP, libc::IP_FREEBIND)
2307-
.map(|freebind| freebind != 0)
2308-
}
2302+
self.ip_bindany_v4()
23092303
}
23102304

2311-
/// Set value for the `IP_FREEBIND` option on this socket.
2312-
///
2313-
/// If enabled, this boolean option allows binding to an IP address that is
2314-
/// nonlocal or does not (yet) exist. This permits listening on a socket,
2315-
/// without requiring the underlying network interface or the specified
2316-
/// dynamic IP address to be up at the time that the application is trying
2317-
/// to bind to it.
2305+
/// This method is deprecated, use [`crate::Socket::set_ip_bindany_v4`].
23182306
#[cfg(all(
23192307
feature = "all",
23202308
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
@@ -2326,80 +2314,31 @@ impl crate::Socket {
23262314
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
23272315
)))
23282316
)]
2317+
#[deprecated = "Use `Socket::set_ip_bindany_v4` instead"]
23292318
pub fn set_freebind(&self, freebind: bool) -> io::Result<()> {
2330-
unsafe {
2331-
setsockopt(
2332-
self.as_raw(),
2333-
libc::SOL_IP,
2334-
libc::IP_FREEBIND,
2335-
freebind as c_int,
2336-
)
2337-
}
2319+
self.set_ip_bindany_v4(freebind)
23382320
}
23392321

2340-
/// Get the value of the `IPV6_FREEBIND` option on this socket.
2341-
///
2342-
/// This is an IPv6 counterpart of `IP_FREEBIND` socket option on
2343-
/// Android/Linux. For more information about this option, see
2344-
/// [`set_freebind`].
2345-
///
2346-
/// [`set_freebind`]: crate::Socket::set_freebind
2322+
/// This method is deprecated, use [`crate::Socket::ip_bindany_v6`].
23472323
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
23482324
#[cfg_attr(
23492325
docsrs,
23502326
doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
23512327
)]
2328+
#[deprecated = "Use `Socket::ip_bindany_v6` instead"]
23522329
pub fn freebind_ipv6(&self) -> io::Result<bool> {
2353-
unsafe {
2354-
getsockopt::<c_int>(self.as_raw(), libc::SOL_IPV6, libc::IPV6_FREEBIND)
2355-
.map(|freebind| freebind != 0)
2356-
}
2330+
self.ip_bindany_v6()
23572331
}
23582332

2359-
/// Set value for the `IPV6_FREEBIND` option on this socket.
2360-
///
2361-
/// This is an IPv6 counterpart of `IP_FREEBIND` socket option on
2362-
/// Android/Linux. For more information about this option, see
2363-
/// [`set_freebind`].
2364-
///
2365-
/// [`set_freebind`]: crate::Socket::set_freebind
2366-
///
2367-
/// # Examples
2368-
///
2369-
/// On Linux:
2370-
///
2371-
/// ```
2372-
/// use socket2::{Domain, Socket, Type};
2373-
/// use std::io::{self, Error, ErrorKind};
2374-
///
2375-
/// fn enable_freebind(socket: &Socket) -> io::Result<()> {
2376-
/// match socket.domain()? {
2377-
/// Domain::IPV4 => socket.set_freebind(true)?,
2378-
/// Domain::IPV6 => socket.set_freebind_ipv6(true)?,
2379-
/// _ => return Err(Error::new(ErrorKind::Other, "unsupported domain")),
2380-
/// };
2381-
/// Ok(())
2382-
/// }
2383-
///
2384-
/// # fn main() -> io::Result<()> {
2385-
/// # let socket = Socket::new(Domain::IPV6, Type::STREAM, None)?;
2386-
/// # enable_freebind(&socket)
2387-
/// # }
2388-
/// ```
2333+
/// This method is deprecated, use [`crate::Socket::set_ip_bindany_v6`].
23892334
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
23902335
#[cfg_attr(
23912336
docsrs,
23922337
doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
23932338
)]
2339+
#[deprecated = "Use `Socket::set_ip_bindany_v6` instead"]
23942340
pub fn set_freebind_ipv6(&self, freebind: bool) -> io::Result<()> {
2395-
unsafe {
2396-
setsockopt(
2397-
self.as_raw(),
2398-
libc::SOL_IPV6,
2399-
libc::IPV6_FREEBIND,
2400-
freebind as c_int,
2401-
)
2402-
}
2341+
self.set_ip_bindany_v6(freebind)
24032342
}
24042343

24052344
/// Get the value for the `SO_ORIGINAL_DST` option on this socket.
@@ -3239,6 +3178,96 @@ impl crate::Socket {
32393178
)
32403179
}
32413180
}
3181+
3182+
/// Get the value of the bind-any-like option on this socket.
3183+
/// The option is that `IP_FREEBIND` on Android/Fuchisa/Linux,
3184+
/// `IP_BINDANY` on FreeBSD and `SO_BINDANY` on FreeBSD.
3185+
///
3186+
/// For more information about this option, see [`set_ip_bindany_v4`].
3187+
///
3188+
/// [`set_ip_bindany_v4`]: crate::Socket::set_ip_bindany_v4
3189+
#[cfg(all(
3190+
feature = "all",
3191+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
3192+
))]
3193+
#[cfg_attr(
3194+
docsrs,
3195+
doc(cfg(all(
3196+
feature = "all",
3197+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
3198+
)))
3199+
)]
3200+
pub fn ip_bindany_v4(&self) -> io::Result<bool> {
3201+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
3202+
let (level, opt) = (libc::SOL_IP, libc::IP_FREEBIND);
3203+
3204+
unsafe { getsockopt::<c_int>(self.as_raw(), level, opt).map(|bindany| bindany != 0) }
3205+
}
3206+
3207+
/// Set the value of the bind-any-like option on this socket.
3208+
/// The option is that `IP_FREEBIND` on Android/Fuchisa/Linux,
3209+
/// `IP_BINDANY` on FreeBSD and `SO_BINDANY` on FreeBSD.
3210+
///
3211+
/// The option allows the socket to be bound to addresses which are not
3212+
/// nonlocal. This permits listening on a socket, without requiring the
3213+
/// underlying network interface or the specified dynamic IP address to
3214+
/// be up at the time that the application is trying to bind to it.
3215+
#[cfg(all(
3216+
feature = "all",
3217+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
3218+
))]
3219+
#[cfg_attr(
3220+
docsrs,
3221+
doc(cfg(all(
3222+
feature = "all",
3223+
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
3224+
)))
3225+
)]
3226+
pub fn set_ip_bindany_v4(&self, bindany: bool) -> io::Result<()> {
3227+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
3228+
let (level, opt) = (libc::SOL_IP, libc::IP_FREEBIND);
3229+
3230+
unsafe { setsockopt(self.as_raw(), level, opt, bindany as c_int) }
3231+
}
3232+
3233+
/// Get the value of the bind-any-like option on this socket.
3234+
/// The option is that `IPV6_FREEBIND` on Android/Linux,
3235+
/// `IPV6_BINDANY` on FreeBSD and `SO_BINDANY` on FreeBSD.
3236+
///
3237+
/// For more information about this option, see [`set_ip_bindany_v6`].
3238+
///
3239+
/// [`set_ip_bindany_v6`]: crate::Socket::set_ip_bindany_v6
3240+
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
3241+
#[cfg_attr(
3242+
docsrs,
3243+
doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
3244+
)]
3245+
pub fn ip_bindany_v6(&self) -> io::Result<bool> {
3246+
#[cfg(any(target_os = "android", target_os = "linux"))]
3247+
let (level, opt) = (libc::SOL_IPV6, libc::IPV6_FREEBIND);
3248+
3249+
unsafe { getsockopt::<c_int>(self.as_raw(), level, opt).map(|bindany| bindany != 0) }
3250+
}
3251+
3252+
/// Set the value of the bind-any-like option on this socket.
3253+
/// The option is that `IPV6_FREEBIND` on Android/Linux,
3254+
/// `IPV6_BINDANY` on FreeBSD and `SO_BINDANY` on FreeBSD.
3255+
///
3256+
/// This is an IPv6 counterpart of `set_ip_bindany_v4` on a socket.
3257+
/// For more information about this option, see [`set_ip_bindany_v4`].
3258+
///
3259+
/// [`set_ip_bindany_v4`]: crate::Socket::set_ip_bindany_v4
3260+
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
3261+
#[cfg_attr(
3262+
docsrs,
3263+
doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
3264+
)]
3265+
pub fn set_ip_bindany_v6(&self, bindany: bool) -> io::Result<()> {
3266+
#[cfg(any(target_os = "android", target_os = "linux"))]
3267+
let (level, opt) = (libc::SOL_IPV6, libc::IPV6_FREEBIND);
3268+
3269+
unsafe { setsockopt(self.as_raw(), level, opt, bindany as c_int) }
3270+
}
32423271
}
32433272

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

tests/socket.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,10 +1405,23 @@ test!(
14051405
set_read_timeout(Some(Duration::from_secs(10)))
14061406
);
14071407
test!(keepalive, set_keepalive(true));
1408-
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
1409-
test!(freebind, set_freebind(true));
1410-
#[cfg(all(feature = "all", target_os = "linux"))]
1411-
test!(IPv6 freebind_ipv6, set_freebind_ipv6(true));
1408+
#[cfg(all(
1409+
feature = "all",
1410+
any(
1411+
target_os = "android",
1412+
target_os = "fuchsia",
1413+
target_os = "linux"
1414+
)
1415+
))]
1416+
test!(ip_bindany_v4, set_ip_bindany_v4(true));
1417+
#[cfg(all(
1418+
feature = "all",
1419+
any(
1420+
target_os = "android",
1421+
target_os = "linux"
1422+
)
1423+
))]
1424+
test!(IPv6 ip_bindany_v6, set_ip_bindany_v6(true));
14121425

14131426
test!(IPv4 ttl, set_ttl(40));
14141427

0 commit comments

Comments
 (0)