Commit 9f883fd7 authored by Senol Korkmaz's avatar Senol Korkmaz

use regular expressions to optimize dhcp_clean_leases()

parent 5b4acfac
......@@ -47,45 +47,25 @@ function dhcp_clean_leases()
return;
}
/* Build list of static MACs */
$staticmacs = array();
/* Build lease patterns for static MACs */
$lease_patterns = array();
foreach (legacy_config_get_interfaces(array("virtual" => false)) as $ifname => $ifarr) {
if (isset($config['dhcpd'][$ifname]['staticmap'])) {
foreach($config['dhcpd'][$ifname]['staticmap'] as $static) {
$staticmacs[] = $static['mac'];
$lease_patterns[] = '(\h*lease\s*[0-9\.]+\s*\{[^\{\}]*hardware\h*ethernet\h*' . $static['mac'] . '[^\{\}]*(.*"[^\{\}]*\}|\})\s?)';
}
}
}
/* Read existing leases */
$leases_contents = explode("\n", file_get_contents($leasesfile));
$newleases_contents = array();
$i=0;
while ($i < count($leases_contents)) {
/* Find a lease definition */
if (substr($leases_contents[$i], 0, 6) == "lease ") {
$templease = array();
$thismac = "";
/* Read to the end of the lease declaration */
do {
if (substr($leases_contents[$i], 0, 20) == " hardware ethernet ") {
$thismac = substr($leases_contents[$i], 20, 17);
}
$templease[] = $leases_contents[$i];
$i++;
} while ($leases_contents[$i-1] != "}");
/* Check for a matching MAC address and if not present, keep it. */
if (! in_array($thismac, $staticmacs)) {
$newleases_contents = array_merge($newleases_contents, $templease);
}
} else {
/* It's a line we want to keep, copy it over. */
$newleases_contents[] = $leases_contents[$i];
$i++;
}
}
/* Read existing leases file */
$leases_contents = file_get_contents($leasesfile);
/* Remove existing leases for static MACs */
$leases_contents = preg_replace($lease_patterns, '', $leases_contents);
/* Write out the new leases file */
$fd = fopen($leasesfile, 'w');
fwrite($fd, implode("\n", $newleases_contents));
fwrite($fd, $leases_contents);
fclose($fd);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment