]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/net.rs
176d230846dc25b0a8d61ba8c9a7162ac99946bf
[rust.git] / src / libstd / sys / sgx / net.rs
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.
4 //
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.
10
11 use fmt;
12 use io;
13 use net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr, ToSocketAddrs};
14 use time::Duration;
15 use sys::{unsupported, Void, sgx_ineffective};
16 use sys::fd::FileDesc;
17 use convert::TryFrom;
18 use error;
19 use sync::Arc;
20
21 use super::abi::usercalls;
22
23 const DEFAULT_FAKE_TTL: u32 = 64;
24
25 #[derive(Debug, Clone)]
26 struct Socket {
27     inner: Arc<FileDesc>,
28     local_addr: String,
29 }
30
31 impl Socket {
32     fn new(fd: usercalls::Fd, local_addr: String) -> Socket {
33         Socket { inner: Arc::new(FileDesc::new(fd)), local_addr }
34     }
35 }
36
37 #[derive(Debug, Clone)]
38 pub struct TcpStream {
39     inner: Socket,
40     peer_addr: String,
41 }
42
43 fn io_err_to_addr(result: io::Result<&SocketAddr>) -> io::Result<String> {
44     match result {
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)
50         } else {
51             Err(e)
52         }
53     }
54 }
55
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())
59 }
60
61 impl TcpStream {
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 })
66     }
67
68     pub fn connect_timeout(addr: &SocketAddr, _: Duration) -> io::Result<TcpStream> {
69         Self::connect(Ok(addr)) // FIXME: ignoring timeout
70     }
71
72     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
73         sgx_ineffective(())
74     }
75
76     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
77         sgx_ineffective(())
78     }
79
80     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
81         sgx_ineffective(None)
82     }
83
84     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
85         sgx_ineffective(None)
86     }
87
88     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
89         Ok(0)
90     }
91
92     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
93         self.inner.inner.read(buf)
94     }
95
96     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
97         self.inner.inner.write(buf)
98     }
99
100     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
101         addr_to_sockaddr(&self.peer_addr)
102     }
103
104     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
105         addr_to_sockaddr(&self.inner.local_addr)
106     }
107
108     pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
109         sgx_ineffective(())
110     }
111
112     pub fn duplicate(&self) -> io::Result<TcpStream> {
113         Ok(self.clone())
114     }
115
116     pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
117         sgx_ineffective(())
118     }
119
120     pub fn nodelay(&self) -> io::Result<bool> {
121         sgx_ineffective(false)
122     }
123
124     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
125         sgx_ineffective(())
126     }
127
128     pub fn ttl(&self) -> io::Result<u32> {
129         sgx_ineffective(DEFAULT_FAKE_TTL)
130     }
131
132     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
133         Ok(None)
134     }
135
136     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
137         sgx_ineffective(())
138     }
139 }
140
141 #[derive(Debug, Clone)]
142 pub struct TcpListener {
143     inner: Socket,
144 }
145
146 impl 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) })
151     }
152
153     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
154         addr_to_sockaddr(&self.inner.local_addr)
155     }
156
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))
161     }
162
163     pub fn duplicate(&self) -> io::Result<TcpListener> {
164         Ok(self.clone())
165     }
166
167     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
168         sgx_ineffective(())
169     }
170
171     pub fn ttl(&self) -> io::Result<u32> {
172         sgx_ineffective(DEFAULT_FAKE_TTL)
173     }
174
175     pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
176         sgx_ineffective(())
177     }
178
179     pub fn only_v6(&self) -> io::Result<bool> {
180         sgx_ineffective(false)
181     }
182
183     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
184         Ok(None)
185     }
186
187     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
188         sgx_ineffective(())
189     }
190 }
191
192 pub struct UdpSocket(Void);
193
194 impl UdpSocket {
195     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
196         unsupported()
197     }
198
199     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
200         match self.0 {}
201     }
202
203     pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
204         match self.0 {}
205     }
206
207     pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
208         match self.0 {}
209     }
210
211     pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
212         match self.0 {}
213     }
214
215     pub fn duplicate(&self) -> io::Result<UdpSocket> {
216         match self.0 {}
217     }
218
219     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
220         match self.0 {}
221     }
222
223     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
224         match self.0 {}
225     }
226
227     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
228         match self.0 {}
229     }
230
231     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
232         match self.0 {}
233     }
234
235     pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
236         match self.0 {}
237     }
238
239     pub fn broadcast(&self) -> io::Result<bool> {
240         match self.0 {}
241     }
242
243     pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
244         match self.0 {}
245     }
246
247     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
248         match self.0 {}
249     }
250
251     pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
252         match self.0 {}
253     }
254
255     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
256         match self.0 {}
257     }
258
259     pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
260         match self.0 {}
261     }
262
263     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
264         match self.0 {}
265     }
266
267     pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
268                          -> io::Result<()> {
269         match self.0 {}
270     }
271
272     pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32)
273                          -> io::Result<()> {
274         match self.0 {}
275     }
276
277     pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
278                           -> io::Result<()> {
279         match self.0 {}
280     }
281
282     pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32)
283                           -> io::Result<()> {
284         match self.0 {}
285     }
286
287     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
288         match self.0 {}
289     }
290
291     pub fn ttl(&self) -> io::Result<u32> {
292         match self.0 {}
293     }
294
295     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
296         match self.0 {}
297     }
298
299     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
300         match self.0 {}
301     }
302
303     pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
304         match self.0 {}
305     }
306
307     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
308         match self.0 {}
309     }
310
311     pub fn send(&self, _: &[u8]) -> io::Result<usize> {
312         match self.0 {}
313     }
314
315     pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
316         match self.0 {}
317     }
318 }
319
320 impl fmt::Debug for UdpSocket {
321     fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
322         match self.0 {}
323     }
324 }
325
326 #[derive(Debug)]
327 pub struct NonIpSockAddr {
328     host: String
329 }
330
331 impl error::Error for NonIpSockAddr {
332     fn description(&self) -> &str {
333         "Failed to convert address to SocketAddr"
334     }
335 }
336
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)
340     }
341 }
342
343 pub struct LookupHost(Void);
344
345 impl LookupHost {
346     fn new(host: String) -> io::Result<LookupHost> {
347         Err(io::Error::new(io::ErrorKind::Other, NonIpSockAddr { host }))
348     }
349
350     pub fn port(&self) -> u16 {
351         match self.0 {}
352     }
353 }
354
355 impl Iterator for LookupHost {
356     type Item = SocketAddr;
357     fn next(&mut self) -> Option<SocketAddr> {
358         match self.0 {}
359     }
360 }
361
362 impl<'a> TryFrom<&'a str> for LookupHost {
363     type Error = io::Error;
364
365     fn try_from(v: &'a str) -> io::Result<LookupHost> {
366         LookupHost::new(v.to_owned())
367     }
368 }
369
370 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
371     type Error = io::Error;
372
373     fn try_from((host, port): (&'a str, u16)) -> io::Result<LookupHost> {
374         LookupHost::new(format!("{}:{}", host, port))
375     }
376 }
377
378 #[allow(bad_style)]
379 pub mod netc {
380     pub const AF_INET: u8 = 0;
381     pub const AF_INET6: u8 = 1;
382     pub type sa_family_t = u8;
383
384     #[derive(Copy, Clone)]
385     pub struct in_addr {
386         pub s_addr: u32,
387     }
388
389     #[derive(Copy, Clone)]
390     pub struct sockaddr_in {
391         pub sin_family: sa_family_t,
392         pub sin_port: u16,
393         pub sin_addr: in_addr,
394     }
395
396     #[derive(Copy, Clone)]
397     pub struct in6_addr {
398         pub s6_addr: [u8; 16],
399     }
400
401     #[derive(Copy, Clone)]
402     pub struct sockaddr_in6 {
403         pub sin6_family: sa_family_t,
404         pub sin6_port: u16,
405         pub sin6_addr: in6_addr,
406         pub sin6_flowinfo: u32,
407         pub sin6_scope_id: u32,
408     }
409
410     #[derive(Copy, Clone)]
411     pub struct sockaddr {
412     }
413
414     pub type socklen_t = usize;
415 }