1 #![deny(unsafe_op_in_unsafe_fn)]
6 use crate::io
::{self, BorrowedCursor, IoSlice, IoSliceMut}
;
7 use crate::net
::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr}
;
8 use crate::os
::wasi
::io
::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd}
;
9 use crate::sys
::unsupported
;
10 use crate::sys_common
::{AsInner, FromInner, IntoInner}
;
11 use crate::time
::Duration
;
13 pub struct Socket(WasiFd
);
15 pub struct TcpStream
{
19 impl AsInner
<WasiFd
> for Socket
{
20 fn as_inner(&self) -> &WasiFd
{
25 impl IntoInner
<WasiFd
> for Socket
{
26 fn into_inner(self) -> WasiFd
{
31 impl FromInner
<WasiFd
> for Socket
{
32 fn from_inner(inner
: WasiFd
) -> Socket
{
37 impl AsFd
for Socket
{
38 fn as_fd(&self) -> BorrowedFd
<'_
> {
43 impl AsRawFd
for Socket
{
44 fn as_raw_fd(&self) -> RawFd
{
49 impl IntoRawFd
for Socket
{
50 fn into_raw_fd(self) -> RawFd
{
55 impl FromRawFd
for Socket
{
56 unsafe fn from_raw_fd(raw_fd
: RawFd
) -> Self {
57 unsafe { Self(FromRawFd::from_raw_fd(raw_fd)) }
62 pub fn connect(_
: io
::Result
<&SocketAddr
>) -> io
::Result
<TcpStream
> {
66 pub fn connect_timeout(_
: &SocketAddr
, _
: Duration
) -> io
::Result
<TcpStream
> {
70 pub fn set_read_timeout(&self, _
: Option
<Duration
>) -> io
::Result
<()> {
74 pub fn set_write_timeout(&self, _
: Option
<Duration
>) -> io
::Result
<()> {
78 pub fn read_timeout(&self) -> io
::Result
<Option
<Duration
>> {
82 pub fn write_timeout(&self) -> io
::Result
<Option
<Duration
>> {
86 pub fn peek(&self, _
: &mut [u8]) -> io
::Result
<usize> {
90 pub fn read(&self, buf
: &mut [u8]) -> io
::Result
<usize> {
91 self.read_vectored(&mut [IoSliceMut
::new(buf
)])
94 pub fn read_buf(&self, buf
: BorrowedCursor
<'_
>) -> io
::Result
<()> {
95 self.socket().as_inner().read_buf(buf
)
98 pub fn read_vectored(&self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
99 self.socket().as_inner().read(bufs
)
102 pub fn is_read_vectored(&self) -> bool
{
106 pub fn write(&self, buf
: &[u8]) -> io
::Result
<usize> {
107 self.write_vectored(&[IoSlice
::new(buf
)])
110 pub fn write_vectored(&self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
111 self.socket().as_inner().write(bufs
)
114 pub fn is_write_vectored(&self) -> bool
{
118 pub fn peer_addr(&self) -> io
::Result
<SocketAddr
> {
122 pub fn socket_addr(&self) -> io
::Result
<SocketAddr
> {
126 pub fn shutdown(&self, how
: Shutdown
) -> io
::Result
<()> {
127 let wasi_how
= match how
{
128 Shutdown
::Read
=> wasi
::SDFLAGS_RD
,
129 Shutdown
::Write
=> wasi
::SDFLAGS_WR
,
130 Shutdown
::Both
=> wasi
::SDFLAGS_RD
| wasi
::SDFLAGS_WR
,
133 unsafe { wasi::sock_shutdown(self.socket().as_raw_fd() as _, wasi_how).map_err(err2io) }
136 pub fn duplicate(&self) -> io
::Result
<TcpStream
> {
140 pub fn set_linger(&self, _
: Option
<Duration
>) -> io
::Result
<()> {
144 pub fn linger(&self) -> io
::Result
<Option
<Duration
>> {
148 pub fn set_nodelay(&self, _
: bool
) -> io
::Result
<()> {
152 pub fn nodelay(&self) -> io
::Result
<bool
> {
156 pub fn set_ttl(&self, _
: u32) -> io
::Result
<()> {
160 pub fn ttl(&self) -> io
::Result
<u32> {
164 pub fn take_error(&self) -> io
::Result
<Option
<io
::Error
>> {
168 pub fn set_nonblocking(&self, state
: bool
) -> io
::Result
<()> {
169 let fdstat
= unsafe {
170 wasi
::fd_fdstat_get(self.socket().as_inner().as_raw_fd() as wasi
::Fd
).map_err(err2io
)?
173 let mut flags
= fdstat
.fs_flags
;
176 flags
|= wasi
::FDFLAGS_NONBLOCK
;
178 flags
&= !wasi
::FDFLAGS_NONBLOCK
;
182 wasi
::fd_fdstat_set_flags(self.socket().as_inner().as_raw_fd() as wasi
::Fd
, flags
)
187 pub fn socket(&self) -> &Socket
{
191 pub fn into_socket(self) -> Socket
{
196 impl FromInner
<Socket
> for TcpStream
{
197 fn from_inner(socket
: Socket
) -> TcpStream
{
198 TcpStream { inner: socket }
202 impl fmt
::Debug
for TcpStream
{
203 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
204 f
.debug_struct("TcpStream").field("fd", &self.inner
.as_raw_fd()).finish()
208 pub struct TcpListener
{
213 pub fn bind(_
: io
::Result
<&SocketAddr
>) -> io
::Result
<TcpListener
> {
217 pub fn socket_addr(&self) -> io
::Result
<SocketAddr
> {
221 pub fn accept(&self) -> io
::Result
<(TcpStream
, SocketAddr
)> {
223 wasi
::sock_accept(self.as_inner().as_inner().as_raw_fd() as _
, 0).map_err(err2io
)?
227 TcpStream
::from_inner(unsafe { Socket::from_raw_fd(fd as _) }
),
228 // WASI has no concept of SocketAddr yet
229 // return an unspecified IPv4Addr
230 SocketAddr
::new(Ipv4Addr
::UNSPECIFIED
.into(), 0),
234 pub fn duplicate(&self) -> io
::Result
<TcpListener
> {
238 pub fn set_ttl(&self, _
: u32) -> io
::Result
<()> {
242 pub fn ttl(&self) -> io
::Result
<u32> {
246 pub fn set_only_v6(&self, _
: bool
) -> io
::Result
<()> {
250 pub fn only_v6(&self) -> io
::Result
<bool
> {
254 pub fn take_error(&self) -> io
::Result
<Option
<io
::Error
>> {
258 pub fn set_nonblocking(&self, state
: bool
) -> io
::Result
<()> {
259 let fdstat
= unsafe {
260 wasi
::fd_fdstat_get(self.socket().as_inner().as_raw_fd() as wasi
::Fd
).map_err(err2io
)?
263 let mut flags
= fdstat
.fs_flags
;
266 flags
|= wasi
::FDFLAGS_NONBLOCK
;
268 flags
&= !wasi
::FDFLAGS_NONBLOCK
;
272 wasi
::fd_fdstat_set_flags(self.socket().as_inner().as_raw_fd() as wasi
::Fd
, flags
)
277 pub fn socket(&self) -> &Socket
{
281 pub fn into_socket(self) -> Socket
{
286 impl AsInner
<Socket
> for TcpListener
{
287 fn as_inner(&self) -> &Socket
{
292 impl IntoInner
<Socket
> for TcpListener
{
293 fn into_inner(self) -> Socket
{
298 impl FromInner
<Socket
> for TcpListener
{
299 fn from_inner(inner
: Socket
) -> TcpListener
{
300 TcpListener { inner }
304 impl fmt
::Debug
for TcpListener
{
305 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
306 f
.debug_struct("TcpListener").field("fd", &self.inner
.as_raw_fd()).finish()
310 pub struct UdpSocket
{
315 pub fn bind(_
: io
::Result
<&SocketAddr
>) -> io
::Result
<UdpSocket
> {
319 pub fn peer_addr(&self) -> io
::Result
<SocketAddr
> {
323 pub fn socket_addr(&self) -> io
::Result
<SocketAddr
> {
327 pub fn recv_from(&self, _
: &mut [u8]) -> io
::Result
<(usize, SocketAddr
)> {
331 pub fn peek_from(&self, _
: &mut [u8]) -> io
::Result
<(usize, SocketAddr
)> {
335 pub fn send_to(&self, _
: &[u8], _
: &SocketAddr
) -> io
::Result
<usize> {
339 pub fn duplicate(&self) -> io
::Result
<UdpSocket
> {
343 pub fn set_read_timeout(&self, _
: Option
<Duration
>) -> io
::Result
<()> {
347 pub fn set_write_timeout(&self, _
: Option
<Duration
>) -> io
::Result
<()> {
351 pub fn read_timeout(&self) -> io
::Result
<Option
<Duration
>> {
355 pub fn write_timeout(&self) -> io
::Result
<Option
<Duration
>> {
359 pub fn set_broadcast(&self, _
: bool
) -> io
::Result
<()> {
363 pub fn broadcast(&self) -> io
::Result
<bool
> {
367 pub fn set_multicast_loop_v4(&self, _
: bool
) -> io
::Result
<()> {
371 pub fn multicast_loop_v4(&self) -> io
::Result
<bool
> {
375 pub fn set_multicast_ttl_v4(&self, _
: u32) -> io
::Result
<()> {
379 pub fn multicast_ttl_v4(&self) -> io
::Result
<u32> {
383 pub fn set_multicast_loop_v6(&self, _
: bool
) -> io
::Result
<()> {
387 pub fn multicast_loop_v6(&self) -> io
::Result
<bool
> {
391 pub fn join_multicast_v4(&self, _
: &Ipv4Addr
, _
: &Ipv4Addr
) -> io
::Result
<()> {
395 pub fn join_multicast_v6(&self, _
: &Ipv6Addr
, _
: u32) -> io
::Result
<()> {
399 pub fn leave_multicast_v4(&self, _
: &Ipv4Addr
, _
: &Ipv4Addr
) -> io
::Result
<()> {
403 pub fn leave_multicast_v6(&self, _
: &Ipv6Addr
, _
: u32) -> io
::Result
<()> {
407 pub fn set_ttl(&self, _
: u32) -> io
::Result
<()> {
411 pub fn ttl(&self) -> io
::Result
<u32> {
415 pub fn take_error(&self) -> io
::Result
<Option
<io
::Error
>> {
419 pub fn set_nonblocking(&self, _
: bool
) -> io
::Result
<()> {
423 pub fn recv(&self, _
: &mut [u8]) -> io
::Result
<usize> {
427 pub fn peek(&self, _
: &mut [u8]) -> io
::Result
<usize> {
431 pub fn send(&self, _
: &[u8]) -> io
::Result
<usize> {
435 pub fn connect(&self, _
: io
::Result
<&SocketAddr
>) -> io
::Result
<()> {
439 pub fn socket(&self) -> &Socket
{
443 pub fn into_socket(self) -> Socket
{
448 impl AsInner
<Socket
> for UdpSocket
{
449 fn as_inner(&self) -> &Socket
{
454 impl IntoInner
<Socket
> for UdpSocket
{
455 fn into_inner(self) -> Socket
{
460 impl FromInner
<Socket
> for UdpSocket
{
461 fn from_inner(inner
: Socket
) -> UdpSocket
{
466 impl fmt
::Debug
for UdpSocket
{
467 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
468 f
.debug_struct("UdpSocket").field("fd", &self.inner
.as_raw_fd()).finish()
472 pub struct LookupHost(!);
475 pub fn port(&self) -> u16 {
480 impl Iterator
for LookupHost
{
481 type Item
= SocketAddr
;
482 fn next(&mut self) -> Option
<SocketAddr
> {
487 impl<'a
> TryFrom
<&'a
str> for LookupHost
{
488 type Error
= io
::Error
;
490 fn try_from(_v
: &'a
str) -> io
::Result
<LookupHost
> {
495 impl<'a
> TryFrom
<(&'a
str, u16)> for LookupHost
{
496 type Error
= io
::Error
;
498 fn try_from(_v
: (&'a
str, u16)) -> io
::Result
<LookupHost
> {
503 #[allow(nonstandard_style)]
505 pub const AF_INET
: u8 = 0;
506 pub const AF_INET6
: u8 = 1;
507 pub type sa_family_t
= u8;
509 #[derive(Copy, Clone)]
514 #[derive(Copy, Clone)]
515 pub struct sockaddr_in
{
516 pub sin_family
: sa_family_t
,
518 pub sin_addr
: in_addr
,
521 #[derive(Copy, Clone)]
522 pub struct in6_addr
{
523 pub s6_addr
: [u8; 16],
526 #[derive(Copy, Clone)]
527 pub struct sockaddr_in6
{
528 pub sin6_family
: sa_family_t
,
530 pub sin6_addr
: in6_addr
,
531 pub sin6_flowinfo
: u32,
532 pub sin6_scope_id
: u32,
535 #[derive(Copy, Clone)]
536 pub struct sockaddr {}