Pull request: all: do not use dhcp clients when server is off

Closes .

Squashed commit of the following:

commit 856ea4ec0c3ffb1da447b93260da90d37cd5d45d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 15 17:39:46 2021 +0300

    dnsforward: imp spacing

commit fa748e5a26cb6a38b5f87c5498287cb734ce7a59
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 15 17:33:44 2021 +0300

    dnsforward: imp code

commit 771ba0de449faffff1cea523e8bbcc1039c992db
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Apr 15 17:06:03 2021 +0300

    all: do not use dhcp clients when server is off
This commit is contained in:
Ainar Garipov 2021-04-15 17:52:53 +03:00
parent b0013065a2
commit a1450c5595
8 changed files with 131 additions and 68 deletions
internal/home

View file

@ -121,7 +121,7 @@ func (clients *clientsContainer) Init(
clients.addFromConfig(objects)
if !clients.testing {
clients.addFromDHCP()
clients.updateFromDHCP(true)
if clients.dhcpServer != nil {
clients.dhcpServer.SetOnLeaseChanged(clients.onDHCPLeaseChanged)
}
@ -244,7 +244,9 @@ func (clients *clientsContainer) onDHCPLeaseChanged(flags int) {
case dhcpd.LeaseChangedAdded,
dhcpd.LeaseChangedAddedStatic,
dhcpd.LeaseChangedRemovedStatic:
clients.addFromDHCP()
clients.updateFromDHCP(true)
case dhcpd.LeaseChangedRemovedAll:
clients.updateFromDHCP(false)
}
}
@ -768,9 +770,9 @@ func (clients *clientsContainer) addFromSystemARP() {
log.Debug("clients: added %d client aliases from 'arp -a' command output", n)
}
// addFromDHCP adds the clients that have a non-empty hostname from the DHCP
// updateFromDHCP adds the clients that have a non-empty hostname from the DHCP
// server.
func (clients *clientsContainer) addFromDHCP() {
func (clients *clientsContainer) updateFromDHCP(add bool) {
if clients.dhcpServer == nil {
return
}
@ -780,6 +782,10 @@ func (clients *clientsContainer) addFromDHCP() {
clients.rmHostsBySrc(ClientSourceDHCP)
if !add {
return
}
leases := clients.dhcpServer.Leases(dhcpd.LeasesAll)
n := 0
for _, l := range leases {