Skip to content

Commit

Permalink
dhcp: last round of changes; closes #4642
Browse files Browse the repository at this point in the history
o Move the IPv6 recompress to dhcpd_staticmap()
o Add DHCPv4 leases page as consumer of dhcpd_staticmap()
o Emit the MAC address in IPv4 case in dhcpd_staticmap()
o Let dhcpd_staticmap() emit valid entries with an IP address
o Check for required hostname in Dnsmasq and Unbound integration
  • Loading branch information
fichtner authored and AdSchellevis committed Aug 13, 2021
1 parent 34508ce commit 3204b6f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
15 changes: 12 additions & 3 deletions src/etc/inc/plugins.inc.d/dhcpd.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1905,14 +1905,21 @@ function dhcpd_staticmap($domain_fallback = 'not.found', $ifconfig_details = nul
interfaces_primary_address6($dhcpif, null, $ifconfig_details) : [null];

foreach ($dhcpifconf['staticmap'] as $host) {
if (empty($host[$ipaddr]) || empty($host['hostname'])) {
if (empty($host[$ipaddr])) {
/* we only return proper entries with an IP address */
continue;
}

if (!empty($ipaddrv6)) {
/* expand IPv6 suffix address */
$host['ipaddrv6'] = make_ipv6_64_address($ipaddrv6, $host['ipaddrv6']);
}

if (!empty($host['ipaddrv6'])) {
/* avoid sloppy input by recompressing the address correctly */
$host['ipaddrv6'] = Net_IPv6::compress(Net_IPv6::uncompress($host['ipaddrv6']));
}

// XXX: dhcpdv6 domain entries have been superseded by domainsearchlist,
// for backward compatibilty support both here.
if (!empty($ipaddrv6) && !empty($host['domainsearchlist'])) {
Expand All @@ -1938,8 +1945,10 @@ function dhcpd_staticmap($domain_fallback = 'not.found', $ifconfig_details = nul
$ipaddr => $host[$ipaddr],
];

if (isset($host['duid'])) {
$entry['duid'] = $host['duid'];
foreach (['mac', 'duid'] as $property) {
if (isset($host[$property])) {
$entry[$property] = $host[$property];
}
}

$staticmap[] = $entry;
Expand Down
4 changes: 4 additions & 0 deletions src/etc/inc/plugins.inc.d/dnsmasq.inc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ function _dnsmasq_add_host_entries()
}

foreach (dhcpd_staticmap($domain, legacy_interfaces_details()) as $host) {
if (empty($host['hostname'])) {
/* cannot register without a hostname */
continue;
}
if (isset($host['ipaddr'])) {
$dhosts .= "{$host['ipaddr']}\t{$host['hostname']}.{$host['domain']} {$host['hostname']}\n";
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/etc/inc/plugins.inc.d/unbound.inc
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,10 @@ function unbound_add_host_entries($ifconfig_details = null)
require_once 'plugins.inc.d/dhcpd.inc'; /* XXX */

foreach (dhcpd_staticmap($config['system']['domain'], $ifconfig_details) as $host) {
if (empty($host['hostname'])) {
/* cannot register without a hostname */
continue;
}
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";
Expand Down
49 changes: 25 additions & 24 deletions src/www/status_dhcp_leases.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* Copyright (C) 2014-2016 Deciso B.V.
* Copyright (C) 2014-2021 Deciso B.V.
* Copyright (C) 2004-2009 Scott Ullrich <sullrich@gmail.com>
* Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>
* All rights reserved.
Expand Down Expand Up @@ -201,7 +201,6 @@ function remove_duplicate($array, $field)
/* remove the old array */
unset($lease_content);

/* remove duplicate items by mac address */
if (count($leases) > 0) {
$leases = remove_duplicate($leases,"ip");
}
Expand All @@ -217,30 +216,32 @@ function remove_duplicate($array, $field)
$macs[$this_lease['mac']] = $i;
}
}
foreach ($interfaces as $ifname => $ifarr) {
if (isset($config['dhcpd'][$ifname]['staticmap'])) {
foreach($config['dhcpd'][$ifname]['staticmap'] as $static) {
$slease = array();
$slease['ip'] = $static['ipaddr'];
$slease['type'] = "static";
$slease['mac'] = $static['mac'];
$slease['start'] = '';
$slease['end'] = '';
$slease['hostname'] = $static['hostname'];
$slease['descr'] = $static['descr'];
$slease['act'] = "static";
$slease['online'] = in_array(strtolower($slease['mac']), $arpdata_mac) ? 'online' : 'offline';
if (isset($macs[$slease['mac']])) {
// update lease with static data
foreach ($slease as $key => $value) {
if (!empty($value)) {
$leases[$macs[$slease['mac']]][$key] = $slease[$key];
}
}
} else {
$leases[] = $slease;

foreach (dhcpd_staticmap() as $static) {
if (!isset($static['ipaddr'])) {
continue;
}

$slease = array();
$slease['ip'] = $static['ipaddr'];
$slease['type'] = 'static';
$slease['mac'] = $static['mac'];
$slease['start'] = '';
$slease['end'] = '';
$slease['hostname'] = $static['hostname'];
$slease['descr'] = $static['descr'];
$slease['act'] = 'static';
$slease['online'] = in_array(strtolower($slease['mac']), $arpdata_mac) ? 'online' : 'offline';

if (isset($macs[$slease['mac']])) {
/* update lease with static data */
foreach ($slease as $key => $value) {
if (!empty($value)) {
$leases[$macs[$slease['mac']]][$key] = $slease[$key];
}
}
} else {
$leases[] = $slease;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/www/status_dhcpv6_leases.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* Copyright (C) 2014-2016 Deciso B.V.
* Copyright (C) 2014-2021 Deciso B.V.
* Copyright (C) 2004-2009 Scott Ullrich <sullrich@gmail.com>
* Copyright (C) 2011 Seth Mos <seth.mos@dds.nl>
* Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>
Expand Down Expand Up @@ -301,7 +301,7 @@ function parse_duid($duid_string)
}

$slease = [];
$slease['ip'] = Net_IPv6::compress($static['ipaddrv6']);
$slease['ip'] = $static['ipaddrv6'];
$slease['if'] = $static['interface'];
$slease['type'] = 'static';
$slease['duid'] = $static['duid'];
Expand Down

0 comments on commit 3204b6f

Please sign in to comment.