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