Skip to content

Commit

Permalink
all: do not check local domains when dhcp srv is off
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Apr 29, 2021
1 parent c8092fe commit 49d3ca5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ and this project adheres to

### Fixed

- Local domain name handling when the DHCP server is disabled ([#3028]).
- Normalization of perviously-saved invalid static DHCP leases ([#3027]).
- Validation of IPv6 addresses with zones in system resolvers ([#3022]).

[#3022]: https://github.com/AdguardTeam/AdGuardHome/issues/3022
[#3027]: https://github.com/AdguardTeam/AdGuardHome/issues/3027
[#3028]: https://github.com/AdguardTeam/AdGuardHome/issues/3028



Expand Down
6 changes: 6 additions & 0 deletions internal/dhcpd/dhcpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type Server struct {

// ServerInterface is an interface for servers.
type ServerInterface interface {
Enabled() (ok bool)
Leases(flags int) []Lease
SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT)
}
Expand Down Expand Up @@ -207,6 +208,11 @@ func Create(conf ServerConfig) *Server {
return s
}

// Enabled returns true when the server is enabled.
func (s *Server) Enabled() (ok bool) {
return s.conf.Enabled
}

// server calls this function after DB is updated
func (s *Server) onNotify(flags uint32) {
if flags == LeaseChangedDBStore {
Expand Down
4 changes: 4 additions & 0 deletions internal/dnsforward/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ func (s *Server) hostToIP(host string) (ip net.IP, ok bool) {
//
// TODO(a.garipov): Adapt to AAAA as well.
func (s *Server) processInternalHosts(dctx *dnsContext) (rc resultCode) {
if !s.dhcpServer.Enabled() {
return resultCodeSuccess
}

req := dctx.proxyCtx.Req
q := req.Question[0]

Expand Down
4 changes: 3 additions & 1 deletion internal/dnsforward/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestServer_ProcessInternalHosts_localRestriction(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
s := &Server{
dhcpServer: &testDHCP{},
localDomainSuffix: defaultLocalDomainSuffix,
tableHostToIP: hostToIPTable{
"example": knownIP,
Expand Down Expand Up @@ -201,6 +202,7 @@ func TestServer_ProcessInternalHosts(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
s := &Server{
dhcpServer: &testDHCP{},
localDomainSuffix: tc.suffix,
tableHostToIP: hostToIPTable{
"example": knownIP,
Expand Down Expand Up @@ -318,7 +320,7 @@ func TestLocalRestriction(t *testing.T) {
}
t.Run(tc.name, func(t *testing.T) {
err = s.handleDNSRequest(nil, pctx)
require.Nil(t, err)
require.NoError(t, err)
require.NotNil(t, pctx.Res)
require.Len(t, pctx.Res.Answer, tc.wantLen)
if tc.wantLen > 0 {
Expand Down
16 changes: 11 additions & 5 deletions internal/dnsforward/dnsforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func createTestServer(
require.NotNil(t, snd)

s, err = NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DNSFilter: f,
SubnetDetector: snd,
})
Expand Down Expand Up @@ -736,6 +737,7 @@ func TestBlockedCustomIP(t *testing.T) {

var s *Server
s, err = NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DNSFilter: dnsfilter.New(&dnsfilter.Config{}, filters),
SubnetDetector: snd,
})
Expand Down Expand Up @@ -873,6 +875,7 @@ func TestRewrite(t *testing.T) {

var s *Server
s, err = NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DNSFilter: f,
SubnetDetector: snd,
})
Expand Down Expand Up @@ -1016,11 +1019,13 @@ func TestMatchDNSName(t *testing.T) {

type testDHCP struct{}

func (d *testDHCP) Enabled() (ok bool) { return true }

func (d *testDHCP) Leases(flags int) []dhcpd.Lease {
l := dhcpd.Lease{
IP: net.IP{127, 0, 0, 1},
IP: net.IP{192, 168, 12, 34},
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
Hostname: "localhost",
Hostname: "myhost",
}

return []dhcpd.Lease{l}
Expand Down Expand Up @@ -1056,19 +1061,19 @@ func TestPTRResponseFromDHCPLeases(t *testing.T) {
})

addr := s.dnsProxy.Addr(proxy.ProtoUDP)
req := createTestMessageWithType("1.0.0.127.in-addr.arpa.", dns.TypePTR)
req := createTestMessageWithType("34.12.168.192.in-addr.arpa.", dns.TypePTR)

resp, err := dns.Exchange(req, addr.String())
require.NoError(t, err)

require.Len(t, resp.Answer, 1)

assert.Equal(t, dns.TypePTR, resp.Answer[0].Header().Rrtype)
assert.Equal(t, "1.0.0.127.in-addr.arpa.", resp.Answer[0].Header().Name)
assert.Equal(t, "34.12.168.192.in-addr.arpa.", resp.Answer[0].Header().Name)

ptr, ok := resp.Answer[0].(*dns.PTR)
require.True(t, ok)
assert.Equal(t, "localhost.", ptr.Ptr)
assert.Equal(t, "myhost.", ptr.Ptr)
}

func TestPTRResponseFromHosts(t *testing.T) {
Expand Down Expand Up @@ -1098,6 +1103,7 @@ func TestPTRResponseFromHosts(t *testing.T) {

var s *Server
s, err = NewServer(DNSCreateParams{
DHCPServer: &testDHCP{},
DNSFilter: dnsfilter.New(&c, nil),
SubnetDetector: snd,
})
Expand Down
4 changes: 4 additions & 0 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func setupConfig(args options) {

Context.dhcpServer = dhcpd.Create(config.DHCP)
if Context.dhcpServer == nil {
// TODO(a.garipov): There are a lot of places in the code right
// now which assume that the DHCP server can be nil despite this
// condition. Inspect them and perhaps rewrite them to use
// Enabled() instead.
log.Fatalf("can't initialize dhcp module")
}

Expand Down

0 comments on commit 49d3ca5

Please sign in to comment.