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