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::{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 file.write(&TimeSpec {
134 tv_sec: duration.as_secs() as i64,
135 tv_nsec: duration.subsec_nanos() as i32
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
157 impl AsInner<File> for TcpStream {
158 fn as_inner(&self) -> &File { &self.0 }
161 impl FromInner<File> for TcpStream {
162 fn from_inner(file: File) -> TcpStream {
167 impl IntoInner<File> for TcpStream {
168 fn into_inner(self) -> File { self.0 }
172 pub struct TcpListener(File);
175 pub fn bind(addr: &SocketAddr) -> Result<TcpListener> {
176 let path = format!("tcp:/{}", addr);
177 let mut options = OpenOptions::new();
180 Ok(TcpListener(File::open(Path::new(path.as_str()), &options)?))
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))
190 pub fn duplicate(&self) -> Result<TcpListener> {
191 Ok(TcpListener(self.0.dup(&[])?))
194 pub fn take_error(&self) -> Result<Option<Error>> {
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("")))
203 pub fn nonblocking(&self) -> Result<bool> {
204 Err(Error::new(ErrorKind::Other, "TcpListener::nonblocking not implemented"))
207 pub fn only_v6(&self) -> Result<bool> {
208 Err(Error::new(ErrorKind::Other, "TcpListener::only_v6 not implemented"))
211 pub fn ttl(&self) -> Result<u32> {
213 let file = self.0.dup(b"ttl")?;
214 file.read(&mut ttl)?;
218 pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> {
219 Err(Error::new(ErrorKind::Other, "TcpListener::set_nonblocking not implemented"))
222 pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
223 Err(Error::new(ErrorKind::Other, "TcpListener::set_only_v6 not implemented"))
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])?;
233 impl AsInner<File> for TcpListener {
234 fn as_inner(&self) -> &File { &self.0 }
237 impl FromInner<File> for TcpListener {
238 fn from_inner(file: File) -> TcpListener {
243 impl IntoInner<File> for TcpListener {
244 fn into_inner(self) -> File { self.0 }