Merge: - dhcp: store lease data in database on each change rather than once on app stop
Close #852 * commit '0b3ba8224255247fa751a9922f83154e71a26c02': - dhcp: store lease data in database on each change rather than once on app stop - dhcp: fix race introduced by static lease add/remove from UI thread
This commit is contained in:
commit
d1987e711d
|
@ -207,8 +207,6 @@ func (s *Server) Stop() error {
|
||||||
s.cond.Wait()
|
s.cond.Wait()
|
||||||
}
|
}
|
||||||
s.mutex.Unlock()
|
s.mutex.Unlock()
|
||||||
|
|
||||||
s.dbStore()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,6 +233,10 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
|
||||||
lease := &Lease{HWAddr: hwaddr, Hostname: string(hostname)}
|
lease := &Lease{HWAddr: hwaddr, Hostname: string(hostname)}
|
||||||
|
|
||||||
log.Tracef("Lease not found for %s: creating new one", hwaddr)
|
log.Tracef("Lease not found for %s: creating new one", hwaddr)
|
||||||
|
|
||||||
|
s.leasesLock.Lock()
|
||||||
|
defer s.leasesLock.Unlock()
|
||||||
|
|
||||||
ip, err := s.findFreeIP(hwaddr)
|
ip, err := s.findFreeIP(hwaddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
i := s.findExpiredLease()
|
i := s.findExpiredLease()
|
||||||
|
@ -245,9 +247,8 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
|
||||||
log.Tracef("Assigning IP address %s to %s (lease for %s expired at %s)",
|
log.Tracef("Assigning IP address %s to %s (lease for %s expired at %s)",
|
||||||
s.leases[i].IP, hwaddr, s.leases[i].HWAddr, s.leases[i].Expiry)
|
s.leases[i].IP, hwaddr, s.leases[i].HWAddr, s.leases[i].Expiry)
|
||||||
lease.IP = s.leases[i].IP
|
lease.IP = s.leases[i].IP
|
||||||
s.leasesLock.Lock()
|
|
||||||
s.leases[i] = lease
|
s.leases[i] = lease
|
||||||
s.leasesLock.Unlock()
|
s.dbStore()
|
||||||
|
|
||||||
s.reserveIP(lease.IP, hwaddr)
|
s.reserveIP(lease.IP, hwaddr)
|
||||||
return lease, nil
|
return lease, nil
|
||||||
|
@ -255,9 +256,8 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
|
||||||
|
|
||||||
log.Tracef("Assigning to %s IP address %s", hwaddr, ip.String())
|
log.Tracef("Assigning to %s IP address %s", hwaddr, ip.String())
|
||||||
lease.IP = ip
|
lease.IP = ip
|
||||||
s.leasesLock.Lock()
|
|
||||||
s.leases = append(s.leases, lease)
|
s.leases = append(s.leases, lease)
|
||||||
s.leasesLock.Unlock()
|
s.dbStore()
|
||||||
return lease, nil
|
return lease, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,11 +405,12 @@ func (s *Server) addrAvailable(target net.IP) bool {
|
||||||
// Add the specified IP to the black list for a time period
|
// Add the specified IP to the black list for a time period
|
||||||
func (s *Server) blacklistLease(lease *Lease) {
|
func (s *Server) blacklistLease(lease *Lease) {
|
||||||
hw := make(net.HardwareAddr, 6)
|
hw := make(net.HardwareAddr, 6)
|
||||||
s.reserveIP(lease.IP, hw)
|
|
||||||
s.leasesLock.Lock()
|
s.leasesLock.Lock()
|
||||||
|
s.reserveIP(lease.IP, hw)
|
||||||
lease.HWAddr = hw
|
lease.HWAddr = hw
|
||||||
lease.Hostname = ""
|
lease.Hostname = ""
|
||||||
lease.Expiry = time.Now().Add(s.leaseTime)
|
lease.Expiry = time.Now().Add(s.leaseTime)
|
||||||
|
s.dbStore()
|
||||||
s.leasesLock.Unlock()
|
s.leasesLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -608,19 +609,18 @@ func (s *Server) Leases() []Lease {
|
||||||
// StaticLeases returns the list of statically-configured DHCP leases (thread-safe)
|
// StaticLeases returns the list of statically-configured DHCP leases (thread-safe)
|
||||||
func (s *Server) StaticLeases() []Lease {
|
func (s *Server) StaticLeases() []Lease {
|
||||||
s.leasesLock.Lock()
|
s.leasesLock.Lock()
|
||||||
|
defer s.leasesLock.Unlock()
|
||||||
|
|
||||||
if s.IPpool == nil {
|
if s.IPpool == nil {
|
||||||
s.dbLoad()
|
s.dbLoad()
|
||||||
}
|
}
|
||||||
s.leasesLock.Unlock()
|
|
||||||
|
|
||||||
var result []Lease
|
var result []Lease
|
||||||
s.leasesLock.RLock()
|
|
||||||
for _, lease := range s.leases {
|
for _, lease := range s.leases {
|
||||||
if lease.Expiry.Unix() == 1 {
|
if lease.Expiry.Unix() == 1 {
|
||||||
result = append(result, *lease)
|
result = append(result, *lease)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.leasesLock.RUnlock()
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -650,6 +650,6 @@ func (s *Server) FindIPbyMAC(mac net.HardwareAddr) net.IP {
|
||||||
func (s *Server) reset() {
|
func (s *Server) reset() {
|
||||||
s.leasesLock.Lock()
|
s.leasesLock.Lock()
|
||||||
s.leases = nil
|
s.leases = nil
|
||||||
s.leasesLock.Unlock()
|
|
||||||
s.IPpool = make(map[[4]byte]net.HardwareAddr)
|
s.IPpool = make(map[[4]byte]net.HardwareAddr)
|
||||||
|
s.leasesLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue