Add setup command to TunInterface

This commit is contained in:
rhaskia 2024-03-03 10:32:32 +13:00 committed by Conrad Kramer
parent 0fe630878d
commit 80ae0f9d0f
5 changed files with 111 additions and 2 deletions

View file

@ -1,6 +1,6 @@
use std::io;
use tokio::io::unix::AsyncFd;
use tokio::io::unix::{AsyncFd, TryIoError};
use tracing::instrument;
#[derive(Debug)]
@ -15,6 +15,13 @@ impl TunInterface {
Ok(Self { inner: AsyncFd::new(tun)? })
}
#[instrument]
pub async fn set_up(&self, up: bool) -> io::Result<()> {
let mut guard = self.inner.readable().await?;
guard.try_io(|inner| inner.get_ref().set_up(up));
Ok(())
}
#[instrument]
pub async fn send(&self, buf: &[u8]) -> io::Result<usize> {
loop {

View file

@ -11,6 +11,7 @@ use std::{
use fehler::throws;
use libc::in6_ifreq;
use rtnetlink::new_connection;
use socket2::{Domain, SockAddr, Socket, Type};
use tracing::{info, instrument};
@ -95,6 +96,15 @@ impl TunInterface {
unsafe { iff.ifr_ifru.ifru_ifindex }
}
#[throws]
#[instrument]
pub fn set_up(&self, up: bool) {
let connection = new_connection()?;
let handle = connection.1;
let link = handle.link().set(self.index()? as u32);
if up { link.up() } else { link.down() }
}
#[throws]
#[instrument]
pub fn set_ipv4_addr(&self, addr: Ipv4Addr) {