Add wrappers for TUN/TAP devices and WireGuard devices

This commit is contained in:
Lucy 2022-02-08 12:27:35 -05:00
parent 4704a1672d
commit 8bdccc1f18
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
7 changed files with 194 additions and 2 deletions

View file

@ -1,7 +1,9 @@
// SPDX-License-Identifier: MPL-2.0
pub mod bluetooth;
pub mod tun;
pub mod wired;
pub mod wireguard;
pub mod wireless;
use crate::{
@ -11,8 +13,8 @@ use crate::{
active_connection::ActiveConnectionProxy,
config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy},
device::{
bluetooth::BluetoothDeviceProxy, wired::WiredDeviceProxy,
wireless::WirelessDeviceProxy, DeviceProxy,
bluetooth::BluetoothDeviceProxy, tun::TunDeviceProxy, wired::WiredDeviceProxy,
wireguard::WireGuardDeviceProxy, wireless::WirelessDeviceProxy, DeviceProxy,
},
enums::{DeviceCapabilities, DeviceState, DeviceType},
},
@ -80,6 +82,20 @@ impl<'a> Device<'a> {
.await?
.into(),
))),
DeviceType::TunTap => Ok(Some(SpecificDevice::TunTap(
TunDeviceProxy::builder(self.0.connection())
.path(self.0.path())?
.build()
.await?
.into(),
))),
DeviceType::WireGuard => Ok(Some(SpecificDevice::WireGuard(
WireGuardDeviceProxy::builder(self.0.connection())
.path(self.0.path())?
.build()
.await?
.into(),
))),
_ => Ok(None),
}
}
@ -127,6 +143,8 @@ pub enum SpecificDevice<'a> {
Bluetooth(bluetooth::BluetoothDevice<'a>),
Wired(wired::WiredDevice<'a>),
Wireless(wireless::WirelessDevice<'a>),
TunTap(tun::TunDevice<'a>),
WireGuard(wireguard::WireGuardDevice<'a>),
}
impl<'a> SpecificDevice<'a> {
@ -150,4 +168,18 @@ impl<'a> SpecificDevice<'a> {
_ => None,
}
}
pub fn into_tun(self) -> Option<tun::TunDevice<'a>> {
match self {
SpecificDevice::TunTap(device) => Some(device),
_ => None,
}
}
pub fn into_wireguard(self) -> Option<wireguard::WireGuardDevice<'a>> {
match self {
SpecificDevice::WireGuard(device) => Some(device),
_ => None,
}
}
}

View file

@ -0,0 +1,41 @@
// SPDX-License-Identifier: MPL-2.0
use super::Device;
use crate::interface::device::{tun::TunDeviceProxy, DeviceProxy};
use std::ops::Deref;
use zbus::Result;
#[derive(Debug)]
pub struct TunDevice<'a>(TunDeviceProxy<'a>);
impl<'a> TunDevice<'a> {
pub async fn upcast(&'a self) -> Result<Device<'a>> {
DeviceProxy::builder(self.0.connection())
.path(self.0.path())?
.build()
.await
.map(Device::from)
}
pub async fn owner(&self) -> Result<Option<u16>> {
let owner = self.0.owner().await?;
match owner {
-1 => Ok(None),
_ => Ok(u16::try_from(owner).ok()),
}
}
}
impl<'a> Deref for TunDevice<'a> {
type Target = TunDeviceProxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> From<TunDeviceProxy<'a>> for TunDevice<'a> {
fn from(device: TunDeviceProxy<'a>) -> Self {
TunDevice(device)
}
}

View file

@ -0,0 +1,33 @@
// SPDX-License-Identifier: MPL-2.0
use super::Device;
use crate::interface::device::{wireguard::WireGuardDeviceProxy, DeviceProxy};
use std::ops::Deref;
use zbus::Result;
#[derive(Debug)]
pub struct WireGuardDevice<'a>(WireGuardDeviceProxy<'a>);
impl<'a> WireGuardDevice<'a> {
pub async fn upcast(&'a self) -> Result<Device<'a>> {
DeviceProxy::builder(self.0.connection())
.path(self.0.path())?
.build()
.await
.map(Device::from)
}
}
impl<'a> Deref for WireGuardDevice<'a> {
type Target = WireGuardDeviceProxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> From<WireGuardDeviceProxy<'a>> for WireGuardDevice<'a> {
fn from(device: WireGuardDeviceProxy<'a>) -> Self {
WireGuardDevice(device)
}
}

View file

@ -21,7 +21,9 @@
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
pub mod bluetooth;
pub mod tun;
pub mod wired;
pub mod wireguard;
pub mod wireless;
use zbus::dbus_proxy;

View file

@ -0,0 +1,48 @@
// SPDX-License-Identifier: MPL-2.0
//! # DBus interface proxy for: `org.freedesktop.NetworkManager.Device.Tun`
//!
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
//! Source: `org.freedesktop.NetworkManager.Device.Tun.xml`.
//!
//! You may prefer to adapt it, instead of using it verbatim.
//!
//! More information can be found in the
//! [Writing a client proxy](https://dbus.pages.freedesktop.org/zbus/client.html)
//! section of the zbus documentation.
//!
use zbus::dbus_proxy;
#[dbus_proxy(
interface = "org.freedesktop.NetworkManager.Device.Tun",
default_service = "org.freedesktop.NetworkManager"
)]
pub trait TunDevice {
/// Group property
#[dbus_proxy(property)]
fn group(&self) -> zbus::Result<i64>;
/// HwAddress property
#[dbus_proxy(property)]
fn hw_address(&self) -> zbus::Result<String>;
/// Mode property
#[dbus_proxy(property)]
fn mode(&self) -> zbus::Result<String>;
/// MultiQueue property
#[dbus_proxy(property)]
fn multi_queue(&self) -> zbus::Result<bool>;
/// NoPi property
#[dbus_proxy(property)]
fn no_pi(&self) -> zbus::Result<bool>;
/// Owner property
#[dbus_proxy(property)]
fn owner(&self) -> zbus::Result<i64>;
/// VnetHdr property
#[dbus_proxy(property)]
fn vnet_hdr(&self) -> zbus::Result<bool>;
}

View file

@ -0,0 +1,32 @@
// SPDX-License-Identifier: MPL-2.0
//! # DBus interface proxy for: `org.freedesktop.NetworkManager.Device.WireGuard`
//!
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
//! Source: `org.freedesktop.NetworkManager.Device.WireGuard.xml`.
//!
//! You may prefer to adapt it, instead of using it verbatim.
//!
//! More information can be found in the
//! [Writing a client proxy](https://dbus.pages.freedesktop.org/zbus/client.html)
//! section of the zbus documentation.
//!
use zbus::dbus_proxy;
#[dbus_proxy(
interface = "org.freedesktop.NetworkManager.Device.WireGuard",
default_service = "org.freedesktop.NetworkManager"
)]
pub trait WireGuardDevice {
/// FwMark property
#[dbus_proxy(property)]
fn fw_mark(&self) -> zbus::Result<u32>;
/// ListenPort property
#[dbus_proxy(property)]
fn listen_port(&self) -> zbus::Result<u16>;
/// PublicKey property
#[dbus_proxy(property)]
fn public_key(&self) -> zbus::Result<Vec<u8>>;
}

View file

@ -51,6 +51,8 @@ pub enum DeviceType {
Ethernet,
Wifi,
Bluetooth,
TunTap,
WireGuard,
Generic,
Other,
Unknown,
@ -63,6 +65,8 @@ impl From<u32> for DeviceType {
2 => DeviceType::Wifi,
5 => DeviceType::Bluetooth,
14 => DeviceType::Generic,
16 => DeviceType::TunTap,
29 => DeviceType::WireGuard,
3..=32 => DeviceType::Other,
_ => DeviceType::Unknown,
}