]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/wasi/net.rs
Rollup merge of #103945 - H4x5:remove-iter-empty-hack, r=compiler-errors
[rust.git] / library / std / src / sys / wasi / net.rs
1 #![deny(unsafe_op_in_unsafe_fn)]
2
3 use super::err2io;
4 use super::fd::WasiFd;
5 use crate::fmt;
6 use crate::io::{self, 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;
12
13 pub struct Socket(WasiFd);
14
15 pub struct TcpStream {
16     inner: Socket,
17 }
18
19 impl AsInner<WasiFd> for Socket {
20     fn as_inner(&self) -> &WasiFd {
21         &self.0
22     }
23 }
24
25 impl IntoInner<WasiFd> for Socket {
26     fn into_inner(self) -> WasiFd {
27         self.0
28     }
29 }
30
31 impl FromInner<WasiFd> for Socket {
32     fn from_inner(inner: WasiFd) -> Socket {
33         Socket(inner)
34     }
35 }
36
37 impl AsFd for Socket {
38     fn as_fd(&self) -> BorrowedFd<'_> {
39         self.0.as_fd()
40     }
41 }
42
43 impl AsRawFd for Socket {
44     fn as_raw_fd(&self) -> RawFd {
45         self.0.as_raw_fd()
46     }
47 }
48
49 impl IntoRawFd for Socket {
50     fn into_raw_fd(self) -> RawFd {
51         self.0.into_raw_fd()
52     }
53 }
54
55 impl FromRawFd for Socket {
56     unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
57         unsafe { Self(FromRawFd::from_raw_fd(raw_fd)) }
58     }
59 }
60
61 impl TcpStream {
62     pub fn connect(_: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
63         unsupported()
64     }
65
66     pub fn connect_timeout(_: &SocketAddr, _: Duration) -> io::Result<TcpStream> {
67         unsupported()
68     }
69
70     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
71         unsupported()
72     }
73
74     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
75         unsupported()
76     }
77
78     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
79         unsupported()
80     }
81
82     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
83         unsupported()
84     }
85
86     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
87         unsupported()
88     }
89
90     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
91         self.read_vectored(&mut [IoSliceMut::new(buf)])
92     }
93
94     pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
95         self.socket().as_inner().read(bufs)
96     }
97
98     pub fn is_read_vectored(&self) -> bool {
99         true
100     }
101
102     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
103         self.write_vectored(&[IoSlice::new(buf)])
104     }
105
106     pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
107         self.socket().as_inner().write(bufs)
108     }
109
110     pub fn is_write_vectored(&self) -> bool {
111         true
112     }
113
114     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
115         unsupported()
116     }
117
118     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
119         unsupported()
120     }
121
122     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
123         let wasi_how = match how {
124             Shutdown::Read => wasi::SDFLAGS_RD,
125             Shutdown::Write => wasi::SDFLAGS_WR,
126             Shutdown::Both => wasi::SDFLAGS_RD | wasi::SDFLAGS_WR,
127         };
128
129         unsafe { wasi::sock_shutdown(self.socket().as_raw_fd() as _, wasi_how).map_err(err2io) }
130     }
131
132     pub fn duplicate(&self) -> io::Result<TcpStream> {
133         unsupported()
134     }
135
136     pub fn set_linger(&self, _: Option<Duration>) -> io::Result<()> {
137         unsupported()
138     }
139
140     pub fn linger(&self) -> io::Result<Option<Duration>> {
141         unsupported()
142     }
143
144     pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
145         unsupported()
146     }
147
148     pub fn nodelay(&self) -> io::Result<bool> {
149         unsupported()
150     }
151
152     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
153         unsupported()
154     }
155
156     pub fn ttl(&self) -> io::Result<u32> {
157         unsupported()
158     }
159
160     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
161         unsupported()
162     }
163
164     pub fn set_nonblocking(&self, state: bool) -> io::Result<()> {
165         let fdstat = unsafe {
166             wasi::fd_fdstat_get(self.socket().as_inner().as_raw_fd() as wasi::Fd).map_err(err2io)?
167         };
168
169         let mut flags = fdstat.fs_flags;
170
171         if state {
172             flags |= wasi::FDFLAGS_NONBLOCK;
173         } else {
174             flags &= !wasi::FDFLAGS_NONBLOCK;
175         }
176
177         unsafe {
178             wasi::fd_fdstat_set_flags(self.socket().as_inner().as_raw_fd() as wasi::Fd, flags)
179                 .map_err(err2io)
180         }
181     }
182
183     pub fn socket(&self) -> &Socket {
184         &self.inner
185     }
186
187     pub fn into_socket(self) -> Socket {
188         self.inner
189     }
190 }
191
192 impl FromInner<Socket> for TcpStream {
193     fn from_inner(socket: Socket) -> TcpStream {
194         TcpStream { inner: socket }
195     }
196 }
197
198 impl fmt::Debug for TcpStream {
199     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200         f.debug_struct("TcpStream").field("fd", &self.inner.as_raw_fd()).finish()
201     }
202 }
203
204 pub struct TcpListener {
205     inner: Socket,
206 }
207
208 impl TcpListener {
209     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
210         unsupported()
211     }
212
213     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
214         unsupported()
215     }
216
217     pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
218         let fd = unsafe {
219             wasi::sock_accept(self.as_inner().as_inner().as_raw_fd() as _, 0).map_err(err2io)?
220         };
221
222         Ok((
223             TcpStream::from_inner(unsafe { Socket::from_raw_fd(fd as _) }),
224             // WASI has no concept of SocketAddr yet
225             // return an unspecified IPv4Addr
226             SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0),
227         ))
228     }
229
230     pub fn duplicate(&self) -> io::Result<TcpListener> {
231         unsupported()
232     }
233
234     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
235         unsupported()
236     }
237
238     pub fn ttl(&self) -> io::Result<u32> {
239         unsupported()
240     }
241
242     pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
243         unsupported()
244     }
245
246     pub fn only_v6(&self) -> io::Result<bool> {
247         unsupported()
248     }
249
250     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
251         unsupported()
252     }
253
254     pub fn set_nonblocking(&self, state: bool) -> io::Result<()> {
255         let fdstat = unsafe {
256             wasi::fd_fdstat_get(self.socket().as_inner().as_raw_fd() as wasi::Fd).map_err(err2io)?
257         };
258
259         let mut flags = fdstat.fs_flags;
260
261         if state {
262             flags |= wasi::FDFLAGS_NONBLOCK;
263         } else {
264             flags &= !wasi::FDFLAGS_NONBLOCK;
265         }
266
267         unsafe {
268             wasi::fd_fdstat_set_flags(self.socket().as_inner().as_raw_fd() as wasi::Fd, flags)
269                 .map_err(err2io)
270         }
271     }
272
273     pub fn socket(&self) -> &Socket {
274         &self.inner
275     }
276
277     pub fn into_socket(self) -> Socket {
278         self.inner
279     }
280 }
281
282 impl AsInner<Socket> for TcpListener {
283     fn as_inner(&self) -> &Socket {
284         &self.inner
285     }
286 }
287
288 impl IntoInner<Socket> for TcpListener {
289     fn into_inner(self) -> Socket {
290         self.inner
291     }
292 }
293
294 impl FromInner<Socket> for TcpListener {
295     fn from_inner(inner: Socket) -> TcpListener {
296         TcpListener { inner }
297     }
298 }
299
300 impl fmt::Debug for TcpListener {
301     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
302         f.debug_struct("TcpListener").field("fd", &self.inner.as_raw_fd()).finish()
303     }
304 }
305
306 pub struct UdpSocket {
307     inner: Socket,
308 }
309
310 impl UdpSocket {
311     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
312         unsupported()
313     }
314
315     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
316         unsupported()
317     }
318
319     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
320         unsupported()
321     }
322
323     pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
324         unsupported()
325     }
326
327     pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
328         unsupported()
329     }
330
331     pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
332         unsupported()
333     }
334
335     pub fn duplicate(&self) -> io::Result<UdpSocket> {
336         unsupported()
337     }
338
339     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
340         unsupported()
341     }
342
343     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
344         unsupported()
345     }
346
347     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
348         unsupported()
349     }
350
351     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
352         unsupported()
353     }
354
355     pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
356         unsupported()
357     }
358
359     pub fn broadcast(&self) -> io::Result<bool> {
360         unsupported()
361     }
362
363     pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
364         unsupported()
365     }
366
367     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
368         unsupported()
369     }
370
371     pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
372         unsupported()
373     }
374
375     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
376         unsupported()
377     }
378
379     pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
380         unsupported()
381     }
382
383     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
384         unsupported()
385     }
386
387     pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
388         unsupported()
389     }
390
391     pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
392         unsupported()
393     }
394
395     pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
396         unsupported()
397     }
398
399     pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
400         unsupported()
401     }
402
403     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
404         unsupported()
405     }
406
407     pub fn ttl(&self) -> io::Result<u32> {
408         unsupported()
409     }
410
411     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
412         unsupported()
413     }
414
415     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
416         unsupported()
417     }
418
419     pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
420         unsupported()
421     }
422
423     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
424         unsupported()
425     }
426
427     pub fn send(&self, _: &[u8]) -> io::Result<usize> {
428         unsupported()
429     }
430
431     pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
432         unsupported()
433     }
434
435     pub fn socket(&self) -> &Socket {
436         &self.inner
437     }
438
439     pub fn into_socket(self) -> Socket {
440         self.inner
441     }
442 }
443
444 impl AsInner<Socket> for UdpSocket {
445     fn as_inner(&self) -> &Socket {
446         &self.inner
447     }
448 }
449
450 impl IntoInner<Socket> for UdpSocket {
451     fn into_inner(self) -> Socket {
452         self.inner
453     }
454 }
455
456 impl FromInner<Socket> for UdpSocket {
457     fn from_inner(inner: Socket) -> UdpSocket {
458         UdpSocket { inner }
459     }
460 }
461
462 impl fmt::Debug for UdpSocket {
463     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
464         f.debug_struct("UdpSocket").field("fd", &self.inner.as_raw_fd()).finish()
465     }
466 }
467
468 pub struct LookupHost(!);
469
470 impl LookupHost {
471     pub fn port(&self) -> u16 {
472         self.0
473     }
474 }
475
476 impl Iterator for LookupHost {
477     type Item = SocketAddr;
478     fn next(&mut self) -> Option<SocketAddr> {
479         self.0
480     }
481 }
482
483 impl<'a> TryFrom<&'a str> for LookupHost {
484     type Error = io::Error;
485
486     fn try_from(_v: &'a str) -> io::Result<LookupHost> {
487         unsupported()
488     }
489 }
490
491 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
492     type Error = io::Error;
493
494     fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
495         unsupported()
496     }
497 }
498
499 #[allow(nonstandard_style)]
500 pub mod netc {
501     pub const AF_INET: u8 = 0;
502     pub const AF_INET6: u8 = 1;
503     pub type sa_family_t = u8;
504
505     #[derive(Copy, Clone)]
506     pub struct in_addr {
507         pub s_addr: u32,
508     }
509
510     #[derive(Copy, Clone)]
511     pub struct sockaddr_in {
512         pub sin_family: sa_family_t,
513         pub sin_port: u16,
514         pub sin_addr: in_addr,
515     }
516
517     #[derive(Copy, Clone)]
518     pub struct in6_addr {
519         pub s6_addr: [u8; 16],
520     }
521
522     #[derive(Copy, Clone)]
523     pub struct sockaddr_in6 {
524         pub sin6_family: sa_family_t,
525         pub sin6_port: u16,
526         pub sin6_addr: in6_addr,
527         pub sin6_flowinfo: u32,
528         pub sin6_scope_id: u32,
529     }
530
531     #[derive(Copy, Clone)]
532     pub struct sockaddr {}
533 }