]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/hermit/net.rs
Rollup merge of #74907 - GuillaumeGomez:cleanup-e0740, r=pickfire
[rust.git] / library / std / src / sys / hermit / net.rs
1 use crate::convert::TryFrom;
2 use crate::fmt;
3 use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
4 use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
5 use crate::str;
6 use crate::sync::Arc;
7 use crate::sys::hermit::abi;
8 use crate::sys::hermit::abi::IpAddress::{Ipv4, Ipv6};
9 use crate::sys::{unsupported, Void};
10 use crate::sys_common::AsInner;
11 use crate::time::Duration;
12
13 /// Checks whether the HermitCore's socket interface has been started already, and
14 /// if not, starts it.
15 pub fn init() -> io::Result<()> {
16     if abi::network_init() < 0 {
17         return Err(io::Error::new(ErrorKind::Other, "Unable to initialize network interface"));
18     }
19
20     Ok(())
21 }
22
23 #[derive(Debug, Clone)]
24 pub struct Socket(abi::Handle);
25
26 impl AsInner<abi::Handle> for Socket {
27     fn as_inner(&self) -> &abi::Handle {
28         &self.0
29     }
30 }
31
32 impl Drop for Socket {
33     fn drop(&mut self) {
34         let _ = abi::tcpstream::close(self.0);
35     }
36 }
37
38 // Arc is used to count the number of used sockets.
39 // Only if all sockets are released, the drop
40 // method will close the socket.
41 #[derive(Clone)]
42 pub struct TcpStream(Arc<Socket>);
43
44 impl TcpStream {
45     pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
46         let addr = addr?;
47
48         match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
49             Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
50             _ => {
51                 Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
52             }
53         }
54     }
55
56     pub fn connect_timeout(saddr: &SocketAddr, duration: Duration) -> io::Result<TcpStream> {
57         match abi::tcpstream::connect(
58             saddr.ip().to_string().as_bytes(),
59             saddr.port(),
60             Some(duration.as_millis() as u64),
61         ) {
62             Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
63             _ => {
64                 Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
65             }
66         }
67     }
68
69     pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
70         abi::tcpstream::set_read_timeout(*self.0.as_inner(), duration.map(|d| d.as_millis() as u64))
71             .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
72     }
73
74     pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
75         abi::tcpstream::set_write_timeout(
76             *self.0.as_inner(),
77             duration.map(|d| d.as_millis() as u64),
78         )
79         .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
80     }
81
82     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
83         let duration = abi::tcpstream::get_read_timeout(*self.0.as_inner())
84             .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
85
86         Ok(duration.map(|d| Duration::from_millis(d)))
87     }
88
89     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
90         let duration = abi::tcpstream::get_write_timeout(*self.0.as_inner())
91             .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
92
93         Ok(duration.map(|d| Duration::from_millis(d)))
94     }
95
96     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
97         abi::tcpstream::peek(*self.0.as_inner(), buf)
98             .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
99     }
100
101     pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
102         self.read_vectored(&mut [IoSliceMut::new(buffer)])
103     }
104
105     pub fn read_vectored(&self, ioslice: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
106         let mut size: usize = 0;
107
108         for i in ioslice.iter_mut() {
109             let ret = abi::tcpstream::read(*self.0.as_inner(), &mut i[0..])
110                 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to read on socket"))?;
111
112             if ret != 0 {
113                 size += ret;
114             }
115         }
116
117         Ok(size)
118     }
119
120     #[inline]
121     pub fn is_read_vectored(&self) -> bool {
122         true
123     }
124
125     pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
126         self.write_vectored(&[IoSlice::new(buffer)])
127     }
128
129     pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
130         let mut size: usize = 0;
131
132         for i in ioslice.iter() {
133             size += abi::tcpstream::write(*self.0.as_inner(), i)
134                 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to write on socket"))?;
135         }
136
137         Ok(size)
138     }
139
140     #[inline]
141     pub fn is_write_vectored(&self) -> bool {
142         true
143     }
144
145     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
146         let (ipaddr, port) = abi::tcpstream::peer_addr(*self.0.as_inner())
147             .map_err(|_| io::Error::new(ErrorKind::Other, "peer_addr failed"))?;
148
149         let saddr = match ipaddr {
150             Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
151             Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
152             _ => {
153                 return Err(io::Error::new(ErrorKind::Other, "peer_addr failed"));
154             }
155         };
156
157         Ok(saddr)
158     }
159
160     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
161         Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
162     }
163
164     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
165         abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
166             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to shutdown socket"))
167     }
168
169     pub fn duplicate(&self) -> io::Result<TcpStream> {
170         Ok(self.clone())
171     }
172
173     pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
174         abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
175             .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
176     }
177
178     pub fn nodelay(&self) -> io::Result<bool> {
179         abi::tcpstream::nodelay(*self.0.as_inner())
180             .map_err(|_| io::Error::new(ErrorKind::Other, "nodelay failed"))
181     }
182
183     pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
184         abi::tcpstream::set_tll(*self.0.as_inner(), tll)
185             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set TTL"))
186     }
187
188     pub fn ttl(&self) -> io::Result<u32> {
189         abi::tcpstream::get_tll(*self.0.as_inner())
190             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to get TTL"))
191     }
192
193     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
194         Err(io::Error::new(ErrorKind::Other, "take_error isn't supported"))
195     }
196
197     pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
198         abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode)
199             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set blocking mode"))
200     }
201 }
202
203 impl fmt::Debug for TcpStream {
204     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
205         Ok(())
206     }
207 }
208
209 #[derive(Clone)]
210 pub struct TcpListener(SocketAddr);
211
212 impl TcpListener {
213     pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
214         let addr = addr?;
215
216         Ok(TcpListener(*addr))
217     }
218
219     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
220         Ok(self.0)
221     }
222
223     pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
224         let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
225             .map_err(|_| io::Error::new(ErrorKind::Other, "accept failed"))?;
226         let saddr = match ipaddr {
227             Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
228             Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
229             _ => {
230                 return Err(io::Error::new(ErrorKind::Other, "accept failed"));
231             }
232         };
233
234         Ok((TcpStream(Arc::new(Socket(handle))), saddr))
235     }
236
237     pub fn duplicate(&self) -> io::Result<TcpListener> {
238         Ok(self.clone())
239     }
240
241     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
242         Err(io::Error::new(ErrorKind::Other, "not supported"))
243     }
244
245     pub fn ttl(&self) -> io::Result<u32> {
246         Err(io::Error::new(ErrorKind::Other, "not supported"))
247     }
248
249     pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
250         Err(io::Error::new(ErrorKind::Other, "not supported"))
251     }
252
253     pub fn only_v6(&self) -> io::Result<bool> {
254         Err(io::Error::new(ErrorKind::Other, "not supported"))
255     }
256
257     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
258         Err(io::Error::new(ErrorKind::Other, "not supported"))
259     }
260
261     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
262         Err(io::Error::new(ErrorKind::Other, "not supported"))
263     }
264 }
265
266 impl fmt::Debug for TcpListener {
267     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
268         Ok(())
269     }
270 }
271
272 pub struct UdpSocket(abi::Handle);
273
274 impl UdpSocket {
275     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
276         Err(io::Error::new(ErrorKind::Other, "not supported"))
277     }
278
279     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
280         Err(io::Error::new(ErrorKind::Other, "not supported"))
281     }
282
283     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
284         Err(io::Error::new(ErrorKind::Other, "not supported"))
285     }
286
287     pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
288         Err(io::Error::new(ErrorKind::Other, "not supported"))
289     }
290
291     pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
292         Err(io::Error::new(ErrorKind::Other, "not supported"))
293     }
294
295     pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
296         Err(io::Error::new(ErrorKind::Other, "not supported"))
297     }
298
299     pub fn duplicate(&self) -> io::Result<UdpSocket> {
300         Err(io::Error::new(ErrorKind::Other, "not supported"))
301     }
302
303     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
304         Err(io::Error::new(ErrorKind::Other, "not supported"))
305     }
306
307     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
308         Err(io::Error::new(ErrorKind::Other, "not supported"))
309     }
310
311     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
312         Err(io::Error::new(ErrorKind::Other, "not supported"))
313     }
314
315     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
316         Err(io::Error::new(ErrorKind::Other, "not supported"))
317     }
318
319     pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
320         Err(io::Error::new(ErrorKind::Other, "not supported"))
321     }
322
323     pub fn broadcast(&self) -> io::Result<bool> {
324         Err(io::Error::new(ErrorKind::Other, "not supported"))
325     }
326
327     pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
328         Err(io::Error::new(ErrorKind::Other, "not supported"))
329     }
330
331     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
332         Err(io::Error::new(ErrorKind::Other, "not supported"))
333     }
334
335     pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
336         Err(io::Error::new(ErrorKind::Other, "not supported"))
337     }
338
339     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
340         Err(io::Error::new(ErrorKind::Other, "not supported"))
341     }
342
343     pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
344         Err(io::Error::new(ErrorKind::Other, "not supported"))
345     }
346
347     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
348         Err(io::Error::new(ErrorKind::Other, "not supported"))
349     }
350
351     pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
352         Err(io::Error::new(ErrorKind::Other, "not supported"))
353     }
354
355     pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
356         Err(io::Error::new(ErrorKind::Other, "not supported"))
357     }
358
359     pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
360         Err(io::Error::new(ErrorKind::Other, "not supported"))
361     }
362
363     pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
364         Err(io::Error::new(ErrorKind::Other, "not supported"))
365     }
366
367     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
368         Err(io::Error::new(ErrorKind::Other, "not supported"))
369     }
370
371     pub fn ttl(&self) -> io::Result<u32> {
372         Err(io::Error::new(ErrorKind::Other, "not supported"))
373     }
374
375     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
376         Err(io::Error::new(ErrorKind::Other, "not supported"))
377     }
378
379     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
380         Err(io::Error::new(ErrorKind::Other, "not supported"))
381     }
382
383     pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
384         Err(io::Error::new(ErrorKind::Other, "not supported"))
385     }
386
387     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
388         Err(io::Error::new(ErrorKind::Other, "not supported"))
389     }
390
391     pub fn send(&self, _: &[u8]) -> io::Result<usize> {
392         Err(io::Error::new(ErrorKind::Other, "not supported"))
393     }
394
395     pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
396         Err(io::Error::new(ErrorKind::Other, "not supported"))
397     }
398 }
399
400 impl fmt::Debug for UdpSocket {
401     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
402         Ok(())
403     }
404 }
405
406 pub struct LookupHost(Void);
407
408 impl LookupHost {
409     pub fn port(&self) -> u16 {
410         match self.0 {}
411     }
412 }
413
414 impl Iterator for LookupHost {
415     type Item = SocketAddr;
416     fn next(&mut self) -> Option<SocketAddr> {
417         match self.0 {}
418     }
419 }
420
421 impl TryFrom<&str> for LookupHost {
422     type Error = io::Error;
423
424     fn try_from(_v: &str) -> io::Result<LookupHost> {
425         unsupported()
426     }
427 }
428
429 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
430     type Error = io::Error;
431
432     fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
433         unsupported()
434     }
435 }
436
437 #[allow(nonstandard_style)]
438 pub mod netc {
439     pub const AF_INET: u8 = 0;
440     pub const AF_INET6: u8 = 1;
441     pub type sa_family_t = u8;
442
443     #[derive(Copy, Clone)]
444     pub struct in_addr {
445         pub s_addr: u32,
446     }
447
448     #[derive(Copy, Clone)]
449     pub struct sockaddr_in {
450         pub sin_family: sa_family_t,
451         pub sin_port: u16,
452         pub sin_addr: in_addr,
453     }
454
455     #[derive(Copy, Clone)]
456     pub struct in6_addr {
457         pub s6_addr: [u8; 16],
458     }
459
460     #[derive(Copy, Clone)]
461     pub struct sockaddr_in6 {
462         pub sin6_family: sa_family_t,
463         pub sin6_port: u16,
464         pub sin6_addr: in6_addr,
465         pub sin6_flowinfo: u32,
466         pub sin6_scope_id: u32,
467     }
468
469     #[derive(Copy, Clone)]
470     pub struct sockaddr {}
471
472     pub type socklen_t = usize;
473 }