Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for UnixStream and UnixListener on Windows #1610

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7f69b97
add first pass implementation of windows uds
sullivan-sean Aug 15, 2022
bec570b
modify src/net for windows compatibility
sullivan-sean Aug 15, 2022
63e50c3
fix tests
sullivan-sean Aug 16, 2022
3591238
add docs back in
sullivan-sean Aug 16, 2022
9015ca2
cleanup
sullivan-sean Aug 16, 2022
7265833
remove log statements
sullivan-sean Aug 16, 2022
3884bb5
clean up selector
sullivan-sean Aug 16, 2022
5ca8952
clean up stream and listener sys logic
sullivan-sean Aug 18, 2022
985a145
fix re-registration
sullivan-sean Aug 18, 2022
f5ec8ce
add test for serial calls to listener.accept
sullivan-sean Aug 18, 2022
cee5c6b
fix serial calls to accept
sullivan-sean Aug 18, 2022
488254d
remove tempfile dependency and fix doc tests
sullivan-sean Aug 18, 2022
b6bae73
revert change in draining behavior
sullivan-sean Aug 20, 2022
2113b9f
re-organize stdnet files to mirror std::os::unix::net
sullivan-sean Aug 21, 2022
86c4c9a
use single syscall vectored approach from rust-lang/socket2
sullivan-sean Aug 21, 2022
9f26286
lint
sullivan-sean Aug 21, 2022
09a9b79
improve support across feature matrix
sullivan-sean Aug 21, 2022
bb914db
fix doc tests
sullivan-sean Aug 22, 2022
648855d
use bcrypt instead of rand
sullivan-sean Aug 22, 2022
3dd3c0f
add -_ to random char set to avoid rejection sampling
sullivan-sean Aug 22, 2022
2283d39
optimize rng syscall logic
sullivan-sean Aug 22, 2022
569de72
fix lint and fmt
sullivan-sean Aug 22, 2022
bdc6933
remove unused functions
sullivan-sean Aug 22, 2022
b07b4f1
fmt
sullivan-sean Aug 26, 2022
a2831ea
simplify windows mod
sullivan-sean Aug 26, 2022
73d5fae
clean up tests
sullivan-sean Aug 26, 2022
0baf112
fix indentation, imports, address other comments
sullivan-sean Aug 26, 2022
d9d4bb3
fmt
sullivan-sean Aug 26, 2022
82b17e8
remove unrelated code changes
sullivan-sean Sep 12, 2022
96e0735
fix lint
sullivan-sean Sep 12, 2022
0e1b6df
remove explicit SetHandleInformation calls
sullivan-sean Sep 13, 2022
77155cc
abstract socketaddr behind common API in net
sullivan-sean Sep 13, 2022
26a060b
fix lint
sullivan-sean Sep 13, 2022
fd8ddc1
add comment clarifying inheritance during calls to accept
sullivan-sean Oct 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ features = [
"Win32_Storage_FileSystem", # Enables NtCreateFile
"Win32_Foundation", # Basic types eg HANDLE
"Win32_Networking_WinSock", # winsock2 types/functions
"Win32_Security_Cryptography", # Random number generation
"Win32_System_IO", # IO types like OVERLAPPED etc
"Win32_System_WindowsProgramming", # General future used for various types/funcs
]
Expand Down
8 changes: 5 additions & 3 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ pub use self::tcp::{TcpListener, TcpStream};
mod udp;
#[cfg(not(target_os = "wasi"))]
pub use self::udp::UdpSocket;

#[cfg(unix)]
#[cfg(not(target_os = "wasi"))]
mod uds;
#[cfg(not(target_os = "wasi"))]
pub use self::uds::{SocketAddr, UnixListener, UnixStream};

#[cfg(unix)]
pub use self::uds::{SocketAddr, UnixDatagram, UnixListener, UnixStream};
pub use self::uds::UnixDatagram;
20 changes: 10 additions & 10 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,49 +269,49 @@ impl TcpStream {

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

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

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

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

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

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

fn flush(&mut self) -> io::Result<()> {
self.inner.do_io(|inner| (&*inner).flush())
self.inner.do_io(|mut inner| inner.flush())
}
}

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

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

fn flush(&mut self) -> io::Result<()> {
self.inner.do_io(|inner| (&*inner).flush())
self.inner.do_io(|mut inner| inner.flush())
}
}

Expand Down
40 changes: 39 additions & 1 deletion src/net/uds/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ use crate::io_source::IoSource;
use crate::net::{SocketAddr, UnixStream};
use crate::{event, sys, Interest, Registry, Token};

#[cfg(windows)]
use crate::sys::windows::stdnet as net;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(unix)]
use std::os::unix::net;
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::path::Path;
use std::{fmt, io};

Expand All @@ -24,18 +30,26 @@ impl UnixListener {
/// standard library in the Mio equivalent. The conversion assumes nothing
/// about the underlying listener; it is left up to the user to set it in
/// non-blocking mode.
#[cfg(unix)]
sullivan-sean marked this conversation as resolved.
Show resolved Hide resolved
pub fn from_std(listener: net::UnixListener) -> UnixListener {
UnixListener {
inner: IoSource::new(listener),
}
}

#[cfg(windows)]
pub(crate) fn from_std(listener: net::UnixListener) -> UnixListener {
UnixListener {
inner: IoSource::new(listener),
}
}

/// Accepts a new incoming connection to this listener.
///
/// The call is responsible for ensuring that the listening socket is in
/// non-blocking mode.
pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
sys::uds::listener::accept(&self.inner)
self.inner.do_io(sys::uds::listener::accept)
}

/// Returns the local socket address of this listener.
Expand Down Expand Up @@ -79,18 +93,21 @@ impl fmt::Debug for UnixListener {
}
}

#[cfg(unix)]
sullivan-sean marked this conversation as resolved.
Show resolved Hide resolved
impl IntoRawFd for UnixListener {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(unix)]
impl AsRawFd for UnixListener {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(unix)]
impl FromRawFd for UnixListener {
/// Converts a `RawFd` to a `UnixListener`.
///
Expand All @@ -102,3 +119,24 @@ impl FromRawFd for UnixListener {
UnixListener::from_std(FromRawFd::from_raw_fd(fd))
}
}

#[cfg(windows)]
impl IntoRawSocket for UnixListener {
fn into_raw_socket(self) -> RawSocket {
self.inner.into_inner().into_raw_socket()
}
}

#[cfg(windows)]
impl AsRawSocket for UnixListener {
fn as_raw_socket(&self) -> RawSocket {
self.inner.as_raw_socket()
}
}

#[cfg(windows)]
impl FromRawSocket for UnixListener {
unsafe fn from_raw_socket(sock: RawSocket) -> Self {
UnixListener::from_std(FromRawSocket::from_raw_socket(sock))
}
}
2 changes: 2 additions & 0 deletions src/net/uds/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(unix)]
mod datagram;
#[cfg(unix)]
sullivan-sean marked this conversation as resolved.
Show resolved Hide resolved
pub use self::datagram::UnixDatagram;

mod listener;
Expand Down
Loading