src/config/network.rs: introduce NetworkInterfaceType

This commit is contained in:
Dietmar Maurer
2020-04-23 08:45:03 +02:00
parent d5ca9bd5df
commit 02269f3dba
4 changed files with 58 additions and 11 deletions

View File

@ -14,15 +14,15 @@ pub use lexer::*;
mod parser;
pub use parser::*;
use crate::api2::types::{Interface, NetworkConfigMethod};
use crate::api2::types::{Interface, NetworkConfigMethod, NetworkInterfaceType};
impl Interface {
pub fn new(name: String) -> Self {
Self {
name,
interface_type: NetworkInterfaceType::Unknown,
auto: false,
exists: false,
active: false,
method_v4: None,
method_v6: None,
@ -153,8 +153,8 @@ impl Interface {
options_v6,
// the rest does not matter
name: _name,
interface_type: _interface_type,
auto: _auto,
exists: _exists,
active: _active,
cidr_v4: _cidr_v4,
cidr_v6: _cidr_v6,

View File

@ -9,7 +9,7 @@ use regex::Regex;
use super::helper::*;
use super::lexer::*;
use super::{NetworkConfig, NetworkOrderEntry, Interface, NetworkConfigMethod};
use super::{NetworkConfig, NetworkOrderEntry, Interface, NetworkConfigMethod, NetworkInterfaceType};
pub struct NetworkParser<R: BufRead> {
input: Peekable<Lexer<R>>,
@ -288,24 +288,44 @@ impl <R: BufRead> NetworkParser<R> {
let existing_interfaces = get_network_interfaces()?;
lazy_static!{
static ref PHYSICAL_NIC_REGEX: Regex = Regex::new(r"(?:eth\d+|en[^:.]+|ib\d+)").unwrap();
static ref PHYSICAL_NIC_REGEX: Regex = Regex::new(r"^(?:eth\d+|en[^:.]+|ib\d+)$").unwrap();
static ref INTERFACE_ALIAS_REGEX: Regex = Regex::new(r"^\S+:\d+$").unwrap();
static ref VLAN_INTERFACE_REGEX: Regex = Regex::new(r"^\S+\.\d+$").unwrap();
}
for (iface, active) in existing_interfaces.iter() {
let exists = PHYSICAL_NIC_REGEX.is_match(iface);
if let Some(interface) = config.interfaces.get_mut(iface) {
interface.exists = exists;
interface.active = *active;
} else if exists { // also add all physical NICs
} else if PHYSICAL_NIC_REGEX.is_match(iface) { // also add all physical NICs
let mut interface = Interface::new(iface.clone());
interface.set_method_v4(NetworkConfigMethod::Manual)?;
interface.exists = true;
interface.interface_type = NetworkInterfaceType::Ethernet;
interface.active = *active;
config.interfaces.insert(interface.name.clone(), interface);
config.order.push(NetworkOrderEntry::Iface(iface.to_string()));
}
}
for (name, interface) in config.interfaces.iter_mut() {
if interface.interface_type != NetworkInterfaceType::Unknown { continue; }
if name == "lo" {
interface.interface_type = NetworkInterfaceType::Loopback;
continue;
}
if INTERFACE_ALIAS_REGEX.is_match(name) {
interface.interface_type = NetworkInterfaceType::Alias;
continue;
}
if VLAN_INTERFACE_REGEX.is_match(name) {
interface.interface_type = NetworkInterfaceType::Vlan;
continue;
}
if PHYSICAL_NIC_REGEX.is_match(name) {
interface.interface_type = NetworkInterfaceType::Ethernet;
continue;
}
}
Ok(config)
}
}