]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/hermit/net.rs
Auto merge of #72357 - ortem:new-dbg-pretty-printers, r=pnkfelix
[rust.git] / src / libstd / 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(
151                 IpAddr::V4(Ipv4Addr::new(addr.0[0], addr.0[1], addr.0[2], addr.0[3])),
152                 port,
153             ),
154             Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
155             _ => {
156                 return Err(io::Error::new(ErrorKind::Other, "peer_addr failed"));
157             }
158         };
159
160         Ok(saddr)
161     }
162
163     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
164         Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
165     }
166
167     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
168         abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
169             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to shutdown socket"))
170     }
171
172     pub fn duplicate(&self) -> io::Result<TcpStream> {
173         Ok(self.clone())
174     }
175
176     pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
177         abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
178             .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
179     }
180
181     pub fn nodelay(&self) -> io::Result<bool> {
182         abi::tcpstream::nodelay(*self.0.as_inner())
183             .map_err(|_| io::Error::new(ErrorKind::Other, "nodelay failed"))
184     }
185
186     pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
187         abi::tcpstream::set_tll(*self.0.as_inner(), tll)
188             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set TTL"))
189     }
190
191     pub fn ttl(&self) -> io::Result<u32> {
192         abi::tcpstream::get_tll(*self.0.as_inner())
193             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to get TTL"))
194     }
195
196     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
197         Err(io::Error::new(ErrorKind::Other, "take_error isn't supported"))
198     }
199
200     pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
201         abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode)
202             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set blocking mode"))
203     }
204 }
205
206 impl fmt::Debug for TcpStream {
207     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
208         Ok(())
209     }
210 }
211
212 #[derive(Clone)]
213 pub struct TcpListener(SocketAddr);
214
215 impl TcpListener {
216     pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
217         let addr = addr?;
218
219         Ok(TcpListener(*addr))
220     }
221
222     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
223         Ok(self.0)
224     }
225
226     pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
227         let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
228             .map_err(|_| io::Error::new(ErrorKind::Other, "accept failed"))?;
229         let saddr = match ipaddr {
230             Ipv4(ref addr) => SocketAddr::new(
231                 IpAddr::V4(Ipv4Addr::new(addr.0[0], addr.0[1], addr.0[2], addr.0[3])),
232                 port,
233             ),
234             Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
235             _ => {
236                 return Err(io::Error::new(ErrorKind::Other, "accept failed"));
237             }
238         };
239
240         Ok((TcpStream(Arc::new(Socket(handle))), saddr))
241     }
242
243     pub fn duplicate(&self) -> io::Result<TcpListener> {
244         Ok(self.clone())
245     }
246
247     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
248         Err(io::Error::new(ErrorKind::Other, "not supported"))
249     }
250
251     pub fn ttl(&self) -> io::Result<u32> {
252         Err(io::Error::new(ErrorKind::Other, "not supported"))
253     }
254
255     pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
256         Err(io::Error::new(ErrorKind::Other, "not supported"))
257     }
258
259     pub fn only_v6(&self) -> io::Result<bool> {
260         Err(io::Error::new(ErrorKind::Other, "not supported"))
261     }
262
263     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
264         Err(io::Error::new(ErrorKind::Other, "not supported"))
265     }
266
267     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
268         Err(io::Error::new(ErrorKind::Other, "not supported"))
269     }
270 }
271
272 impl fmt::Debug for TcpListener {
273     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
274         Ok(())
275     }
276 }
277
278 pub struct UdpSocket(abi::Handle);
279
280 impl UdpSocket {
281     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
282         Err(io::Error::new(ErrorKind::Other, "not supported"))
283     }
284
285     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
286         Err(io::Error::new(ErrorKind::Other, "not supported"))
287     }
288
289     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
290         Err(io::Error::new(ErrorKind::Other, "not supported"))
291     }
292
293     pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
294         Err(io::Error::new(ErrorKind::Other, "not supported"))
295     }
296
297     pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
298         Err(io::Error::new(ErrorKind::Other, "not supported"))
299     }
300
301     pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
302         Err(io::Error::new(ErrorKind::Other, "not supported"))
303     }
304
305     pub fn duplicate(&self) -> io::Result<UdpSocket> {
306         Err(io::Error::new(ErrorKind::Other, "not supported"))
307     }
308
309     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
310         Err(io::Error::new(ErrorKind::Other, "not supported"))
311     }
312
313     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
314         Err(io::Error::new(ErrorKind::Other, "not supported"))
315     }
316
317     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
318         Err(io::Error::new(ErrorKind::Other, "not supported"))
319     }
320
321     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
322         Err(io::Error::new(ErrorKind::Other, "not supported"))
323     }
324
325     pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
326         Err(io::Error::new(ErrorKind::Other, "not supported"))
327     }
328
329     pub fn broadcast(&self) -> io::Result<bool> {
330         Err(io::Error::new(ErrorKind::Other, "not supported"))
331     }
332
333     pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
334         Err(io::Error::new(ErrorKind::Other, "not supported"))
335     }
336
337     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
338         Err(io::Error::new(ErrorKind::Other, "not supported"))
339     }
340
341     pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
342         Err(io::Error::new(ErrorKind::Other, "not supported"))
343     }
344
345     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
346         Err(io::Error::new(ErrorKind::Other, "not supported"))
347     }
348
349     pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
350         Err(io::Error::new(ErrorKind::Other, "not supported"))
351     }
352
353     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
354         Err(io::Error::new(ErrorKind::Other, "not supported"))
355     }
356
357     pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
358         Err(io::Error::new(ErrorKind::Other, "not supported"))
359     }
360
361     pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
362         Err(io::Error::new(ErrorKind::Other, "not supported"))
363     }
364
365     pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
366         Err(io::Error::new(ErrorKind::Other, "not supported"))
367     }
368
369     pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
370         Err(io::Error::new(ErrorKind::Other, "not supported"))
371     }
372
373     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
374         Err(io::Error::new(ErrorKind::Other, "not supported"))
375     }
376
377     pub fn ttl(&self) -> io::Result<u32> {
378         Err(io::Error::new(ErrorKind::Other, "not supported"))
379     }
380
381     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
382         Err(io::Error::new(ErrorKind::Other, "not supported"))
383     }
384
385     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
386         Err(io::Error::new(ErrorKind::Other, "not supported"))
387     }
388
389     pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
390         Err(io::Error::new(ErrorKind::Other, "not supported"))
391     }
392
393     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
394         Err(io::Error::new(ErrorKind::Other, "not supported"))
395     }
396
397     pub fn send(&self, _: &[u8]) -> io::Result<usize> {
398         Err(io::Error::new(ErrorKind::Other, "not supported"))
399     }
400
401     pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
402         Err(io::Error::new(ErrorKind::Other, "not supported"))
403     }
404 }
405
406 impl fmt::Debug for UdpSocket {
407     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
408         Ok(())
409     }
410 }
411
412 pub struct LookupHost(Void);
413
414 impl LookupHost {
415     pub fn port(&self) -> u16 {
416         match self.0 {}
417     }
418 }
419
420 impl Iterator for LookupHost {
421     type Item = SocketAddr;
422     fn next(&mut self) -> Option<SocketAddr> {
423         match self.0 {}
424     }
425 }
426
427 impl TryFrom<&str> for LookupHost {
428     type Error = io::Error;
429
430     fn try_from(_v: &str) -> io::Result<LookupHost> {
431         unsupported()
432     }
433 }
434
435 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
436     type Error = io::Error;
437
438     fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
439         unsupported()
440     }
441 }
442
443 #[allow(nonstandard_style)]
444 pub mod netc {
445     pub const AF_INET: u8 = 0;
446     pub const AF_INET6: u8 = 1;
447     pub type sa_family_t = u8;
448
449     #[derive(Copy, Clone)]
450     pub struct in_addr {
451         pub s_addr: u32,
452     }
453
454     #[derive(Copy, Clone)]
455     pub struct sockaddr_in {
456         pub sin_family: sa_family_t,
457         pub sin_port: u16,
458         pub sin_addr: in_addr,
459     }
460
461     #[derive(Copy, Clone)]
462     pub struct in6_addr {
463         pub s6_addr: [u8; 16],
464     }
465
466     #[derive(Copy, Clone)]
467     pub struct sockaddr_in6 {
468         pub sin6_family: sa_family_t,
469         pub sin6_port: u16,
470         pub sin6_addr: in6_addr,
471         pub sin6_flowinfo: u32,
472         pub sin6_scope_id: u32,
473     }
474
475     #[derive(Copy, Clone)]
476     pub struct sockaddr {}
477
478     pub type socklen_t = usize;
479 }