1 use crate::convert::TryFrom;
3 use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
4 use crate::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
6 use crate::sys::hermit::abi;
7 use crate::sys::{unsupported, Void};
8 use crate::time::Duration;
10 /// Checks whether the HermitCore's socket interface has been started already, and
11 /// if not, starts it.
12 pub fn init() -> io::Result<()> {
13 if abi::network_init() < 0 {
14 return Err(io::Error::new(ErrorKind::Other, "Unable to initialize network interface"));
20 pub struct TcpStream(abi::Handle);
23 pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
26 match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
27 Ok(handle) => Ok(TcpStream(handle)),
29 Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
34 pub fn connect_timeout(saddr: &SocketAddr, duration: Duration) -> io::Result<TcpStream> {
35 match abi::tcpstream::connect(
36 saddr.ip().to_string().as_bytes(),
38 Some(duration.as_millis() as u64),
40 Ok(handle) => Ok(TcpStream(handle)),
42 Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
47 pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
48 abi::tcpstream::set_read_timeout(self.0, duration.map(|d| d.as_millis() as u64))
49 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
52 pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
53 abi::tcpstream::set_write_timeout(self.0, duration.map(|d| d.as_millis() as u64))
54 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
57 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
58 let duration = abi::tcpstream::get_read_timeout(self.0)
59 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
61 Ok(duration.map(|d| Duration::from_millis(d)))
64 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
65 let duration = abi::tcpstream::get_write_timeout(self.0)
66 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
68 Ok(duration.map(|d| Duration::from_millis(d)))
71 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
72 abi::tcpstream::peek(self.0, buf)
73 .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
76 pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
77 self.read_vectored(&mut [IoSliceMut::new(buffer)])
80 pub fn read_vectored(&self, ioslice: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
81 let mut size: usize = 0;
83 for i in ioslice.iter_mut() {
84 let mut pos: usize = 0;
87 let ret = abi::tcpstream::read(self.0, &mut i[pos..])
88 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to read on socket"))?;
103 pub fn is_read_vectored(&self) -> bool {
107 pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
108 self.write_vectored(&[IoSlice::new(buffer)])
111 pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
112 let mut size: usize = 0;
114 for i in ioslice.iter() {
115 size += abi::tcpstream::write(self.0, i)
116 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to write on socket"))?;
123 pub fn is_write_vectored(&self) -> bool {
127 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
128 Err(io::Error::new(ErrorKind::Other, "peer_addr isn't supported"))
131 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
132 Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
135 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
136 abi::tcpstream::shutdown(self.0, how as i32)
137 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to shutdown socket"))
140 pub fn duplicate(&self) -> io::Result<TcpStream> {
141 let handle = abi::tcpstream::duplicate(self.0)
142 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to duplicate stream"))?;
144 Ok(TcpStream(handle))
147 pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
148 abi::tcpstream::set_nodelay(self.0, mode)
149 .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
152 pub fn nodelay(&self) -> io::Result<bool> {
153 abi::tcpstream::nodelay(self.0)
154 .map_err(|_| io::Error::new(ErrorKind::Other, "nodelay failed"))
157 pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
158 abi::tcpstream::set_tll(self.0, tll)
159 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set TTL"))
162 pub fn ttl(&self) -> io::Result<u32> {
163 abi::tcpstream::get_tll(self.0)
164 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to get TTL"))
167 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
168 Err(io::Error::new(ErrorKind::Other, "take_error isn't supported"))
171 pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
172 abi::tcpstream::set_nonblocking(self.0, mode)
173 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set blocking mode"))
177 impl Drop for TcpStream {
179 let _ = abi::tcpstream::close(self.0);
183 impl fmt::Debug for TcpStream {
184 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
189 pub struct TcpListener(abi::Handle);
192 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
193 Err(io::Error::new(ErrorKind::Other, "not supported"))
196 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
197 Err(io::Error::new(ErrorKind::Other, "not supported"))
200 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
201 Err(io::Error::new(ErrorKind::Other, "not supported"))
204 pub fn duplicate(&self) -> io::Result<TcpListener> {
205 Err(io::Error::new(ErrorKind::Other, "not supported"))
208 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
209 Err(io::Error::new(ErrorKind::Other, "not supported"))
212 pub fn ttl(&self) -> io::Result<u32> {
213 Err(io::Error::new(ErrorKind::Other, "not supported"))
216 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
217 Err(io::Error::new(ErrorKind::Other, "not supported"))
220 pub fn only_v6(&self) -> io::Result<bool> {
221 Err(io::Error::new(ErrorKind::Other, "not supported"))
224 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
225 Err(io::Error::new(ErrorKind::Other, "not supported"))
228 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
229 Err(io::Error::new(ErrorKind::Other, "not supported"))
233 impl fmt::Debug for TcpListener {
234 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
239 pub struct UdpSocket(abi::Handle);
242 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
243 Err(io::Error::new(ErrorKind::Other, "not supported"))
246 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
247 Err(io::Error::new(ErrorKind::Other, "not supported"))
250 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
251 Err(io::Error::new(ErrorKind::Other, "not supported"))
254 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
255 Err(io::Error::new(ErrorKind::Other, "not supported"))
258 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
259 Err(io::Error::new(ErrorKind::Other, "not supported"))
262 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
263 Err(io::Error::new(ErrorKind::Other, "not supported"))
266 pub fn duplicate(&self) -> io::Result<UdpSocket> {
267 Err(io::Error::new(ErrorKind::Other, "not supported"))
270 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
271 Err(io::Error::new(ErrorKind::Other, "not supported"))
274 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
275 Err(io::Error::new(ErrorKind::Other, "not supported"))
278 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
279 Err(io::Error::new(ErrorKind::Other, "not supported"))
282 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
283 Err(io::Error::new(ErrorKind::Other, "not supported"))
286 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
287 Err(io::Error::new(ErrorKind::Other, "not supported"))
290 pub fn broadcast(&self) -> io::Result<bool> {
291 Err(io::Error::new(ErrorKind::Other, "not supported"))
294 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
295 Err(io::Error::new(ErrorKind::Other, "not supported"))
298 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
299 Err(io::Error::new(ErrorKind::Other, "not supported"))
302 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
303 Err(io::Error::new(ErrorKind::Other, "not supported"))
306 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
307 Err(io::Error::new(ErrorKind::Other, "not supported"))
310 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
311 Err(io::Error::new(ErrorKind::Other, "not supported"))
314 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
315 Err(io::Error::new(ErrorKind::Other, "not supported"))
318 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
319 Err(io::Error::new(ErrorKind::Other, "not supported"))
322 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
323 Err(io::Error::new(ErrorKind::Other, "not supported"))
326 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
327 Err(io::Error::new(ErrorKind::Other, "not supported"))
330 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
331 Err(io::Error::new(ErrorKind::Other, "not supported"))
334 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
335 Err(io::Error::new(ErrorKind::Other, "not supported"))
338 pub fn ttl(&self) -> io::Result<u32> {
339 Err(io::Error::new(ErrorKind::Other, "not supported"))
342 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
343 Err(io::Error::new(ErrorKind::Other, "not supported"))
346 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
347 Err(io::Error::new(ErrorKind::Other, "not supported"))
350 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
351 Err(io::Error::new(ErrorKind::Other, "not supported"))
354 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
355 Err(io::Error::new(ErrorKind::Other, "not supported"))
358 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
359 Err(io::Error::new(ErrorKind::Other, "not supported"))
362 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
363 Err(io::Error::new(ErrorKind::Other, "not supported"))
367 impl fmt::Debug for UdpSocket {
368 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
373 pub struct LookupHost(Void);
376 pub fn port(&self) -> u16 {
381 impl Iterator for LookupHost {
382 type Item = SocketAddr;
383 fn next(&mut self) -> Option<SocketAddr> {
388 impl TryFrom<&str> for LookupHost {
389 type Error = io::Error;
391 fn try_from(_v: &str) -> io::Result<LookupHost> {
396 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
397 type Error = io::Error;
399 fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
404 #[allow(nonstandard_style)]
406 pub const AF_INET: u8 = 0;
407 pub const AF_INET6: u8 = 1;
408 pub type sa_family_t = u8;
410 #[derive(Copy, Clone)]
415 #[derive(Copy, Clone)]
416 pub struct sockaddr_in {
417 pub sin_family: sa_family_t,
419 pub sin_addr: in_addr,
422 #[derive(Copy, Clone)]
423 pub struct in6_addr {
424 pub s6_addr: [u8; 16],
427 #[derive(Copy, Clone)]
428 pub struct sockaddr_in6 {
429 pub sin6_family: sa_family_t,
431 pub sin6_addr: in6_addr,
432 pub sin6_flowinfo: u32,
433 pub sin6_scope_id: u32,
436 #[derive(Copy, Clone)]
437 pub struct sockaddr {}
439 pub type socklen_t = usize;