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.
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.
12 use io::{self, Error, ErrorKind, Result};
14 use net::{SocketAddr, Shutdown};
16 use sys::fs::{File, OpenOptions};
17 use sys::syscall::TimeSpec;
18 use sys_common::{AsInner, FromInner, IntoInner};
21 use super::{path_to_peer_addr, path_to_local_addr};
24 pub struct TcpStream(File);
27 pub fn connect(addr: &SocketAddr) -> Result<TcpStream> {
28 let path = format!("tcp:{}", addr);
29 let mut options = OpenOptions::new();
32 Ok(TcpStream(File::open(Path::new(path.as_str()), &options)?))
35 pub fn connect_timeout(_addr: &SocketAddr, _timeout: Duration) -> Result<TcpStream> {
36 Err(Error::new(ErrorKind::Other, "TcpStream::connect_timeout not implemented"))
39 pub fn duplicate(&self) -> Result<TcpStream> {
40 Ok(TcpStream(self.0.dup(&[])?))
43 pub fn read(&self, buf: &mut [u8]) -> Result<usize> {
47 pub fn write(&self, buf: &[u8]) -> Result<usize> {
51 pub fn take_error(&self) -> Result<Option<Error>> {
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("")))
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("")))
65 pub fn peek(&self, _buf: &mut [u8]) -> Result<usize> {
66 Err(Error::new(ErrorKind::Other, "TcpStream::peek not implemented"))
69 pub fn shutdown(&self, _how: Shutdown) -> Result<()> {
70 Err(Error::new(ErrorKind::Other, "TcpStream::shutdown not implemented"))
73 pub fn nodelay(&self) -> Result<bool> {
74 Err(Error::new(ErrorKind::Other, "TcpStream::nodelay not implemented"))
77 pub fn nonblocking(&self) -> Result<bool> {
78 self.0.fd().nonblocking()
81 pub fn only_v6(&self) -> Result<bool> {
82 Err(Error::new(ErrorKind::Other, "TcpStream::only_v6 not implemented"))
85 pub fn ttl(&self) -> Result<u32> {
87 let file = self.0.dup(b"ttl")?;
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)))
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)))
112 pub fn set_nodelay(&self, _nodelay: bool) -> Result<()> {
113 Err(Error::new(ErrorKind::Other, "TcpStream::set_nodelay not implemented"))
116 pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
117 self.0.fd().set_nonblocking(nonblocking)
120 pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
121 Err(Error::new(ErrorKind::Other, "TcpStream::set_only_v6 not implemented"))
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])?;
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"));
137 file.write(&TimeSpec {
138 tv_sec: duration.as_secs() as i64,
139 tv_nsec: duration.subsec_nanos() as i32
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"));
154 file.write(&TimeSpec {
155 tv_sec: duration.as_secs() as i64,
156 tv_nsec: duration.subsec_nanos() as i32
165 impl AsInner<File> for TcpStream {
166 fn as_inner(&self) -> &File { &self.0 }
169 impl FromInner<File> for TcpStream {
170 fn from_inner(file: File) -> TcpStream {
175 impl IntoInner<File> for TcpStream {
176 fn into_inner(self) -> File { self.0 }
180 pub struct TcpListener(File);
183 pub fn bind(addr: &SocketAddr) -> Result<TcpListener> {
184 let path = format!("tcp:/{}", addr);
185 let mut options = OpenOptions::new();
188 Ok(TcpListener(File::open(Path::new(path.as_str()), &options)?))
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))
198 pub fn duplicate(&self) -> Result<TcpListener> {
199 Ok(TcpListener(self.0.dup(&[])?))
202 pub fn take_error(&self) -> Result<Option<Error>> {
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("")))
211 pub fn nonblocking(&self) -> Result<bool> {
212 Err(Error::new(ErrorKind::Other, "TcpListener::nonblocking not implemented"))
215 pub fn only_v6(&self) -> Result<bool> {
216 Err(Error::new(ErrorKind::Other, "TcpListener::only_v6 not implemented"))
219 pub fn ttl(&self) -> Result<u32> {
221 let file = self.0.dup(b"ttl")?;
222 file.read(&mut ttl)?;
226 pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
227 Err(Error::new(ErrorKind::Other, "TcpListener::set_nonblocking not implemented"))
230 pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
231 Err(Error::new(ErrorKind::Other, "TcpListener::set_only_v6 not implemented"))
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])?;
241 impl AsInner<File> for TcpListener {
242 fn as_inner(&self) -> &File { &self.0 }
245 impl FromInner<File> for TcpListener {
246 fn from_inner(file: File) -> TcpListener {
251 impl IntoInner<File> for TcpListener {
252 fn into_inner(self) -> File { self.0 }