]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/net/tcp.rs
Auto merge of #58408 - alexcrichton:update-llvm, r=michaelwoerister
[rust.git] / src / libstd / sys / redox / net / tcp.rs
1 use crate::cmp;
2 use crate::io::{self, Error, ErrorKind, Result, IoVec, IoVecMut};
3 use crate::mem;
4 use crate::net::{SocketAddr, Shutdown};
5 use crate::path::Path;
6 use crate::sys::fs::{File, OpenOptions};
7 use crate::sys::syscall::TimeSpec;
8 use crate::sys_common::{AsInner, FromInner, IntoInner};
9 use crate::time::Duration;
10
11 use super::{path_to_peer_addr, path_to_local_addr};
12
13 #[derive(Debug)]
14 pub struct TcpStream(File);
15
16 impl TcpStream {
17     pub fn connect(addr: Result<&SocketAddr>) -> Result<TcpStream> {
18         let path = format!("tcp:{}", addr?);
19         let mut options = OpenOptions::new();
20         options.read(true);
21         options.write(true);
22         Ok(TcpStream(File::open(Path::new(path.as_str()), &options)?))
23     }
24
25     pub fn connect_timeout(_addr: &SocketAddr, _timeout: Duration) -> Result<TcpStream> {
26         Err(Error::new(ErrorKind::Other, "TcpStream::connect_timeout not implemented"))
27     }
28
29     pub fn duplicate(&self) -> Result<TcpStream> {
30         Ok(TcpStream(self.0.dup(&[])?))
31     }
32
33     pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
34         self.0.read(buf)
35     }
36
37     pub fn read_vectored(&self, bufs: &mut [IoVecMut<'_>]) -> io::Result<usize> {
38         match bufs.iter_mut().find(|b| !b.is_empty()) {
39             Some(buf) => self.read(buf),
40             None => Ok(0),
41         }
42     }
43
44     pub fn write(&self, buf: &[u8]) -> Result<usize> {
45         self.0.write(buf)
46     }
47
48     pub fn write_vectored(&self, bufs: &[IoVec<'_>]) -> io::Result<usize> {
49         match bufs.iter().find(|b| !b.is_empty()) {
50             Some(buf) => self.write(buf),
51             None => Ok(0),
52         }
53     }
54
55     pub fn take_error(&self) -> Result<Option<Error>> {
56         Ok(None)
57     }
58
59     pub fn peer_addr(&self) -> Result<SocketAddr> {
60         let path = self.0.path()?;
61         Ok(path_to_peer_addr(path.to_str().unwrap_or("")))
62     }
63
64     pub fn socket_addr(&self) -> Result<SocketAddr> {
65         let path = self.0.path()?;
66         Ok(path_to_local_addr(path.to_str().unwrap_or("")))
67     }
68
69     pub fn peek(&self, _buf: &mut [u8]) -> Result<usize> {
70         Err(Error::new(ErrorKind::Other, "TcpStream::peek not implemented"))
71     }
72
73     pub fn shutdown(&self, _how: Shutdown) -> Result<()> {
74         Err(Error::new(ErrorKind::Other, "TcpStream::shutdown not implemented"))
75     }
76
77     pub fn nodelay(&self) -> Result<bool> {
78         Err(Error::new(ErrorKind::Other, "TcpStream::nodelay not implemented"))
79     }
80
81     pub fn nonblocking(&self) -> Result<bool> {
82         self.0.fd().nonblocking()
83     }
84
85     pub fn only_v6(&self) -> Result<bool> {
86         Err(Error::new(ErrorKind::Other, "TcpStream::only_v6 not implemented"))
87     }
88
89     pub fn ttl(&self) -> Result<u32> {
90         let mut ttl = [0];
91         let file = self.0.dup(b"ttl")?;
92         file.read(&mut ttl)?;
93         Ok(ttl[0] as u32)
94     }
95
96     pub fn read_timeout(&self) -> Result<Option<Duration>> {
97         let mut time = TimeSpec::default();
98         let file = self.0.dup(b"read_timeout")?;
99         if file.read(&mut time)? >= mem::size_of::<TimeSpec>() {
100             Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32)))
101         } else {
102             Ok(None)
103         }
104     }
105
106     pub fn write_timeout(&self) -> Result<Option<Duration>> {
107         let mut time = TimeSpec::default();
108         let file = self.0.dup(b"write_timeout")?;
109         if file.read(&mut time)? >= mem::size_of::<TimeSpec>() {
110             Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32)))
111         } else {
112             Ok(None)
113         }
114     }
115
116     pub fn set_nodelay(&self, _nodelay: bool) -> Result<()> {
117         Err(Error::new(ErrorKind::Other, "TcpStream::set_nodelay not implemented"))
118     }
119
120     pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
121         self.0.fd().set_nonblocking(nonblocking)
122     }
123
124     pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
125         Err(Error::new(ErrorKind::Other, "TcpStream::set_only_v6 not implemented"))
126     }
127
128     pub fn set_ttl(&self, ttl: u32) -> Result<()> {
129         let file = self.0.dup(b"ttl")?;
130         file.write(&[cmp::min(ttl, 255) as u8])?;
131         Ok(())
132     }
133
134     pub fn set_read_timeout(&self, duration_option: Option<Duration>) -> Result<()> {
135         let file = self.0.dup(b"read_timeout")?;
136         if let Some(duration) = duration_option {
137             if duration.as_secs() == 0 && duration.subsec_nanos() == 0 {
138                 return Err(io::Error::new(io::ErrorKind::InvalidInput,
139                                           "cannot set a 0 duration timeout"));
140             }
141             file.write(&TimeSpec {
142                 tv_sec: duration.as_secs() as i64,
143                 tv_nsec: duration.subsec_nanos() as i32
144             })?;
145         } else {
146             file.write(&[])?;
147         }
148         Ok(())
149     }
150
151     pub fn set_write_timeout(&self, duration_option: Option<Duration>) -> Result<()> {
152         let file = self.0.dup(b"write_timeout")?;
153         if let Some(duration) = duration_option {
154             if duration.as_secs() == 0 && duration.subsec_nanos() == 0 {
155                 return Err(io::Error::new(io::ErrorKind::InvalidInput,
156                                           "cannot set a 0 duration timeout"));
157             }
158             file.write(&TimeSpec {
159                 tv_sec: duration.as_secs() as i64,
160                 tv_nsec: duration.subsec_nanos() as i32
161             })?;
162         } else {
163             file.write(&[])?;
164         }
165         Ok(())
166     }
167 }
168
169 impl AsInner<File> for TcpStream {
170     fn as_inner(&self) -> &File { &self.0 }
171 }
172
173 impl FromInner<File> for TcpStream {
174     fn from_inner(file: File) -> TcpStream {
175         TcpStream(file)
176     }
177 }
178
179 impl IntoInner<File> for TcpStream {
180     fn into_inner(self) -> File { self.0 }
181 }
182
183 #[derive(Debug)]
184 pub struct TcpListener(File);
185
186 impl TcpListener {
187     pub fn bind(addr: Result<&SocketAddr>) -> Result<TcpListener> {
188         let path = format!("tcp:/{}", addr?);
189         let mut options = OpenOptions::new();
190         options.read(true);
191         options.write(true);
192         Ok(TcpListener(File::open(Path::new(path.as_str()), &options)?))
193     }
194
195     pub fn accept(&self) -> Result<(TcpStream, SocketAddr)> {
196         let file = self.0.dup(b"listen")?;
197         let path = file.path()?;
198         let peer_addr = path_to_peer_addr(path.to_str().unwrap_or(""));
199         Ok((TcpStream(file), peer_addr))
200     }
201
202     pub fn duplicate(&self) -> Result<TcpListener> {
203         Ok(TcpListener(self.0.dup(&[])?))
204     }
205
206     pub fn take_error(&self) -> Result<Option<Error>> {
207         Ok(None)
208     }
209
210     pub fn socket_addr(&self) -> Result<SocketAddr> {
211         let path = self.0.path()?;
212         Ok(path_to_local_addr(path.to_str().unwrap_or("")))
213     }
214
215     pub fn nonblocking(&self) -> Result<bool> {
216         Err(Error::new(ErrorKind::Other, "TcpListener::nonblocking not implemented"))
217     }
218
219     pub fn only_v6(&self) -> Result<bool> {
220         Err(Error::new(ErrorKind::Other, "TcpListener::only_v6 not implemented"))
221     }
222
223     pub fn ttl(&self) -> Result<u32> {
224         let mut ttl = [0];
225         let file = self.0.dup(b"ttl")?;
226         file.read(&mut ttl)?;
227         Ok(ttl[0] as u32)
228     }
229
230     pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
231         Err(Error::new(ErrorKind::Other, "TcpListener::set_nonblocking not implemented"))
232     }
233
234     pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
235         Err(Error::new(ErrorKind::Other, "TcpListener::set_only_v6 not implemented"))
236     }
237
238     pub fn set_ttl(&self, ttl: u32) -> Result<()> {
239         let file = self.0.dup(b"ttl")?;
240         file.write(&[cmp::min(ttl, 255) as u8])?;
241         Ok(())
242     }
243 }
244
245 impl AsInner<File> for TcpListener {
246     fn as_inner(&self) -> &File { &self.0 }
247 }
248
249 impl FromInner<File> for TcpListener {
250     fn from_inner(file: File) -> TcpListener {
251         TcpListener(file)
252     }
253 }
254
255 impl IntoInner<File> for TcpListener {
256     fn into_inner(self) -> File { self.0 }
257 }