- DHCP: when adding a new static lease: remove dynamic lease with the same MAC

This commit is contained in:
Simon Zolin
2020-04-08 11:55:58 +03:00
parent 105e2dd1ee
commit 33195b9155
2 changed files with 59 additions and 3 deletions

View File

@@ -623,6 +623,12 @@ func (s *Server) AddStaticLease(l Lease) error {
s.leasesLock.Unlock()
return err
}
} else {
err := s.rmDynamicLeaseWithMAC(l.HWAddr)
if err != nil {
s.leasesLock.Unlock()
return err
}
}
s.leases = append(s.leases, &l)
s.reserveIP(l.IP, l.HWAddr)
@@ -649,6 +655,23 @@ func (s *Server) rmDynamicLeaseWithIP(ip net.IP) error {
return nil
}
// Remove a dynamic lease by IP address
func (s *Server) rmDynamicLeaseWithMAC(mac net.HardwareAddr) error {
var newLeases []*Lease
for _, lease := range s.leases {
if bytes.Equal(lease.HWAddr, mac) {
if lease.Expiry.Unix() == leaseExpireStatic {
return fmt.Errorf("static lease with the same IP already exists")
}
s.unreserveIP(lease.IP)
continue
}
newLeases = append(newLeases, lease)
}
s.leases = newLeases
return nil
}
// Remove a lease
func (s *Server) rmLease(l Lease) error {
var newLeases []*Lease