diff --git a/src/sys/unix/uds/listener.rs b/src/sys/unix/uds/listener.rs index 794a9f7bb..ff77c53bd 100644 --- a/src/sys/unix/uds/listener.rs +++ b/src/sys/unix/uds/listener.rs @@ -1,6 +1,5 @@ use super::socket_addr; -use super::SocketAddr; -use crate::net::UnixStream; +use crate::net::{SocketAddr, UnixStream}; use crate::sys::unix::net::new_socket; use std::os::unix::ffi::OsStrExt; use std::os::unix::io::{AsRawFd, FromRawFd}; diff --git a/src/sys/windows/stdnet/addr.rs b/src/sys/windows/stdnet/addr.rs index 88a96a024..3df201d93 100644 --- a/src/sys/windows/stdnet/addr.rs +++ b/src/sys/windows/stdnet/addr.rs @@ -67,7 +67,7 @@ cfg_os_poll! { } /// An address associated with a Unix socket. -pub struct SocketAddr { +pub(crate) struct SocketAddr { addr: SOCKADDR_UN, len: c_int, } @@ -121,6 +121,14 @@ impl SocketAddr { len, }) } + + pub(crate) fn raw_sockaddr(&self) -> &SOCKADDR_UN { + &self.addr + } + + pub(crate) fn raw_socklen(&self) -> c_int { + self.len + } } /// Returns the contents of this address if it is a `pathname` address. diff --git a/src/sys/windows/stdnet/listener.rs b/src/sys/windows/stdnet/listener.rs index 487aac9eb..a971cd2cb 100644 --- a/src/sys/windows/stdnet/listener.rs +++ b/src/sys/windows/stdnet/listener.rs @@ -47,6 +47,20 @@ cfg_os_poll! { }) } + /// Creates a new `UnixListener` bound to the specified address. + pub fn bind_addr(socket_addr: &SocketAddr) -> io::Result { + let inner = Socket::new()?; + + wsa_syscall!( + bind(inner.as_raw_socket() as _, &socket_addr.raw_sockaddr() as *const _ as *const _, socket_addr.raw_socklen() as _), + SOCKET_ERROR + )?; + wsa_syscall!(listen(inner.as_raw_socket() as _, 1024), SOCKET_ERROR)?; + Ok(UnixListener { + inner + }) + } + /// Accepts a new incoming connection to this listener. /// /// This function will block the calling thread until a new Unix connection diff --git a/src/sys/windows/stdnet/mod.rs b/src/sys/windows/stdnet/mod.rs index c1e1cc748..72c97e8d9 100644 --- a/src/sys/windows/stdnet/mod.rs +++ b/src/sys/windows/stdnet/mod.rs @@ -4,7 +4,7 @@ mod listener; mod socket; mod stream; -pub use self::addr::SocketAddr; +pub(crate) use self::addr::SocketAddr; pub use self::listener::UnixListener; pub use self::stream::UnixStream; diff --git a/src/sys/windows/stdnet/stream.rs b/src/sys/windows/stdnet/stream.rs index 9d78f5383..f433772a5 100644 --- a/src/sys/windows/stdnet/stream.rs +++ b/src/sys/windows/stdnet/stream.rs @@ -71,6 +71,25 @@ cfg_os_poll! { Ok(UnixStream(inner)) } + /// Connects to the socket named by `socker_addr`. + pub fn connect_addr(socket_addr: &SocketAddr) -> io::Result { + let inner = Socket::new()?; + + match wsa_syscall!( + connect( + inner.as_raw_socket() as _, + &socket_addr.raw_sockaddr() as *const _ as *const _, + socket_addr.raw_socklen(), + ), + SOCKET_ERROR + ) { + Ok(_) => {} + Err(ref err) if err.kind() == std::io::ErrorKind::Other => {} + Err(e) => return Err(e), + } + Ok(UnixStream(inner)) + } + /// Moves the socket into or out of nonblocking mode. pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { self.0.set_nonblocking(nonblocking) diff --git a/src/sys/windows/uds/listener.rs b/src/sys/windows/uds/listener.rs index f33312d50..84d96c179 100644 --- a/src/sys/windows/uds/listener.rs +++ b/src/sys/windows/uds/listener.rs @@ -2,8 +2,7 @@ use std::io; use std::os::windows::io::AsRawSocket; use std::path::Path; -use super::SocketAddr; -use crate::net::UnixStream; +use crate::net::{SocketAddr, UnixStream}; use crate::sys::windows::stdnet as net; pub(crate) fn bind(path: &Path) -> io::Result { @@ -12,6 +11,12 @@ pub(crate) fn bind(path: &Path) -> io::Result { Ok(listener) } +pub(crate) fn bind_addr(socket_addr: &SocketAddr) -> io::Result { + let listener = net::UnixListener::bind_addr(socket_addr)?; + listener.set_nonblocking(true)?; + Ok(listener) +} + pub(crate) fn accept(listener: &net::UnixListener) -> io::Result<(UnixStream, SocketAddr)> { listener.set_nonblocking(true)?; let (stream, addr) = listener.accept()?; diff --git a/src/sys/windows/uds/stream.rs b/src/sys/windows/uds/stream.rs index b02f32e8f..ae1a98e78 100644 --- a/src/sys/windows/uds/stream.rs +++ b/src/sys/windows/uds/stream.rs @@ -10,6 +10,12 @@ pub(crate) fn connect(path: &Path) -> io::Result { Ok(socket) } +pub(crate) fn connect_addr(socker_addr: &SocketAddr) -> io::Result { + let socket = net::UnixStream::connect_addr(socker_addr)?; + socket.set_nonblocking(true)?; + Ok(socket) +} + pub(crate) fn local_addr(socket: &net::UnixStream) -> io::Result { super::local_addr(socket.as_raw_socket()) }