Skip to content

Commit

Permalink
dhcpd: fix static leases
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Mar 17, 2021
1 parent 3701c3f commit f036b50
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
17 changes: 15 additions & 2 deletions internal/dhcpd/iprange.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
)

// ipRange is an inclusive range of IP addresses.
// ipRange is an inclusive range of IP addresses. A nil range is a range that
// doesn't contain any IP addresses.
//
// It is safe for concurrent use.
//
Expand Down Expand Up @@ -53,12 +54,16 @@ func newIPRange(start, end net.IP) (r *ipRange, err error) {

// contains returns true if r contains ip.
func (r *ipRange) contains(ip net.IP) (ok bool) {
if r == nil {
return false
}

ipInt := (&big.Int{}).SetBytes(ip.To16())

return r.containsInt(ipInt)
}

// containsInt returns true if r contains ipInt.
// containsInt returns true if r contains ipInt. For internal use only.
func (r *ipRange) containsInt(ipInt *big.Int) (ok bool) {
return ipInt.Cmp(r.start) >= 0 && ipInt.Cmp(r.end) <= 0
}
Expand All @@ -70,6 +75,10 @@ type ipPredicate func(ip net.IP) (ok bool)
// find finds the first IP address in r for which p returns true. ip is in the
// 16-byte form.
func (r *ipRange) find(p ipPredicate) (ip net.IP) {
if r == nil {
return nil
}

ip = make(net.IP, net.IPv6len)
_1 := big.NewInt(1)
for i := (&big.Int{}).Set(r.start); i.Cmp(r.end) <= 0; i.Add(i, _1) {
Expand All @@ -85,6 +94,10 @@ func (r *ipRange) find(p ipPredicate) (ip net.IP) {
// offset returns the offset of ip from the beginning of r. It returns 0 and
// false if ip is not in r.
func (r *ipRange) offset(ip net.IP) (offset uint, ok bool) {
if r == nil {
return 0, false
}

ip = ip.To16()
ipInt := (&big.Int{}).SetBytes(ip)
if !r.containsInt(ipInt) {
Expand Down
29 changes: 22 additions & 7 deletions internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,31 @@ func (s *v4Server) FindMACbyIP(ip net.IP) net.HardwareAddr {
}

// Add the specified IP to the black list for a time period
func (s *v4Server) blacklistLease(lease *Lease) {
func (s *v4Server) blacklistLease(l *Lease) {
hw := make(net.HardwareAddr, 6)
lease.HWAddr = hw
lease.Hostname = ""
lease.Expiry = time.Now().Add(s.conf.leaseTime)
l.HWAddr = hw
l.Hostname = ""
l.Expiry = time.Now().Add(s.conf.leaseTime)
}

// rmLeaseByIndex removes a lease by its index in the leases slice.
func (s *v4Server) rmLeaseByIndex(i int) {
n := len(s.leases)
if i >= n {
// TODO(a.garipov): Better error handling.
log.Debug("dhcpv4: can't remove lease at index %d: no such lease", i)

return
}

l := s.leases[i]
s.leases = append(s.leases[:i], s.leases[i+1:]...)

n = len(s.leases)
if n > 0 {
s.leases = s.leases[:n-1]
}

r := s.conf.ipRange
offset, ok := r.offset(l.IP)
if ok {
Expand Down Expand Up @@ -175,7 +188,7 @@ func (s *v4Server) addLease(l *Lease) {
}

s.leases = append(s.leases, l)
s.leasedOffsets.Set(uint(offset))
s.leasedOffsets.Set(offset)

log.Debug("dhcpv4: added lease %s (%s)", l.IP, l.HWAddr)
}
Expand Down Expand Up @@ -303,7 +316,7 @@ func (s *v4Server) nextIP() (ip net.IP) {
return false
}

return !s.leasedOffsets.Test(uint(offset))
return !s.leasedOffsets.Test(offset)
})

return ip.To4()
Expand All @@ -325,7 +338,7 @@ func (s *v4Server) findExpiredLease() int {
// nil if it couldn't allocate a new lease.
func (s *v4Server) reserveLease(mac net.HardwareAddr) (l *Lease) {
l = &Lease{
HWAddr: make([]byte, 6),
HWAddr: make([]byte, len(mac)),
}

copy(l.HWAddr, mac)
Expand Down Expand Up @@ -646,6 +659,8 @@ func v4Create(conf V4ServerConf) (srv DHCPServer, err error) {
s := &v4Server{}
s.conf = conf

// TODO(a.garipov): Don't use a disabled server in other places or just
// use an interface.
if !conf.Enabled {
return s, nil
}
Expand Down

0 comments on commit f036b50

Please sign in to comment.