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 }
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, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
107 io::default_read_vectored(|b| self.read(b), bufs)
110 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
111 self.inner.inner.write(buf)
114 pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
115 io::default_write_vectored(|b| self.write(b), bufs)
118 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
119 addr_to_sockaddr(&self.peer_addr)
122 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
123 addr_to_sockaddr(&self.inner.local_addr)
126 pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
130 pub fn duplicate(&self) -> io::Result<TcpStream> {
134 pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
138 pub fn nodelay(&self) -> io::Result<bool> {
139 sgx_ineffective(false)
142 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
146 pub fn ttl(&self) -> io::Result<u32> {
147 sgx_ineffective(DEFAULT_FAKE_TTL)
150 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
154 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
159 impl AsInner<Socket> for TcpStream {
160 fn as_inner(&self) -> &Socket { &self.inner }
163 // `Inner` includes `peer_addr` so that a `TcpStream` maybe correctly
164 // reconstructed if `Socket::try_into_inner` fails.
165 impl IntoInner<(Socket, Option<String>)> for TcpStream {
166 fn into_inner(self) -> (Socket, Option<String>) {
167 (self.inner, self.peer_addr)
171 impl FromInner<(Socket, Option<String>)> for TcpStream {
172 fn from_inner((inner, peer_addr): (Socket, Option<String>)) -> TcpStream {
173 TcpStream { inner, peer_addr }
177 #[derive(Debug, Clone)]
178 pub struct TcpListener {
183 pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
184 let addr = io_err_to_addr(addr)?;
185 let (fd, local_addr) = usercalls::bind_stream(&addr)?;
186 Ok(TcpListener { inner: Socket::new(fd, local_addr) })
189 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
190 addr_to_sockaddr(&self.inner.local_addr)
193 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
194 let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?;
195 let peer_addr = Some(peer_addr);
196 let ret_peer = addr_to_sockaddr(&peer_addr).unwrap_or_else(|_| ([0; 4], 0).into());
197 Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer))
200 pub fn duplicate(&self) -> io::Result<TcpListener> {
204 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
208 pub fn ttl(&self) -> io::Result<u32> {
209 sgx_ineffective(DEFAULT_FAKE_TTL)
212 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
216 pub fn only_v6(&self) -> io::Result<bool> {
217 sgx_ineffective(false)
220 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
224 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
229 impl AsInner<Socket> for TcpListener {
230 fn as_inner(&self) -> &Socket { &self.inner }
233 impl IntoInner<Socket> for TcpListener {
234 fn into_inner(self) -> Socket {
239 impl FromInner<Socket> for TcpListener {
240 fn from_inner(inner: Socket) -> TcpListener {
241 TcpListener { inner }
245 pub struct UdpSocket(Void);
248 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
252 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
256 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
260 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
264 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
268 pub fn duplicate(&self) -> io::Result<UdpSocket> {
272 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
276 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
280 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
284 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
288 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
292 pub fn broadcast(&self) -> io::Result<bool> {
296 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
300 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
304 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
308 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
312 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
316 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
320 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
325 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32)
330 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
335 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32)
340 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
344 pub fn ttl(&self) -> io::Result<u32> {
348 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
352 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
356 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
360 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
364 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
368 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
373 impl fmt::Debug for UdpSocket {
374 fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
380 pub struct NonIpSockAddr {
384 impl error::Error for NonIpSockAddr {
385 fn description(&self) -> &str {
386 "Failed to convert address to SocketAddr"
390 impl fmt::Display for NonIpSockAddr {
391 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
392 write!(f, "Failed to convert address to SocketAddr: {}", self.host)
396 pub struct LookupHost(Void);
399 fn new(host: String) -> io::Result<LookupHost> {
400 Err(io::Error::new(io::ErrorKind::Other, NonIpSockAddr { host }))
403 pub fn port(&self) -> u16 {
408 impl Iterator for LookupHost {
409 type Item = SocketAddr;
410 fn next(&mut self) -> Option<SocketAddr> {
415 impl TryFrom<&str> for LookupHost {
416 type Error = io::Error;
418 fn try_from(v: &str) -> io::Result<LookupHost> {
419 LookupHost::new(v.to_owned())
423 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
424 type Error = io::Error;
426 fn try_from((host, port): (&'a str, u16)) -> io::Result<LookupHost> {
427 LookupHost::new(format!("{}:{}", host, port))
433 pub const AF_INET: u8 = 0;
434 pub const AF_INET6: u8 = 1;
435 pub type sa_family_t = u8;
437 #[derive(Copy, Clone)]
442 #[derive(Copy, Clone)]
443 pub struct sockaddr_in {
444 pub sin_family: sa_family_t,
446 pub sin_addr: in_addr,
449 #[derive(Copy, Clone)]
450 pub struct in6_addr {
451 pub s6_addr: [u8; 16],
454 #[derive(Copy, Clone)]
455 pub struct sockaddr_in6 {
456 pub sin6_family: sa_family_t,
458 pub sin6_addr: in6_addr,
459 pub sin6_flowinfo: u32,
460 pub sin6_scope_id: u32,
463 #[derive(Copy, Clone)]
464 pub struct sockaddr {
467 pub type socklen_t = usize;