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

Closes #2934.

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

View File

@@ -154,44 +154,60 @@ func isHostnameOK(hostname string) bool {
return true
}
func (s *Server) setTableHostToIP(t hostToIPTable) {
s.tableHostToIPLock.Lock()
defer s.tableHostToIPLock.Unlock()
s.tableHostToIP = t
}
func (s *Server) setTableIPToHost(t ipToHostTable) {
s.tableIPToHostLock.Lock()
defer s.tableIPToHostLock.Unlock()
s.tableIPToHost = t
}
func (s *Server) onDHCPLeaseChanged(flags int) {
add := true
switch flags {
case dhcpd.LeaseChangedAdded,
dhcpd.LeaseChangedAddedStatic,
dhcpd.LeaseChangedRemovedStatic:
//
// Go on.
case dhcpd.LeaseChangedRemovedAll:
add = false
default:
return
}
hostToIP := make(map[string]net.IP)
m := make(map[string]string)
var hostToIP hostToIPTable
var ipToHost ipToHostTable
if add {
hostToIP = make(hostToIPTable)
ipToHost = make(ipToHostTable)
ll := s.dhcpServer.Leases(dhcpd.LeasesAll)
ll := s.dhcpServer.Leases(dhcpd.LeasesAll)
for _, l := range ll {
if len(l.Hostname) == 0 || !isHostnameOK(l.Hostname) {
continue
for _, l := range ll {
if len(l.Hostname) == 0 || !isHostnameOK(l.Hostname) {
continue
}
lowhost := strings.ToLower(l.Hostname)
ipToHost[l.IP.String()] = lowhost
ip := make(net.IP, 4)
copy(ip, l.IP.To4())
hostToIP[lowhost] = ip
}
lowhost := strings.ToLower(l.Hostname)
m[l.IP.String()] = lowhost
ip := make(net.IP, 4)
copy(ip, l.IP.To4())
hostToIP[lowhost] = ip
log.Debug("dns: added %d A/PTR entries from DHCP", len(ipToHost))
}
log.Debug("dns: added %d A/PTR entries from DHCP", len(m))
s.tableHostToIPLock.Lock()
s.tableHostToIP = hostToIP
s.tableHostToIPLock.Unlock()
s.tablePTRLock.Lock()
s.tablePTR = m
s.tablePTRLock.Unlock()
s.setTableHostToIP(hostToIP)
s.setTableIPToHost(ipToHost)
}
// processDetermineLocal determines if the client's IP address is from
@@ -336,14 +352,14 @@ func (s *Server) processRestrictLocal(ctx *dnsContext) (rc resultCode) {
// ipToHost tries to get a hostname leased by DHCP. It's safe for concurrent
// use.
func (s *Server) ipToHost(ip net.IP) (host string, ok bool) {
s.tablePTRLock.Lock()
defer s.tablePTRLock.Unlock()
s.tableIPToHostLock.Lock()
defer s.tableIPToHostLock.Unlock()
if s.tablePTR == nil {
if s.tableIPToHost == nil {
return "", false
}
host, ok = s.tablePTR[ip.String()]
host, ok = s.tableIPToHost[ip.String()]
return host, ok
}

View File

@@ -91,7 +91,7 @@ func TestServer_ProcessInternalHosts_localRestriction(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
s := &Server{
autohostSuffix: defaultAutohostSuffix,
tableHostToIP: map[string]net.IP{
tableHostToIP: hostToIPTable{
"example": knownIP,
},
}
@@ -202,7 +202,7 @@ func TestServer_ProcessInternalHosts(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
s := &Server{
autohostSuffix: tc.suffix,
tableHostToIP: map[string]net.IP{
tableHostToIP: hostToIPTable{
"example": knownIP,
},
}

View File

@@ -43,6 +43,15 @@ var defaultBlockedHosts = []string{"version.bind", "id.server", "hostname.bind"}
var webRegistered bool
// hostToIPTable is an alias for the type of Server.tableHostToIP.
type hostToIPTable = map[string]net.IP
// ipToHostTable is an alias for the type of Server.tableIPToHost.
//
// TODO(a.garipov): Define an IPMap type in aghnet and use here and in other
// places?
type ipToHostTable = map[string]string
// Server is the main way to start a DNS server.
//
// Example:
@@ -69,11 +78,11 @@ type Server struct {
subnetDetector *aghnet.SubnetDetector
localResolvers *proxy.Proxy
tableHostToIP map[string]net.IP // "hostname -> IP" table for internal addresses (DHCP)
tableHostToIP hostToIPTable
tableHostToIPLock sync.Mutex
tablePTR map[string]string // "IP -> hostname" table for reverse lookup
tablePTRLock sync.Mutex
tableIPToHost ipToHostTable
tableIPToHostLock sync.Mutex
// DNS proxy instance for internal usage
// We don't Start() it and so no listen port is required.