dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use super::DeviceClient;
use std::io::{Error, ErrorKind};
use std::sync::Arc;
use zbus::proxy::CacheProperties;
use zbus::{Connection, Result as ZResult, proxy};

#[proxy(
    interface = "org.freedesktop.NetworkManager.Device.Tun",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait Tun {
    // Properties
    #[zbus(property)]
    fn owner(&self) -> ZResult<i64>;

    #[zbus(property)]
    fn group(&self) -> ZResult<i64>;

    #[zbus(property)]
    fn mode(&self) -> ZResult<String>;

    #[zbus(property)]
    fn no_pi(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn vnet_hdr(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn multi_queue(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn hw_address(&self) -> ZResult<String>;
}

#[derive(Debug, Clone)]
pub struct TunClient {
    device: DeviceClient,
    proxy: TunProxy<'static>,
}

impl TunClient {
    pub async fn new(connection: Arc<Connection>, service_path: String) -> Result<Self, Error> {
        let device = DeviceClient::new(connection.clone(), service_path.clone()).await?;

        let proxy = TunProxy::builder(&connection)
            .path(service_path)
            .map_err(|e| Error::new(ErrorKind::InvalidInput, e.to_string()))?
            .cache_properties(CacheProperties::Yes)
            .build()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;

        // Verify we can access tun-specific properties
        proxy
            .mode()
            .await
            .map_err(|e| Error::new(ErrorKind::NotFound, e.to_string()))?;

        Ok(Self { device, proxy })
    }

    // Forward common device methods to the base device client
    pub fn service_path(&self) -> &str {
        self.device.service_path()
    }

    pub async fn interface(&self) -> Result<String, Error> {
        self.device.interface().await
    }

    pub async fn disconnect(&self) -> Result<(), Error> {
        self.device.disconnect().await
    }

    // Tun-specific methods
    pub async fn owner(&self) -> Result<i64, Error> {
        self.proxy
            .owner()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn group(&self) -> Result<i64, Error> {
        self.proxy
            .group()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn mode(&self) -> Result<String, Error> {
        self.proxy
            .mode()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn no_pi(&self) -> Result<bool, Error> {
        self.proxy
            .no_pi()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn vnet_hdr(&self) -> Result<bool, Error> {
        self.proxy
            .vnet_hdr()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn multi_queue(&self) -> Result<bool, Error> {
        self.proxy
            .multi_queue()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn hw_address(&self) -> Result<String, Error> {
        self.proxy
            .hw_address()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }
}