Revision 306b9d00
Added by Jim Pingle about 10 years ago
usr/local/www/services_dhcp.php | ||
---|---|---|
49 | 49 |
exit; |
50 | 50 |
} |
51 | 51 |
|
52 |
/* This function will remove entries from dhcpd.leases that would otherwise |
|
53 |
* overlap with static DHCP reservations. If we don't clean these out, |
|
54 |
* then DHCP will print a warning in the logs about a duplicate lease |
|
55 |
*/ |
|
56 |
function dhcp_clean_leases() { |
|
57 |
global $g, $config; |
|
58 |
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases"; |
|
59 |
if (!file_exists($leasesfile)) |
|
60 |
return; |
|
61 |
/* Build list of static MACs */ |
|
62 |
$staticmacs = array(); |
|
63 |
foreach($config['interfaces'] as $ifname => $ifarr) |
|
64 |
if (is_array($config['dhcpd'][$ifname]['staticmap'])) |
|
65 |
foreach($config['dhcpd'][$ifname]['staticmap'] as $static) |
|
66 |
$staticmacs[] = $static['mac']; |
|
67 |
/* Read existing leases */ |
|
68 |
$leases_contents = explode("\n", file_get_contents($leasesfile)); |
|
69 |
$newleases_contents = array(); |
|
70 |
$i=0; |
|
71 |
while ($i < count($leases_contents)) { |
|
72 |
/* Find a lease definition */ |
|
73 |
if (substr($leases_contents[$i], 0, 6) == "lease ") { |
|
74 |
$templease = array(); |
|
75 |
$thismac = ""; |
|
76 |
/* Read to the end of the lease declaration */ |
|
77 |
do { |
|
78 |
if (substr($leases_contents[$i], 0, 20) == " hardware ethernet ") |
|
79 |
$thismac = substr($leases_contents[$i], 20, 17); |
|
80 |
$templease[] = $leases_contents[$i]; |
|
81 |
$i++; |
|
82 |
} while ($leases_contents[$i-1] != "}"); |
|
83 |
/* Check for a matching MAC address and if not present, keep it. */ |
|
84 |
if (! in_array($thismac, $staticmacs)) |
|
85 |
$newleases_contents = array_merge($newleases_contents, $templease); |
|
86 |
} else { |
|
87 |
/* It's a line we want to keep, copy it over. */ |
|
88 |
$newleases_contents[] = $leases_contents[$i]; |
|
89 |
$i++; |
|
90 |
} |
|
91 |
} |
|
92 |
/* Write out the new leases file */ |
|
93 |
$fd = fopen($leasesfile, 'w'); |
|
94 |
fwrite($fd, implode("\n", $newleases_contents)); |
|
95 |
fclose($fd); |
|
96 |
} |
|
97 |
|
|
98 | 52 |
$if = $_GET['if']; |
99 | 53 |
if (!empty($_POST['if'])) |
100 | 54 |
$if = $_POST['if']; |
... | ... | |
540 | 494 |
$retval = 0; |
541 | 495 |
$retvaldhcp = 0; |
542 | 496 |
$retvaldns = 0; |
543 |
/* Stop DHCP so we can cleanup leases */ |
|
544 |
killbyname("dhcpd"); |
|
545 |
dhcp_clean_leases(); |
|
546 | 497 |
/* dnsmasq_configure calls dhcpd_configure */ |
547 | 498 |
/* no need to restart dhcpd twice */ |
548 | 499 |
if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcpstatic'])) { |
Also available in: Unified diff
Remove the DHCP static lease overlap cleanup and associated function and kill, as it can cause problems with failover scenarios.