fix: Support multiple mac address on static host binding
Some checks failed
Run Check Script / check (pull_request) Failing after 27s

This commit is contained in:
2025-09-03 08:38:30 -04:00
parent ceea03d6ce
commit 5142e2dd2d
6 changed files with 375 additions and 359 deletions

View File

@@ -75,16 +75,16 @@ impl<'a> DhcpConfigDnsMasq<'a> {
/// ambiguous state.
pub fn add_static_mapping(
&mut self,
mac: &str,
ipaddr: Ipv4Addr,
mac: &Vec<String>,
ipaddr: &Ipv4Addr,
hostname: &str,
) -> Result<(), DhcpError> {
let mut hostname_split = hostname.split(".");
let hostname = hostname_split.next().expect("hostname cannot be empty");
let domain_name = hostname_split.collect::<Vec<&str>>().join(".");
if !Self::is_valid_mac(mac) {
return Err(DhcpError::InvalidMacAddress(mac.to_string()));
if let Some(m) = mac.iter().find(|m| !Self::is_valid_mac(m)) {
return Err(DhcpError::InvalidMacAddress(m.to_string()));
}
let ip_str = ipaddr.to_string();
@@ -120,17 +120,19 @@ impl<'a> DhcpConfigDnsMasq<'a> {
let mut all_indices: Vec<&usize> = ip_set.union(&hostname_set).collect();
all_indices.sort();
let mac_list = mac.join(",");
match all_indices.len() {
0 => {
info!(
"Creating new static host for {} ({}) with MAC {}",
hostname, ipaddr, mac
hostname, ipaddr, mac_list
);
let new_host = DnsmasqHost {
uuid: Uuid::new_v4().to_string(),
host: hostname.to_string(),
ip: ip_str.into(),
hwaddr: mac.to_string().into(),
hwaddr: mac_list.into(),
local: MaybeString::from("1"),
ignore: Some(0),
domain: domain_name.into(),
@@ -145,36 +147,38 @@ impl<'a> DhcpConfigDnsMasq<'a> {
if host_to_modify_ip != ip_str {
warn!(
"Hostname '{}' already exists with a different IP ({}). Setting new IP {ip_str}. Appending MAC {}.",
hostname, host_to_modify_ip, mac
hostname, host_to_modify_ip, mac_list
);
host_to_modify.ip.content = Some(ip_str);
} else if host_to_modify.host != hostname {
warn!(
"IP {} already exists with a different hostname ('{}'). Setting hostname to {hostname}. Appending MAC {}.",
ipaddr, host_to_modify.host, mac
ipaddr, host_to_modify.host, mac_list
);
host_to_modify.host = hostname.to_string();
}
if !host_to_modify
.hwaddr
.content_string()
.split(',')
.any(|m| m.eq_ignore_ascii_case(mac))
{
info!(
"Appending MAC {} to existing static host for {} ({})",
mac, host_to_modify.host, host_to_modify_ip
);
let mut updated_macs = host_to_modify.hwaddr.content_string().to_string();
updated_macs.push(',');
updated_macs.push_str(mac);
host_to_modify.hwaddr.content = updated_macs.into();
} else {
info!(
for single_mac in mac.iter() {
if !host_to_modify
.hwaddr
.content_string()
.split(',')
.any(|m| m.eq_ignore_ascii_case(single_mac))
{
info!(
"Appending MAC {} to existing static host for {} ({})",
single_mac, host_to_modify.host, host_to_modify_ip
);
let mut updated_macs = host_to_modify.hwaddr.content_string().to_string();
updated_macs.push(',');
updated_macs.push_str(single_mac);
host_to_modify.hwaddr.content = updated_macs.into();
} else {
debug!(
"MAC {} already present in static host entry for {} ({}). No changes made.",
mac, host_to_modify.host, host_to_modify_ip
single_mac, host_to_modify.host, host_to_modify_ip
);
}
}
}
_ => {