]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/hermit/net.rs
Rollup merge of #70950 - nikomatsakis:leak-check-nll-2, r=matthewjasper
[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     #[inline]
103     pub fn is_read_vectored(&self) -> bool {
104         true
105     }
106
107     pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
108         self.write_vectored(&[IoSlice::new(buffer)])
109     }
110
111     pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
112         let mut size: usize = 0;
113
114         for i in ioslice.iter() {
115             size += abi::tcpstream::write(self.0, i)
116                 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to write on socket"))?;
117         }
118
119         Ok(size)
120     }
121
122     #[inline]
123     pub fn is_write_vectored(&self) -> bool {
124         true
125     }
126
127     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
128         Err(io::Error::new(ErrorKind::Other, "peer_addr isn't supported"))
129     }
130
131     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
132         Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
133     }
134
135     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
136         abi::tcpstream::shutdown(self.0, how as i32)
137             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to shutdown socket"))
138     }
139
140     pub fn duplicate(&self) -> io::Result<TcpStream> {
141         let handle = abi::tcpstream::duplicate(self.0)
142             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to duplicate stream"))?;
143
144         Ok(TcpStream(handle))
145     }
146
147     pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
148         abi::tcpstream::set_nodelay(self.0, mode)
149             .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
150     }
151
152     pub fn nodelay(&self) -> io::Result<bool> {
153         abi::tcpstream::nodelay(self.0)
154             .map_err(|_| io::Error::new(ErrorKind::Other, "nodelay failed"))
155     }
156
157     pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
158         abi::tcpstream::set_tll(self.0, tll)
159             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set TTL"))
160     }
161
162     pub fn ttl(&self) -> io::Result<u32> {
163         abi::tcpstream::get_tll(self.0)
164             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to get TTL"))
165     }
166
167     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
168         Err(io::Error::new(ErrorKind::Other, "take_error isn't supported"))
169     }
170
171     pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
172         abi::tcpstream::set_nonblocking(self.0, mode)
173             .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set blocking mode"))
174     }
175 }
176
177 impl Drop for TcpStream {
178     fn drop(&mut self) {
179         let _ = abi::tcpstream::close(self.0);
180     }
181 }
182
183 impl fmt::Debug for TcpStream {
184     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
185         Ok(())
186     }
187 }
188
189 pub struct TcpListener(abi::Handle);
190
191 impl TcpListener {
192     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
193         Err(io::Error::new(ErrorKind::Other, "not supported"))
194     }
195
196     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
197         Err(io::Error::new(ErrorKind::Other, "not supported"))
198     }
199
200     pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
201         Err(io::Error::new(ErrorKind::Other, "not supported"))
202     }
203
204     pub fn duplicate(&self) -> io::Result<TcpListener> {
205         Err(io::Error::new(ErrorKind::Other, "not supported"))
206     }
207
208     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
209         Err(io::Error::new(ErrorKind::Other, "not supported"))
210     }
211
212     pub fn ttl(&self) -> io::Result<u32> {
213         Err(io::Error::new(ErrorKind::Other, "not supported"))
214     }
215
216     pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
217         Err(io::Error::new(ErrorKind::Other, "not supported"))
218     }
219
220     pub fn only_v6(&self) -> io::Result<bool> {
221         Err(io::Error::new(ErrorKind::Other, "not supported"))
222     }
223
224     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
225         Err(io::Error::new(ErrorKind::Other, "not supported"))
226     }
227
228     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
229         Err(io::Error::new(ErrorKind::Other, "not supported"))
230     }
231 }
232
233 impl fmt::Debug for TcpListener {
234     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
235         Ok(())
236     }
237 }
238
239 pub struct UdpSocket(abi::Handle);
240
241 impl UdpSocket {
242     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
243         Err(io::Error::new(ErrorKind::Other, "not supported"))
244     }
245
246     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
247         Err(io::Error::new(ErrorKind::Other, "not supported"))
248     }
249
250     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
251         Err(io::Error::new(ErrorKind::Other, "not supported"))
252     }
253
254     pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
255         Err(io::Error::new(ErrorKind::Other, "not supported"))
256     }
257
258     pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
259         Err(io::Error::new(ErrorKind::Other, "not supported"))
260     }
261
262     pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
263         Err(io::Error::new(ErrorKind::Other, "not supported"))
264     }
265
266     pub fn duplicate(&self) -> io::Result<UdpSocket> {
267         Err(io::Error::new(ErrorKind::Other, "not supported"))
268     }
269
270     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
271         Err(io::Error::new(ErrorKind::Other, "not supported"))
272     }
273
274     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
275         Err(io::Error::new(ErrorKind::Other, "not supported"))
276     }
277
278     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
279         Err(io::Error::new(ErrorKind::Other, "not supported"))
280     }
281
282     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
283         Err(io::Error::new(ErrorKind::Other, "not supported"))
284     }
285
286     pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
287         Err(io::Error::new(ErrorKind::Other, "not supported"))
288     }
289
290     pub fn broadcast(&self) -> io::Result<bool> {
291         Err(io::Error::new(ErrorKind::Other, "not supported"))
292     }
293
294     pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
295         Err(io::Error::new(ErrorKind::Other, "not supported"))
296     }
297
298     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
299         Err(io::Error::new(ErrorKind::Other, "not supported"))
300     }
301
302     pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
303         Err(io::Error::new(ErrorKind::Other, "not supported"))
304     }
305
306     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
307         Err(io::Error::new(ErrorKind::Other, "not supported"))
308     }
309
310     pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
311         Err(io::Error::new(ErrorKind::Other, "not supported"))
312     }
313
314     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
315         Err(io::Error::new(ErrorKind::Other, "not supported"))
316     }
317
318     pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
319         Err(io::Error::new(ErrorKind::Other, "not supported"))
320     }
321
322     pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
323         Err(io::Error::new(ErrorKind::Other, "not supported"))
324     }
325
326     pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
327         Err(io::Error::new(ErrorKind::Other, "not supported"))
328     }
329
330     pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
331         Err(io::Error::new(ErrorKind::Other, "not supported"))
332     }
333
334     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
335         Err(io::Error::new(ErrorKind::Other, "not supported"))
336     }
337
338     pub fn ttl(&self) -> io::Result<u32> {
339         Err(io::Error::new(ErrorKind::Other, "not supported"))
340     }
341
342     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
343         Err(io::Error::new(ErrorKind::Other, "not supported"))
344     }
345
346     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
347         Err(io::Error::new(ErrorKind::Other, "not supported"))
348     }
349
350     pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
351         Err(io::Error::new(ErrorKind::Other, "not supported"))
352     }
353
354     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
355         Err(io::Error::new(ErrorKind::Other, "not supported"))
356     }
357
358     pub fn send(&self, _: &[u8]) -> io::Result<usize> {
359         Err(io::Error::new(ErrorKind::Other, "not supported"))
360     }
361
362     pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
363         Err(io::Error::new(ErrorKind::Other, "not supported"))
364     }
365 }
366
367 impl fmt::Debug for UdpSocket {
368     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
369         Ok(())
370     }
371 }
372
373 pub struct LookupHost(Void);
374
375 impl LookupHost {
376     pub fn port(&self) -> u16 {
377         match self.0 {}
378     }
379 }
380
381 impl Iterator for LookupHost {
382     type Item = SocketAddr;
383     fn next(&mut self) -> Option<SocketAddr> {
384         match self.0 {}
385     }
386 }
387
388 impl TryFrom<&str> for LookupHost {
389     type Error = io::Error;
390
391     fn try_from(_v: &str) -> io::Result<LookupHost> {
392         unsupported()
393     }
394 }
395
396 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
397     type Error = io::Error;
398
399     fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
400         unsupported()
401     }
402 }
403
404 #[allow(nonstandard_style)]
405 pub mod netc {
406     pub const AF_INET: u8 = 0;
407     pub const AF_INET6: u8 = 1;
408     pub type sa_family_t = u8;
409
410     #[derive(Copy, Clone)]
411     pub struct in_addr {
412         pub s_addr: u32,
413     }
414
415     #[derive(Copy, Clone)]
416     pub struct sockaddr_in {
417         pub sin_family: sa_family_t,
418         pub sin_port: u16,
419         pub sin_addr: in_addr,
420     }
421
422     #[derive(Copy, Clone)]
423     pub struct in6_addr {
424         pub s6_addr: [u8; 16],
425     }
426
427     #[derive(Copy, Clone)]
428     pub struct sockaddr_in6 {
429         pub sin6_family: sa_family_t,
430         pub sin6_port: u16,
431         pub sin6_addr: in6_addr,
432         pub sin6_flowinfo: u32,
433         pub sin6_scope_id: u32,
434     }
435
436     #[derive(Copy, Clone)]
437     pub struct sockaddr {}
438
439     pub type socklen_t = usize;
440 }