// Copyright 2018 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use fmt; use io; use net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr, ToSocketAddrs}; use time::Duration; use sys::{unsupported, Void, sgx_ineffective}; use sys::fd::FileDesc; use convert::TryFrom; use error; use sync::Arc; use super::abi::usercalls; const DEFAULT_FAKE_TTL: u32 = 64; #[derive(Debug, Clone)] struct Socket { inner: Arc, local_addr: String, } impl Socket { fn new(fd: usercalls::Fd, local_addr: String) -> Socket { Socket { inner: Arc::new(FileDesc::new(fd)), local_addr } } } #[derive(Debug, Clone)] pub struct TcpStream { inner: Socket, peer_addr: String, } fn io_err_to_addr(result: io::Result<&SocketAddr>) -> io::Result { match result { Ok(saddr) => Ok(saddr.to_string()), // need to downcast twice because io::Error::into_inner doesn't return the original // value if the conversion fails Err(e) => if e.get_ref().and_then(|e| e.downcast_ref::()).is_some() { Ok(e.into_inner().unwrap().downcast::().unwrap().host) } else { Err(e) } } } fn addr_to_sockaddr(addr: &str) -> io::Result { // unwrap OK: if an iterator is returned, we're guaranteed to get exactly one entry addr.to_socket_addrs().map(|mut it| it.next().unwrap()) } impl TcpStream { pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result { let addr = io_err_to_addr(addr)?; let (fd, local_addr, peer_addr) = usercalls::connect_stream(&addr)?; Ok(TcpStream { inner: Socket::new(fd, local_addr), peer_addr }) } pub fn connect_timeout(addr: &SocketAddr, _: Duration) -> io::Result { Self::connect(Ok(addr)) // FIXME: ignoring timeout } pub fn set_read_timeout(&self, _: Option) -> io::Result<()> { sgx_ineffective(()) } pub fn set_write_timeout(&self, _: Option) -> io::Result<()> { sgx_ineffective(()) } pub fn read_timeout(&self) -> io::Result> { sgx_ineffective(None) } pub fn write_timeout(&self) -> io::Result> { sgx_ineffective(None) } pub fn peek(&self, _: &mut [u8]) -> io::Result { Ok(0) } pub fn read(&self, buf: &mut [u8]) -> io::Result { self.inner.inner.read(buf) } pub fn write(&self, buf: &[u8]) -> io::Result { self.inner.inner.write(buf) } pub fn peer_addr(&self) -> io::Result { addr_to_sockaddr(&self.peer_addr) } pub fn socket_addr(&self) -> io::Result { addr_to_sockaddr(&self.inner.local_addr) } pub fn shutdown(&self, _: Shutdown) -> io::Result<()> { sgx_ineffective(()) } pub fn duplicate(&self) -> io::Result { Ok(self.clone()) } pub fn set_nodelay(&self, _: bool) -> io::Result<()> { sgx_ineffective(()) } pub fn nodelay(&self) -> io::Result { sgx_ineffective(false) } pub fn set_ttl(&self, _: u32) -> io::Result<()> { sgx_ineffective(()) } pub fn ttl(&self) -> io::Result { sgx_ineffective(DEFAULT_FAKE_TTL) } pub fn take_error(&self) -> io::Result> { Ok(None) } pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { sgx_ineffective(()) } } #[derive(Debug, Clone)] pub struct TcpListener { inner: Socket, } impl TcpListener { pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result { let addr = io_err_to_addr(addr)?; let (fd, local_addr) = usercalls::bind_stream(&addr)?; Ok(TcpListener { inner: Socket::new(fd, local_addr) }) } pub fn socket_addr(&self) -> io::Result { addr_to_sockaddr(&self.inner.local_addr) } pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> { let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?; let ret_peer = addr_to_sockaddr(&peer_addr).unwrap_or_else(|_| ([0; 4], 0).into()); Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer)) } pub fn duplicate(&self) -> io::Result { Ok(self.clone()) } pub fn set_ttl(&self, _: u32) -> io::Result<()> { sgx_ineffective(()) } pub fn ttl(&self) -> io::Result { sgx_ineffective(DEFAULT_FAKE_TTL) } pub fn set_only_v6(&self, _: bool) -> io::Result<()> { sgx_ineffective(()) } pub fn only_v6(&self) -> io::Result { sgx_ineffective(false) } pub fn take_error(&self) -> io::Result> { Ok(None) } pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { sgx_ineffective(()) } } pub struct UdpSocket(Void); impl UdpSocket { pub fn bind(_: io::Result<&SocketAddr>) -> io::Result { unsupported() } pub fn socket_addr(&self) -> io::Result { match self.0 {} } pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> { match self.0 {} } pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> { match self.0 {} } pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result { match self.0 {} } pub fn duplicate(&self) -> io::Result { match self.0 {} } pub fn set_read_timeout(&self, _: Option) -> io::Result<()> { match self.0 {} } pub fn set_write_timeout(&self, _: Option) -> io::Result<()> { match self.0 {} } pub fn read_timeout(&self) -> io::Result> { match self.0 {} } pub fn write_timeout(&self) -> io::Result> { match self.0 {} } pub fn set_broadcast(&self, _: bool) -> io::Result<()> { match self.0 {} } pub fn broadcast(&self) -> io::Result { match self.0 {} } pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> { match self.0 {} } pub fn multicast_loop_v4(&self) -> io::Result { match self.0 {} } pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> { match self.0 {} } pub fn multicast_ttl_v4(&self) -> io::Result { match self.0 {} } pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> { match self.0 {} } pub fn multicast_loop_v6(&self) -> io::Result { match self.0 {} } pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> { match self.0 {} } pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> { match self.0 {} } pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> { match self.0 {} } pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> { match self.0 {} } pub fn set_ttl(&self, _: u32) -> io::Result<()> { match self.0 {} } pub fn ttl(&self) -> io::Result { match self.0 {} } pub fn take_error(&self) -> io::Result> { match self.0 {} } pub fn set_nonblocking(&self, _: bool) -> io::Result<()> { match self.0 {} } pub fn recv(&self, _: &mut [u8]) -> io::Result { match self.0 {} } pub fn peek(&self, _: &mut [u8]) -> io::Result { match self.0 {} } pub fn send(&self, _: &[u8]) -> io::Result { match self.0 {} } pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> { match self.0 {} } } impl fmt::Debug for UdpSocket { fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { match self.0 {} } } #[derive(Debug)] pub struct NonIpSockAddr { host: String } impl error::Error for NonIpSockAddr { fn description(&self) -> &str { "Failed to convert address to SocketAddr" } } impl fmt::Display for NonIpSockAddr { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Failed to convert address to SocketAddr: {}", self.host) } } pub struct LookupHost(Void); impl LookupHost { fn new(host: String) -> io::Result { Err(io::Error::new(io::ErrorKind::Other, NonIpSockAddr { host })) } pub fn port(&self) -> u16 { match self.0 {} } } impl Iterator for LookupHost { type Item = SocketAddr; fn next(&mut self) -> Option { match self.0 {} } } impl<'a> TryFrom<&'a str> for LookupHost { type Error = io::Error; fn try_from(v: &'a str) -> io::Result { LookupHost::new(v.to_owned()) } } impl<'a> TryFrom<(&'a str, u16)> for LookupHost { type Error = io::Error; fn try_from((host, port): (&'a str, u16)) -> io::Result { LookupHost::new(format!("{}:{}", host, port)) } } #[allow(bad_style)] pub mod netc { pub const AF_INET: u8 = 0; pub const AF_INET6: u8 = 1; pub type sa_family_t = u8; #[derive(Copy, Clone)] pub struct in_addr { pub s_addr: u32, } #[derive(Copy, Clone)] pub struct sockaddr_in { pub sin_family: sa_family_t, pub sin_port: u16, pub sin_addr: in_addr, } #[derive(Copy, Clone)] pub struct in6_addr { pub s6_addr: [u8; 16], } #[derive(Copy, Clone)] pub struct sockaddr_in6 { pub sin6_family: sa_family_t, pub sin6_port: u16, pub sin6_addr: in6_addr, pub sin6_flowinfo: u32, pub sin6_scope_id: u32, } #[derive(Copy, Clone)] pub struct sockaddr { } pub type socklen_t = usize; }