2 use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
3 use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
6 use crate::sys::hermit::abi;
7 use crate::sys::hermit::abi::IpAddress::{Ipv4, Ipv6};
8 use crate::sys::unsupported;
9 use crate::sys_common::AsInner;
10 use crate::time::Duration;
12 /// Checks whether the HermitCore's socket interface has been started already, and
13 /// if not, starts it.
14 pub fn init() -> io::Result<()> {
15 if abi::network_init() < 0 {
16 return Err(io::const_io_error!(
17 ErrorKind::Uncategorized,
18 "Unable to initialize network interface",
25 #[derive(Debug, Clone)]
26 pub struct Socket(abi::Handle);
28 impl AsInner<abi::Handle> for Socket {
29 fn as_inner(&self) -> &abi::Handle {
34 impl Drop for Socket {
36 let _ = abi::tcpstream::close(self.0);
40 // Arc is used to count the number of used sockets.
41 // Only if all sockets are released, the drop
42 // method will close the socket.
44 pub struct TcpStream(Arc<Socket>);
47 pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
50 match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
51 Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
52 _ => Err(io::const_io_error!(
53 ErrorKind::Uncategorized,
54 "Unable to initiate a connection on a socket",
59 pub fn connect_timeout(saddr: &SocketAddr, duration: Duration) -> io::Result<TcpStream> {
60 match abi::tcpstream::connect(
61 saddr.ip().to_string().as_bytes(),
63 Some(duration.as_millis() as u64),
65 Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
66 _ => Err(io::const_io_error!(
67 ErrorKind::Uncategorized,
68 "Unable to initiate a connection on a socket",
73 pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
74 abi::tcpstream::set_read_timeout(*self.0.as_inner(), duration.map(|d| d.as_millis() as u64))
76 io::const_io_error!(ErrorKind::Uncategorized, "Unable to set timeout value")
80 pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
81 abi::tcpstream::set_write_timeout(
83 duration.map(|d| d.as_millis() as u64),
85 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "Unable to set timeout value"))
88 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
89 let duration = abi::tcpstream::get_read_timeout(*self.0.as_inner()).map_err(|_| {
90 io::const_io_error!(ErrorKind::Uncategorized, "Unable to determine timeout value")
93 Ok(duration.map(|d| Duration::from_millis(d)))
96 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
97 let duration = abi::tcpstream::get_write_timeout(*self.0.as_inner()).map_err(|_| {
98 io::const_io_error!(ErrorKind::Uncategorized, "Unable to determine timeout value")
101 Ok(duration.map(|d| Duration::from_millis(d)))
104 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
105 abi::tcpstream::peek(*self.0.as_inner(), buf)
106 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "peek failed"))
109 pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
110 self.read_vectored(&mut [IoSliceMut::new(buffer)])
113 pub fn read_vectored(&self, ioslice: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
114 let mut size: usize = 0;
116 for i in ioslice.iter_mut() {
117 let ret = abi::tcpstream::read(*self.0.as_inner(), &mut i[0..]).map_err(|_| {
118 io::const_io_error!(ErrorKind::Uncategorized, "Unable to read on socket")
130 pub fn is_read_vectored(&self) -> bool {
134 pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
135 self.write_vectored(&[IoSlice::new(buffer)])
138 pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
139 let mut size: usize = 0;
141 for i in ioslice.iter() {
142 size += abi::tcpstream::write(*self.0.as_inner(), i).map_err(|_| {
143 io::const_io_error!(ErrorKind::Uncategorized, "Unable to write on socket")
151 pub fn is_write_vectored(&self) -> bool {
155 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
156 let (ipaddr, port) = abi::tcpstream::peer_addr(*self.0.as_inner())
157 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "peer_addr failed"))?;
159 let saddr = match ipaddr {
160 Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
161 Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
163 return Err(io::const_io_error!(ErrorKind::Uncategorized, "peer_addr failed"));
170 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
174 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
175 abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
176 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "unable to shutdown socket"))
179 pub fn duplicate(&self) -> io::Result<TcpStream> {
183 pub fn set_linger(&self, _linger: Option<Duration>) -> io::Result<()> {
187 pub fn linger(&self) -> io::Result<Option<Duration>> {
191 pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
192 abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
193 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "set_nodelay failed"))
196 pub fn nodelay(&self) -> io::Result<bool> {
197 abi::tcpstream::nodelay(*self.0.as_inner())
198 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "nodelay failed"))
201 pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
202 abi::tcpstream::set_tll(*self.0.as_inner(), tll)
203 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "unable to set TTL"))
206 pub fn ttl(&self) -> io::Result<u32> {
207 abi::tcpstream::get_tll(*self.0.as_inner())
208 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "unable to get TTL"))
211 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
215 pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
216 abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode).map_err(|_| {
217 io::const_io_error!(ErrorKind::Uncategorized, "unable to set blocking mode")
222 impl fmt::Debug for TcpStream {
223 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
229 pub struct TcpListener(SocketAddr);
232 pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
235 Ok(TcpListener(*addr))
238 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
242 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
243 let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
244 .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "accept failed"))?;
245 let saddr = match ipaddr {
246 Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
247 Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
249 return Err(io::const_io_error!(ErrorKind::Uncategorized, "accept failed"));
253 Ok((TcpStream(Arc::new(Socket(handle))), saddr))
256 pub fn duplicate(&self) -> io::Result<TcpListener> {
260 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
264 pub fn ttl(&self) -> io::Result<u32> {
268 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
272 pub fn only_v6(&self) -> io::Result<bool> {
276 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
280 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
285 impl fmt::Debug for TcpListener {
286 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
291 pub struct UdpSocket(abi::Handle);
294 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
298 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
302 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
306 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
310 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
314 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
318 pub fn duplicate(&self) -> io::Result<UdpSocket> {
322 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
326 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
330 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
334 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
338 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
342 pub fn broadcast(&self) -> io::Result<bool> {
346 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
350 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
354 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
358 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
362 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
366 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
370 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
374 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
378 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
382 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
386 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
390 pub fn ttl(&self) -> io::Result<u32> {
394 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
398 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
402 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
406 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
410 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
414 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
419 impl fmt::Debug for UdpSocket {
420 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
425 pub struct LookupHost(!);
428 pub fn port(&self) -> u16 {
433 impl Iterator for LookupHost {
434 type Item = SocketAddr;
435 fn next(&mut self) -> Option<SocketAddr> {
440 impl TryFrom<&str> for LookupHost {
441 type Error = io::Error;
443 fn try_from(_v: &str) -> io::Result<LookupHost> {
448 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
449 type Error = io::Error;
451 fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
456 #[allow(nonstandard_style)]
458 pub const AF_INET: u8 = 0;
459 pub const AF_INET6: u8 = 1;
460 pub type sa_family_t = u8;
462 #[derive(Copy, Clone)]
467 #[derive(Copy, Clone)]
468 pub struct sockaddr_in {
469 pub sin_family: sa_family_t,
471 pub sin_addr: in_addr,
474 #[derive(Copy, Clone)]
475 pub struct in6_addr {
476 pub s6_addr: [u8; 16],
479 #[derive(Copy, Clone)]
480 pub struct sockaddr_in6 {
481 pub sin6_family: sa_family_t,
483 pub sin6_addr: in6_addr,
484 pub sin6_flowinfo: u32,
485 pub sin6_scope_id: u32,
488 #[derive(Copy, Clone)]
489 pub struct sockaddr {}