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;
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_const(
19 &"Unable to initialize network interface",
26 #[derive(Debug, Clone)]
27 pub struct Socket(abi::Handle);
29 impl AsInner<abi::Handle> for Socket {
30 fn as_inner(&self) -> &abi::Handle {
35 impl Drop for Socket {
37 let _ = abi::tcpstream::close(self.0);
41 // Arc is used to count the number of used sockets.
42 // Only if all sockets are released, the drop
43 // method will close the socket.
45 pub struct TcpStream(Arc<Socket>);
48 pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
51 match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
52 Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
53 _ => Err(io::Error::new_const(
55 &"Unable to initiate a connection on a socket",
60 pub fn connect_timeout(saddr: &SocketAddr, duration: Duration) -> io::Result<TcpStream> {
61 match abi::tcpstream::connect(
62 saddr.ip().to_string().as_bytes(),
64 Some(duration.as_millis() as u64),
66 Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
67 _ => Err(io::Error::new_const(
69 &"Unable to initiate a connection on a socket",
74 pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
75 abi::tcpstream::set_read_timeout(*self.0.as_inner(), duration.map(|d| d.as_millis() as u64))
76 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"Unable to set timeout value"))
79 pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
80 abi::tcpstream::set_write_timeout(
82 duration.map(|d| d.as_millis() as u64),
84 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"Unable to set timeout value"))
87 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
88 let duration = abi::tcpstream::get_read_timeout(*self.0.as_inner()).map_err(|_| {
89 io::Error::new_const(ErrorKind::Unknown, &"Unable to determine timeout value")
92 Ok(duration.map(|d| Duration::from_millis(d)))
95 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
96 let duration = abi::tcpstream::get_write_timeout(*self.0.as_inner()).map_err(|_| {
97 io::Error::new_const(ErrorKind::Unknown, &"Unable to determine timeout value")
100 Ok(duration.map(|d| Duration::from_millis(d)))
103 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
104 abi::tcpstream::peek(*self.0.as_inner(), buf)
105 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"peek failed"))
108 pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
109 self.read_vectored(&mut [IoSliceMut::new(buffer)])
112 pub fn read_vectored(&self, ioslice: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
113 let mut size: usize = 0;
115 for i in ioslice.iter_mut() {
116 let ret = abi::tcpstream::read(*self.0.as_inner(), &mut i[0..]).map_err(|_| {
117 io::Error::new_const(ErrorKind::Unknown, &"Unable to read on socket")
129 pub fn is_read_vectored(&self) -> bool {
133 pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
134 self.write_vectored(&[IoSlice::new(buffer)])
137 pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
138 let mut size: usize = 0;
140 for i in ioslice.iter() {
141 size += abi::tcpstream::write(*self.0.as_inner(), i).map_err(|_| {
142 io::Error::new_const(ErrorKind::Unknown, &"Unable to write on socket")
150 pub fn is_write_vectored(&self) -> bool {
154 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
155 let (ipaddr, port) = abi::tcpstream::peer_addr(*self.0.as_inner())
156 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"peer_addr failed"))?;
158 let saddr = match ipaddr {
159 Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
160 Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
162 return Err(io::Error::new_const(ErrorKind::Unknown, &"peer_addr failed"));
169 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
173 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
174 abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
175 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"unable to shutdown socket"))
178 pub fn duplicate(&self) -> io::Result<TcpStream> {
182 pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
183 abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
184 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"set_nodelay failed"))
187 pub fn nodelay(&self) -> io::Result<bool> {
188 abi::tcpstream::nodelay(*self.0.as_inner())
189 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"nodelay failed"))
192 pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
193 abi::tcpstream::set_tll(*self.0.as_inner(), tll)
194 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"unable to set TTL"))
197 pub fn ttl(&self) -> io::Result<u32> {
198 abi::tcpstream::get_tll(*self.0.as_inner())
199 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"unable to get TTL"))
202 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
206 pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
207 abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode)
208 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"unable to set blocking mode"))
212 impl fmt::Debug for TcpStream {
213 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 pub struct TcpListener(SocketAddr);
222 pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
225 Ok(TcpListener(*addr))
228 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
232 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
233 let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
234 .map_err(|_| io::Error::new_const(ErrorKind::Unknown, &"accept failed"))?;
235 let saddr = match ipaddr {
236 Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
237 Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
239 return Err(io::Error::new_const(ErrorKind::Unknown, &"accept failed"));
243 Ok((TcpStream(Arc::new(Socket(handle))), saddr))
246 pub fn duplicate(&self) -> io::Result<TcpListener> {
250 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
254 pub fn ttl(&self) -> io::Result<u32> {
258 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
262 pub fn only_v6(&self) -> io::Result<bool> {
266 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
270 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
275 impl fmt::Debug for TcpListener {
276 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
281 pub struct UdpSocket(abi::Handle);
284 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
288 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
292 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
296 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
300 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
304 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
308 pub fn duplicate(&self) -> io::Result<UdpSocket> {
312 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
316 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
320 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
324 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
328 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
332 pub fn broadcast(&self) -> io::Result<bool> {
336 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
340 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
344 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
348 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
352 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
356 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
360 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
364 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
368 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
372 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
376 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
380 pub fn ttl(&self) -> io::Result<u32> {
384 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
388 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
392 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
396 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
400 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
404 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
409 impl fmt::Debug for UdpSocket {
410 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
415 pub struct LookupHost(!);
418 pub fn port(&self) -> u16 {
423 impl Iterator for LookupHost {
424 type Item = SocketAddr;
425 fn next(&mut self) -> Option<SocketAddr> {
430 impl TryFrom<&str> for LookupHost {
431 type Error = io::Error;
433 fn try_from(_v: &str) -> io::Result<LookupHost> {
438 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
439 type Error = io::Error;
441 fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
446 #[allow(nonstandard_style)]
448 pub const AF_INET: u8 = 0;
449 pub const AF_INET6: u8 = 1;
450 pub type sa_family_t = u8;
452 #[derive(Copy, Clone)]
457 #[derive(Copy, Clone)]
458 pub struct sockaddr_in {
459 pub sin_family: sa_family_t,
461 pub sin_addr: in_addr,
464 #[derive(Copy, Clone)]
465 pub struct in6_addr {
466 pub s6_addr: [u8; 16],
469 #[derive(Copy, Clone)]
470 pub struct sockaddr_in6 {
471 pub sin6_family: sa_family_t,
473 pub sin6_addr: in6_addr,
474 pub sin6_flowinfo: u32,
475 pub sin6_scope_id: u32,
478 #[derive(Copy, Clone)]
479 pub struct sockaddr {}
481 pub type socklen_t = usize;