]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/net/tcp.rs
b5664908479cfbc342cb3a0dcceeeaa3bc1d581e
[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::{self, 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             if duration.as_secs() == 0 && duration.subsec_nanos() == 0 {
134                 return Err(io::Error::new(io::ErrorKind::InvalidInput,
135                                           "cannot set a 0 duration timeout"));
136             }
137             file.write(&TimeSpec {
138                 tv_sec: duration.as_secs() as i64,
139                 tv_nsec: duration.subsec_nanos() as i32
140             })?;
141         } else {
142             file.write(&[])?;
143         }
144         Ok(())
145     }
146
147     pub fn set_write_timeout(&self, duration_option: Option<Duration>) -> Result<()> {
148         let file = self.0.dup(b"write_timeout")?;
149         if let Some(duration) = duration_option {
150             if duration.as_secs() == 0 && duration.subsec_nanos() == 0 {
151                 return Err(io::Error::new(io::ErrorKind::InvalidInput,
152                                           "cannot set a 0 duration timeout"));
153             }
154             file.write(&TimeSpec {
155                 tv_sec: duration.as_secs() as i64,
156                 tv_nsec: duration.subsec_nanos() as i32
157             })?;
158         } else {
159             file.write(&[])?;
160         }
161         Ok(())
162     }
163 }
164
165 impl AsInner<File> for TcpStream {
166     fn as_inner(&self) -> &File { &self.0 }
167 }
168
169 impl FromInner<File> for TcpStream {
170     fn from_inner(file: File) -> TcpStream {
171         TcpStream(file)
172     }
173 }
174
175 impl IntoInner<File> for TcpStream {
176     fn into_inner(self) -> File { self.0 }
177 }
178
179 #[derive(Debug)]
180 pub struct TcpListener(File);
181
182 impl TcpListener {
183     pub fn bind(addr: &SocketAddr) -> Result<TcpListener> {
184         let path = format!("tcp:/{}", addr);
185         let mut options = OpenOptions::new();
186         options.read(true);
187         options.write(true);
188         Ok(TcpListener(File::open(Path::new(path.as_str()), &options)?))
189     }
190
191     pub fn accept(&self) -> Result<(TcpStream, SocketAddr)> {
192         let file = self.0.dup(b"listen")?;
193         let path = file.path()?;
194         let peer_addr = path_to_peer_addr(path.to_str().unwrap_or(""));
195         Ok((TcpStream(file), peer_addr))
196     }
197
198     pub fn duplicate(&self) -> Result<TcpListener> {
199         Ok(TcpListener(self.0.dup(&[])?))
200     }
201
202     pub fn take_error(&self) -> Result<Option<Error>> {
203         Ok(None)
204     }
205
206     pub fn socket_addr(&self) -> Result<SocketAddr> {
207         let path = self.0.path()?;
208         Ok(path_to_local_addr(path.to_str().unwrap_or("")))
209     }
210
211     pub fn nonblocking(&self) -> Result<bool> {
212         Err(Error::new(ErrorKind::Other, "TcpListener::nonblocking not implemented"))
213     }
214
215     pub fn only_v6(&self) -> Result<bool> {
216         Err(Error::new(ErrorKind::Other, "TcpListener::only_v6 not implemented"))
217     }
218
219     pub fn ttl(&self) -> Result<u32> {
220         let mut ttl = [0];
221         let file = self.0.dup(b"ttl")?;
222         file.read(&mut ttl)?;
223         Ok(ttl[0] as u32)
224     }
225
226     pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
227         Err(Error::new(ErrorKind::Other, "TcpListener::set_nonblocking not implemented"))
228     }
229
230     pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
231         Err(Error::new(ErrorKind::Other, "TcpListener::set_only_v6 not implemented"))
232     }
233
234     pub fn set_ttl(&self, ttl: u32) -> Result<()> {
235         let file = self.0.dup(b"ttl")?;
236         file.write(&[cmp::min(ttl, 255) as u8])?;
237         Ok(())
238     }
239 }
240
241 impl AsInner<File> for TcpListener {
242     fn as_inner(&self) -> &File { &self.0 }
243 }
244
245 impl FromInner<File> for TcpListener {
246     fn from_inner(file: File) -> TcpListener {
247         TcpListener(file)
248     }
249 }
250
251 impl IntoInner<File> for TcpListener {
252     fn into_inner(self) -> File { self.0 }
253 }