Skip to content

Commit 457f753

Browse files
sullivan-seanKolbyML
authored andcommitted
add first pass implementation of windows uds
modify src/net for windows compatibility fix tests add docs back in cleanup remove log statements clean up selector clean up stream and listener sys logic fix re-registration add test for serial calls to listener.accept fix serial calls to accept remove tempfile dependency and fix doc tests revert change in draining behavior re-organize stdnet files to mirror std::os::unix::net use single syscall vectored approach from rust-lang/socket2 lint improve support across feature matrix fix doc tests use bcrypt instead of rand add -_ to random char set to avoid rejection sampling optimize rng syscall logic fix lint and fmt remove unused functions fmt simplify windows mod clean up tests fix indentation, imports, address other comments fmt remove unrelated code changes fix lint remove explicit SetHandleInformation calls abstract socketaddr behind common API in net fix lint add comment clarifying inheritance during calls to accept
1 parent 0757ec8 commit 457f753

File tree

28 files changed

+1249
-299
lines changed

28 files changed

+1249
-299
lines changed

src/net/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ pub use self::tcp::{TcpListener, TcpStream};
3232
mod udp;
3333
#[cfg(not(target_os = "wasi"))]
3434
pub use self::udp::UdpSocket;
35-
36-
#[cfg(unix)]
35+
#[cfg(not(target_os = "wasi"))]
3736
mod uds;
37+
#[cfg(not(target_os = "wasi"))]
38+
pub use self::uds::{SocketAddr, UnixListener, UnixStream};
39+
40+
#[cfg(not(target_os = "wasi"))]
41+
pub(crate) use self::uds::AddressKind;
42+
3843
#[cfg(unix)]
39-
pub use self::uds::{SocketAddr, UnixDatagram, UnixListener, UnixStream};
44+
pub use self::uds::UnixDatagram;

src/net/tcp/stream.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,49 +269,49 @@ impl TcpStream {
269269

270270
impl Read for TcpStream {
271271
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
272-
self.inner.do_io(|mut inner| inner.read(buf))
272+
self.inner.do_io(|inner| (&*inner).read(buf))
273273
}
274274

275275
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
276-
self.inner.do_io(|mut inner| inner.read_vectored(bufs))
276+
self.inner.do_io(|inner| (&*inner).read_vectored(bufs))
277277
}
278278
}
279279

280280
impl<'a> Read for &'a TcpStream {
281281
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
282-
self.inner.do_io(|mut inner| inner.read(buf))
282+
self.inner.do_io(|inner| (&*inner).read(buf))
283283
}
284284

285285
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
286-
self.inner.do_io(|mut inner| inner.read_vectored(bufs))
286+
self.inner.do_io(|inner| (&*inner).read_vectored(bufs))
287287
}
288288
}
289289

290290
impl Write for TcpStream {
291291
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
292-
self.inner.do_io(|mut inner| inner.write(buf))
292+
self.inner.do_io(|inner| (&*inner).write(buf))
293293
}
294294

295295
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
296-
self.inner.do_io(|mut inner| inner.write_vectored(bufs))
296+
self.inner.do_io(|inner| (&*inner).write_vectored(bufs))
297297
}
298298

299299
fn flush(&mut self) -> io::Result<()> {
300-
self.inner.do_io(|mut inner| inner.flush())
300+
self.inner.do_io(|inner| (&*inner).flush())
301301
}
302302
}
303303

304304
impl<'a> Write for &'a TcpStream {
305305
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
306-
self.inner.do_io(|mut inner| inner.write(buf))
306+
self.inner.do_io(|inner| (&*inner).write(buf))
307307
}
308308

309309
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
310-
self.inner.do_io(|mut inner| inner.write_vectored(bufs))
310+
self.inner.do_io(|inner| (&*inner).write_vectored(bufs))
311311
}
312312

313313
fn flush(&mut self) -> io::Result<()> {
314-
self.inner.do_io(|mut inner| inner.flush())
314+
self.inner.do_io(|inner| (&*inner).flush())
315315
}
316316
}
317317

src/net/uds/addr.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use crate::sys;
2+
use std::path::Path;
3+
use std::{ascii, fmt};
4+
5+
/// An address associated with a `mio` specific Unix socket.
6+
///
7+
/// This is implemented instead of imported from [`net::SocketAddr`] because
8+
/// there is no way to create a [`net::SocketAddr`]. One must be returned by
9+
/// [`accept`], so this is returned instead.
10+
///
11+
/// [`net::SocketAddr`]: std::os::unix::net::SocketAddr
12+
/// [`accept`]: #method.accept
13+
pub struct SocketAddr {
14+
inner: sys::SocketAddr,
15+
}
16+
17+
struct AsciiEscaped<'a>(&'a [u8]);
18+
19+
pub(crate) enum AddressKind<'a> {
20+
Unnamed,
21+
Pathname(&'a Path),
22+
Abstract(&'a [u8]),
23+
}
24+
25+
impl SocketAddr {
26+
pub(crate) fn new(inner: sys::SocketAddr) -> Self {
27+
SocketAddr { inner }
28+
}
29+
30+
fn address(&self) -> AddressKind<'_> {
31+
self.inner.address()
32+
}
33+
}
34+
35+
cfg_os_poll! {
36+
impl SocketAddr {
37+
/// Returns `true` if the address is unnamed.
38+
///
39+
/// Documentation reflected in [`SocketAddr`]
40+
///
41+
/// [`SocketAddr`]: std::os::unix::net::SocketAddr
42+
pub fn is_unnamed(&self) -> bool {
43+
matches!(self.address(), AddressKind::Unnamed)
44+
}
45+
46+
/// Returns the contents of this address if it is a `pathname` address.
47+
///
48+
/// Documentation reflected in [`SocketAddr`]
49+
///
50+
/// [`SocketAddr`]: std::os::unix::net::SocketAddr
51+
pub fn as_pathname(&self) -> Option<&Path> {
52+
if let AddressKind::Pathname(path) = self.address() {
53+
Some(path)
54+
} else {
55+
None
56+
}
57+
}
58+
59+
/// Returns the contents of this address if it is an abstract namespace
60+
/// without the leading null byte.
61+
// Link to std::os::unix::net::SocketAddr pending
62+
// https://github.com/rust-lang/rust/issues/85410.
63+
pub fn as_abstract_namespace(&self) -> Option<&[u8]> {
64+
if let AddressKind::Abstract(path) = self.address() {
65+
Some(path)
66+
} else {
67+
None
68+
}
69+
}
70+
}
71+
}
72+
73+
impl fmt::Debug for SocketAddr {
74+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
75+
write!(fmt, "{:?}", self.address())
76+
}
77+
}
78+
79+
impl fmt::Debug for AddressKind<'_> {
80+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
81+
match self {
82+
AddressKind::Unnamed => write!(fmt, "(unnamed)"),
83+
AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
84+
AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
85+
}
86+
}
87+
}
88+
89+
impl<'a> fmt::Display for AsciiEscaped<'a> {
90+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
91+
write!(fmt, "\"")?;
92+
for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
93+
write!(fmt, "{}", byte as char)?;
94+
}
95+
write!(fmt, "\"")
96+
}
97+
}

src/net/uds/datagram.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::io_source::IoSource;
2+
use crate::net::SocketAddr;
23
use crate::{event, sys, Interest, Registry, Token};
34

45
use std::net::Shutdown;
@@ -54,24 +55,25 @@ impl UnixDatagram {
5455
}
5556

5657
/// Returns the address of this socket.
57-
pub fn local_addr(&self) -> io::Result<sys::SocketAddr> {
58-
sys::uds::datagram::local_addr(&self.inner)
58+
pub fn local_addr(&self) -> io::Result<SocketAddr> {
59+
sys::uds::datagram::local_addr(&self.inner).map(SocketAddr::new)
5960
}
6061

6162
/// Returns the address of this socket's peer.
6263
///
6364
/// The `connect` method will connect the socket to a peer.
64-
pub fn peer_addr(&self) -> io::Result<sys::SocketAddr> {
65-
sys::uds::datagram::peer_addr(&self.inner)
65+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
66+
sys::uds::datagram::peer_addr(&self.inner).map(SocketAddr::new)
6667
}
6768

6869
/// Receives data from the socket.
6970
///
7071
/// On success, returns the number of bytes read and the address from
7172
/// whence the data came.
72-
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, sys::SocketAddr)> {
73+
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
7374
self.inner
7475
.do_io(|inner| sys::uds::datagram::recv_from(inner, buf))
76+
.map(|(nread, addr)| (nread, SocketAddr::new(addr)))
7577
}
7678

7779
/// Receives data from the socket.

src/net/uds/listener.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ use crate::io_source::IoSource;
22
use crate::net::{SocketAddr, UnixStream};
33
use crate::{event, sys, Interest, Registry, Token};
44

5+
#[cfg(windows)]
6+
use crate::sys::windows::stdnet as net;
7+
#[cfg(unix)]
58
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
9+
#[cfg(unix)]
610
use std::os::unix::net;
11+
#[cfg(windows)]
12+
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
713
use std::path::Path;
814
use std::{fmt, io};
915

@@ -24,23 +30,34 @@ impl UnixListener {
2430
/// standard library in the Mio equivalent. The conversion assumes nothing
2531
/// about the underlying listener; it is left up to the user to set it in
2632
/// non-blocking mode.
33+
#[cfg(unix)]
34+
#[cfg_attr(docsrs, doc(cfg(unix)))]
2735
pub fn from_std(listener: net::UnixListener) -> UnixListener {
2836
UnixListener {
2937
inner: IoSource::new(listener),
3038
}
3139
}
3240

41+
#[cfg(windows)]
42+
pub(crate) fn from_std(listener: net::UnixListener) -> UnixListener {
43+
UnixListener {
44+
inner: IoSource::new(listener),
45+
}
46+
}
47+
3348
/// Accepts a new incoming connection to this listener.
3449
///
3550
/// The call is responsible for ensuring that the listening socket is in
3651
/// non-blocking mode.
3752
pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
38-
sys::uds::listener::accept(&self.inner)
53+
self.inner
54+
.do_io(sys::uds::listener::accept)
55+
.map(|(stream, addr)| (stream, SocketAddr::new(addr)))
3956
}
4057

4158
/// Returns the local socket address of this listener.
42-
pub fn local_addr(&self) -> io::Result<sys::SocketAddr> {
43-
sys::uds::listener::local_addr(&self.inner)
59+
pub fn local_addr(&self) -> io::Result<SocketAddr> {
60+
sys::uds::listener::local_addr(&self.inner).map(SocketAddr::new)
4461
}
4562

4663
/// Returns the value of the `SO_ERROR` option.
@@ -79,18 +96,24 @@ impl fmt::Debug for UnixListener {
7996
}
8097
}
8198

99+
#[cfg(unix)]
100+
#[cfg_attr(docsrs, doc(cfg(unix)))]
82101
impl IntoRawFd for UnixListener {
83102
fn into_raw_fd(self) -> RawFd {
84103
self.inner.into_inner().into_raw_fd()
85104
}
86105
}
87106

107+
#[cfg(unix)]
108+
#[cfg_attr(docsrs, doc(cfg(unix)))]
88109
impl AsRawFd for UnixListener {
89110
fn as_raw_fd(&self) -> RawFd {
90111
self.inner.as_raw_fd()
91112
}
92113
}
93114

115+
#[cfg(unix)]
116+
#[cfg_attr(docsrs, doc(cfg(unix)))]
94117
impl FromRawFd for UnixListener {
95118
/// Converts a `RawFd` to a `UnixListener`.
96119
///
@@ -102,3 +125,27 @@ impl FromRawFd for UnixListener {
102125
UnixListener::from_std(FromRawFd::from_raw_fd(fd))
103126
}
104127
}
128+
129+
#[cfg(windows)]
130+
#[cfg_attr(docsrs, doc(cfg(windows)))]
131+
impl IntoRawSocket for UnixListener {
132+
fn into_raw_socket(self) -> RawSocket {
133+
self.inner.into_inner().into_raw_socket()
134+
}
135+
}
136+
137+
#[cfg(windows)]
138+
#[cfg_attr(docsrs, doc(cfg(windows)))]
139+
impl AsRawSocket for UnixListener {
140+
fn as_raw_socket(&self) -> RawSocket {
141+
self.inner.as_raw_socket()
142+
}
143+
}
144+
145+
#[cfg(windows)]
146+
#[cfg_attr(docsrs, doc(cfg(windows)))]
147+
impl FromRawSocket for UnixListener {
148+
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
149+
UnixListener::from_std(FromRawSocket::from_raw_socket(sock))
150+
}
151+
}

src/net/uds/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#[cfg(unix)]
12
mod datagram;
3+
#[cfg(unix)]
4+
#[cfg_attr(docsrs, doc(cfg(unix)))]
25
pub use self::datagram::UnixDatagram;
36

47
mod listener;
@@ -7,4 +10,6 @@ pub use self::listener::UnixListener;
710
mod stream;
811
pub use self::stream::UnixStream;
912

10-
pub use crate::sys::SocketAddr;
13+
mod addr;
14+
pub(crate) use self::addr::AddressKind;
15+
pub use self::addr::SocketAddr;

0 commit comments

Comments
 (0)