diff options
author | jim-p <jim@pingle.org> | 2009-04-17 14:39:52 -0400 |
---|---|---|
committer | jim-p <jim@pingle.org> | 2009-04-17 14:41:17 -0400 |
commit | c2ffc6c158e890f05ddc63a863f39b0604e6c675 (patch) | |
tree | 03d9eb05c5e61b2999671931847c9738d0117748 | |
parent | f6a11dac62899a60378f91089f0ea53650eca09f (diff) | |
download | pfsense-c2ffc6c158e890f05ddc63a863f39b0604e6c675.zip pfsense-c2ffc6c158e890f05ddc63a863f39b0604e6c675.tar.gz |
Remove DHCP leases for MAC addresses that now have static/fixed addresses. If these are not cleared, DHCP complains in the logs about duplicate leases.
-rwxr-xr-x | usr/local/www/services_dhcp.php | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/usr/local/www/services_dhcp.php b/usr/local/www/services_dhcp.php index 59b3415..e7f70f6 100755 --- a/usr/local/www/services_dhcp.php +++ b/usr/local/www/services_dhcp.php @@ -43,6 +43,50 @@ if(!$g['services_dhcp_server_enable']) { exit; } +/* This function will remove entries from dhcpd.leases that would otherwise + * overlap with static DHCP reservations. If we don't clean these out, + * then DHCP will print a warning in the logs about a duplicate lease + */ +function dhcp_clean_leases() { + global $g, $config; + $leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases"; + /* Build list of static MACs */ + $staticmacs = array(); + foreach($config['interfaces'] as $ifname => $ifarr) + if (is_array($config['dhcpd'][$ifname]['staticmap'])) + foreach($config['dhcpd'][$ifname]['staticmap'] as $static) + $staticmacs[] = $static['mac']; + /* 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++; + } + } + /* Write out the new leases file */ + $fd = fopen($leasesfile, 'w'); + fwrite($fd, implode("\n", $newleases_contents)); + fclose($fd); +} + $if = $_GET['if']; if ($_POST['if']) $if = $_POST['if']; @@ -259,6 +303,9 @@ if ($_POST) { $retvaldhcp = 0; $retvaldns = 0; config_lock(); + /* Stop DHCP so we can cleanup leases */ + killbyname("dhcpd"); + dhcp_clean_leases(); /* dnsmasq_configure calls dhcpd_configure */ /* no need to restart dhcpd twice */ if (isset($config['dnsmasq']['regdhcpstatic'])) { |