Add Read and Write for Async TunInterface
Those features are implemented using AsyncFD. While write doesn't require a mutable reference to self, read does. Make Async Tun a feature remove async tun from workspace rename write/read to send/recv
This commit is contained in:
parent
d3882bd008
commit
beae8c0f79
12 changed files with 135 additions and 64 deletions
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
|
|
@ -19,5 +19,5 @@
|
||||||
],
|
],
|
||||||
"[rust]": {
|
"[rust]": {
|
||||||
"editor.defaultFormatter": "rust-lang.rust-analyzer",
|
"editor.defaultFormatter": "rust-lang.rust-analyzer",
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
62
Cargo.lock
generated
62
Cargo.lock
generated
|
|
@ -482,6 +482,21 @@ dependencies = [
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures"
|
||||||
|
version = "0.3.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40"
|
||||||
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
|
"futures-core",
|
||||||
|
"futures-executor",
|
||||||
|
"futures-io",
|
||||||
|
"futures-sink",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-channel"
|
name = "futures-channel"
|
||||||
version = "0.3.28"
|
version = "0.3.28"
|
||||||
|
|
@ -489,6 +504,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2"
|
checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-sink",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -497,6 +513,34 @@ version = "0.3.28"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
|
checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-executor"
|
||||||
|
version = "0.3.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-io"
|
||||||
|
version = "0.3.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-macro"
|
||||||
|
version = "0.3.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.15",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-sink"
|
name = "futures-sink"
|
||||||
version = "0.3.28"
|
version = "0.3.28"
|
||||||
|
|
@ -515,10 +559,16 @@ version = "0.3.28"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533"
|
checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-io",
|
||||||
|
"futures-macro",
|
||||||
|
"futures-sink",
|
||||||
"futures-task",
|
"futures-task",
|
||||||
|
"memchr",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
|
"slab",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -1383,9 +1433,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.28.1"
|
version = "1.28.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0aa32867d44e6f2ce3385e89dceb990188b8bb0fb25b0cf576647a6f98ac5105"
|
checksum = "94d7b1cfd2aa4011f2de74c2c4c63665e27a71006b0a192dcd2710272e73dfa2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
|
@ -1472,6 +1522,7 @@ dependencies = [
|
||||||
"bindgen",
|
"bindgen",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"fehler",
|
"fehler",
|
||||||
|
"futures",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
"libloading",
|
"libloading",
|
||||||
|
|
@ -1487,13 +1538,6 @@ dependencies = [
|
||||||
"zip",
|
"zip",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tun-async"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"tun",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "typenum"
|
name = "typenum"
|
||||||
version = "1.16.0"
|
version = "1.16.0"
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["burrow", "tun-async", "tun"]
|
members = ["burrow", "tun"]
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "tun-async"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
tun = { version = "0.1", path = "../tun" }
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
pub fn add(left: usize, right: usize) -> usize {
|
|
||||||
left + right
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_works() {
|
|
||||||
let result = add(2, 2);
|
|
||||||
assert_eq!(result, 4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -12,6 +12,14 @@ tokio = { version = "1.28", features = [] }
|
||||||
byteorder = "1.4"
|
byteorder = "1.4"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
|
futures = { version = "0.3.28", optional = true }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
tokio = ["tokio/net", "dep:futures"]
|
||||||
|
|
||||||
|
[target.'cfg(feature = "tokio")'.dev-dependencies]
|
||||||
|
tokio = { features = ["rt", "macros"] }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
libloading = "0.7"
|
libloading = "0.7"
|
||||||
|
|
|
||||||
|
|
@ -8,5 +8,9 @@ pub(crate) mod imp;
|
||||||
|
|
||||||
mod options;
|
mod options;
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
|
||||||
|
#[cfg(feature = "tokio")]
|
||||||
|
pub mod tokio;
|
||||||
|
|
||||||
pub use imp::{TunInterface, TunQueue};
|
pub use imp::{TunInterface, TunQueue};
|
||||||
pub use options::TunOptions;
|
pub use options::TunOptions;
|
||||||
|
|
|
||||||
58
tun/src/tokio/mod.rs
Normal file
58
tun/src/tokio/mod.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
use std::io;
|
||||||
|
use tokio::io::unix::AsyncFd;
|
||||||
|
|
||||||
|
pub struct TunInterface {
|
||||||
|
inner: AsyncFd<crate::TunInterface>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TunInterface {
|
||||||
|
pub fn new(tun: crate::TunInterface) -> io::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
inner: AsyncFd::new(tun)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
loop {
|
||||||
|
let mut guard = self.inner.writable().await?;
|
||||||
|
match guard.try_io(|inner| inner.get_ref().send(buf)) {
|
||||||
|
Ok(result) => return result,
|
||||||
|
Err(_would_block) => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
loop {
|
||||||
|
let mut guard = self.inner.readable_mut().await?;
|
||||||
|
match guard.try_io(|inner| (*inner).get_mut().recv(buf)) {
|
||||||
|
Ok(result) => return result,
|
||||||
|
Err(_would_block) => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::net::Ipv4Addr;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_create() {
|
||||||
|
let tun = crate::TunInterface::new().unwrap();
|
||||||
|
let _async_tun = TunInterface::new(tun).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_write() {
|
||||||
|
let tun = crate::TunInterface::new().unwrap();
|
||||||
|
tun.set_ipv4_addr(Ipv4Addr::from([192, 168, 1, 10]))
|
||||||
|
.unwrap();
|
||||||
|
let async_tun = TunInterface::new(tun).unwrap();
|
||||||
|
let mut buf = [0u8; 1500];
|
||||||
|
buf[0] = 6 << 4;
|
||||||
|
let bytes_written = async_tun.write(&buf).await.unwrap();
|
||||||
|
assert!(bytes_written > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,13 +2,10 @@ use byteorder::{ByteOrder, NetworkEndian};
|
||||||
use fehler::throws;
|
use fehler::throws;
|
||||||
use libc::{c_char, iovec, writev, AF_INET, AF_INET6};
|
use libc::{c_char, iovec, writev, AF_INET, AF_INET6};
|
||||||
use socket2::{Domain, SockAddr, Socket, Type};
|
use socket2::{Domain, SockAddr, Socket, Type};
|
||||||
use std::io::{IoSlice, Write};
|
use std::io::IoSlice;
|
||||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||||
use std::os::fd::{AsRawFd, RawFd};
|
use std::os::fd::{AsRawFd, RawFd};
|
||||||
use std::{
|
use std::{io::Error, mem};
|
||||||
io::{Error},
|
|
||||||
mem,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod kern_control;
|
mod kern_control;
|
||||||
mod sys;
|
mod sys;
|
||||||
|
|
@ -130,11 +127,9 @@ impl TunInterface {
|
||||||
|
|
||||||
self.perform(|fd| unsafe { sys::if_set_netmask(fd, &iff) })?;
|
self.perform(|fd| unsafe { sys::if_set_netmask(fd, &iff) })?;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Write for TunInterface {
|
|
||||||
#[throws]
|
#[throws]
|
||||||
fn write(&mut self, buf: &[u8]) -> usize {
|
pub fn send(&self, buf: &[u8]) -> usize {
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
let proto = match buf[0] >> 4 {
|
let proto = match buf[0] >> 4 {
|
||||||
6 => Ok(AF_INET6),
|
6 => Ok(AF_INET6),
|
||||||
|
|
@ -156,10 +151,6 @@ impl Write for TunInterface {
|
||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| Error::new(ErrorKind::Other, "Conversion error"))?
|
.map_err(|_| Error::new(ErrorKind::Other, "Conversion error"))?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> std::io::Result<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -166,17 +166,11 @@ impl TunInterface {
|
||||||
let socket = Socket::new(Domain::IPV6, Type::DGRAM, None)?;
|
let socket = Socket::new(Domain::IPV6, Type::DGRAM, None)?;
|
||||||
perform(socket.as_raw_fd())?
|
perform(socket.as_raw_fd())?
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Write for TunInterface {
|
|
||||||
#[throws]
|
#[throws]
|
||||||
fn write(&mut self, buf: &[u8]) -> usize {
|
pub fn send(&self, buf: &[u8]) -> usize {
|
||||||
self.socket.send(buf)?
|
self.socket.send(buf)?
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> std::io::Result<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -40,13 +40,8 @@ impl IntoRawFd for TunInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TunInterface {
|
impl TunInterface {
|
||||||
// #[throws]
|
|
||||||
// pub fn write(&self, buf: &[u8]) -> usize {
|
|
||||||
// self.socket.send(buf)?
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[throws]
|
#[throws]
|
||||||
pub fn read(&mut self, buf: &mut [u8]) -> usize {
|
pub fn recv(&mut self, buf: &mut [u8]) -> usize {
|
||||||
self.socket.read(buf)?
|
self.socket.read(buf)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +67,6 @@ pub fn string_to_ifname(name: &str) -> [libc::c_char; libc::IFNAMSIZ] {
|
||||||
mod test {
|
mod test {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
use std::net::Ipv4Addr;
|
use std::net::Ipv4Addr;
|
||||||
|
|
||||||
|
|
@ -88,7 +82,7 @@ mod test {
|
||||||
println!("tun ip: {:?}", tun.ipv4_addr()?);
|
println!("tun ip: {:?}", tun.ipv4_addr()?);
|
||||||
println!("Waiting for a packet...");
|
println!("Waiting for a packet...");
|
||||||
let buf = &mut [0u8; 1500];
|
let buf = &mut [0u8; 1500];
|
||||||
let res = tun.read(buf);
|
let res = tun.recv(buf);
|
||||||
println!("Received!");
|
println!("Received!");
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
}
|
}
|
||||||
|
|
@ -96,10 +90,10 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
#[throws]
|
#[throws]
|
||||||
fn write_packets() {
|
fn write_packets() {
|
||||||
let mut tun = TunInterface::new()?;
|
let tun = TunInterface::new()?;
|
||||||
let mut buf = [0u8; 1500];
|
let mut buf = [0u8; 1500];
|
||||||
buf[0] = 6 << 4;
|
buf[0] = 6 << 4;
|
||||||
let bytes_written = tun.write(&buf)?;
|
let bytes_written = tun.send(&buf)?;
|
||||||
assert_eq!(bytes_written, 1504);
|
assert_eq!(bytes_written, 1504);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use fehler::throws;
|
use fehler::throws;
|
||||||
use std::io::Error;
|
use std::io::Error;
|
||||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
use std::net::{Ipv4Addr};
|
||||||
use tun::TunInterface;
|
use tun::TunInterface;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -23,6 +23,7 @@ fn test_set_get_ipv4() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[throws]
|
#[throws]
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
fn test_set_get_ipv6() {
|
fn test_set_get_ipv6() {
|
||||||
let tun = TunInterface::new()?;
|
let tun = TunInterface::new()?;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue