1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr, ToSocketAddrs};
15 use sys::{unsupported, Void, sgx_ineffective};
16 use sys::fd::FileDesc;
21 use super::abi::usercalls;
23 const DEFAULT_FAKE_TTL: u32 = 64;
25 #[derive(Debug, Clone)]
32 fn new(fd: usercalls::Fd, local_addr: String) -> Socket {
33 Socket { inner: Arc::new(FileDesc::new(fd)), local_addr }
37 #[derive(Debug, Clone)]
38 pub struct TcpStream {
43 fn io_err_to_addr(result: io::Result<&SocketAddr>) -> io::Result<String> {
45 Ok(saddr) => Ok(saddr.to_string()),
46 // need to downcast twice because io::Error::into_inner doesn't return the original
47 // value if the conversion fails
48 Err(e) => if e.get_ref().and_then(|e| e.downcast_ref::<NonIpSockAddr>()).is_some() {
49 Ok(e.into_inner().unwrap().downcast::<NonIpSockAddr>().unwrap().host)
56 fn addr_to_sockaddr(addr: &str) -> io::Result<SocketAddr> {
57 // unwrap OK: if an iterator is returned, we're guaranteed to get exactly one entry
58 addr.to_socket_addrs().map(|mut it| it.next().unwrap())
62 pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
63 let addr = io_err_to_addr(addr)?;
64 let (fd, local_addr, peer_addr) = usercalls::connect_stream(&addr)?;
65 Ok(TcpStream { inner: Socket::new(fd, local_addr), peer_addr })
68 pub fn connect_timeout(addr: &SocketAddr, _: Duration) -> io::Result<TcpStream> {
69 Self::connect(Ok(addr)) // FIXME: ignoring timeout
72 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
76 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
80 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
84 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
88 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
92 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
93 self.inner.inner.read(buf)
96 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
97 self.inner.inner.write(buf)
100 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
101 addr_to_sockaddr(&self.peer_addr)
104 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
105 addr_to_sockaddr(&self.inner.local_addr)
108 pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
112 pub fn duplicate(&self) -> io::Result<TcpStream> {
116 pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
120 pub fn nodelay(&self) -> io::Result<bool> {
121 sgx_ineffective(false)
124 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
128 pub fn ttl(&self) -> io::Result<u32> {
129 sgx_ineffective(DEFAULT_FAKE_TTL)
132 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
136 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
141 #[derive(Debug, Clone)]
142 pub struct TcpListener {
147 pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
148 let addr = io_err_to_addr(addr)?;
149 let (fd, local_addr) = usercalls::bind_stream(&addr)?;
150 Ok(TcpListener { inner: Socket::new(fd, local_addr) })
153 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
154 addr_to_sockaddr(&self.inner.local_addr)
157 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
158 let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?;
159 let ret_peer = addr_to_sockaddr(&peer_addr).unwrap_or_else(|_| ([0; 4], 0).into());
160 Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer))
163 pub fn duplicate(&self) -> io::Result<TcpListener> {
167 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
171 pub fn ttl(&self) -> io::Result<u32> {
172 sgx_ineffective(DEFAULT_FAKE_TTL)
175 pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
179 pub fn only_v6(&self) -> io::Result<bool> {
180 sgx_ineffective(false)
183 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
187 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
192 pub struct UdpSocket(Void);
195 pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
199 pub fn socket_addr(&self) -> io::Result<SocketAddr> {
203 pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
207 pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
211 pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
215 pub fn duplicate(&self) -> io::Result<UdpSocket> {
219 pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
223 pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
227 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
231 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
235 pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
239 pub fn broadcast(&self) -> io::Result<bool> {
243 pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
247 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
251 pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
255 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
259 pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
263 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
267 pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
272 pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32)
277 pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
282 pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32)
287 pub fn set_ttl(&self, _: u32) -> io::Result<()> {
291 pub fn ttl(&self) -> io::Result<u32> {
295 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
299 pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
303 pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
307 pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
311 pub fn send(&self, _: &[u8]) -> io::Result<usize> {
315 pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
320 impl fmt::Debug for UdpSocket {
321 fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
327 pub struct NonIpSockAddr {
331 impl error::Error for NonIpSockAddr {
332 fn description(&self) -> &str {
333 "Failed to convert address to SocketAddr"
337 impl fmt::Display for NonIpSockAddr {
338 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
339 write!(f, "Failed to convert address to SocketAddr: {}", self.host)
343 pub struct LookupHost(Void);
346 fn new(host: String) -> io::Result<LookupHost> {
347 Err(io::Error::new(io::ErrorKind::Other, NonIpSockAddr { host }))
350 pub fn port(&self) -> u16 {
355 impl Iterator for LookupHost {
356 type Item = SocketAddr;
357 fn next(&mut self) -> Option<SocketAddr> {
362 impl<'a> TryFrom<&'a str> for LookupHost {
363 type Error = io::Error;
365 fn try_from(v: &'a str) -> io::Result<LookupHost> {
366 LookupHost::new(v.to_owned())
370 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
371 type Error = io::Error;
373 fn try_from((host, port): (&'a str, u16)) -> io::Result<LookupHost> {
374 LookupHost::new(format!("{}:{}", host, port))
380 pub const AF_INET: u8 = 0;
381 pub const AF_INET6: u8 = 1;
382 pub type sa_family_t = u8;
384 #[derive(Copy, Clone)]
389 #[derive(Copy, Clone)]
390 pub struct sockaddr_in {
391 pub sin_family: sa_family_t,
393 pub sin_addr: in_addr,
396 #[derive(Copy, Clone)]
397 pub struct in6_addr {
398 pub s6_addr: [u8; 16],
401 #[derive(Copy, Clone)]
402 pub struct sockaddr_in6 {
403 pub sin6_family: sa_family_t,
405 pub sin6_addr: in6_addr,
406 pub sin6_flowinfo: u32,
407 pub sin6_scope_id: u32,
410 #[derive(Copy, Clone)]
411 pub struct sockaddr {
414 pub type socklen_t = usize;