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() ...@@ -47,45 +47,25 @@ function dhcp_clean_leases()
return; return;
} }
/* Build list of static MACs */ /* Build lease patterns for static MACs */
$staticmacs = array(); $lease_patterns = array();
foreach (legacy_config_get_interfaces(array("virtual" => false)) as $ifname => $ifarr) { foreach (legacy_config_get_interfaces(array("virtual" => false)) as $ifname => $ifarr) {
if (isset($config['dhcpd'][$ifname]['staticmap'])) { if (isset($config['dhcpd'][$ifname]['staticmap'])) {
foreach($config['dhcpd'][$ifname]['staticmap'] as $static) { 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)); /* Read existing leases file */
$newleases_contents = array(); $leases_contents = file_get_contents($leasesfile);
$i=0;
while ($i < count($leases_contents)) { /* Remove existing leases for static MACs */
/* Find a lease definition */ $leases_contents = preg_replace($lease_patterns, '', $leases_contents);
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++;
}
}
/* Write out the new leases file */ /* Write out the new leases file */
$fd = fopen($leasesfile, 'w'); $fd = fopen($leasesfile, 'w');
fwrite($fd, implode("\n", $newleases_contents)); fwrite($fd, $leases_contents);
fclose($fd); 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