Skip to content

Commit 72f141f

Browse files
committed
std::net: adding unix_socket_exclbind feature for solaris/illumos.
allows to have a tigher control over the binding exclusivness of the socket.
1 parent a4b11c8 commit 72f141f

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

library/std/src/os/illumos/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#![stable(feature = "raw_ext", since = "1.1.0")]
44

55
pub mod fs;
6+
pub mod net;
67
pub mod raw;

library/std/src/os/illumos/net.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//! illumos-specific networking functionality.
2+
3+
#![unstable(feature = "unix_socket_exclbind", issue = "none")]
4+
5+
use crate::io;
6+
use crate::os::unix::net;
7+
use crate::sealed::Sealed;
8+
use crate::sys_common::AsInner;
9+
10+
/// illumos-specific functionality for `AF_UNIX` sockets [`UnixDatagram`]
11+
/// and [`UnixStream`].
12+
///
13+
/// [`UnixDatagram`]: net::UnixDatagram
14+
/// [`UnixStream`]: net::UnixStream
15+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
16+
pub trait UnixSocketExt: Sealed {
17+
/// Enables exclusive binding on the socket.
18+
///
19+
/// If true and if the socket had been set with `SO_REUSEADDR`,
20+
/// it neutralises its effect.
21+
/// See [`man 3 tcp`](https://docs.oracle.com/cd/E88353_01/html/E37843/setsockopt-3c.html)
22+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
23+
fn so_exclbind(&self, excl: bool) -> io::Result<()>;
24+
25+
/// Get the bind exclusivity bind state of the socket.
26+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
27+
fn exclbind(&self) -> io::Result<bool>;
28+
}
29+
30+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
31+
impl UnixSocketExt for net::UnixDatagram {
32+
fn exclbind(&self) -> io::Result<bool> {
33+
self.as_inner().exclbind()
34+
}
35+
36+
fn so_exclbind(&self, excl: bool) -> io::Result<()> {
37+
self.as_inner().set_exclbind(excl)
38+
}
39+
}
40+
41+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
42+
impl UnixSocketExt for net::UnixStream {
43+
fn exclbind(&self) -> io::Result<bool> {
44+
self.as_inner().exclbind()
45+
}
46+
47+
fn so_exclbind(&self, excl: bool) -> io::Result<()> {
48+
self.as_inner().set_exclbind(excl)
49+
}
50+
}

library/std/src/os/solaris/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#![stable(feature = "raw_ext", since = "1.1.0")]
44

55
pub mod fs;
6+
pub mod net;
67
pub mod raw;

library/std/src/os/solaris/net.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//! solaris-specific networking functionality.
2+
3+
#![unstable(feature = "unix_socket_exclbind", issue = "none")]
4+
5+
use crate::io;
6+
use crate::os::unix::net;
7+
use crate::sealed::Sealed;
8+
use crate::sys_common::AsInner;
9+
10+
/// solaris-specific functionality for `AF_UNIX` sockets [`UnixDatagram`]
11+
/// and [`UnixStream`].
12+
///
13+
/// [`UnixDatagram`]: net::UnixDatagram
14+
/// [`UnixStream`]: net::UnixStream
15+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
16+
pub trait UnixSocketExt: Sealed {
17+
/// Enables exclusive binding on the socket.
18+
///
19+
/// If true and if the socket had been set with `SO_REUSEADDR`,
20+
/// it neutralises its effect.
21+
/// See [`man 3 tcp`](https://docs.oracle.com/cd/E88353_01/html/E37843/setsockopt-3c.html)
22+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
23+
fn so_exclbind(&self, excl: bool) -> io::Result<()>;
24+
25+
/// Get the bind exclusivity bind state of the socket.
26+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
27+
fn exclbind(&self) -> io::Result<bool>;
28+
}
29+
30+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
31+
impl UnixSocketExt for net::UnixDatagram {
32+
fn exclbind(&self) -> io::Result<bool> {
33+
self.as_inner().exclbind()
34+
}
35+
36+
fn so_exclbind(&self, excl: bool) -> io::Result<()> {
37+
self.as_inner().set_exclbind(excl)
38+
}
39+
}
40+
41+
#[unstable(feature = "unix_socket_exclbind", issue = "none")]
42+
impl UnixSocketExt for net::UnixStream {
43+
fn exclbind(&self) -> io::Result<bool> {
44+
self.as_inner().exclbind()
45+
}
46+
47+
fn so_exclbind(&self, excl: bool) -> io::Result<()> {
48+
self.as_inner().set_exclbind(excl)
49+
}
50+
}

library/std/src/sys/pal/unix/net.rs

+15
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,21 @@ impl Socket {
484484
Ok(name)
485485
}
486486

487+
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
488+
pub fn set_exclbind(&self, excl: bool) -> io::Result<()> {
489+
// not yet on libc crate
490+
const SO_EXCLBIND: i32 = 0x1015;
491+
setsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND, excl)
492+
}
493+
494+
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
495+
pub fn exclbind(&self) -> io::Result<bool> {
496+
// not yet on libc crate
497+
const SO_EXCLBIND: i32 = 0x1015;
498+
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND)?;
499+
Ok(raw != 0)
500+
}
501+
487502
#[cfg(any(target_os = "android", target_os = "linux",))]
488503
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
489504
setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)

0 commit comments

Comments
 (0)