]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/net.rs
Fix SGX implementations of read/write_vectored.
[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         match bufs.iter_mut().find(|b| !b.is_empty()) {
108             Some(buf) => self.read(buf),
109             None => Ok(0),
110         }
111     }
112
113     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
114         self.inner.inner.write(buf)
115     }
116
117     pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
118         match bufs.iter().find(|b| !b.is_empty()) {
119             Some(buf) => self.write(buf),
120             None => Ok(0),
121         }
122     }
123
124     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
125         addr_to_sockaddr(&self.peer_addr)
126     }
127
128     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
129         addr_to_sockaddr(&self.inner.local_addr)
130     }
131
132     pub fn shutdown(&self, _: Shutdown) -> io::Result<()> {
133         sgx_ineffective(())
134     }
135
136     pub fn duplicate(&self) -> io::Result<TcpStream> {
137         Ok(self.clone())
138     }
139
140     pub fn set_nodelay(&self, _: bool) -> io::Result<()> {
141         sgx_ineffective(())
142     }
143
144     pub fn nodelay(&self) -> io::Result<bool> {
145         sgx_ineffective(false)
146     }
147
148     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
149         sgx_ineffective(())
150     }
151
152     pub fn ttl(&self) -> io::Result<u32> {
153         sgx_ineffective(DEFAULT_FAKE_TTL)
154     }
155
156     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
157         Ok(None)
158     }
159
160     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
161         sgx_ineffective(())
162     }
163 }
164
165 impl AsInner<Socket> for TcpStream {
166     fn as_inner(&self) -> &Socket { &self.inner }
167 }
168
169 // `Inner` includes `peer_addr` so that a `TcpStream` maybe correctly
170 // reconstructed if `Socket::try_into_inner` fails.
171 impl IntoInner<(Socket, Option<String>)> for TcpStream {
172     fn into_inner(self) -> (Socket, Option<String>) {
173         (self.inner, self.peer_addr)
174     }
175 }
176
177 impl FromInner<(Socket, Option<String>)> for TcpStream {
178     fn from_inner((inner, peer_addr): (Socket, Option<String>)) -> TcpStream {
179         TcpStream { inner, peer_addr }
180     }
181 }
182
183 #[derive(Debug, Clone)]
184 pub struct TcpListener {
185     inner: Socket,
186 }
187
188 impl TcpListener {
189     pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
190         let addr = io_err_to_addr(addr)?;
191         let (fd, local_addr) = usercalls::bind_stream(&addr)?;
192         Ok(TcpListener { inner: Socket::new(fd, local_addr) })
193     }
194
195     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
196         addr_to_sockaddr(&self.inner.local_addr)
197     }
198
199     pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
200         let (fd, local_addr, peer_addr) = usercalls::accept_stream(self.inner.inner.raw())?;
201         let peer_addr = Some(peer_addr);
202         let ret_peer = addr_to_sockaddr(&peer_addr).unwrap_or_else(|_| ([0; 4], 0).into());
203         Ok((TcpStream { inner: Socket::new(fd, local_addr), peer_addr }, ret_peer))
204     }
205
206     pub fn duplicate(&self) -> io::Result<TcpListener> {
207         Ok(self.clone())
208     }
209
210     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
211         sgx_ineffective(())
212     }
213
214     pub fn ttl(&self) -> io::Result<u32> {
215         sgx_ineffective(DEFAULT_FAKE_TTL)
216     }
217
218     pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
219         sgx_ineffective(())
220     }
221
222     pub fn only_v6(&self) -> io::Result<bool> {
223         sgx_ineffective(false)
224     }
225
226     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
227         Ok(None)
228     }
229
230     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
231         sgx_ineffective(())
232     }
233 }
234
235 impl AsInner<Socket> for TcpListener {
236     fn as_inner(&self) -> &Socket { &self.inner }
237 }
238
239 impl IntoInner<Socket> for TcpListener {
240     fn into_inner(self) -> Socket {
241         self.inner
242     }
243 }
244
245 impl FromInner<Socket> for TcpListener {
246     fn from_inner(inner: Socket) -> TcpListener {
247         TcpListener { inner }
248     }
249 }
250
251 pub struct UdpSocket(Void);
252
253 impl UdpSocket {
254     pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
255         unsupported()
256     }
257
258     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
259         match self.0 {}
260     }
261
262     pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
263         match self.0 {}
264     }
265
266     pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
267         match self.0 {}
268     }
269
270     pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
271         match self.0 {}
272     }
273
274     pub fn duplicate(&self) -> io::Result<UdpSocket> {
275         match self.0 {}
276     }
277
278     pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
279         match self.0 {}
280     }
281
282     pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
283         match self.0 {}
284     }
285
286     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
287         match self.0 {}
288     }
289
290     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
291         match self.0 {}
292     }
293
294     pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
295         match self.0 {}
296     }
297
298     pub fn broadcast(&self) -> io::Result<bool> {
299         match self.0 {}
300     }
301
302     pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
303         match self.0 {}
304     }
305
306     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
307         match self.0 {}
308     }
309
310     pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
311         match self.0 {}
312     }
313
314     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
315         match self.0 {}
316     }
317
318     pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
319         match self.0 {}
320     }
321
322     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
323         match self.0 {}
324     }
325
326     pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
327                          -> io::Result<()> {
328         match self.0 {}
329     }
330
331     pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32)
332                          -> io::Result<()> {
333         match self.0 {}
334     }
335
336     pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr)
337                           -> io::Result<()> {
338         match self.0 {}
339     }
340
341     pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32)
342                           -> io::Result<()> {
343         match self.0 {}
344     }
345
346     pub fn set_ttl(&self, _: u32) -> io::Result<()> {
347         match self.0 {}
348     }
349
350     pub fn ttl(&self) -> io::Result<u32> {
351         match self.0 {}
352     }
353
354     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
355         match self.0 {}
356     }
357
358     pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
359         match self.0 {}
360     }
361
362     pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
363         match self.0 {}
364     }
365
366     pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
367         match self.0 {}
368     }
369
370     pub fn send(&self, _: &[u8]) -> io::Result<usize> {
371         match self.0 {}
372     }
373
374     pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
375         match self.0 {}
376     }
377 }
378
379 impl fmt::Debug for UdpSocket {
380     fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
381         match self.0 {}
382     }
383 }
384
385 #[derive(Debug)]
386 pub struct NonIpSockAddr {
387     host: String
388 }
389
390 impl error::Error for NonIpSockAddr {
391     fn description(&self) -> &str {
392         "Failed to convert address to SocketAddr"
393     }
394 }
395
396 impl fmt::Display for NonIpSockAddr {
397     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
398         write!(f, "Failed to convert address to SocketAddr: {}", self.host)
399     }
400 }
401
402 pub struct LookupHost(Void);
403
404 impl LookupHost {
405     fn new(host: String) -> io::Result<LookupHost> {
406         Err(io::Error::new(io::ErrorKind::Other, NonIpSockAddr { host }))
407     }
408
409     pub fn port(&self) -> u16 {
410         match self.0 {}
411     }
412 }
413
414 impl Iterator for LookupHost {
415     type Item = SocketAddr;
416     fn next(&mut self) -> Option<SocketAddr> {
417         match self.0 {}
418     }
419 }
420
421 impl<'a> TryFrom<&'a str> for LookupHost {
422     type Error = io::Error;
423
424     fn try_from(v: &'a str) -> io::Result<LookupHost> {
425         LookupHost::new(v.to_owned())
426     }
427 }
428
429 impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
430     type Error = io::Error;
431
432     fn try_from((host, port): (&'a str, u16)) -> io::Result<LookupHost> {
433         LookupHost::new(format!("{}:{}", host, port))
434     }
435 }
436
437 #[allow(bad_style)]
438 pub mod netc {
439     pub const AF_INET: u8 = 0;
440     pub const AF_INET6: u8 = 1;
441     pub type sa_family_t = u8;
442
443     #[derive(Copy, Clone)]
444     pub struct in_addr {
445         pub s_addr: u32,
446     }
447
448     #[derive(Copy, Clone)]
449     pub struct sockaddr_in {
450         pub sin_family: sa_family_t,
451         pub sin_port: u16,
452         pub sin_addr: in_addr,
453     }
454
455     #[derive(Copy, Clone)]
456     pub struct in6_addr {
457         pub s6_addr: [u8; 16],
458     }
459
460     #[derive(Copy, Clone)]
461     pub struct sockaddr_in6 {
462         pub sin6_family: sa_family_t,
463         pub sin6_port: u16,
464         pub sin6_addr: in6_addr,
465         pub sin6_flowinfo: u32,
466         pub sin6_scope_id: u32,
467     }
468
469     #[derive(Copy, Clone)]
470     pub struct sockaddr {
471     }
472
473     pub type socklen_t = usize;
474 }