]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/net.rs
Improve some compiletest documentation
[rust.git] / src / libstd / sys / sgx / net.rs
1 use crate::fmt;
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;
8 use crate::error;
9 use crate::sync::Arc;
10
11 use super::abi::usercalls;
12
13 const DEFAULT_FAKE_TTL: u32 = 64;
14
15 #[derive(Debug, Clone)]
16 pub struct Socket {
17     inner: Arc<FileDesc>,
18     local_addr: Option<String>,
19 }
20
21 impl Socket {
22     fn new(fd: usercalls::raw::Fd, local_addr: String) -> Socket {
23         Socket { inner: Arc::new(FileDesc::new(fd)), local_addr: Some(local_addr) }
24     }
25 }
26
27 impl AsInner<FileDesc> for Socket {
28     fn as_inner(&self) -> &FileDesc { &self.inner }
29 }
30
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 } )
35     }
36 }
37
38 impl FromInner<FileDesc> for Socket {
39     fn from_inner(inner: FileDesc) -> Socket {
40         Socket { inner: Arc::new(inner), local_addr: None }
41     }
42 }
43
44 #[derive(Debug, Clone)]
45 pub struct TcpStream {
46     inner: Socket,
47     peer_addr: Option<String>,
48 }
49
50 fn io_err_to_addr(result: io::Result<&SocketAddr>) -> io::Result<String> {
51     match result {
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)
57         } else {
58             Err(e)
59         }
60     }
61 }
62
63 fn addr_to_sockaddr(addr: &Option<String>) -> io::Result<SocketAddr> {
64     addr.as_ref()
65         .ok_or(io::ErrorKind::AddrNotAvailable)?
66         .to_socket_addrs()
67         // unwrap OK: if an iterator is returned, we're guaranteed to get exactly one entry
68         .map(|mut it| it.next().unwrap())
69 }
70
71 impl TcpStream {
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) })
76     }
77
78     pub fn connect_timeout(addr: &SocketAddr, _: Duration) -> io::Result<TcpStream> {
79         Self::connect(Ok(addr)) // FIXME: ignoring timeout
80     }
81
82     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
83         sgx_ineffective(())
84     }
85
86     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
87         sgx_ineffective(())
88     }
89
90     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
91         sgx_ineffective(None)
92     }
93
94     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
95         sgx_ineffective(None)
96     }
97
98     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
99         Ok(0)
100     }
101
102     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
103         self.inner.inner.read(buf)
104     }
105
106     pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
107         io::default_read_vectored(|b| self.read(b), bufs)
108     }
109
110     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
111         self.inner.inner.write(buf)
112     }
113
114     pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
115         io::default_write_vectored(|b| self.write(b), bufs)
116     }
117
118     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
119         addr_to_sockaddr(&self.peer_addr)
120     }
121
122     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
123         addr_to_sockaddr(&self.inner.local_addr)
124     }
125
126     pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
127         sgx_ineffective(())
128     }
129
130     pub fn duplicate(&self) -> io::Result<TcpStream> {
131         Ok(self.clone())
132     }
133
134     pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
135         sgx_ineffective(())
136     }
137
138     pub fn nodelay(&self) -> io::Result<bool> {
139         sgx_ineffective(false)
140     }
141
142     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
143         sgx_ineffective(())
144     }
145
146     pub fn ttl(&self) -> io::Result<u32> {
147         sgx_ineffective(DEFAULT_FAKE_TTL)
148     }
149
150     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
151         Ok(None)
152     }
153
154     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
155         sgx_ineffective(())
156     }
157 }
158
159 impl AsInner<Socket> for TcpStream {
160     fn as_inner(&self) -> &Socket { &self.inner }
161 }
162
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)
168     }
169 }
170
171 impl FromInner<(Socket, Option<String>)> for TcpStream {
172     fn from_inner((inner, peer_addr): (Socket, Option<String>)) -> TcpStream {
173         TcpStream { inner, peer_addr }
174     }
175 }
176
177 #[derive(Debug, Clone)]
178 pub struct TcpListener {
179     inner: Socket,
180 }
181
182 impl 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) })
187     }
188
189     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
190         addr_to_sockaddr(&self.inner.local_addr)
191     }
192
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))
198     }
199
200     pub fn duplicate(&self) -> io::Result<TcpListener> {
201         Ok(self.clone())
202     }
203
204     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
205         sgx_ineffective(())
206     }
207
208     pub fn ttl(&self) -> io::Result<u32> {
209         sgx_ineffective(DEFAULT_FAKE_TTL)
210     }
211
212     pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
213         sgx_ineffective(())
214     }
215
216     pub fn only_v6(&self) -> io::Result<bool> {
217         sgx_ineffective(false)
218     }
219
220     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
221         Ok(None)
222     }
223
224     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
225         sgx_ineffective(())
226     }
227 }
228
229 impl AsInner<Socket> for TcpListener {
230     fn as_inner(&self) -> &Socket { &self.inner }
231 }
232
233 impl IntoInner<Socket> for TcpListener {
234     fn into_inner(self) -> Socket {
235         self.inner
236     }
237 }
238
239 impl FromInner<Socket> for TcpListener {
240     fn from_inner(inner: Socket) -> TcpListener {
241         TcpListener { inner }
242     }
243 }
244
245 pub struct UdpSocket(Void);
246
247 impl UdpSocket {
248     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
249         unsupported()
250     }
251
252     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
253         match self.0 {}
254     }
255
256     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
257         match self.0 {}
258     }
259
260     pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
261         match self.0 {}
262     }
263
264     pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
265         match self.0 {}
266     }
267
268     pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
269         match self.0 {}
270     }
271
272     pub fn duplicate(&self) -> io::Result<UdpSocket> {
273         match self.0 {}
274     }
275
276     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
277         match self.0 {}
278     }
279
280     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
281         match self.0 {}
282     }
283
284     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
285         match self.0 {}
286     }
287
288     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
289         match self.0 {}
290     }
291
292     pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
293         match self.0 {}
294     }
295
296     pub fn broadcast(&self) -> io::Result<bool> {
297         match self.0 {}
298     }
299
300     pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
301         match self.0 {}
302     }
303
304     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
305         match self.0 {}
306     }
307
308     pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
309         match self.0 {}
310     }
311
312     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
313         match self.0 {}
314     }
315
316     pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
317         match self.0 {}
318     }
319
320     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
321         match self.0 {}
322     }
323
324     pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
325                          -> io::Result<()> {
326         match self.0 {}
327     }
328
329     pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32)
330                          -> io::Result<()> {
331         match self.0 {}
332     }
333
334     pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
335                           -> io::Result<()> {
336         match self.0 {}
337     }
338
339     pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32)
340                           -> io::Result<()> {
341         match self.0 {}
342     }
343
344     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
345         match self.0 {}
346     }
347
348     pub fn ttl(&self) -> io::Result<u32> {
349         match self.0 {}
350     }
351
352     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
353         match self.0 {}
354     }
355
356     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
357         match self.0 {}
358     }
359
360     pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
361         match self.0 {}
362     }
363
364     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
365         match self.0 {}
366     }
367
368     pub fn send(&self, _: &[u8]) -> io::Result<usize> {
369         match self.0 {}
370     }
371
372     pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
373         match self.0 {}
374     }
375 }
376
377 impl fmt::Debug for UdpSocket {
378     fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
379         match self.0 {}
380     }
381 }
382
383 #[derive(Debug)]
384 pub struct NonIpSockAddr {
385     host: String
386 }
387
388 impl error::Error for NonIpSockAddr {
389     fn description(&self) -> &str {
390         "Failed to convert address to SocketAddr"
391     }
392 }
393
394 impl fmt::Display for NonIpSockAddr {
395     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
396         write!(f, "Failed to convert address to SocketAddr: {}", self.host)
397     }
398 }
399
400 pub struct LookupHost(Void);
401
402 impl LookupHost {
403     fn new(host: String) -> io::Result<LookupHost> {
404         Err(io::Error::new(io::ErrorKind::Other, NonIpSockAddr { host }))
405     }
406
407     pub fn port(&self) -> u16 {
408         match self.0 {}
409     }
410 }
411
412 impl Iterator for LookupHost {
413     type Item = SocketAddr;
414     fn next(&mut self) -> Option<SocketAddr> {
415         match self.0 {}
416     }
417 }
418
419 impl TryFrom<&str> for LookupHost {
420     type Error = io::Error;
421
422     fn try_from(v: &str) -> io::Result<LookupHost> {
423         LookupHost::new(v.to_owned())
424     }
425 }
426
427 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
428     type Error = io::Error;
429
430     fn try_from((host, port): (&'a str, u16)) -> io::Result<LookupHost> {
431         LookupHost::new(format!("{}:{}", host, port))
432     }
433 }
434
435 #[allow(bad_style)]
436 pub mod netc {
437     pub const AF_INET: u8 = 0;
438     pub const AF_INET6: u8 = 1;
439     pub type sa_family_t = u8;
440
441     #[derive(Copy, Clone)]
442     pub struct in_addr {
443         pub s_addr: u32,
444     }
445
446     #[derive(Copy, Clone)]
447     pub struct sockaddr_in {
448         pub sin_family: sa_family_t,
449         pub sin_port: u16,
450         pub sin_addr: in_addr,
451     }
452
453     #[derive(Copy, Clone)]
454     pub struct in6_addr {
455         pub s6_addr: [u8; 16],
456     }
457
458     #[derive(Copy, Clone)]
459     pub struct sockaddr_in6 {
460         pub sin6_family: sa_family_t,
461         pub sin6_port: u16,
462         pub sin6_addr: in6_addr,
463         pub sin6_flowinfo: u32,
464         pub sin6_scope_id: u32,
465     }
466
467     #[derive(Copy, Clone)]
468     pub struct sockaddr {
469     }
470
471     pub type socklen_t = usize;
472 }