1 use std::fmt;
2 use std::io::{Read, Write};
3 use std::net::{self, SocketAddr};
4 use std::os::unix::io::{RawFd, FromRawFd, IntoRawFd, AsRawFd};
5 use std::time::Duration;
6 
7 use libc;
8 use net2::TcpStreamExt;
9 use iovec::IoVec;
10 
11 use {io, Ready, Poll, PollOpt, Token};
12 use event::Evented;
13 
14 use sys::unix::eventedfd::EventedFd;
15 use sys::unix::io::set_nonblock;
16 use sys::unix::uio::VecIo;
17 
18 pub struct TcpStream {
19     inner: net::TcpStream,
20 }
21 
22 pub struct TcpListener {
23     inner: net::TcpListener,
24 }
25 
26 impl TcpStream {
connect(stream: net::TcpStream, addr: &SocketAddr) -> io::Result<TcpStream>27     pub fn connect(stream: net::TcpStream, addr: &SocketAddr) -> io::Result<TcpStream> {
28         set_nonblock(stream.as_raw_fd())?;
29 
30         match stream.connect(addr) {
31             Ok(..) => {}
32             Err(ref e) if e.raw_os_error() == Some(libc::EINPROGRESS) => {}
33             Err(e) => return Err(e),
34         }
35 
36         Ok(TcpStream {
37             inner: stream,
38         })
39     }
40 
from_stream(stream: net::TcpStream) -> TcpStream41     pub fn from_stream(stream: net::TcpStream) -> TcpStream {
42         TcpStream {
43             inner: stream,
44         }
45     }
46 
peer_addr(&self) -> io::Result<SocketAddr>47     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
48         self.inner.peer_addr()
49     }
50 
local_addr(&self) -> io::Result<SocketAddr>51     pub fn local_addr(&self) -> io::Result<SocketAddr> {
52         self.inner.local_addr()
53     }
54 
try_clone(&self) -> io::Result<TcpStream>55     pub fn try_clone(&self) -> io::Result<TcpStream> {
56         self.inner.try_clone().map(|s| {
57             TcpStream {
58                 inner: s,
59             }
60         })
61     }
62 
shutdown(&self, how: net::Shutdown) -> io::Result<()>63     pub fn shutdown(&self, how: net::Shutdown) -> io::Result<()> {
64         self.inner.shutdown(how)
65     }
66 
set_nodelay(&self, nodelay: bool) -> io::Result<()>67     pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
68         self.inner.set_nodelay(nodelay)
69     }
70 
nodelay(&self) -> io::Result<bool>71     pub fn nodelay(&self) -> io::Result<bool> {
72         self.inner.nodelay()
73     }
74 
set_recv_buffer_size(&self, size: usize) -> io::Result<()>75     pub fn set_recv_buffer_size(&self, size: usize) -> io::Result<()> {
76         self.inner.set_recv_buffer_size(size)
77     }
78 
recv_buffer_size(&self) -> io::Result<usize>79     pub fn recv_buffer_size(&self) -> io::Result<usize> {
80         self.inner.recv_buffer_size()
81     }
82 
set_send_buffer_size(&self, size: usize) -> io::Result<()>83     pub fn set_send_buffer_size(&self, size: usize) -> io::Result<()> {
84         self.inner.set_send_buffer_size(size)
85     }
86 
send_buffer_size(&self) -> io::Result<usize>87     pub fn send_buffer_size(&self) -> io::Result<usize> {
88         self.inner.send_buffer_size()
89     }
90 
set_keepalive(&self, keepalive: Option<Duration>) -> io::Result<()>91     pub fn set_keepalive(&self, keepalive: Option<Duration>) -> io::Result<()> {
92         self.inner.set_keepalive(keepalive)
93     }
94 
keepalive(&self) -> io::Result<Option<Duration>>95     pub fn keepalive(&self) -> io::Result<Option<Duration>> {
96         self.inner.keepalive()
97     }
98 
set_ttl(&self, ttl: u32) -> io::Result<()>99     pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
100         self.inner.set_ttl(ttl)
101     }
102 
ttl(&self) -> io::Result<u32>103     pub fn ttl(&self) -> io::Result<u32> {
104         self.inner.ttl()
105     }
106 
set_only_v6(&self, only_v6: bool) -> io::Result<()>107     pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
108         self.inner.set_only_v6(only_v6)
109     }
110 
only_v6(&self) -> io::Result<bool>111     pub fn only_v6(&self) -> io::Result<bool> {
112         self.inner.only_v6()
113     }
114 
set_linger(&self, dur: Option<Duration>) -> io::Result<()>115     pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
116         self.inner.set_linger(dur)
117     }
118 
linger(&self) -> io::Result<Option<Duration>>119     pub fn linger(&self) -> io::Result<Option<Duration>> {
120         self.inner.linger()
121     }
122 
take_error(&self) -> io::Result<Option<io::Error>>123     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
124         self.inner.take_error()
125     }
126 
peek(&self, buf: &mut [u8]) -> io::Result<usize>127     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
128         self.inner.peek(buf)
129     }
130 
readv(&self, bufs: &mut [&mut IoVec]) -> io::Result<usize>131     pub fn readv(&self, bufs: &mut [&mut IoVec]) -> io::Result<usize> {
132         self.inner.readv(bufs)
133     }
134 
writev(&self, bufs: &[&IoVec]) -> io::Result<usize>135     pub fn writev(&self, bufs: &[&IoVec]) -> io::Result<usize> {
136         self.inner.writev(bufs)
137     }
138 }
139 
140 impl<'a> Read for &'a TcpStream {
read(&mut self, buf: &mut [u8]) -> io::Result<usize>141     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
142         (&self.inner).read(buf)
143     }
144 }
145 
146 impl<'a> Write for &'a TcpStream {
write(&mut self, buf: &[u8]) -> io::Result<usize>147     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
148         (&self.inner).write(buf)
149     }
150 
flush(&mut self) -> io::Result<()>151     fn flush(&mut self) -> io::Result<()> {
152         (&self.inner).flush()
153     }
154 }
155 
156 impl Evented for TcpStream {
register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>157     fn register(&self, poll: &Poll, token: Token,
158                 interest: Ready, opts: PollOpt) -> io::Result<()> {
159         EventedFd(&self.as_raw_fd()).register(poll, token, interest, opts)
160     }
161 
reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>162     fn reregister(&self, poll: &Poll, token: Token,
163                   interest: Ready, opts: PollOpt) -> io::Result<()> {
164         EventedFd(&self.as_raw_fd()).reregister(poll, token, interest, opts)
165     }
166 
deregister(&self, poll: &Poll) -> io::Result<()>167     fn deregister(&self, poll: &Poll) -> io::Result<()> {
168         EventedFd(&self.as_raw_fd()).deregister(poll)
169     }
170 }
171 
172 impl fmt::Debug for TcpStream {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result173     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
174         fmt::Debug::fmt(&self.inner, f)
175     }
176 }
177 
178 impl FromRawFd for TcpStream {
from_raw_fd(fd: RawFd) -> TcpStream179     unsafe fn from_raw_fd(fd: RawFd) -> TcpStream {
180         TcpStream {
181             inner: net::TcpStream::from_raw_fd(fd),
182         }
183     }
184 }
185 
186 impl IntoRawFd for TcpStream {
into_raw_fd(self) -> RawFd187     fn into_raw_fd(self) -> RawFd {
188         self.inner.into_raw_fd()
189     }
190 }
191 
192 impl AsRawFd for TcpStream {
as_raw_fd(&self) -> RawFd193     fn as_raw_fd(&self) -> RawFd {
194         self.inner.as_raw_fd()
195     }
196 }
197 
198 impl TcpListener {
new(inner: net::TcpListener) -> io::Result<TcpListener>199     pub fn new(inner: net::TcpListener) -> io::Result<TcpListener> {
200         set_nonblock(inner.as_raw_fd())?;
201         Ok(TcpListener {
202             inner,
203         })
204     }
205 
local_addr(&self) -> io::Result<SocketAddr>206     pub fn local_addr(&self) -> io::Result<SocketAddr> {
207         self.inner.local_addr()
208     }
209 
try_clone(&self) -> io::Result<TcpListener>210     pub fn try_clone(&self) -> io::Result<TcpListener> {
211         self.inner.try_clone().map(|s| {
212             TcpListener {
213                 inner: s,
214             }
215         })
216     }
217 
accept(&self) -> io::Result<(net::TcpStream, SocketAddr)>218     pub fn accept(&self) -> io::Result<(net::TcpStream, SocketAddr)> {
219         self.inner.accept()
220     }
221 
222     #[allow(deprecated)]
set_only_v6(&self, only_v6: bool) -> io::Result<()>223     pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
224         self.inner.set_only_v6(only_v6)
225     }
226 
227     #[allow(deprecated)]
only_v6(&self) -> io::Result<bool>228     pub fn only_v6(&self) -> io::Result<bool> {
229         self.inner.only_v6()
230     }
231 
set_ttl(&self, ttl: u32) -> io::Result<()>232     pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
233         self.inner.set_ttl(ttl)
234     }
235 
ttl(&self) -> io::Result<u32>236     pub fn ttl(&self) -> io::Result<u32> {
237         self.inner.ttl()
238     }
239 
take_error(&self) -> io::Result<Option<io::Error>>240     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
241         self.inner.take_error()
242     }
243 }
244 
245 impl Evented for TcpListener {
register(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>246     fn register(&self, poll: &Poll, token: Token,
247                 interest: Ready, opts: PollOpt) -> io::Result<()> {
248         EventedFd(&self.as_raw_fd()).register(poll, token, interest, opts)
249     }
250 
reregister(&self, poll: &Poll, token: Token, interest: Ready, opts: PollOpt) -> io::Result<()>251     fn reregister(&self, poll: &Poll, token: Token,
252                   interest: Ready, opts: PollOpt) -> io::Result<()> {
253         EventedFd(&self.as_raw_fd()).reregister(poll, token, interest, opts)
254     }
255 
deregister(&self, poll: &Poll) -> io::Result<()>256     fn deregister(&self, poll: &Poll) -> io::Result<()> {
257         EventedFd(&self.as_raw_fd()).deregister(poll)
258     }
259 }
260 
261 impl fmt::Debug for TcpListener {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result262     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
263         fmt::Debug::fmt(&self.inner, f)
264     }
265 }
266 
267 impl FromRawFd for TcpListener {
from_raw_fd(fd: RawFd) -> TcpListener268     unsafe fn from_raw_fd(fd: RawFd) -> TcpListener {
269         TcpListener {
270             inner: net::TcpListener::from_raw_fd(fd),
271         }
272     }
273 }
274 
275 impl IntoRawFd for TcpListener {
into_raw_fd(self) -> RawFd276     fn into_raw_fd(self) -> RawFd {
277         self.inner.into_raw_fd()
278     }
279 }
280 
281 impl AsRawFd for TcpListener {
as_raw_fd(&self) -> RawFd282     fn as_raw_fd(&self) -> RawFd {
283         self.inner.as_raw_fd()
284     }
285 }
286 
287