Add setup command to TunInterface
This commit is contained in:
parent
0fe630878d
commit
80ae0f9d0f
5 changed files with 111 additions and 2 deletions
|
|
@ -14,6 +14,7 @@ tracing = "0.1"
|
|||
log = "0.4"
|
||||
serde = { version = "1", features = ["derive"], optional = true }
|
||||
schemars = { version = "0.8", optional = true }
|
||||
rtnetlink = "0.14"
|
||||
|
||||
futures = { version = "0.3.28", optional = true }
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue