add basic IP support in HermitCore

- add initial version to support sockets
- use TcpStream as test case
- HermitCore uses smoltcp as IP stack for pure Rust applications
- further functionalities (e.g. UDP support) will be added step by step
This commit is contained in:
Stefan Lankes 2020-03-19 08:34:22 +01:00
parent 57e1da59cd
commit 57b7203047
3 changed files with 158 additions and 89 deletions

View File

@ -1349,9 +1349,9 @@ dependencies = [
[[package]]
name = "hermit-abi"
version = "0.1.1"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f22b8f315b98f415780ddbe9163c7dbbc5a07225b6d102ace1d8aeef85775140"
checksum = "1010591b26bbfe835e9faeabeb11866061cc7dcebffd56ad7d0942d0e61aefd8"
dependencies = [
"compiler_builtins",
"libc",

View File

@ -93,9 +93,7 @@ pub fn hashmap_random_keys() -> (u64, u64) {
#[cfg(not(test))]
pub fn init() {
unsafe {
let _ = net::init();
}
let _ = net::init();
}
#[cfg(not(test))]

View File

@ -1,291 +1,362 @@
use crate::convert::TryFrom;
use crate::fmt;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
use crate::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
use crate::str;
use crate::sys::hermit::abi;
use crate::sys::{unsupported, Void};
use crate::time::Duration;
//// Iinitializes HermitCore's network stack
pub unsafe fn init() -> io::Result<()> {
/// Checks whether the HermitCore's socket interface has been started already, and
/// if not, starts it.
pub fn init() -> io::Result<()> {
if abi::network_init() < 0 {
return Err(io::Error::new(ErrorKind::Other, "Unable to initialize network interface"));
}
Ok(())
}
pub struct TcpStream(Void);
pub struct TcpStream(abi::Handle);
impl TcpStream {
pub fn connect(_: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
unsupported()
pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
let addr = addr?;
match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
Ok(handle) => Ok(TcpStream(handle)),
_ => {
Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
}
}
}
pub fn connect_timeout(_: &SocketAddr, _: Duration) -> io::Result<TcpStream> {
unsupported()
pub fn connect_timeout(saddr: &SocketAddr, duration: Duration) -> io::Result<TcpStream> {
match abi::tcpstream::connect(
saddr.ip().to_string().as_bytes(),
saddr.port(),
Some(duration.as_millis() as u64),
) {
Ok(handle) => Ok(TcpStream(handle)),
_ => {
Err(io::Error::new(ErrorKind::Other, "Unable to initiate a connection on a socket"))
}
}
}
pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
match self.0 {}
pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
abi::tcpstream::set_read_timeout(self.0, duration.map(|d| d.as_millis() as u64))
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
}
pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
match self.0 {}
pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
abi::tcpstream::set_write_timeout(self.0, duration.map(|d| d.as_millis() as u64))
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to set timeout value"))
}
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
match self.0 {}
let duration = abi::tcpstream::get_read_timeout(self.0)
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
Ok(duration.map(|d| Duration::from_millis(d)))
}
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
match self.0 {}
let duration = abi::tcpstream::get_write_timeout(self.0)
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to determine timeout value"))?;
Ok(duration.map(|d| Duration::from_millis(d)))
}
pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
match self.0 {}
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
abi::tcpstream::peek(self.0, buf)
.map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
}
pub fn read(&self, _: &mut [u8]) -> io::Result<usize> {
match self.0 {}
pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
self.read_vectored(&mut [IoSliceMut::new(buffer)])
}
pub fn read_vectored(&self, _: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
match self.0 {}
pub fn read_vectored(&self, ioslice: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
let mut size: usize = 0;
for i in ioslice.iter_mut() {
let mut pos: usize = 0;
while pos < i.len() {
let ret = abi::tcpstream::read(self.0, &mut i[pos..])
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to read on socket"))?;
if ret == 0 {
return Ok(size);
} else {
size += ret;
pos += ret;
}
}
}
Ok(size)
}
pub fn write(&self, _: &[u8]) -> io::Result<usize> {
match self.0 {}
pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(buffer)])
}
pub fn write_vectored(&self, _: &[IoSlice<'_>]) -> io::Result<usize> {
match self.0 {}
pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
let mut size: usize = 0;
for i in ioslice.iter() {
size += abi::tcpstream::write(self.0, i)
.map_err(|_| io::Error::new(ErrorKind::Other, "Unable to write on socket"))?;
}
Ok(size)
}
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "peer_addr isn't supported"))
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "socket_addr isn't supported"))
}
pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
match self.0 {}
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
abi::tcpstream::shutdown(self.0, how as i32)
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to shutdown socket"))
}
pub fn duplicate(&self) -> io::Result<TcpStream> {
match self.0 {}
let handle = abi::tcpstream::duplicate(self.0)
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to duplicate stream"))?;
Ok(TcpStream(handle))
}
pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
match self.0 {}
pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
abi::tcpstream::set_nodelay(self.0, mode)
.map_err(|_| io::Error::new(ErrorKind::Other, "set_nodelay failed"))
}
pub fn nodelay(&self) -> io::Result<bool> {
match self.0 {}
abi::tcpstream::nodelay(self.0)
.map_err(|_| io::Error::new(ErrorKind::Other, "nodelay failed"))
}
pub fn set_ttl(&self, _: u32) -> io::Result<()> {
match self.0 {}
pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
abi::tcpstream::set_tll(self.0, tll)
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to set TTL"))
}
pub fn ttl(&self) -> io::Result<u32> {
match self.0 {}
abi::tcpstream::get_tll(self.0)
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to get TTL"))
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "take_error isn't supported"))
}
pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
match self.0 {}
pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
abi::tcpstream::set_nonblocking(self.0, mode)
.map_err(|_| io::Error::new(ErrorKind::Other, "unable to set blocking mode"))
}
}
impl Drop for TcpStream {
fn drop(&mut self) {
let _ = abi::tcpstream::close(self.0);
}
}
impl fmt::Debug for TcpStream {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {}
Ok(())
}
}
pub struct TcpListener(Void);
pub struct TcpListener(abi::Handle);
impl TcpListener {
pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
unsupported()
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn duplicate(&self) -> io::Result<TcpListener> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_ttl(&self, _: u32) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn ttl(&self) -> io::Result<u32> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn only_v6(&self) -> io::Result<bool> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
}
impl fmt::Debug for TcpListener {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {}
Ok(())
}
}
pub struct UdpSocket(Void);
pub struct UdpSocket(abi::Handle);
impl UdpSocket {
pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
unsupported()
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn duplicate(&self) -> io::Result<UdpSocket> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn broadcast(&self) -> io::Result<bool> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn multicast_loop_v6(&self) -> io::Result<bool> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_ttl(&self, _: u32) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn ttl(&self) -> io::Result<u32> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn send(&self, _: &[u8]) -> io::Result<usize> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
match self.0 {}
Err(io::Error::new(ErrorKind::Other, "not supported"))
}
}
impl fmt::Debug for UdpSocket {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {}
Ok(())
}
}