2 use crate::io::{self, IoVec, IoVecMut};
3 use crate::net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr, ToSocketAddrs};
4 use crate::time::Duration;
5 use crate::sys::{unsupported, Void, sgx_ineffective, AsInner, FromInner, IntoInner, TryIntoInner};
6 use crate::sys::fd::FileDesc;
7 use crate::convert::TryFrom;
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 }
45 pub struct TcpStream {
47 peer_addr: Option<String>,
50 impl fmt::Debug for TcpStream {
51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 let mut res = f.debug_struct("TcpStream");
54 if let Some(ref addr) = self.inner.local_addr {
55 res.field("addr", addr);
58 if let Some(ref peer) = self.peer_addr {
59 res.field("peer", peer);
62 res.field("fd", &self.inner.inner.as_inner())
67 fn io_err_to_addr(result: io::Result<&SocketAddr>) -> io::Result<String> {
69 Ok(saddr) => Ok(saddr.to_string()),
70 // need to downcast twice because io::Error::into_inner doesn't return the original
71 // value if the conversion fails
72 Err(e) => if e.get_ref().and_then(|e| e.downcast_ref::<NonIpSockAddr>()).is_some() {
73 Ok(e.into_inner().unwrap().downcast::<NonIpSockAddr>().unwrap().host)
80 fn addr_to_sockaddr(addr: &Option<String>) -> io::Result<SocketAddr> {
82 .ok_or(io::ErrorKind::AddrNotAvailable)?
84 // unwrap OK: if an iterator is returned, we're guaranteed to get exactly one entry
85 .map(|mut it| it.next().unwrap())
89 pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
90 let addr = io_err_to_addr(addr)?;
91 let (fd, local_addr, peer_addr) = usercalls::connect_stream(&addr)?;
92 Ok(TcpStream { inner: Socket::new(fd, local_addr), peer_addr: Some(peer_addr) })
95 pub fn connect_timeout(addr: &SocketAddr, dur: Duration) -> io::Result<TcpStream> {
96 if dur == Duration::default() {
97 return Err(io::Error::new(io::ErrorKind::InvalidInput,
98 "cannot set a 0 duration timeout"));
100 Self::connect(Ok(addr)) // FIXME: ignoring timeout
103 pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
105 Some(dur) if dur == Duration::default() => {
106 return Err(io::Error::new(io::ErrorKind::InvalidInput,
107 "cannot set a 0 duration timeout"));
109 _ => sgx_ineffective(())
113 pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
115 Some(dur) if dur == Duration::default() => {
116 return Err(io::Error::new(io::ErrorKind::InvalidInput,
117 "cannot set a 0 duration timeout"));
119 _ => sgx_ineffective(())
123 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
124 sgx_ineffective(None)
127 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
128 sgx_ineffective(None)
131 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
135 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
136 self.inner.inner.read(buf)
139 pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
140 io::default_read_vectored(|b| self.read(b), bufs)
143 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
144 self.inner.inner.write(buf)
147 pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
148 io::default_write_vectored(|b| self.write(b), bufs)
151 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
152 addr_to_sockaddr(&self.peer_addr)
155 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
156 addr_to_sockaddr(&self.inner.local_addr)
159 pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
163 pub fn duplicate(&self) -> io::Result<TcpStream> {
167 pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
171 pub fn nodelay(&self) -> io::Result<bool> {
172 sgx_ineffective(false)
175 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
179 pub fn ttl(&self) -> io::Result<u32> {
180 sgx_ineffective(DEFAULT_FAKE_TTL)
183 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
187 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
192 impl AsInner<Socket> for TcpStream {
193 fn as_inner(&self) -> &Socket { &self.inner }
196 // `Inner` includes `peer_addr` so that a `TcpStream` maybe correctly
197 // reconstructed if `Socket::try_into_inner` fails.
198 impl IntoInner<(Socket, Option<String>)> for TcpStream {
199 fn into_inner(self) -> (Socket, Option<String>) {
200 (self.inner, self.peer_addr)
204 impl FromInner<(Socket, Option<String>)> for TcpStream {
205 fn from_inner((inner, peer_addr): (Socket, Option<String>)) -> TcpStream {
206 TcpStream { inner, peer_addr }
211 pub struct TcpListener {
215 impl fmt::Debug for TcpListener {
216 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
217 let mut res = f.debug_struct("TcpListener");
219 if let Some(ref addr) = self.inner.local_addr {
220 res.field("addr", addr);
223 res.field("fd", &self.inner.inner.as_inner())
229 pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
230 let addr = io_err_to_addr(addr)?;
231 let (fd, local_addr) = usercalls::bind_stream(&addr)?;
232 Ok(TcpListener { inner: Socket::new(fd, local_addr) })
235 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
236 addr_to_sockaddr(&self.inner.local_addr)
239 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
240 let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?;
241 let peer_addr = Some(peer_addr);
242 let ret_peer = addr_to_sockaddr(&peer_addr).unwrap_or_else(|_| ([0; 4], 0).into());
243 Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer))
246 pub fn duplicate(&self) -> io::Result<TcpListener> {
250 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
254 pub fn ttl(&self) -> io::Result<u32> {
255 sgx_ineffective(DEFAULT_FAKE_TTL)
258 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
262 pub fn only_v6(&self) -> io::Result<bool> {
263 sgx_ineffective(false)
266 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
270 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
275 impl AsInner<Socket> for TcpListener {
276 fn as_inner(&self) -> &Socket { &self.inner }
279 impl IntoInner<Socket> for TcpListener {
280 fn into_inner(self) -> Socket {
285 impl FromInner<Socket> for TcpListener {
286 fn from_inner(inner: Socket) -> TcpListener {
287 TcpListener { inner }
291 pub struct UdpSocket(Void);
294 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
298 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
302 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
306 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
310 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
314 pub fn duplicate(&self) -> io::Result<UdpSocket> {
318 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
322 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
326 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
330 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
334 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
338 pub fn broadcast(&self) -> io::Result<bool> {
342 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
346 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
350 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
354 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
358 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
362 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
366 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
371 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32)
376 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
381 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32)
386 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
390 pub fn ttl(&self) -> io::Result<u32> {
394 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
398 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
402 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
406 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
410 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
414 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
419 impl fmt::Debug for UdpSocket {
420 fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
426 pub struct NonIpSockAddr {
430 impl error::Error for NonIpSockAddr {
431 fn description(&self) -> &str {
432 "Failed to convert address to SocketAddr"
436 impl fmt::Display for NonIpSockAddr {
437 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
438 write!(f, "Failed to convert address to SocketAddr: {}", self.host)
442 pub struct LookupHost(Void);
445 fn new(host: String) -> io::Result<LookupHost> {
446 Err(io::Error::new(io::ErrorKind::Other, NonIpSockAddr { host }))
449 pub fn port(&self) -> u16 {
454 impl Iterator for LookupHost {
455 type Item = SocketAddr;
456 fn next(&mut self) -> Option<SocketAddr> {
461 impl TryFrom<&str> for LookupHost {
462 type Error = io::Error;
464 fn try_from(v: &str) -> io::Result<LookupHost> {
465 LookupHost::new(v.to_owned())
469 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
470 type Error = io::Error;
472 fn try_from((host, port): (&'a str, u16)) -> io::Result<LookupHost> {
473 LookupHost::new(format!("{}:{}", host, port))
479 pub const AF_INET: u8 = 0;
480 pub const AF_INET6: u8 = 1;
481 pub type sa_family_t = u8;
483 #[derive(Copy, Clone)]
488 #[derive(Copy, Clone)]
489 pub struct sockaddr_in {
490 pub sin_family: sa_family_t,
492 pub sin_addr: in_addr,
495 #[derive(Copy, Clone)]
496 pub struct in6_addr {
497 pub s6_addr: [u8; 16],
500 #[derive(Copy, Clone)]
501 pub struct sockaddr_in6 {
502 pub sin6_family: sa_family_t,
504 pub sin6_addr: in6_addr,
505 pub sin6_flowinfo: u32,
506 pub sin6_scope_id: u32,
509 #[derive(Copy, Clone)]
510 pub struct sockaddr {
513 pub type socklen_t = usize;