Add AddressData to Ipv4Config and Ipv6Config

This commit is contained in:
Lucy 2022-02-09 11:31:01 -05:00
parent a81830cd20
commit 4f28c3c02b
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
3 changed files with 52 additions and 4 deletions

View file

@ -10,5 +10,4 @@ derive_builder = "0.10.2"
procfs = { version = "0.12.0", default-features = false }
time = "0.3.7"
zbus = "2.0.1"
[features]
zvariant = "3.1.2"

View file

@ -1,7 +1,8 @@
// SPDX-License-Identifier: MPL-2.0
use crate::interface::config::ip4::Ipv4ConfigProxy;
use std::{net::Ipv4Addr, ops::Deref};
use std::{net::Ipv4Addr, ops::Deref, str::FromStr};
use zbus::Result;
use zvariant::DeserializeDict;
#[derive(Debug)]
pub struct Ipv4Config<'a>(Ipv4ConfigProxy<'a>);
@ -20,6 +21,24 @@ impl<'a> Ipv4Config<'a> {
})
.collect())
}
pub async fn address_data(&self) -> Result<Vec<AddressData>> {
Ok(self
.0
.address_data()
.await?
.into_iter()
.filter_map(|mut map| {
let address = {
let address_str = map.remove("address")?;
let address_str = address_str.downcast_ref::<zvariant::Str>()?;
Ipv4Addr::from_str(address_str).ok()?
};
let prefix = u64::try_from(map.remove("prefix")?).ok()? as usize;
Some(AddressData { address, prefix })
})
.collect())
}
}
impl<'a> Deref for Ipv4Config<'a> {
@ -35,3 +54,9 @@ impl<'a> From<Ipv4ConfigProxy<'a>> for Ipv4Config<'a> {
Ipv4Config(config)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AddressData {
pub address: Ipv4Addr,
pub prefix: usize,
}

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
use crate::interface::config::ip6::Ipv6ConfigProxy;
use std::{net::Ipv6Addr, ops::Deref};
use std::{net::Ipv6Addr, ops::Deref, str::FromStr};
use zbus::Result;
#[derive(Debug)]
@ -19,6 +19,24 @@ impl<'a> Ipv6Config<'a> {
})
.collect())
}
pub async fn address_data(&self) -> Result<Vec<AddressData>> {
Ok(self
.0
.address_data()
.await?
.into_iter()
.filter_map(|mut map| {
let address = {
let address_str = map.remove("address")?;
let address_str = address_str.downcast_ref::<zvariant::Str>()?;
Ipv6Addr::from_str(address_str).ok()?
};
let prefix = u64::try_from(map.remove("prefix")?).ok()? as usize;
Some(AddressData { address, prefix })
})
.collect())
}
}
impl<'a> Deref for Ipv6Config<'a> {
@ -34,3 +52,9 @@ impl<'a> From<Ipv6ConfigProxy<'a>> for Ipv6Config<'a> {
Ipv6Config(config)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AddressData {
pub address: Ipv6Addr,
pub prefix: usize,
}