From c7b83f9bedd69f74586627d7e5705cd6e4548239 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 15 Oct 2016 19:08:22 +0200 Subject: [PATCH] unbound-dhcp-bridge: Rewrite update algorithm Before the bridge tries reading any existing leases from unbound but this makes it difficult to destinguish between what is a DHCP lease, static host entry or anything else. This patch will change the bridge back to just remember what has been added to the cache already which makes it easier to keep track. Signed-off-by: Michael Tremer --- config/unbound/unbound-dhcp-leases-bridge | 69 +++++++++-------------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/config/unbound/unbound-dhcp-leases-bridge b/config/unbound/unbound-dhcp-leases-bridge index 70da1e0318..a41354349d 100644 --- a/config/unbound/unbound-dhcp-leases-bridge +++ b/config/unbound/unbound-dhcp-leases-bridge @@ -123,6 +123,23 @@ class UnboundDHCPLeasesBridge(object): for lease in FixLeases(self.fix_leases_file): leases.append(lease) + # Skip any leases that also are a static host + leases = [l for l in leases if not l.fqdn in self.hosts] + + # Remove any inactive or expired leases + leases = [l for l in leases if l.active and not l.expired] + + # Dump leases + if leases: + log.debug("DHCP Leases:") + for lease in leases: + log.debug(" %s:" % lease.fqdn) + log.debug(" State: %s" % lease.binding_state) + log.debug(" Start: %s" % lease.time_starts) + log.debug(" End : %s" % lease.time_ends) + if lease.expired: + log.debug(" Expired") + self.unbound.update_dhcp_leases(leases) def read_static_hosts(self): @@ -455,63 +472,31 @@ class UnboundConfigWriter(object): def __init__(self, path): self.path = path - @property - def existing_leases(self): - local_data = self._control("list_local_data") - ret = {} - - for line in local_data.splitlines(): - try: - hostname, ttl, x, record_type, content = line.split("\t") - except ValueError: - continue - - # Ignore everything that is not A or PTR - if not record_type in ("A", "PTR"): - continue - - if hostname.endswith("."): - hostname = hostname[:-1] - - if content.endswith("."): - content = content[:-1] - - if record_type == "A": - ret[hostname] = content - elif record_type == "PTR": - ret[content] = reverse_pointer_to_ip_address(hostname) - - return ret + self._cached_leases = [] def update_dhcp_leases(self, leases): - # Cache all expired or inactive leases - expired_leases = [l for l in leases if l.expired or not l.active] - # Find any leases that have expired or do not exist any more # but are still in the unbound local data - removed_leases = [] - for fqdn, address in self.existing_leases.items(): - if fqdn in (l.fqdn for l in expired_leases): - removed_leases += [fqdn, address] - - # Strip all non-active or expired leases - leases = [l for l in leases if l.active and not l.expired] + removed_leases = [l for l in self._cached_leases if not l in leases] # Find any leases that have been added - new_leases = [l for l in leases - if l.fqdn not in self.existing_leases] + new_leases = [l for l in leases if l not in self._cached_leases] # End here if nothing has changed if not new_leases and not removed_leases: return + # Update cache + self._cached_leases = leases + # Write out all leases self.write_dhcp_leases(leases) # Update unbound about changes - for hostname in removed_leases: - log.debug("Removing all records for %s" % hostname) - self._control("local_data_remove", hostname) + for l in removed_leases: + for name, ttl, type, content in l.rrset: + log.debug("Removing records for %s" % name) + self._control("local_data_remove", name) for l in new_leases: for rr in l.rrset: -- 2.39.2