]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/wasi/net.rs
59d94a3686dc513e94370cb2ecaa74a2c0b0ba66
[rustc.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, 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;
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_buf(&self, buf: BorrowedCursor<'_>) -> io::Result<()> {
95 self.socket().as_inner().read_buf(buf)
96 }
97
98 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
99 self.socket().as_inner().read(bufs)
100 }
101
102 pub fn is_read_vectored(&self) -> bool {
103 true
104 }
105
106 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
107 self.write_vectored(&[IoSlice::new(buf)])
108 }
109
110 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
111 self.socket().as_inner().write(bufs)
112 }
113
114 pub fn is_write_vectored(&self) -> bool {
115 true
116 }
117
118 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
119 unsupported()
120 }
121
122 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
123 unsupported()
124 }
125
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,
131 };
132
133 unsafe { wasi::sock_shutdown(self.socket().as_raw_fd() as _, wasi_how).map_err(err2io) }
134 }
135
136 pub fn duplicate(&self) -> io::Result<TcpStream> {
137 unsupported()
138 }
139
140 pub fn set_linger(&self, _: Option<Duration>) -> io::Result<()> {
141 unsupported()
142 }
143
144 pub fn linger(&self) -> io::Result<Option<Duration>> {
145 unsupported()
146 }
147
148 pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
149 unsupported()
150 }
151
152 pub fn nodelay(&self) -> io::Result<bool> {
153 unsupported()
154 }
155
156 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
157 unsupported()
158 }
159
160 pub fn ttl(&self) -> io::Result<u32> {
161 unsupported()
162 }
163
164 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
165 unsupported()
166 }
167
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)?
171 };
172
173 let mut flags = fdstat.fs_flags;
174
175 if state {
176 flags |= wasi::FDFLAGS_NONBLOCK;
177 } else {
178 flags &= !wasi::FDFLAGS_NONBLOCK;
179 }
180
181 unsafe {
182 wasi::fd_fdstat_set_flags(self.socket().as_inner().as_raw_fd() as wasi::Fd, flags)
183 .map_err(err2io)
184 }
185 }
186
187 pub fn socket(&self) -> &Socket {
188 &self.inner
189 }
190
191 pub fn into_socket(self) -> Socket {
192 self.inner
193 }
194 }
195
196 impl FromInner<Socket> for TcpStream {
197 fn from_inner(socket: Socket) -> TcpStream {
198 TcpStream { inner: socket }
199 }
200 }
201
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()
205 }
206 }
207
208 pub struct TcpListener {
209 inner: Socket,
210 }
211
212 impl TcpListener {
213 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
214 unsupported()
215 }
216
217 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
218 unsupported()
219 }
220
221 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
222 let fd = unsafe {
223 wasi::sock_accept(self.as_inner().as_inner().as_raw_fd() as _, 0).map_err(err2io)?
224 };
225
226 Ok((
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),
231 ))
232 }
233
234 pub fn duplicate(&self) -> io::Result<TcpListener> {
235 unsupported()
236 }
237
238 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
239 unsupported()
240 }
241
242 pub fn ttl(&self) -> io::Result<u32> {
243 unsupported()
244 }
245
246 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
247 unsupported()
248 }
249
250 pub fn only_v6(&self) -> io::Result<bool> {
251 unsupported()
252 }
253
254 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
255 unsupported()
256 }
257
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)?
261 };
262
263 let mut flags = fdstat.fs_flags;
264
265 if state {
266 flags |= wasi::FDFLAGS_NONBLOCK;
267 } else {
268 flags &= !wasi::FDFLAGS_NONBLOCK;
269 }
270
271 unsafe {
272 wasi::fd_fdstat_set_flags(self.socket().as_inner().as_raw_fd() as wasi::Fd, flags)
273 .map_err(err2io)
274 }
275 }
276
277 pub fn socket(&self) -> &Socket {
278 &self.inner
279 }
280
281 pub fn into_socket(self) -> Socket {
282 self.inner
283 }
284 }
285
286 impl AsInner<Socket> for TcpListener {
287 fn as_inner(&self) -> &Socket {
288 &self.inner
289 }
290 }
291
292 impl IntoInner<Socket> for TcpListener {
293 fn into_inner(self) -> Socket {
294 self.inner
295 }
296 }
297
298 impl FromInner<Socket> for TcpListener {
299 fn from_inner(inner: Socket) -> TcpListener {
300 TcpListener { inner }
301 }
302 }
303
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()
307 }
308 }
309
310 pub struct UdpSocket {
311 inner: Socket,
312 }
313
314 impl UdpSocket {
315 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
316 unsupported()
317 }
318
319 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
320 unsupported()
321 }
322
323 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
324 unsupported()
325 }
326
327 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
328 unsupported()
329 }
330
331 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
332 unsupported()
333 }
334
335 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
336 unsupported()
337 }
338
339 pub fn duplicate(&self) -> io::Result<UdpSocket> {
340 unsupported()
341 }
342
343 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
344 unsupported()
345 }
346
347 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
348 unsupported()
349 }
350
351 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
352 unsupported()
353 }
354
355 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
356 unsupported()
357 }
358
359 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
360 unsupported()
361 }
362
363 pub fn broadcast(&self) -> io::Result<bool> {
364 unsupported()
365 }
366
367 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
368 unsupported()
369 }
370
371 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
372 unsupported()
373 }
374
375 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
376 unsupported()
377 }
378
379 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
380 unsupported()
381 }
382
383 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
384 unsupported()
385 }
386
387 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
388 unsupported()
389 }
390
391 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
392 unsupported()
393 }
394
395 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
396 unsupported()
397 }
398
399 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
400 unsupported()
401 }
402
403 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
404 unsupported()
405 }
406
407 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
408 unsupported()
409 }
410
411 pub fn ttl(&self) -> io::Result<u32> {
412 unsupported()
413 }
414
415 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
416 unsupported()
417 }
418
419 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
420 unsupported()
421 }
422
423 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
424 unsupported()
425 }
426
427 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
428 unsupported()
429 }
430
431 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
432 unsupported()
433 }
434
435 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
436 unsupported()
437 }
438
439 pub fn socket(&self) -> &Socket {
440 &self.inner
441 }
442
443 pub fn into_socket(self) -> Socket {
444 self.inner
445 }
446 }
447
448 impl AsInner<Socket> for UdpSocket {
449 fn as_inner(&self) -> &Socket {
450 &self.inner
451 }
452 }
453
454 impl IntoInner<Socket> for UdpSocket {
455 fn into_inner(self) -> Socket {
456 self.inner
457 }
458 }
459
460 impl FromInner<Socket> for UdpSocket {
461 fn from_inner(inner: Socket) -> UdpSocket {
462 UdpSocket { inner }
463 }
464 }
465
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()
469 }
470 }
471
472 pub struct LookupHost(!);
473
474 impl LookupHost {
475 pub fn port(&self) -> u16 {
476 self.0
477 }
478 }
479
480 impl Iterator for LookupHost {
481 type Item = SocketAddr;
482 fn next(&mut self) -> Option<SocketAddr> {
483 self.0
484 }
485 }
486
487 impl<'a> TryFrom<&'a str> for LookupHost {
488 type Error = io::Error;
489
490 fn try_from(_v: &'a str) -> io::Result<LookupHost> {
491 unsupported()
492 }
493 }
494
495 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
496 type Error = io::Error;
497
498 fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
499 unsupported()
500 }
501 }
502
503 #[allow(nonstandard_style)]
504 pub mod netc {
505 pub const AF_INET: u8 = 0;
506 pub const AF_INET6: u8 = 1;
507 pub type sa_family_t = u8;
508
509 #[derive(Copy, Clone)]
510 pub struct in_addr {
511 pub s_addr: u32,
512 }
513
514 #[derive(Copy, Clone)]
515 pub struct sockaddr_in {
516 pub sin_family: sa_family_t,
517 pub sin_port: u16,
518 pub sin_addr: in_addr,
519 }
520
521 #[derive(Copy, Clone)]
522 pub struct in6_addr {
523 pub s6_addr: [u8; 16],
524 }
525
526 #[derive(Copy, Clone)]
527 pub struct sockaddr_in6 {
528 pub sin6_family: sa_family_t,
529 pub sin6_port: u16,
530 pub sin6_addr: in6_addr,
531 pub sin6_flowinfo: u32,
532 pub sin6_scope_id: u32,
533 }
534
535 #[derive(Copy, Clone)]
536 pub struct sockaddr {}
537 }