Skip to content

Commit

Permalink
Add Ipv6Method
Browse files Browse the repository at this point in the history
  • Loading branch information
jcronenberg committed Oct 23, 2023
1 parent 0b494c9 commit 8f0f671
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 31 deletions.
12 changes: 6 additions & 6 deletions rust/agama-dbus-server/src/network/dbus/interfaces/ip_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! to the `Ip<T>` struct.
use crate::network::{
action::Action,
model::{Connection as NetworkConnection, IpConfig, IpMethod},
model::{Connection as NetworkConnection, IpConfig, Ipv4Method, Ipv6Method},
};
use async_std::{channel::Sender, sync::Arc};
use cidr::IpInet;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Ip {
///
/// Possible values: "disabled", "auto", "manual" or "link-local".
///
/// See [crate::network::model::IpMethod].
/// See [crate::network::model::Ipv4Method].
#[dbus_interface(property)]
pub async fn method4(&self) -> String {
let ip_config = self.get_ip_config().await;
Expand All @@ -104,15 +104,15 @@ impl Ip {

#[dbus_interface(property)]
pub async fn set_method4(&mut self, method: &str) -> zbus::fdo::Result<()> {
let method: IpMethod = method.parse()?;
let method: Ipv4Method = method.parse()?;
self.update_config(|ip| ip.method4 = method).await
}

/// IPv6 configuration method.
///
/// Possible values: "disabled", "auto", "manual" or "link-local".
/// Possible values: "disabled", "auto", "manual", "link-local", "ignore" or "dhcp".
///
/// See [crate::network::model::IpMethod].
/// See [crate::network::model::Ipv6Method].
#[dbus_interface(property)]
pub async fn method6(&self) -> String {
let ip_config = self.get_ip_config().await;
Expand All @@ -121,7 +121,7 @@ impl Ip {

#[dbus_interface(property)]
pub async fn set_method6(&mut self, method: &str) -> zbus::fdo::Result<()> {
let method: IpMethod = method.parse()?;
let method: Ipv6Method = method.parse()?;
self.update_config(|ip| ip.method6 = method).await
}

Expand Down
68 changes: 55 additions & 13 deletions rust/agama-dbus-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ pub enum Status {

#[derive(Default, Debug, PartialEq, Clone)]
pub struct IpConfig {
pub method4: IpMethod,
pub method6: IpMethod,
pub method4: Ipv4Method,
pub method6: Ipv6Method,
pub addresses: Vec<IpInet>,
pub nameservers: Vec<IpAddr>,
pub gateway4: Option<IpAddr>,
Expand All @@ -352,34 +352,76 @@ pub struct MatchConfig {
pub struct UnknownIpMethod(String);

#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub enum IpMethod {
pub enum Ipv4Method {
#[default]
Disabled = 0,
Auto = 1,
Manual = 2,
LinkLocal = 3,
}
impl fmt::Display for IpMethod {

impl fmt::Display for Ipv4Method {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match &self {
Ipv4Method::Disabled => "disabled",
Ipv4Method::Auto => "auto",
Ipv4Method::Manual => "manual",
Ipv4Method::LinkLocal => "link-local",
};
write!(f, "{}", name)
}
}

impl FromStr for Ipv4Method {
type Err = UnknownIpMethod;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"disabled" => Ok(Ipv4Method::Disabled),
"auto" => Ok(Ipv4Method::Auto),
"manual" => Ok(Ipv4Method::Manual),
"link-local" => Ok(Ipv4Method::LinkLocal),
_ => Err(UnknownIpMethod(s.to_string())),
}
}
}

#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub enum Ipv6Method {
#[default]
Disabled = 0,
Auto = 1,
Manual = 2,
LinkLocal = 3,
Ignore = 4,
Dhcp = 5,
}

impl fmt::Display for Ipv6Method {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match &self {
IpMethod::Disabled => "disabled",
IpMethod::Auto => "auto",
IpMethod::Manual => "manual",
IpMethod::LinkLocal => "link-local",
Ipv6Method::Disabled => "disabled",
Ipv6Method::Auto => "auto",
Ipv6Method::Manual => "manual",
Ipv6Method::LinkLocal => "link-local",
Ipv6Method::Ignore => "ignore",
Ipv6Method::Dhcp => "dhcp",
};
write!(f, "{}", name)
}
}

impl FromStr for IpMethod {
impl FromStr for Ipv6Method {
type Err = UnknownIpMethod;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"disabled" => Ok(IpMethod::Disabled),
"auto" => Ok(IpMethod::Auto),
"manual" => Ok(IpMethod::Manual),
"link-local" => Ok(IpMethod::LinkLocal),
"disabled" => Ok(Ipv6Method::Disabled),
"auto" => Ok(Ipv6Method::Auto),
"manual" => Ok(Ipv6Method::Manual),
"link-local" => Ok(Ipv6Method::LinkLocal),
"ignore" => Ok(Ipv6Method::Ignore),
"dhcp" => Ok(Ipv6Method::Dhcp),
_ => Err(UnknownIpMethod(s.to_string())),
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/agama-dbus-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ mod test {
"::ffff:c0a8:102".parse::<IpAddr>().unwrap()
]
);
assert_eq!(ip_config.method4, IpMethod::Auto);
assert_eq!(ip_config.method6, IpMethod::Auto);
assert_eq!(ip_config.method4, Ipv4Method::Auto);
assert_eq!(ip_config.method6, Ipv6Method::Auto);
}

#[test]
Expand Down
23 changes: 16 additions & 7 deletions rust/agama-dbus-server/src/network/nm/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
/// Using the newtype pattern around an String is enough. For proper support, we might replace this
/// struct with an enum.
use crate::network::{
model::{IpMethod, SecurityProtocol, WirelessMode},
model::{Ipv4Method, Ipv6Method, SecurityProtocol, WirelessMode},
nm::error::NmError,
};
use agama_lib::network::types::DeviceType;
use std::fmt;
use std::str::FromStr;

#[derive(Debug, PartialEq)]
pub struct NmWirelessMode(pub String);
Expand Down Expand Up @@ -150,15 +151,23 @@ impl NmMethod {
}
}

impl TryFrom<NmMethod> for IpMethod {
impl TryFrom<NmMethod> for Ipv4Method {
type Error = NmError;

fn try_from(value: NmMethod) -> Result<Self, Self::Error> {
match value.as_str() {
"auto" => Ok(IpMethod::Auto),
"manual" => Ok(IpMethod::Manual),
"disabled" => Ok(IpMethod::Disabled),
"link-local" => Ok(IpMethod::LinkLocal),
match Ipv4Method::from_str(value.as_str()) {
Ok(method) => Ok(method),
_ => Err(NmError::UnsupportedIpMethod(value.to_string())),
}
}
}

impl TryFrom<NmMethod> for Ipv6Method {
type Error = NmError;

fn try_from(value: NmMethod) -> Result<Self, Self::Error> {
match Ipv6Method::from_str(value.as_str()) {
Ok(method) => Ok(method),
_ => Err(NmError::UnsupportedIpMethod(value.to_string())),
}
}
Expand Down
6 changes: 3 additions & 3 deletions rust/agama-dbus-server/tests/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod common;
use self::common::DBusServer;
use agama_dbus_server::network::{
self,
model::{self, IpMethod},
model::{self, Ipv4Method, Ipv6Method},
Adapter, NetworkService, NetworkState,
};
use agama_lib::network::{settings, types::DeviceType, NetworkClient};
Expand Down Expand Up @@ -91,9 +91,9 @@ async fn test_add_connection() {
assert_eq!(conn.device_type(), DeviceType::Wireless);
assert_eq!(&conn.addresses, &addresses);
let method4 = conn.method4.as_ref().unwrap();
assert_eq!(method4, &IpMethod::Auto.to_string());
assert_eq!(method4, &Ipv4Method::Auto.to_string());
let method6 = conn.method6.as_ref().unwrap();
assert_eq!(method6, &IpMethod::Disabled.to_string());
assert_eq!(method6, &Ipv6Method::Disabled.to_string());
}

#[test]
Expand Down

0 comments on commit 8f0f671

Please sign in to comment.