dhcpd: unify loop, improve leases page #4642

This commit is contained in:
Franco Fichtner 2021-02-25 13:40:50 +01:00
parent 43b50ed086
commit db29e02ecb
4 changed files with 78 additions and 96 deletions

View File

@ -1884,52 +1884,61 @@ function dhcpd_dhcrelay6_configure($verbose = false)
}
}
function dhcpd_staticmap($inet = 4, $domain_fallback = 'not.found', $ifconfig_details = null)
function dhcpd_staticmap($domain_fallback = 'not.found', $ifconfig_details = null)
{
$root = $inet == 6 ? 'dhcpdv6' : 'dhcpd';
$ipaddr = $inet == 6 ? 'ipaddrv6' : 'ipaddr';
$staticmap = [];
foreach (config_read_array($root) as $dhcpif => $dhcpifconf) {
if (!isset($dhcpifconf['staticmap']) || !isset($dhcpifconf['enable'])) {
continue;
}
foreach ([4, 6] as $inet) {
$ipaddr = $inet == 6 ? 'ipaddrv6' : 'ipaddr';
$ifconf = config_read_array('interfaces', $dhcpif);
list ($ipaddrv6) = $inet == 6 && isset($ifconf['dhcpd6track6allowoverride']) ?
interfaces_primary_address6($dhcpif, null, $ifconfig_details) : [null];
foreach ($dhcpifconf['staticmap'] as $host) {
if (empty($host[$ipaddr]) || empty($host['hostname'])) {
foreach (config_read_array($inet == 6 ? 'dhcpdv6' : 'dhcpd') as $dhcpif => $dhcpifconf) {
if (!isset($dhcpifconf['staticmap']) || !isset($dhcpifconf['enable'])) {
continue;
}
if (!empty($ipaddrv6)) {
$host['ipaddrv6'] = make_ipv6_64_address($ipaddrv6, $host['ipaddrv6']);
$ifconf = config_read_array('interfaces', $dhcpif);
list ($ipaddrv6) = $inet == 6 && isset($ifconf['dhcpd6track6allowoverride']) ?
interfaces_primary_address6($dhcpif, null, $ifconfig_details) : [null];
foreach ($dhcpifconf['staticmap'] as $host) {
if (empty($host[$ipaddr]) || empty($host['hostname'])) {
continue;
}
if (!empty($ipaddrv6)) {
$host['ipaddrv6'] = make_ipv6_64_address($ipaddrv6, $host['ipaddrv6']);
}
// XXX: dhcpdv6 domain entries have been superseded by domainsearchlist,
// for backward compatibilty support both here.
if (!empty($host['domainsearchlist'])) {
$domain = $host['domainsearchlist'];
} elseif (!empty($host['domain'])) {
$domain = $host['domain'];
} elseif (!empty($dhcpifconf['domainsearchlist'])) {
$domain = $dhcpifconf['domainsearchlist'];
} elseif (!empty($dhcpifconf['domain'])) {
$domain = $dhcpifconf['domain'];
} else {
$domain = $domain_fallback;
}
$domain = explode(";", $domain)[0]; // XXX: first entry of domainsearchlist
$entry = [
'descr' => $host['descr'],
'domain' => $domain,
'hostname' => $host['hostname'],
'interface' => $dhcpif,
$ipaddr => $host[$ipaddr],
];
if (isset($host['duid'])) {
$entry['duid'] = $host['duid'];
}
$staticmap[] = $entry;
}
// XXX: dhcpdv6 domain entries have been superseded by domainsearchlist,
// for backward compatibilty support both here.
if (!empty($host['domainsearchlist'])) {
$domain = $host['domainsearchlist'];
} elseif (!empty($host['domain'])) {
$domain = $host['domain'];
} elseif (!empty($dhcpifconf['domainsearchlist'])) {
$domain = $dhcpifconf['domainsearchlist'];
} elseif (!empty($dhcpifconf['domain'])) {
$domain = $dhcpifconf['domain'];
} else {
$domain = $domain_fallback;
}
$domain = explode(";", $domain)[0]; // XXX: first entry of domainsearchlist
$staticmap[] = [
'descr' => $host['descr'],
'domain' => $domain,
'hostname' => $host['hostname'],
$ipaddr => $host[$ipaddr],
];
}
}

View File

@ -231,14 +231,12 @@ function _dnsmasq_add_host_entries()
$domain = $config['dnsmasq']['regdhcpdomain'];
}
$ifconfig_details = legacy_interfaces_details();
foreach (dhcpd_staticmap(4, $domain, $ifconfig_details) as $host) {
$dhosts .= "{$host['ipaddr']}\t{$host['hostname']}.{$host['domain']} {$host['hostname']}\n";
}
foreach (dhcpd_staticmap(6, $domain, $ifconfig_details) as $host) {
$dhosts .= "{$host['ipaddrv6']}\t{$host['hostname']}.{$host['domain']} {$host['hostname']}\n";
foreach (dhcpd_staticmap($domain, legacy_interfaces_details()) as $host) {
if (isset($host['ipaddr'])) {
$dhosts .= "{$host['ipaddr']}\t{$host['hostname']}.{$host['domain']} {$host['hostname']}\n";
} else {
$dhosts .= "{$host['ipaddrv6']}\t{$host['hostname']}.{$host['domain']} {$host['hostname']}\n";
}
}
}

View File

@ -650,17 +650,14 @@ function unbound_add_host_entries($ifconfig_details = null)
if (isset($config['unbound']['regdhcpstatic'])) {
require_once 'plugins.inc.d/dhcpd.inc'; /* XXX */
foreach (dhcpd_staticmap(4, $config['system']['domain'], $ifconfig_details) as $host) {
$unbound_entries .= "local-data-ptr: \"{$host['ipaddr']} {$host['hostname']}.{$host['domain']}\"\n";
$unbound_entries .= "local-data: \"{$host['hostname']}.{$host['domain']} IN A {$host['ipaddr']}\"\n";
if (!empty($host['descr']) && $unboundcfg['txtsupport'] == 'on') {
$unbound_entries .= "local-data: '{$host['hostname']}.{$host['domain']} TXT \"" . addslashes($host['descr']) . "\"'\n";
foreach (dhcpd_staticmap($config['system']['domain'], $ifconfig_details) as $host) {
if (isset($host['ipaddr'])) {
$unbound_entries .= "local-data-ptr: \"{$host['ipaddr']} {$host['hostname']}.{$host['domain']}\"\n";
$unbound_entries .= "local-data: \"{$host['hostname']}.{$host['domain']} IN A {$host['ipaddr']}\"\n";
} else {
$unbound_entries .= "local-data-ptr: \"{$host['ipaddrv6']} {$host['hostname']}.{$host['domain']}\"\n";
$unbound_entries .= "local-data: \"{$host['hostname']}.{$host['domain']} IN AAAA {$host['ipaddrv6']}\"\n";
}
}
foreach (dhcpd_staticmap(6, $config['system']['domain'], $ifconfig_details) as $host) {
$unbound_entries .= "local-data-ptr: \"{$host['ipaddrv6']} {$host['hostname']}.{$host['domain']}\"\n";
$unbound_entries .= "local-data: \"{$host['hostname']}.{$host['domain']} IN AAAA {$host['ipaddrv6']}\"\n";
if (!empty($host['descr']) && $unboundcfg['txtsupport'] == 'on') {
$unbound_entries .= "local-data: '{$host['hostname']}.{$host['domain']} TXT \"" . addslashes($host['descr']) . "\"'\n";
}

View File

@ -288,27 +288,22 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
asort($pools);
}
foreach ($interfaces as $ifname => $ifarr) {
if (isset($config['dhcpdv6'][$ifname]['staticmap'])) {
foreach($config['dhcpdv6'][$ifname]['staticmap'] as $static) {
$slease = array();
$slease['ip'] = $static['ipaddrv6'];
$slease['type'] = "static";
$slease['duid'] = $static['duid'];
$slease['start'] = "";
$slease['end'] = "";
$slease['hostname'] = $static['hostname'];
$slease['descr'] = $static['descr'];
$slease['act'] = "static";
if (in_array($slease['ip'], array_keys($ndpdata))) {
$slease['online'] = 'online';
} else {
$slease['online'] = 'offline';
}
$leases[] = $slease;
}
foreach (dhcpd_staticmap() as $static) {
if (!isset($static['ipaddrv6'])) {
continue;
}
$slease = [];
$slease['ip'] = $static['ipaddrv6'];
$slease['if'] = $static['interface'];
$slease['type'] = 'static';
$slease['duid'] = $static['duid'];
$slease['start'] = '';
$slease['end'] = '';
$slease['hostname'] = $static['hostname'];
$slease['descr'] = $static['descr'];
$slease['act'] = 'static';
$slease['online'] = in_array($slease['ip'], array_keys($ndpdata)) ? 'online' : 'offline';
$leases[] = $slease;
}
if ($_GET['order']) {
@ -449,31 +444,14 @@ endif;?>
</thead>
<tbody>
<?php
$mac_man = json_decode(configd_run("interface list macdb json"), true);
foreach ($leases as $data):
if (!($data['act'] == 'active' || $data['act'] == 'static' || $_GET['all'] == 1)) {
continue;
}
if ($data['act'] == "static") {
foreach ($config['dhcpdv6'] as $dhcpif => $dhcpifconf) {
if (isset($dhcpifconf['staticmap'])) {
foreach ($dhcpifconf['staticmap'] as $staticent) {
if ($data['ip'] == $staticent['ipaddr']) {
$data['int'] = htmlspecialchars($interfaces[$dhcpif]['descr']);
$data['if'] = $dhcpif;
break;
}
}
}
/* exit as soon as we have an interface */
if ($data['if'] != "") {
break;
}
}
} else {
$data['if'] = convert_real_interface_to_friendly_interface_name(guess_interface_from_ip($data['ip']));
$data['int'] = htmlspecialchars($interfaces[$data['if']]['descr']);
if (!isset($data['if'])) {
$data['if'] = convert_real_interface_to_friendly_interface_name(guess_interface_from_ip($data['ip']));
}
$data['int'] = htmlspecialchars($interfaces[$data['if']]['descr']);
?>
<tr>
<td><?=$data['int'];?></td>