-
Notifications
You must be signed in to change notification settings - Fork 786
Adds support for UnixStream and UnixListener on Windows #1610
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
Closed
Closed
Changes from 7 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 bec570b
modify src/net for windows compatibility
sullivan-sean 63e50c3
fix tests
sullivan-sean 3591238
add docs back in
sullivan-sean 9015ca2
cleanup
sullivan-sean 7265833
remove log statements
sullivan-sean 3884bb5
clean up selector
sullivan-sean 5ca8952
clean up stream and listener sys logic
sullivan-sean 985a145
fix re-registration
sullivan-sean f5ec8ce
add test for serial calls to listener.accept
sullivan-sean cee5c6b
fix serial calls to accept
sullivan-sean 488254d
remove tempfile dependency and fix doc tests
sullivan-sean b6bae73
revert change in draining behavior
sullivan-sean 2113b9f
re-organize stdnet files to mirror std::os::unix::net
sullivan-sean 86c4c9a
use single syscall vectored approach from rust-lang/socket2
sullivan-sean 9f26286
lint
sullivan-sean 09a9b79
improve support across feature matrix
sullivan-sean bb914db
fix doc tests
sullivan-sean 648855d
use bcrypt instead of rand
sullivan-sean 3dd3c0f
add -_ to random char set to avoid rejection sampling
sullivan-sean 2283d39
optimize rng syscall logic
sullivan-sean 569de72
fix lint and fmt
sullivan-sean bdc6933
remove unused functions
sullivan-sean b07b4f1
fmt
sullivan-sean a2831ea
simplify windows mod
sullivan-sean 73d5fae
clean up tests
sullivan-sean 0baf112
fix indentation, imports, address other comments
sullivan-sean d9d4bb3
fmt
sullivan-sean 82b17e8
remove unrelated code changes
sullivan-sean 96e0735
fix lint
sullivan-sean 0e1b6df
remove explicit SetHandleInformation calls
sullivan-sean 77155cc
abstract socketaddr behind common API in net
sullivan-sean 26a060b
fix lint
sullivan-sean fd8ddc1
add comment clarifying inheritance during calls to accept
sullivan-sean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::{io, mem}; | ||
use std::convert::TryInto; | ||
use std::os::windows::io::{AsRawSocket, FromRawSocket}; | ||
use std::path::Path; | ||
use std::os::raw::c_int; | ||
use windows_sys::Win32::Networking::WinSock::{ | ||
self, | ||
SOCKET_ERROR, | ||
INVALID_SOCKET, | ||
bind as sys_bind, | ||
listen, | ||
accept as sys_accept | ||
}; | ||
|
||
use super::stdnet::{self as net, socket_addr}; | ||
use crate::net::{SocketAddr, UnixStream}; | ||
use crate::sys::windows::net::{init, new_socket}; | ||
|
||
pub(crate) fn bind(path: &Path) -> io::Result<net::UnixListener> { | ||
init(); | ||
let socket = new_socket(WinSock::AF_UNIX.into(), WinSock::SOCK_STREAM)?; | ||
let (sockaddr, socklen) = socket_addr(path)?; | ||
let sockaddr = &sockaddr as *const WinSock::sockaddr_un as *const WinSock::SOCKADDR; | ||
|
||
wsa_syscall!(sys_bind(socket, sockaddr, socklen as _), PartialEq::eq, SOCKET_ERROR) | ||
.and_then(|_| wsa_syscall!(listen(socket, 128), PartialEq::eq, SOCKET_ERROR)) | ||
.map_err(|err| { | ||
// Close the socket if we hit an error, ignoring the error from | ||
// closing since we can't pass back two errors. | ||
let _ = unsafe { WinSock::closesocket(socket) }; | ||
err | ||
}) | ||
.map(|_| unsafe { net::UnixListener::from_raw_socket(socket.try_into().unwrap()) }) | ||
} | ||
|
||
pub(crate) fn accept(listener: &net::UnixListener) -> io::Result<(UnixStream, SocketAddr)> { | ||
let sockaddr = mem::MaybeUninit::<WinSock::sockaddr_un>::zeroed(); | ||
|
||
// This is safe to assume because a `WinSock::sockaddr_un` filled with `0` | ||
// bytes is properly initialized. | ||
// | ||
// `0` is a valid value for `sockaddr_un::sun_family`; it is | ||
// `WinSock::AF_UNSPEC`. | ||
// | ||
// `[0; 108]` is a valid value for `sockaddr_un::sun_path`; it begins an | ||
// abstract path. | ||
let mut sockaddr = unsafe { sockaddr.assume_init() }; | ||
|
||
sockaddr.sun_family = WinSock::AF_UNIX; | ||
let mut socklen = mem::size_of_val(&sockaddr) as c_int; | ||
|
||
let socket = wsa_syscall!( | ||
sys_accept( | ||
listener.as_raw_socket().try_into().unwrap(), | ||
&mut sockaddr as *mut WinSock::sockaddr_un as *mut WinSock::SOCKADDR, | ||
&mut socklen as _ | ||
), | ||
PartialEq::eq, | ||
INVALID_SOCKET | ||
); | ||
|
||
socket | ||
.map(|socket| unsafe { net::UnixStream::from_raw_socket(socket.try_into().unwrap()) }) | ||
.map(UnixStream::from_std) | ||
.map(|stream| (stream, SocketAddr::from_parts(sockaddr, socklen))) | ||
} | ||
|
||
pub(crate) fn local_addr(listener: &net::UnixListener) -> io::Result<SocketAddr> { | ||
super::local_addr(listener.as_raw_socket()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
pub mod stdnet; | ||
pub use self::stdnet::SocketAddr; | ||
|
||
cfg_os_poll! { | ||
use std::convert::TryInto; | ||
use windows_sys::Win32::Networking::WinSock::{ | ||
getsockname, | ||
getpeername, | ||
SOCKET_ERROR | ||
}; | ||
use std::os::windows::io::RawSocket; | ||
use std::io; | ||
|
||
pub(crate) mod listener; | ||
pub(crate) mod stream; | ||
|
||
pub(crate) fn local_addr(socket: RawSocket) -> io::Result<SocketAddr> { | ||
SocketAddr::new(|sockaddr, socklen| { | ||
wsa_syscall!( | ||
getsockname(socket.try_into().unwrap(), sockaddr, socklen), | ||
PartialEq::eq, | ||
SOCKET_ERROR | ||
) | ||
}) | ||
} | ||
|
||
pub(crate) fn peer_addr(socket: RawSocket) -> io::Result<SocketAddr> { | ||
SocketAddr::new(|sockaddr, socklen| { | ||
wsa_syscall!( | ||
getpeername(socket.try_into().unwrap(), sockaddr, socklen), | ||
PartialEq::eq, | ||
SOCKET_ERROR | ||
) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.