1 use crate::convert::TryFrom;
3 use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
4 use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
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;
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"));
23 #[derive(Debug, Clone)]
24 pub struct Socket(abi::Handle);
26 impl AsInner<abi::Handle> for Socket {
27 fn as_inner(&self) -> &abi::Handle {
32 impl Drop for Socket {
34 let _ = abi::tcpstream::close(self.0);
40 pub struct TcpStream(Arc<Socket>);
43 pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
46 match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
47 Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
49 Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
54 pub fn connect_timeout(saddr: &SocketAddr, duration: Duration) -> io::Result<TcpStream> {
55 match abi::tcpstream::connect(
56 saddr.ip().to_string().as_bytes(),
58 Some(duration.as_millis() as u64),
60 Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
62 Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
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"))
72 pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
73 abi::tcpstream::set_write_timeout(
75 duration.map(|d| d.as_millis() as u64),
77 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
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"))?;
84 Ok(duration.map(|d| Duration::from_millis(d)))
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"))?;
91 Ok(duration.map(|d| Duration::from_millis(d)))
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"))
99 pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
100 self.read_vectored(&mut [IoSliceMut::new(buffer)])
103 pub fn read_vectored(&self, ioslice: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
104 let mut size: usize = 0;
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"))?;
119 pub fn is_read_vectored(&self) -> bool {
123 pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
124 self.write_vectored(&[IoSlice::new(buffer)])
127 pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
128 let mut size: usize = 0;
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"))?;
139 pub fn is_write_vectored(&self) -> bool {
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"))?;
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])),
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)),
165 return Err(io::Error::new(ErrorKind::Other, "peer_addr failed"));
172 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
173 Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
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"))
181 pub fn duplicate(&self) -> io::Result<TcpStream> {
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"))
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"))
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"))
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"))
205 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
206 Err(io::Error::new(ErrorKind::Other, "take_error isn't supported"))
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"))
215 impl fmt::Debug for TcpStream {
216 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
222 pub struct TcpListener(SocketAddr);
225 pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
228 Ok(TcpListener(*addr))
231 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
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])),
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)),
256 return Err(io::Error::new(ErrorKind::Other, "accept failed"));
260 Ok((TcpStream(Arc::new(Socket(handle))), saddr))
263 pub fn duplicate(&self) -> io::Result<TcpListener> {
267 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
268 Err(io::Error::new(ErrorKind::Other, "not supported"))
271 pub fn ttl(&self) -> io::Result<u32> {
272 Err(io::Error::new(ErrorKind::Other, "not supported"))
275 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
276 Err(io::Error::new(ErrorKind::Other, "not supported"))
279 pub fn only_v6(&self) -> io::Result<bool> {
280 Err(io::Error::new(ErrorKind::Other, "not supported"))
283 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
284 Err(io::Error::new(ErrorKind::Other, "not supported"))
287 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
288 Err(io::Error::new(ErrorKind::Other, "not supported"))
292 impl fmt::Debug for TcpListener {
293 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
298 pub struct UdpSocket(abi::Handle);
301 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
302 Err(io::Error::new(ErrorKind::Other, "not supported"))
305 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
306 Err(io::Error::new(ErrorKind::Other, "not supported"))
309 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
310 Err(io::Error::new(ErrorKind::Other, "not supported"))
313 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
314 Err(io::Error::new(ErrorKind::Other, "not supported"))
317 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
318 Err(io::Error::new(ErrorKind::Other, "not supported"))
321 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
322 Err(io::Error::new(ErrorKind::Other, "not supported"))
325 pub fn duplicate(&self) -> io::Result<UdpSocket> {
326 Err(io::Error::new(ErrorKind::Other, "not supported"))
329 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
330 Err(io::Error::new(ErrorKind::Other, "not supported"))
333 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
334 Err(io::Error::new(ErrorKind::Other, "not supported"))
337 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
338 Err(io::Error::new(ErrorKind::Other, "not supported"))
341 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
342 Err(io::Error::new(ErrorKind::Other, "not supported"))
345 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
346 Err(io::Error::new(ErrorKind::Other, "not supported"))
349 pub fn broadcast(&self) -> io::Result<bool> {
350 Err(io::Error::new(ErrorKind::Other, "not supported"))
353 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
354 Err(io::Error::new(ErrorKind::Other, "not supported"))
357 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
358 Err(io::Error::new(ErrorKind::Other, "not supported"))
361 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
362 Err(io::Error::new(ErrorKind::Other, "not supported"))
365 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
366 Err(io::Error::new(ErrorKind::Other, "not supported"))
369 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
370 Err(io::Error::new(ErrorKind::Other, "not supported"))
373 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
374 Err(io::Error::new(ErrorKind::Other, "not supported"))
377 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
378 Err(io::Error::new(ErrorKind::Other, "not supported"))
381 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
382 Err(io::Error::new(ErrorKind::Other, "not supported"))
385 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
386 Err(io::Error::new(ErrorKind::Other, "not supported"))
389 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
390 Err(io::Error::new(ErrorKind::Other, "not supported"))
393 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
394 Err(io::Error::new(ErrorKind::Other, "not supported"))
397 pub fn ttl(&self) -> io::Result<u32> {
398 Err(io::Error::new(ErrorKind::Other, "not supported"))
401 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
402 Err(io::Error::new(ErrorKind::Other, "not supported"))
405 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
406 Err(io::Error::new(ErrorKind::Other, "not supported"))
409 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
410 Err(io::Error::new(ErrorKind::Other, "not supported"))
413 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
414 Err(io::Error::new(ErrorKind::Other, "not supported"))
417 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
418 Err(io::Error::new(ErrorKind::Other, "not supported"))
421 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
422 Err(io::Error::new(ErrorKind::Other, "not supported"))
426 impl fmt::Debug for UdpSocket {
427 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
432 pub struct LookupHost(Void);
435 pub fn port(&self) -> u16 {
440 impl Iterator for LookupHost {
441 type Item = SocketAddr;
442 fn next(&mut self) -> Option<SocketAddr> {
447 impl TryFrom<&str> for LookupHost {
448 type Error = io::Error;
450 fn try_from(_v: &str) -> io::Result<LookupHost> {
455 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
456 type Error = io::Error;
458 fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
463 #[allow(nonstandard_style)]
465 pub const AF_INET: u8 = 0;
466 pub const AF_INET6: u8 = 1;
467 pub type sa_family_t = u8;
469 #[derive(Copy, Clone)]
474 #[derive(Copy, Clone)]
475 pub struct sockaddr_in {
476 pub sin_family: sa_family_t,
478 pub sin_addr: in_addr,
481 #[derive(Copy, Clone)]
482 pub struct in6_addr {
483 pub s6_addr: [u8; 16],
486 #[derive(Copy, Clone)]
487 pub struct sockaddr_in6 {
488 pub sin6_family: sa_family_t,
490 pub sin6_addr: in6_addr,
491 pub sin6_flowinfo: u32,
492 pub sin6_scope_id: u32,
495 #[derive(Copy, Clone)]
496 pub struct sockaddr {}
498 pub type socklen_t = usize;