From 4bb7b654ab06f31514ac6b2a78cf2ed8a159cf9f Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 28 May 2019 14:11:47 +0300 Subject: [PATCH] + dhcp: FindIPbyMAC() --- dhcpd/dhcpd.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/dhcpd/dhcpd.go b/dhcpd/dhcpd.go index 4a9eaf73..fd3e807a 100644 --- a/dhcpd/dhcpd.go +++ b/dhcpd/dhcpd.go @@ -540,6 +540,19 @@ func (s *Server) printLeases() { } } +// FindIPbyMAC finds an IP address by MAC address in the currently active DHCP leases +func (s *Server) FindIPbyMAC(mac net.HardwareAddr) net.IP { + now := time.Now().Unix() + s.RLock() + defer s.RUnlock() + for _, l := range s.leases { + if l.Expiry.Unix() > now && bytes.Equal(mac, l.HWAddr) { + return l.IP + } + } + return nil +} + // Reset internal state func (s *Server) reset() { s.Lock()