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::Other, &"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::Other, &"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::Other, &"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::Other, &"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::Other, &"set_nodelay 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..])
117 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"Unable to read on socket"))?;
128 pub fn is_read_vectored(&self) -> bool {
132 pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
133 self.write_vectored(&[IoSlice::new(buffer)])
136 pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
137 let mut size: usize = 0;
139 for i in ioslice.iter() {
140 size += abi::tcpstream::write(*self.0.as_inner(), i).map_err(|_| {
141 io::Error::new_const(ErrorKind::Other, &"Unable to write on socket")
149 pub fn is_write_vectored(&self) -> bool {
153 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
154 let (ipaddr, port) = abi::tcpstream::peer_addr(*self.0.as_inner())
155 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"peer_addr failed"))?;
157 let saddr = match ipaddr {
158 Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
159 Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
161 return Err(io::Error::new_const(ErrorKind::Other, &"peer_addr failed"));
168 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
172 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
173 abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
174 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to shutdown socket"))
177 pub fn duplicate(&self) -> io::Result<TcpStream> {
181 pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
182 abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
183 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"set_nodelay failed"))
186 pub fn nodelay(&self) -> io::Result<bool> {
187 abi::tcpstream::nodelay(*self.0.as_inner())
188 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"nodelay failed"))
191 pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
192 abi::tcpstream::set_tll(*self.0.as_inner(), tll)
193 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to set TTL"))
196 pub fn ttl(&self) -> io::Result<u32> {
197 abi::tcpstream::get_tll(*self.0.as_inner())
198 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to get TTL"))
201 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
205 pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
206 abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode)
207 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"unable to set blocking mode"))
211 impl fmt::Debug for TcpStream {
212 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 pub struct TcpListener(SocketAddr);
221 pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
224 Ok(TcpListener(*addr))
227 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
231 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
232 let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
233 .map_err(|_| io::Error::new_const(ErrorKind::Other, &"accept failed"))?;
234 let saddr = match ipaddr {
235 Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
236 Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
238 return Err(io::Error::new_const(ErrorKind::Other, &"accept failed"));
242 Ok((TcpStream(Arc::new(Socket(handle))), saddr))
245 pub fn duplicate(&self) -> io::Result<TcpListener> {
249 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
253 pub fn ttl(&self) -> io::Result<u32> {
257 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
261 pub fn only_v6(&self) -> io::Result<bool> {
265 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
269 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
274 impl fmt::Debug for TcpListener {
275 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
280 pub struct UdpSocket(abi::Handle);
283 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
287 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
291 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
295 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
299 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
303 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
307 pub fn duplicate(&self) -> io::Result<UdpSocket> {
311 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
315 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
319 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
323 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
327 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
331 pub fn broadcast(&self) -> io::Result<bool> {
335 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
339 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
343 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
347 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
351 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
355 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
359 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
363 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
367 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
371 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
375 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
379 pub fn ttl(&self) -> io::Result<u32> {
383 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
387 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
391 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
395 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
399 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
403 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
408 impl fmt::Debug for UdpSocket {
409 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
414 pub struct LookupHost(!);
417 pub fn port(&self) -> u16 {
422 impl Iterator for LookupHost {
423 type Item = SocketAddr;
424 fn next(&mut self) -> Option<SocketAddr> {
429 impl TryFrom<&str> for LookupHost {
430 type Error = io::Error;
432 fn try_from(_v: &str) -> io::Result<LookupHost> {
437 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
438 type Error = io::Error;
440 fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
445 #[allow(nonstandard_style)]
447 pub const AF_INET: u8 = 0;
448 pub const AF_INET6: u8 = 1;
449 pub type sa_family_t = u8;
451 #[derive(Copy, Clone)]
456 #[derive(Copy, Clone)]
457 pub struct sockaddr_in {
458 pub sin_family: sa_family_t,
460 pub sin_addr: in_addr,
463 #[derive(Copy, Clone)]
464 pub struct in6_addr {
465 pub s6_addr: [u8; 16],
468 #[derive(Copy, Clone)]
469 pub struct sockaddr_in6 {
470 pub sin6_family: sa_family_t,
472 pub sin6_addr: in6_addr,
473 pub sin6_flowinfo: u32,
474 pub sin6_scope_id: u32,
477 #[derive(Copy, Clone)]
478 pub struct sockaddr {}
480 pub type socklen_t = usize;