2 use io::{self, IoVec, IoVecMut};
3 use net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr, ToSocketAddrs};
5 use sys::{unsupported, Void, sgx_ineffective, AsInner, FromInner, IntoInner, TryIntoInner};
11 use super::abi::usercalls;
13 const DEFAULT_FAKE_TTL: u32 = 64;
15 #[derive(Debug, Clone)]
18 local_addr: Option<String>,
22 fn new(fd: usercalls::raw::Fd, local_addr: String) -> Socket {
23 Socket { inner: Arc::new(FileDesc::new(fd)), local_addr: Some(local_addr) }
27 impl AsInner<FileDesc> for Socket {
28 fn as_inner(&self) -> &FileDesc { &self.inner }
31 impl TryIntoInner<FileDesc> for Socket {
32 fn try_into_inner(self) -> Result<FileDesc, Socket> {
33 let Socket { inner, local_addr } = self;
34 Arc::try_unwrap(inner).map_err(|inner| Socket { inner, local_addr } )
38 impl FromInner<FileDesc> for Socket {
39 fn from_inner(inner: FileDesc) -> Socket {
40 Socket { inner: Arc::new(inner), local_addr: None }
44 #[derive(Debug, Clone)]
45 pub struct TcpStream {
47 peer_addr: Option<String>,
50 fn io_err_to_addr(result: io::Result<&SocketAddr>) -> io::Result<String> {
52 Ok(saddr) => Ok(saddr.to_string()),
53 // need to downcast twice because io::Error::into_inner doesn't return the original
54 // value if the conversion fails
55 Err(e) => if e.get_ref().and_then(|e| e.downcast_ref::<NonIpSockAddr>()).is_some() {
56 Ok(e.into_inner().unwrap().downcast::<NonIpSockAddr>().unwrap().host)
63 fn addr_to_sockaddr(addr: &Option<String>) -> io::Result<SocketAddr> {
65 .ok_or(io::ErrorKind::AddrNotAvailable)?
67 // unwrap OK: if an iterator is returned, we're guaranteed to get exactly one entry
68 .map(|mut it| it.next().unwrap())
72 pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
73 let addr = io_err_to_addr(addr)?;
74 let (fd, local_addr, peer_addr) = usercalls::connect_stream(&addr)?;
75 Ok(TcpStream { inner: Socket::new(fd, local_addr), peer_addr: Some(peer_addr) })
78 pub fn connect_timeout(addr: &SocketAddr, _: Duration) -> io::Result<TcpStream> {
79 Self::connect(Ok(addr)) // FIXME: ignoring timeout
82 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
86 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
90 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
94 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
98 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
102 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
103 self.inner.inner.read(buf)
106 pub fn read_vectored(&self, buf: &mut [IoVecMut<'_>]) -> io::Result<usize> {
107 let buf = match buf.get_mut(0) {
109 None => return Ok(0),
114 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
115 self.inner.inner.write(buf)
118 pub fn write_vectored(&self, buf: &[IoVec<'_>]) -> io::Result<usize> {
119 let buf = match buf.get(0) {
121 None => return Ok(0),
126 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
127 addr_to_sockaddr(&self.peer_addr)
130 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
131 addr_to_sockaddr(&self.inner.local_addr)
134 pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
138 pub fn duplicate(&self) -> io::Result<TcpStream> {
142 pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
146 pub fn nodelay(&self) -> io::Result<bool> {
147 sgx_ineffective(false)
150 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
154 pub fn ttl(&self) -> io::Result<u32> {
155 sgx_ineffective(DEFAULT_FAKE_TTL)
158 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
162 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
167 impl AsInner<Socket> for TcpStream {
168 fn as_inner(&self) -> &Socket { &self.inner }
171 // `Inner` includes `peer_addr` so that a `TcpStream` maybe correctly
172 // reconstructed if `Socket::try_into_inner` fails.
173 impl IntoInner<(Socket, Option<String>)> for TcpStream {
174 fn into_inner(self) -> (Socket, Option<String>) {
175 (self.inner, self.peer_addr)
179 impl FromInner<(Socket, Option<String>)> for TcpStream {
180 fn from_inner((inner, peer_addr): (Socket, Option<String>)) -> TcpStream {
181 TcpStream { inner, peer_addr }
185 #[derive(Debug, Clone)]
186 pub struct TcpListener {
191 pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
192 let addr = io_err_to_addr(addr)?;
193 let (fd, local_addr) = usercalls::bind_stream(&addr)?;
194 Ok(TcpListener { inner: Socket::new(fd, local_addr) })
197 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
198 addr_to_sockaddr(&self.inner.local_addr)
201 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
202 let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?;
203 let peer_addr = Some(peer_addr);
204 let ret_peer = addr_to_sockaddr(&peer_addr).unwrap_or_else(|_| ([0; 4], 0).into());
205 Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer))
208 pub fn duplicate(&self) -> io::Result<TcpListener> {
212 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
216 pub fn ttl(&self) -> io::Result<u32> {
217 sgx_ineffective(DEFAULT_FAKE_TTL)
220 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
224 pub fn only_v6(&self) -> io::Result<bool> {
225 sgx_ineffective(false)
228 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
232 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
237 impl AsInner<Socket> for TcpListener {
238 fn as_inner(&self) -> &Socket { &self.inner }
241 impl IntoInner<Socket> for TcpListener {
242 fn into_inner(self) -> Socket {
247 impl FromInner<Socket> for TcpListener {
248 fn from_inner(inner: Socket) -> TcpListener {
249 TcpListener { inner }
253 pub struct UdpSocket(Void);
256 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
260 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
264 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
268 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
272 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
276 pub fn duplicate(&self) -> io::Result<UdpSocket> {
280 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
284 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
288 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
292 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
296 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
300 pub fn broadcast(&self) -> io::Result<bool> {
304 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
308 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
312 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
316 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
320 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
324 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
328 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
333 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32)
338 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
343 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32)
348 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
352 pub fn ttl(&self) -> io::Result<u32> {
356 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
360 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
364 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
368 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
372 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
376 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
381 impl fmt::Debug for UdpSocket {
382 fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
388 pub struct NonIpSockAddr {
392 impl error::Error for NonIpSockAddr {
393 fn description(&self) -> &str {
394 "Failed to convert address to SocketAddr"
398 impl fmt::Display for NonIpSockAddr {
399 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
400 write!(f, "Failed to convert address to SocketAddr: {}", self.host)
404 pub struct LookupHost(Void);
407 fn new(host: String) -> io::Result<LookupHost> {
408 Err(io::Error::new(io::ErrorKind::Other, NonIpSockAddr { host }))
411 pub fn port(&self) -> u16 {
416 impl Iterator for LookupHost {
417 type Item = SocketAddr;
418 fn next(&mut self) -> Option<SocketAddr> {
423 impl<'a> TryFrom<&'a str> for LookupHost {
424 type Error = io::Error;
426 fn try_from(v: &'a str) -> io::Result<LookupHost> {
427 LookupHost::new(v.to_owned())
431 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
432 type Error = io::Error;
434 fn try_from((host, port): (&'a str, u16)) -> io::Result<LookupHost> {
435 LookupHost::new(format!("{}:{}", host, port))
441 pub const AF_INET: u8 = 0;
442 pub const AF_INET6: u8 = 1;
443 pub type sa_family_t = u8;
445 #[derive(Copy, Clone)]
450 #[derive(Copy, Clone)]
451 pub struct sockaddr_in {
452 pub sin_family: sa_family_t,
454 pub sin_addr: in_addr,
457 #[derive(Copy, Clone)]
458 pub struct in6_addr {
459 pub s6_addr: [u8; 16],
462 #[derive(Copy, Clone)]
463 pub struct sockaddr_in6 {
464 pub sin6_family: sa_family_t,
466 pub sin6_addr: in6_addr,
467 pub sin6_flowinfo: u32,
468 pub sin6_scope_id: u32,
471 #[derive(Copy, Clone)]
472 pub struct sockaddr {
475 pub type socklen_t = usize;