From 6bf512f96fbe5693473a580330ef954fc7457cd2 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Mon, 23 Dec 2019 16:59:02 +0300 Subject: [PATCH] -(home): fix searching clients by mac address --- dhcpd/dhcpd.go | 12 +++++++++-- dhcpd/dhcpd_test.go | 5 +++-- home/clients_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/dhcpd/dhcpd.go b/dhcpd/dhcpd.go index 0fb38f24..06bb40ea 100644 --- a/dhcpd/dhcpd.go +++ b/dhcpd/dhcpd.go @@ -709,9 +709,17 @@ func (s *Server) FindMACbyIP(ip net.IP) net.HardwareAddr { s.leasesLock.RLock() defer s.leasesLock.RUnlock() + ip4 := ip.To4() + if ip4 == nil { + return nil + } + for _, l := range s.leases { - if l.Expiry.Unix() > now && l.IP.Equal(ip) { - return l.HWAddr + if l.IP.Equal(ip4) { + unix := l.Expiry.Unix() + if unix > now || unix == leaseExpireStatic { + return l.HWAddr + } } } return nil diff --git a/dhcpd/dhcpd_test.go b/dhcpd/dhcpd_test.go index 00c17276..95522f3e 100644 --- a/dhcpd/dhcpd_test.go +++ b/dhcpd/dhcpd_test.go @@ -23,6 +23,7 @@ func check(t *testing.T, result bool, msg string) { func TestDHCP(t *testing.T) { var s = Server{} s.conf.DBFilePath = dbFilename + defer func() { _ = os.Remove(dbFilename) }() var p, p2 dhcp4.Packet var hw net.HardwareAddr var lease *Lease @@ -185,7 +186,7 @@ func TestDB(t *testing.T) { lease, _ = s.reserveLease(p) lease.Expiry = time.Unix(4000000002, 0) - os.Remove("leases.db") + _ = os.Remove("leases.db") s.dbStore() s.reset() @@ -198,7 +199,7 @@ func TestDB(t *testing.T) { check(t, bytes.Equal(s.leases[1].IP, []byte{1, 1, 1, 2}), "leases[1].IP") check(t, s.leases[1].Expiry.Unix() == 4000000002, "leases[1].Expiry") - os.Remove("leases.db") + _ = os.Remove("leases.db") } func TestIsValidSubnetMask(t *testing.T) { diff --git a/home/clients_test.go b/home/clients_test.go index 2573d48f..8ba06a1c 100644 --- a/home/clients_test.go +++ b/home/clients_test.go @@ -1,7 +1,12 @@ package home import ( + "net" + "os" "testing" + "time" + + "github.com/AdguardTeam/AdGuardHome/dhcpd" "github.com/stretchr/testify/assert" ) @@ -172,3 +177,48 @@ func TestClientsWhois(t *testing.T) { assert.True(t, clients.idIndex["1.1.1.2"].WhoisInfo[0][1] == "orgname-val") _ = clients.Del("client1") } + +func TestClientsAddExistingHost(t *testing.T) { + var c Client + clients := clientsContainer{} + clients.testing = true + clients.Init(nil, nil) + + // some test variables + mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa") + testIP := "1.2.3.4" + + // add a client + c = Client{ + IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"}, + Name: "client1", + } + ok, err := clients.Add(c) + assert.True(t, ok) + assert.Nil(t, err) + + // try adding a duplicate by IP + ok, err = clients.AddHost("1.1.1.1", "test", ClientSourceRDNS) + assert.False(t, ok) + assert.Nil(t, err) + + // now some more complicated stuff + // first, init a DHCP server with a single static lease + config := dhcpd.ServerConfig{ + DBFilePath: "leases.db", + } + defer func() { _ = os.Remove("leases.db") }() + clients.dhcpServer = dhcpd.Create(config) + err = clients.dhcpServer.AddStaticLease(dhcpd.Lease{ + HWAddr: mac, + IP: net.ParseIP(testIP).To4(), + Hostname: "testhost", + Expiry: time.Now().Add(time.Hour), + }) + assert.Nil(t, err) + + // try adding a duplicate IP which for a Mac-based client + ok, err = clients.AddHost(testIP, "test", ClientSourceRDNS) + assert.False(t, ok) + assert.Nil(t, err) +}