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:
JettChenT 2023-06-19 22:45:25 +08:00 committed by Conrad Kramer
parent d3882bd008
commit beae8c0f79
12 changed files with 135 additions and 64 deletions

View file

@ -8,5 +8,9 @@ pub(crate) mod imp;
mod options;
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
#[cfg(feature = "tokio")]
pub mod tokio;
pub use imp::{TunInterface, TunQueue};
pub use options::TunOptions;

58
tun/src/tokio/mod.rs Normal file
View 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);
}
}

View file

@ -2,13 +2,10 @@ use byteorder::{ByteOrder, NetworkEndian};
use fehler::throws;
use libc::{c_char, iovec, writev, AF_INET, AF_INET6};
use socket2::{Domain, SockAddr, Socket, Type};
use std::io::{IoSlice, Write};
use std::io::IoSlice;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::os::fd::{AsRawFd, RawFd};
use std::{
io::{Error},
mem,
};
use std::{io::Error, mem};
mod kern_control;
mod sys;
@ -130,11 +127,9 @@ impl TunInterface {
self.perform(|fd| unsafe { sys::if_set_netmask(fd, &iff) })?;
}
}
impl Write for TunInterface {
#[throws]
fn write(&mut self, buf: &[u8]) -> usize {
pub fn send(&self, buf: &[u8]) -> usize {
use std::io::ErrorKind;
let proto = match buf[0] >> 4 {
6 => Ok(AF_INET6),
@ -156,10 +151,6 @@ impl Write for TunInterface {
.try_into()
.map_err(|_| Error::new(ErrorKind::Other, "Conversion error"))?
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[cfg(test)]

View file

@ -166,17 +166,11 @@ impl TunInterface {
let socket = Socket::new(Domain::IPV6, Type::DGRAM, None)?;
perform(socket.as_raw_fd())?
}
}
impl Write for TunInterface {
#[throws]
fn write(&mut self, buf: &[u8]) -> usize {
pub fn send(&self, buf: &[u8]) -> usize {
self.socket.send(buf)?
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[cfg(test)]

View file

@ -40,13 +40,8 @@ impl IntoRawFd for TunInterface {
}
impl TunInterface {
// #[throws]
// pub fn write(&self, buf: &[u8]) -> usize {
// self.socket.send(buf)?
// }
#[throws]
pub fn read(&mut self, buf: &mut [u8]) -> usize {
pub fn recv(&mut self, buf: &mut [u8]) -> usize {
self.socket.read(buf)?
}
}
@ -72,7 +67,6 @@ pub fn string_to_ifname(name: &str) -> [libc::c_char; libc::IFNAMSIZ] {
mod test {
use super::*;
use std::io::Write;
use std::net::Ipv4Addr;
@ -88,7 +82,7 @@ mod test {
println!("tun ip: {:?}", tun.ipv4_addr()?);
println!("Waiting for a packet...");
let buf = &mut [0u8; 1500];
let res = tun.read(buf);
let res = tun.recv(buf);
println!("Received!");
assert!(res.is_ok());
}
@ -96,10 +90,10 @@ mod test {
#[test]
#[throws]
fn write_packets() {
let mut tun = TunInterface::new()?;
let tun = TunInterface::new()?;
let mut buf = [0u8; 1500];
buf[0] = 6 << 4;
let bytes_written = tun.write(&buf)?;
let bytes_written = tun.send(&buf)?;
assert_eq!(bytes_written, 1504);
}
}