Skip to content

Commit

Permalink
Add routes to network model
Browse files Browse the repository at this point in the history
  • Loading branch information
jcronenberg committed Oct 27, 2023
1 parent ff4fc26 commit 1594e7d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
30 changes: 30 additions & 0 deletions rust/agama-dbus-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use crate::network::error::NetworkStateError;
use agama_lib::network::types::{DeviceType, SSID};
use cidr::IpInet;
use std::{
collections::HashMap,
default::Default,
fmt,
net::IpAddr,
str::{self, FromStr},
};
use thiserror::Error;
use uuid::Uuid;
use zbus::zvariant::Value;

#[derive(Default, Clone)]
pub struct NetworkState {
Expand Down Expand Up @@ -337,6 +339,8 @@ pub struct IpConfig {
pub nameservers: Vec<IpAddr>,
pub gateway4: Option<IpAddr>,
pub gateway6: Option<IpAddr>,
pub routes4: Option<Vec<IpRoute>>,
pub routes6: Option<Vec<IpRoute>>,
}

#[derive(Debug, Default, PartialEq, Clone)]
Expand Down Expand Up @@ -433,6 +437,32 @@ impl From<UnknownIpMethod> for zbus::fdo::Error {
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct IpRoute {
pub destination: IpInet,
pub next_hop: Option<IpAddr>,
pub metric: Option<u32>,
}

impl From<&IpRoute> for HashMap<&str, Value<'_>> {
fn from(route: &IpRoute) -> Self {
let mut map: HashMap<&str, Value> = HashMap::from([
("dest", Value::new(route.destination.address().to_string())),
(
"prefix",
Value::new(route.destination.network_length() as u32),
),
]);
if let Some(next_hop) = route.next_hop {
map.insert("next-hop", Value::new(next_hop.to_string()));
}
if let Some(metric) = route.metric {
map.insert("metric", Value::new(metric));
}
map
}
}

#[derive(Debug, Default, PartialEq, Clone)]
pub struct EthernetConnection {
pub base: BaseConnection,
Expand Down
60 changes: 59 additions & 1 deletion rust/agama-dbus-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use agama_lib::{
network::types::SSID,
};
use cidr::IpInet;
use std::{collections::HashMap, net::IpAddr};
use std::{collections::HashMap, net::IpAddr, str::FromStr};
use uuid::Uuid;
use zbus::zvariant::{self, OwnedValue, Value};

Expand Down Expand Up @@ -151,6 +151,17 @@ fn ip_config_to_ipv4_dbus(ip_config: &IpConfig) -> HashMap<&str, zvariant::Value
("method", ip_config.method4.to_string().into()),
]);

if let Some(routes4) = &ip_config.routes4 {
ipv4_dbus.insert(
"route-data",
routes4
.iter()
.map(|route| route.into())
.collect::<Vec<HashMap<&str, Value>>>()
.into(),
);
}

if let Some(gateway) = &ip_config.gateway4 {
ipv4_dbus.insert("gateway", gateway.to_string().into());
}
Expand Down Expand Up @@ -185,6 +196,17 @@ fn ip_config_to_ipv6_dbus(ip_config: &IpConfig) -> HashMap<&str, zvariant::Value
("method", ip_config.method6.to_string().into()),
]);

if let Some(routes6) = &ip_config.routes6 {
ipv6_dbus.insert(
"route-data",
routes6
.iter()
.map(|route| route.into())
.collect::<Vec<HashMap<&str, Value>>>()
.into(),
);
}

if let Some(gateway) = &ip_config.gateway6 {
ipv6_dbus.insert("gateway", gateway.to_string().into());
}
Expand Down Expand Up @@ -331,6 +353,10 @@ fn ip_config_from_dbus(conn: &OwnedNestedHash) -> Option<IpConfig> {
ip_config.nameservers.append(&mut servers);
}

if let Some(route_data) = ipv4.get("route-data") {
ip_config.routes4 = routes_from_dbus(route_data);
}

if let Some(gateway) = ipv4.get("gateway") {
let gateway: &str = gateway.downcast_ref()?;
ip_config.gateway4 = Some(gateway.parse().unwrap());
Expand All @@ -351,6 +377,10 @@ fn ip_config_from_dbus(conn: &OwnedNestedHash) -> Option<IpConfig> {
ip_config.nameservers.append(&mut servers);
}

if let Some(route_data) = ipv6.get("route-data") {
ip_config.routes6 = routes_from_dbus(route_data);
}

if let Some(gateway) = ipv6.get("gateway") {
let gateway: &str = gateway.downcast_ref()?;
ip_config.gateway6 = Some(gateway.parse().unwrap());
Expand All @@ -375,6 +405,34 @@ fn addresses_with_prefix_from_dbus(address_data: &OwnedValue) -> Option<Vec<IpIn
Some(addresses)
}

fn routes_from_dbus(route_data: &OwnedValue) -> Option<Vec<IpRoute>> {
let route_data = route_data.downcast_ref::<zbus::zvariant::Array>()?;
let mut routes: Vec<IpRoute> = vec![];
for route in route_data.get() {
let route_dict = route.downcast_ref::<zvariant::Dict>()?;
let route_map =
<HashMap<String, zvariant::Value<'_>>>::try_from(route_dict.clone()).unwrap();
let dest_str: &str = route_map.get("dest")?.downcast_ref()?;
let prefix: u8 = *route_map.get("prefix")?.downcast_ref::<u32>()? as u8;
let destination = IpInet::new(dest_str.parse().unwrap(), prefix).ok()?;
let mut new_route = IpRoute {
destination,
next_hop: None,
metric: None,
};
if let Some(next_hop) = route_map.get("next-hop") {
let next_hop_str: &str = next_hop.downcast_ref()?;
new_route.next_hop = Some(IpAddr::from_str(next_hop_str).unwrap());
}
if let Some(metric) = route_map.get("metric") {
let metric: u32 = *metric.downcast_ref()?;
new_route.metric = Some(metric);
}
routes.push(new_route)
}
Some(routes)
}

fn nameservers_from_dbus(dns_data: &OwnedValue) -> Option<Vec<IpAddr>> {
let dns_data = dns_data.downcast_ref::<zbus::zvariant::Array>()?;
let mut servers: Vec<IpAddr> = vec![];
Expand Down

0 comments on commit 1594e7d

Please sign in to comment.