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"))?;
102 pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
103 self.write_vectored(&[IoSlice::new(buffer)])
106 pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
107 let mut size: usize = 0;
109 for i in ioslice.iter() {
110 size += abi::tcpstream::write(self.0, i)
111 .map_err(|_| io::Error::new(ErrorKind::Other, "Unable to write on socket"))?;
117 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
118 Err(io::Error::new(ErrorKind::Other, "peer_addr isn't supported"))
121 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
122 Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
125 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
126 abi::tcpstream::shutdown(self.0, how as i32)
127 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to shutdown socket"))
130 pub fn duplicate(&self) -> io::Result<TcpStream> {
131 let handle = abi::tcpstream::duplicate(self.0)
132 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to duplicate stream"))?;
134 Ok(TcpStream(handle))
137 pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
138 abi::tcpstream::set_nodelay(self.0, mode)
139 .map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
142 pub fn nodelay(&self) -> io::Result<bool> {
143 abi::tcpstream::nodelay(self.0)
144 .map_err(|_| io::Error::new(ErrorKind::Other, "nodelay failed"))
147 pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
148 abi::tcpstream::set_tll(self.0, tll)
149 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set TTL"))
152 pub fn ttl(&self) -> io::Result<u32> {
153 abi::tcpstream::get_tll(self.0)
154 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to get TTL"))
157 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
158 Err(io::Error::new(ErrorKind::Other, "take_error isn't supported"))
161 pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
162 abi::tcpstream::set_nonblocking(self.0, mode)
163 .map_err(|_| io::Error::new(ErrorKind::Other, "unable to set blocking mode"))
167 impl Drop for TcpStream {
169 let _ = abi::tcpstream::close(self.0);
173 impl fmt::Debug for TcpStream {
174 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
179 pub struct TcpListener(abi::Handle);
182 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
183 Err(io::Error::new(ErrorKind::Other, "not supported"))
186 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
187 Err(io::Error::new(ErrorKind::Other, "not supported"))
190 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
191 Err(io::Error::new(ErrorKind::Other, "not supported"))
194 pub fn duplicate(&self) -> io::Result<TcpListener> {
195 Err(io::Error::new(ErrorKind::Other, "not supported"))
198 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
199 Err(io::Error::new(ErrorKind::Other, "not supported"))
202 pub fn ttl(&self) -> io::Result<u32> {
203 Err(io::Error::new(ErrorKind::Other, "not supported"))
206 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
207 Err(io::Error::new(ErrorKind::Other, "not supported"))
210 pub fn only_v6(&self) -> io::Result<bool> {
211 Err(io::Error::new(ErrorKind::Other, "not supported"))
214 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
215 Err(io::Error::new(ErrorKind::Other, "not supported"))
218 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
219 Err(io::Error::new(ErrorKind::Other, "not supported"))
223 impl fmt::Debug for TcpListener {
224 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
229 pub struct UdpSocket(abi::Handle);
232 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
233 Err(io::Error::new(ErrorKind::Other, "not supported"))
236 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
237 Err(io::Error::new(ErrorKind::Other, "not supported"))
240 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
241 Err(io::Error::new(ErrorKind::Other, "not supported"))
244 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
245 Err(io::Error::new(ErrorKind::Other, "not supported"))
248 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
249 Err(io::Error::new(ErrorKind::Other, "not supported"))
252 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
253 Err(io::Error::new(ErrorKind::Other, "not supported"))
256 pub fn duplicate(&self) -> io::Result<UdpSocket> {
257 Err(io::Error::new(ErrorKind::Other, "not supported"))
260 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
261 Err(io::Error::new(ErrorKind::Other, "not supported"))
264 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
265 Err(io::Error::new(ErrorKind::Other, "not supported"))
268 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
269 Err(io::Error::new(ErrorKind::Other, "not supported"))
272 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
273 Err(io::Error::new(ErrorKind::Other, "not supported"))
276 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
277 Err(io::Error::new(ErrorKind::Other, "not supported"))
280 pub fn broadcast(&self) -> io::Result<bool> {
281 Err(io::Error::new(ErrorKind::Other, "not supported"))
284 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
285 Err(io::Error::new(ErrorKind::Other, "not supported"))
288 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
289 Err(io::Error::new(ErrorKind::Other, "not supported"))
292 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
293 Err(io::Error::new(ErrorKind::Other, "not supported"))
296 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
297 Err(io::Error::new(ErrorKind::Other, "not supported"))
300 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
301 Err(io::Error::new(ErrorKind::Other, "not supported"))
304 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
305 Err(io::Error::new(ErrorKind::Other, "not supported"))
308 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
309 Err(io::Error::new(ErrorKind::Other, "not supported"))
312 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
313 Err(io::Error::new(ErrorKind::Other, "not supported"))
316 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
317 Err(io::Error::new(ErrorKind::Other, "not supported"))
320 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
321 Err(io::Error::new(ErrorKind::Other, "not supported"))
324 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
325 Err(io::Error::new(ErrorKind::Other, "not supported"))
328 pub fn ttl(&self) -> io::Result<u32> {
329 Err(io::Error::new(ErrorKind::Other, "not supported"))
332 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
333 Err(io::Error::new(ErrorKind::Other, "not supported"))
336 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
337 Err(io::Error::new(ErrorKind::Other, "not supported"))
340 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
341 Err(io::Error::new(ErrorKind::Other, "not supported"))
344 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
345 Err(io::Error::new(ErrorKind::Other, "not supported"))
348 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
349 Err(io::Error::new(ErrorKind::Other, "not supported"))
352 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
353 Err(io::Error::new(ErrorKind::Other, "not supported"))
357 impl fmt::Debug for UdpSocket {
358 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
363 pub struct LookupHost(Void);
366 pub fn port(&self) -> u16 {
371 impl Iterator for LookupHost {
372 type Item = SocketAddr;
373 fn next(&mut self) -> Option<SocketAddr> {
378 impl TryFrom<&str> for LookupHost {
379 type Error = io::Error;
381 fn try_from(_v: &str) -> io::Result<LookupHost> {
386 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
387 type Error = io::Error;
389 fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
394 #[allow(nonstandard_style)]
396 pub const AF_INET: u8 = 0;
397 pub const AF_INET6: u8 = 1;
398 pub type sa_family_t = u8;
400 #[derive(Copy, Clone)]
405 #[derive(Copy, Clone)]
406 pub struct sockaddr_in {
407 pub sin_family: sa_family_t,
409 pub sin_addr: in_addr,
412 #[derive(Copy, Clone)]
413 pub struct in6_addr {
414 pub s6_addr: [u8; 16],
417 #[derive(Copy, Clone)]
418 pub struct sockaddr_in6 {
419 pub sin6_family: sa_family_t,
421 pub sin6_addr: in6_addr,
422 pub sin6_flowinfo: u32,
423 pub sin6_scope_id: u32,
426 #[derive(Copy, Clone)]
427 pub struct sockaddr {}
429 pub type socklen_t = usize;