use super::Error;
use crate::commands;
use crate::model;
use futures::stream::BoxStream;
use validator::Validate;
#[derive(Clone)]
pub struct Client;
impl Default for Client {
fn default() -> Self {
Self::new()
}
}
impl Client {
pub fn new() -> Self {
Self {}
}
async fn selector_to_ifindex(&self, selector: model::MeshSelector) -> Result<u32, Error> {
selector
.validate()
.map_err(|err| Error::Argument(err.to_string()))?;
if let Some(ifindex) = selector.ifindex {
return Ok(ifindex);
}
if let Some(name) = selector.name {
return commands::if_nametoindex(name.as_str()).await.map_err(|_| {
Error::Netlink(format!(
"Error - interface '{}' is not present or not a batman-adv interface",
name
))
});
}
Err(Error::Argument("Invalid selector".to_string()))
}
async fn interface_selector_to_ifindex(
&self,
selector: model::InterfaceSelector,
) -> Result<u32, Error> {
selector
.validate()
.map_err(|err| Error::Argument(err.to_string()))?;
if let Some(ifindex) = selector.ifindex {
return Ok(ifindex);
}
if let Some(name) = selector.name {
return commands::if_nametoindex(name.as_str()).await.map_err(|_| {
Error::Netlink(format!("Error - interface '{}' is not present", name))
});
}
Err(Error::Argument("Invalid selector".to_string()))
}
#[tracing::instrument(skip(self))]
pub async fn originators(
&self,
selector: model::MeshSelector,
) -> Result<Vec<model::Originator>, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_originators(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn gateways(
&self,
selector: Option<model::MeshSelector>,
) -> Result<Vec<model::Gateway>, Error> {
let ifindex = if let Some(selector) = selector {
Some(self.selector_to_ifindex(selector).await?)
} else {
None
};
commands::get_gateways_list(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn subscribe_gateway_events(
&self,
selector: Option<model::MeshSelector>,
) -> Result<BoxStream<'static, Result<model::GatewayEvent, Error>>, Error> {
let ifindex = if let Some(selector) = selector {
Some(self.selector_to_ifindex(selector).await?)
} else {
None
};
commands::UeventListener::subscribe_events(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn get_gw_mode(
&self,
selector: model::MeshSelector,
) -> Result<model::GatewayInfo, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_gateway(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn set_gw_mode(
&self,
selector: model::MeshSelector,
mode: model::GwMode,
down: Option<u32>,
up: Option<u32>,
sel_class: Option<u32>,
) -> Result<(), Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::set_gateway(mode, down, up, sel_class, ifindex).await
}
pub async fn transglobal(
&self,
selector: model::MeshSelector,
) -> Result<Vec<model::TransglobalEntry>, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_transglobal(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn translocal(
&self,
selector: model::MeshSelector,
) -> Result<Vec<model::TranslocalEntry>, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_translocal(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn neighbors(
&self,
selector: model::MeshSelector,
) -> Result<Vec<model::Neighbor>, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_neighbors(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn interface_list(
&self,
selector: model::MeshSelector,
) -> Result<Vec<model::Interface>, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_interfaces(ifindex).await
}
pub async fn interface_add(
&self,
selector: model::MeshSelector,
interface_selector: model::InterfaceSelector,
) -> Result<(), Error> {
let mesh_ifindex = self.selector_to_ifindex(selector).await?;
let iface_ifindex = self
.interface_selector_to_ifindex(interface_selector)
.await?;
commands::set_interface(iface_ifindex, Some(mesh_ifindex)).await
}
#[tracing::instrument(skip(self))]
pub async fn interface_remove(
&self,
interface_selector: model::InterfaceSelector,
) -> Result<(), Error> {
let iface_ifindex = self
.interface_selector_to_ifindex(interface_selector)
.await?;
commands::set_interface(iface_ifindex, None).await
}
#[tracing::instrument(skip(self))]
pub async fn mesh_create(
&self,
mesh_if: &str,
routing_algo: Option<&str>,
mac_addr: Option<macaddr::MacAddr6>,
) -> Result<(), Error> {
commands::create_mesh(mesh_if, routing_algo, mac_addr).await
}
#[tracing::instrument(skip(self))]
pub async fn mesh_list(&self) -> Result<Vec<String>, Error> {
commands::list_meshes().await
}
#[tracing::instrument(skip(self))]
pub async fn mesh_delete(&self, selector: model::MeshSelector) -> Result<(), Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::delete_mesh(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn interfaces_count(&self, selector: model::MeshSelector) -> Result<u32, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::count_interfaces(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn get_aggregation(&self, selector: model::MeshSelector) -> Result<bool, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_aggregation(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn set_aggregation(
&self,
selector: model::MeshSelector,
val: bool,
) -> Result<(), Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::set_aggregation(ifindex, val).await
}
#[tracing::instrument(skip(self))]
pub async fn get_ap_isolation(&self, selector: model::MeshSelector) -> Result<bool, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_ap_isolation(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn set_ap_isolation(
&self,
selector: model::MeshSelector,
val: bool,
) -> Result<(), Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::set_ap_isolation(ifindex, val).await
}
#[tracing::instrument(skip(self))]
pub async fn get_bridge_loop_avoidance(
&self,
selector: model::MeshSelector,
) -> Result<bool, Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::get_bridge_loop_avoidance(ifindex).await
}
#[tracing::instrument(skip(self))]
pub async fn set_bridge_loop_avoidance(
&self,
selector: model::MeshSelector,
val: bool,
) -> Result<(), Error> {
let ifindex = self.selector_to_ifindex(selector).await?;
commands::set_bridge_loop_avoidance(ifindex, val).await
}
#[tracing::instrument(skip(self))]
pub async fn get_default_routing_algo(&self) -> Result<String, Error> {
commands::get_default_routing_algo().await
}
#[tracing::instrument(skip(self))]
pub async fn get_active_routing_algos(&self) -> Result<Vec<(String, String)>, Error> {
commands::get_active_routing_algos().await
}
#[tracing::instrument(skip(self))]
pub async fn get_available_routing_algos(&self) -> Result<Vec<String>, Error> {
commands::get_available_routing_algos().await
}
#[tracing::instrument(skip(self))]
pub async fn set_default_routing_algo(&self, algo: &str) -> Result<(), Error> {
commands::set_default_routing_algo(algo).await
}
}