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