dg_network_manager 1.0.0

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

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

    #[zbus(property)]
    fn parent(&self) -> ZResult<OwnedObjectPath>;

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

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

    #[zbus(property)]
    fn ttl(&self) -> ZResult<u8>;

    #[zbus(property)]
    fn tos(&self) -> ZResult<u8>;

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

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

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

    #[zbus(property)]
    fn encapsulation_limit(&self) -> ZResult<u8>;

    #[zbus(property)]
    fn flow_label(&self) -> ZResult<u32>;

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

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

impl IPTunnelClient {
    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 = IPTunnelProxy::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 iptunnel-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
    }

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

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

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

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

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

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

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

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

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

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

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

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

    // Helper method to get parent device
    pub async fn get_parent_device(&self, connection: Arc<Connection>) -> Result<Device, Error> {
        let parent_path = self.parent().await?;
        Device::new(connection, parent_path.to_string()).await
    }
}