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
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue