Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setup command to TunInterface #262

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion burrow/src/daemon/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ impl DaemonInstance {
}
RunState::Idle => {
let tun_if = st.tun.open()?;
tun_if.set_up(true)?;

debug!("Setting tun on wg_interface");
self.wg_interface.read().await.set_tun(tun_if).await;
debug!("tun set on wg_interface");
Expand All @@ -59,7 +61,6 @@ impl DaemonInstance {
self.tun_interface = self.wg_interface.read().await.get_tun();
debug!("tun_interface set: {:?}", self.tun_interface);


debug!("Cloning wg_interface");
let tmp_wg = self.wg_interface.clone();
debug!("wg_interface cloned");
Expand Down
6 changes: 6 additions & 0 deletions tun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ serde = { version = "1", features = ["derive"], optional = true }
schemars = { version = "0.8", optional = true }

futures = { version = "0.3.28", optional = true }
netlink-packet-route = "0.19.0"
netlink-packet-core = "0.7.0"


[features]
serde = ["dep:serde", "dep:schemars"]
Expand All @@ -24,6 +27,9 @@ tokio = ["tokio/net", "dep:futures"]
[target.'cfg(feature = "tokio")'.dev-dependencies]
tokio = { features = ["rt", "macros"] }

[target.'cfg(target_os = "linux")'.dependencies]
interfaces = "0.0.9"

[target.'cfg(windows)'.dependencies]
lazy_static = "1.4"
libloading = "0.7"
Expand Down
7 changes: 6 additions & 1 deletion tun/src/tokio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io;

use tokio::io::unix::AsyncFd;
use tokio::io::unix::{AsyncFd, TryIoError};
use tracing::instrument;

#[derive(Debug)]
Expand All @@ -15,6 +15,11 @@ impl TunInterface {
Ok(Self { inner: AsyncFd::new(tun)? })
}

#[instrument]
pub fn set_up(&self, up: bool) -> io::Result<()> {
self.inner.get_ref().set_up(up)
}

#[instrument]
pub async fn send(&self, buf: &[u8]) -> io::Result<usize> {
loop {
Expand Down
6 changes: 6 additions & 0 deletions tun/src/unix/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,10 @@ impl TunInterface {
.try_into()
.map_err(|_| Error::new(ErrorKind::Other, "Conversion error"))?
}

#[throws]
#[instrument]
pub fn set_up(&self, up: bool) {
tracing::warn!("Setting tun up is not supported yet on apple.");
}
}
18 changes: 18 additions & 0 deletions tun/src/unix/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ impl TunInterface {
unsafe { iff.ifr_ifru.ifru_ifindex }
}

#[throws]
#[instrument]
pub fn set_up(&self, up: bool) {
let mut inter = interfaces::Interface::get_by_name(&self.name()?)
.unwrap()
.unwrap();
inter.set_up(up).unwrap();
}

#[throws]
#[instrument]
pub fn is_up(&self) -> bool {
let inter = interfaces::Interface::get_by_name(&self.name()?)
.unwrap()
.unwrap();
inter.is_up()
}

#[throws]
#[instrument]
pub fn set_ipv4_addr(&self, addr: Ipv4Addr) {
Expand Down
15 changes: 15 additions & 0 deletions tun/tests/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ fn test_set_get_broadcast_addr() {
assert_eq!(broadcast_addr, result);
}

#[test]
#[throws]
#[cfg(not(any(target_os = "windows", target_vendor = "apple")))]
fn test_set_get_up() {
let tun = TunInterface::new()?;
let addr = Ipv4Addr::new(10, 0, 0, 1);
tun.set_ipv4_addr(addr)?;

let broadcast_addr = Ipv4Addr::new(255, 255, 255, 0);
tun.set_broadcast_addr(broadcast_addr)?;
tun.set_up(true)?;

assert!(tun.is_up()?);
}

#[test]
#[throws]
#[cfg(not(target_os = "windows"))]
Expand Down
Loading