Pull request: dhcpd: fix static leases

Updates #2541.
Updates #2834.

Squashed commit of the following:

commit d1580182db3b5866213e017405aa2cf8a6ee2f24
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 17 14:51:43 2021 +0300

    all: doc changes; imp naming

commit f036b50a60ba030aa6866944fc7a7b8776ce01d4
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 17 14:09:19 2021 +0300

    dhcpd: fix static leases
This commit is contained in:
Ainar Garipov
2021-03-17 15:02:17 +03:00
parent 3701c3f4bb
commit 5243399d9d
3 changed files with 47 additions and 15 deletions

View File

@@ -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.
//
@@ -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
}
@@ -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) {
@@ -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) {