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