1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![cfg(unix)]
#![doc(html_root_url = "https://docs.rs/tokio-utun")]
extern crate futures;
extern crate tokio_io;
extern crate log;
extern crate bytes;
extern crate mio;
extern crate mio_utun;
extern crate tokio_codec;
extern crate tokio_reactor;
use std::io::{self, Read, Write};
use std::os::unix::io::{FromRawFd, RawFd};
use bytes::{Buf, BufMut};
use futures::{Async, Poll};
use mio::Ready;
use tokio_reactor::{PollEvented, Handle};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_codec::{Framed, Encoder, Decoder};
#[derive(Debug)]
pub struct UtunStream {
io: PollEvented<mio_utun::UtunStream>
}
impl UtunStream {
pub fn connect(name: &str) -> io::Result<UtunStream> {
let stream = mio_utun::UtunStream::connect(name)?;
let io = PollEvented::new(stream);
Ok(UtunStream { io })
}
pub fn connect_with_handle(name: &str, handle: &Handle) -> io::Result<UtunStream> {
let stream = mio_utun::UtunStream::connect(name)?;
let io = PollEvented::new_with_handle(stream, handle)?;
Ok(UtunStream { io })
}
pub fn from_fd(fd: RawFd) -> UtunStream {
let stream = unsafe { mio_utun::UtunStream::from_raw_fd(fd) };
let io = PollEvented::new(stream);
UtunStream { io }
}
pub fn from_fd_with_handle(fd: RawFd, handle: &Handle) -> io::Result<UtunStream> {
let stream = unsafe { mio_utun::UtunStream::from_raw_fd(fd) };
let io = PollEvented::new_with_handle(stream, handle)?;
Ok(UtunStream { io })
}
pub fn name(&self) -> io::Result<String> {
self.io.get_ref().name()
}
pub fn framed<C: Decoder + Encoder>(self, codec: C) -> Framed<UtunStream, C> {
Framed::new(self, codec)
}
pub fn poll_read_ready(&self, ready: Ready) -> Poll<Ready, io::Error> {
self.io.poll_read_ready(ready)
}
pub fn poll_write_ready(&self) -> Poll<Ready, io::Error> {
self.io.poll_write_ready()
}
}
impl Read for UtunStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.io.read(buf)
}
}
impl Write for UtunStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.io.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.io.flush()
}
}
impl AsyncRead for UtunStream {
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
false
}
fn read_buf<B: BufMut>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
match self.io.read(unsafe { buf.bytes_mut() }) {
Ok(n) => {
unsafe { buf.advance_mut(n); }
Ok(Async::Ready(n))
},
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
self.io.clear_read_ready(Ready::readable())?;
Ok(Async::NotReady)
},
Err(e) => Err(e)
}
}
}
impl AsyncWrite for UtunStream {
fn shutdown(&mut self) -> Poll<(), io::Error> {
Ok(().into())
}
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
match self.io.write(buf.bytes()) {
Ok(n) => {
buf.advance(n);
Ok(Async::Ready(n))
},
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
self.io.clear_write_ready()?;
Ok(Async::NotReady)
},
Err(e) => Err(e)
}
}
}