* dhcpd: minor improvements

This commit is contained in:
Simon Zolin 2019-05-14 14:50:47 +03:00
parent fd593f5282
commit 342699d933
2 changed files with 13 additions and 9 deletions

View File

@ -23,8 +23,20 @@ type leaseJSON struct {
Expiry int64 `json:"exp"` Expiry int64 `json:"exp"`
} }
// Safe version of dhcp4.IPInRange()
func ipInRange(start, stop, ip net.IP) bool {
if len(start) != len(stop) ||
len(start) != len(ip) {
return false
}
return dhcp4.IPInRange(start, stop, ip)
}
// Load lease table from DB // Load lease table from DB
func (s *Server) dbLoad() { func (s *Server) dbLoad() {
s.leases = nil
s.IPpool = make(map[[4]byte]net.HardwareAddr)
data, err := ioutil.ReadFile(dbFilename) data, err := ioutil.ReadFile(dbFilename)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
@ -40,13 +52,10 @@ func (s *Server) dbLoad() {
return return
} }
s.leases = nil
s.IPpool = make(map[[4]byte]net.HardwareAddr)
numLeases := len(obj) numLeases := len(obj)
for i := range obj { for i := range obj {
if !dhcp4.IPInRange(s.leaseStart, s.leaseStop, obj[i].IP) { if !ipInRange(s.leaseStart, s.leaseStop, obj[i].IP) {
log.Tracef("Skipping a lease with IP %s: not within current IP range", obj[i].IP) log.Tracef("Skipping a lease with IP %s: not within current IP range", obj[i].IP)
continue continue
} }

View File

@ -286,11 +286,6 @@ func (s *Server) findExpiredLease() int {
} }
func (s *Server) findFreeIP(hwaddr net.HardwareAddr) (net.IP, error) { func (s *Server) findFreeIP(hwaddr net.HardwareAddr) (net.IP, error) {
// if IP pool is nil, lazy initialize it
if s.IPpool == nil {
s.IPpool = make(map[[4]byte]net.HardwareAddr)
}
// go from start to end, find unreserved IP // go from start to end, find unreserved IP
var foundIP net.IP var foundIP net.IP
for i := 0; i < dhcp4.IPRange(s.leaseStart, s.leaseStop); i++ { for i := 0; i < dhcp4.IPRange(s.leaseStart, s.leaseStop); i++ {