dhcp4/6: cleanup LeasesControllers

a typecast was missing in the DHCPv6 LeasesController, interfaces were also not properly listed here due to variable renaming. While here, also clean up the DHCPv4 controller to be more symmetrical.
This commit is contained in:
Stephan de Wit 2023-07-27 09:37:23 +02:00
parent 4fc8865b8f
commit 2eca088bd5
2 changed files with 39 additions and 16 deletions

View File

@ -100,6 +100,8 @@ class LeasesController extends ApiControllerBase
$static['ends'] = '';
$static['hostname'] = $slease['hostname'];
$static['descr'] = $slease['descr'];
$static['if_descr'] = '';
$static['if'] = $slease['interface'];
$static['state'] = 'active';
$static['status'] = in_array(strtolower($static['mac']), $online) ? 'online' : 'offline';
$statics[] = $static;
@ -109,6 +111,16 @@ class LeasesController extends ApiControllerBase
$mac_man = json_decode($backend->configdRun('interface list macdb json'), true);
$interfaces = [];
/* fetch interfaces ranges so we can match leases to interfaces */
$if_ranges = [];
foreach ($config->dhcpd->children() as $dhcpif => $dhcpifconf) {
$if = $config->interfaces->$dhcpif;
if (!empty((string)$if->ipaddr) && !empty((string)$if->subnet)) {
$if_ranges[$dhcpif] = (string)$if->ipaddr . '/' . (string)$if->subnet;
}
}
foreach ($leases as $idx => $lease) {
/* include manufacturer info */
$leases[$idx]['man'] = '';
@ -118,22 +130,32 @@ class LeasesController extends ApiControllerBase
}
/* include interface */
$leases[$idx]['if_descr'] = '';
$leases[$idx]['if'] = '';
foreach ($config->dhcpd->children() as $dhcpif => $dhcpifconf) {
$if = $config->interfaces->$dhcpif;
if (!empty((string)$if->ipaddr) && Util::isIpAddress((string)$if->ipaddr)) {
if (Util::isIPInCIDR($lease['address'], (string)$if->ipaddr . '/' . (string)$if->subnet)) {
$intf = (string)$if->descr;
$leases[$idx]['if_descr'] = $intf;
$leases[$idx]['if'] = $dhcpif;
if (!array_key_exists($dhcpif, $interfaces)) {
$interfaces[$dhcpif] = $intf;
}
$intf = '';
$intf_descr = '';
if (!empty($lease['if'])) {
/* interface already included */
$if = $config->interfaces->{$lease['if']};
if (!empty((string)$if->ipaddr)) {
$intf = $lease['if'];
$intf_descr = (string)$if->descr;
}
} else {
/* interface not known, check range */
foreach ($if_ranges as $if_name => $if_range) {
if (!empty($lease['address']) && Util::isIPInCIDR($lease['address'], $if_range)) {
$intf = $if_name;
$intf_descr = (string)$config->interfaces->$if_name->descr;
break;
}
}
}
$leases[$idx]['if'] = $intf;
$leases[$idx]['if_descr'] = $intf_descr;
if (!empty($intf) && !array_key_exists($intf, $interfaces)) {
$interfaces[$intf] = $intf_descr;
}
}
$response = $this->searchRecordsetBase($leases, null, 'address', function ($key) use ($selected_interfaces) {

View File

@ -174,9 +174,10 @@ class LeasesController extends ApiControllerBase
}
} else {
foreach ($if_ranges as $if_name => $if_range) {
if (Util::isIPInCIDR($lease['address'], $if_range)) {
if (!empty($lease['address']) && Util::isIPInCIDR($lease['address'], $if_range)) {
$intf = $if_name;
$intf_descr = $config->interfaces->$if_name->descr;
$intf_descr = (string)$config->interfaces->$if_name->descr;
break;
}
}
}
@ -184,7 +185,7 @@ class LeasesController extends ApiControllerBase
$leases[$idx]['if'] = $intf;
$leases[$idx]['if_descr'] = $intf_descr;
if (!empty($if_name) && !array_key_exists($if_name, $interfaces)) {
if (!empty($intf) && !array_key_exists($intf, $interfaces)) {
$interfaces[$intf] = $intf_descr;
}
}