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