DHCP static leases w/ optional comment

* Ability to add an optional Comment to DHCP  static Leases
    * Sort DHCP static leases by IP (more human readable)
This commit is contained in:
Fabrice Colas
2021-07-27 18:33:00 +02:00
parent 859b99fd60
commit 17597cd2cc
3 changed files with 47 additions and 8 deletions

View File

@@ -154,6 +154,20 @@ function validateDHCPInput()
return $errors;
}
/**
* Compares to string IPs
*
* @param string $ip1
* @param string $ip2
* @return boolean $result
*/
function compareIPs($ip1, $ip2)
{
$ipu1 = sprintf('%u', ip2long($ip1["ip"])) + 0;
$ipu2 = sprintf('%u', ip2long($ip2["ip"])) + 0;
return $ipu1 > $ipu2;
}
/**
* Updates a dnsmasq configuration
*
@@ -171,13 +185,25 @@ function updateDnsmasqConfig($iface,$status)
$config .= $_POST['RangeLeaseTime'];
}
$config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL;
// Static leases
$staticLeases = array();
for ($i=0; $i < count($_POST["static_leases"]["mac"]); $i++) {
$mac = trim($_POST["static_leases"]["mac"][$i]);
$ip = trim($_POST["static_leases"]["ip"][$i]);
$comment = trim($_POST["static_leases"]["comment"][$i]);
if ($mac != "" && $ip != "") {
$config .= "dhcp-host=$mac,$ip".PHP_EOL;
$staticLeases[] = array('mac' => $mac, 'ip' => $ip, 'comment' => $comment);
}
}
// Sort ascending by IPs
usort($staticLeases, "compareIPs");
// Update config
for ($i = 0; $i < count($staticLeases); $i++) {
$mac = $staticLeases[$i]['mac'];
$ip = $staticLeases[$i]['ip'];
$comment = $staticLeases[$i]['comment'];
$config .= "dhcp-host=$mac,$ip # $comment".PHP_EOL;
}
if ($_POST['no-resolv'] == "1") {
$config .= "no-resolv".PHP_EOL;
}