AdGuardHome/internal/dhcpd/db.go
Stanislav Chzhen bea39934bd Pull request 1771: AG-20352-dhcpd-lease-netip-addr
Merge in DNS/adguard-home from AG-20352-dhcpd-lease-netip-addr to master

Squashed commit of the following:

commit 4acd094e2d6ed972bac99cdb671670f6d8e61721
Merge: 51f61c19 df61741f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Mar 23 16:44:17 2023 +0300

    Merge branch 'master' into AG-20352-dhcpd-lease-netip-addr

commit 51f61c193fdd31ee675be5598fc361228e407eb3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Mar 22 18:25:30 2023 +0300

    dhcpd: fix typo

commit 2e64ad55475957925d2a3010c649e0adc5f18c4f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Mar 22 17:20:31 2023 +0300

    dhcpd: add todo

commit 668d4f62fd2c5b2e168025bf0b6bb36d7b617c80
Merge: 0020006e 306c1983
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Mar 22 15:09:38 2023 +0300

    Merge branch 'master' into AG-20352-dhcpd-lease-netip-addr

commit 0020006e89f336dc58db1a2ca3ce90d2e7a5ca16
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Mar 22 15:08:58 2023 +0300

    all: imp code

commit 9a77f79869cdbde6de760734c0d8cf504e0464ef
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Mar 20 18:13:35 2023 +0300

    dhcpd: add todo

commit 638c4ce2af72235bf065a6492d58f3f1b1e7644a
Merge: c82b18f1 48431f8b
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Mar 20 13:57:19 2023 +0300

    Merge branch 'master' into AG-20352-dhcpd-lease-netip-addr

commit c82b18f1408d9e353aec517f4283e2f3eb260890
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Mar 20 13:56:31 2023 +0300

    all: imp code

commit 27e518120024103c292ac1cf134c6801fffc967e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Fri Mar 17 12:14:02 2023 +0300

    dhcpd: imp tests

commit 8e919b0ceb0b20d1935587e717c7965cd8a33ad9
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Fri Mar 17 11:02:50 2023 +0300

    dhcpd: add tests

commit 78ddefa73a255509af1c788147d6b1c332bb66ba
Merge: c68e85c4 9f7a582d
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Mar 16 14:15:24 2023 +0300

    Merge branch 'master' into AG-20352-dhcpd-lease-netip-addr

commit c68e85c40947b6c83516424a2bd7af89b99447b4
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Thu Mar 16 14:14:43 2023 +0300

    all: add tests

commit f338086309a68c4b71036fa14f757e39358702d4
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Wed Mar 15 12:44:06 2023 +0300

    all: dhcpd lease netip addr
2023-03-23 16:52:01 +03:00

192 lines
3.9 KiB
Go

// On-disk database for lease table
package dhcpd
import (
"encoding/json"
"fmt"
"net"
"net/netip"
"os"
"time"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/google/renameio/maybe"
)
const dbFilename = "leases.db"
type leaseJSON struct {
HWAddr []byte `json:"mac"`
IP []byte `json:"ip"`
Hostname string `json:"host"`
Expiry int64 `json:"exp"`
}
func normalizeIP(ip net.IP) net.IP {
ip4 := ip.To4()
if ip4 != nil {
return ip4
}
return ip
}
// Load lease table from DB
//
// TODO(s.chzhen): Decrease complexity.
func (s *server) dbLoad() (err error) {
dynLeases := []*Lease{}
staticLeases := []*Lease{}
v6StaticLeases := []*Lease{}
v6DynLeases := []*Lease{}
data, err := os.ReadFile(s.conf.DBFilePath)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("reading db: %w", err)
}
return nil
}
obj := []leaseJSON{}
err = json.Unmarshal(data, &obj)
if err != nil {
return fmt.Errorf("decoding db: %w", err)
}
numLeases := len(obj)
for i := range obj {
obj[i].IP = normalizeIP(obj[i].IP)
ip, ok := netip.AddrFromSlice(obj[i].IP)
if !ok {
log.Info("dhcp: invalid IP: %s", obj[i].IP)
continue
}
lease := Lease{
HWAddr: obj[i].HWAddr,
IP: ip,
Hostname: obj[i].Hostname,
Expiry: time.Unix(obj[i].Expiry, 0),
}
if len(obj[i].IP) == 16 {
if obj[i].Expiry == leaseExpireStatic {
v6StaticLeases = append(v6StaticLeases, &lease)
} else {
v6DynLeases = append(v6DynLeases, &lease)
}
} else {
if obj[i].Expiry == leaseExpireStatic {
staticLeases = append(staticLeases, &lease)
} else {
dynLeases = append(dynLeases, &lease)
}
}
}
leases4 := normalizeLeases(staticLeases, dynLeases)
err = s.srv4.ResetLeases(leases4)
if err != nil {
return fmt.Errorf("resetting dhcpv4 leases: %w", err)
}
leases6 := normalizeLeases(v6StaticLeases, v6DynLeases)
if s.srv6 != nil {
err = s.srv6.ResetLeases(leases6)
if err != nil {
return fmt.Errorf("resetting dhcpv6 leases: %w", err)
}
}
log.Info("dhcp: loaded leases v4:%d v6:%d total-read:%d from DB",
len(leases4), len(leases6), numLeases)
return nil
}
// Skip duplicate leases
// Static leases have a priority over dynamic leases
func normalizeLeases(staticLeases, dynLeases []*Lease) []*Lease {
leases := []*Lease{}
index := map[string]int{}
for i, lease := range staticLeases {
_, ok := index[lease.HWAddr.String()]
if ok {
continue // skip the lease with the same HW address
}
index[lease.HWAddr.String()] = i
leases = append(leases, lease)
}
for i, lease := range dynLeases {
_, ok := index[lease.HWAddr.String()]
if ok {
continue // skip the lease with the same HW address
}
index[lease.HWAddr.String()] = i
leases = append(leases, lease)
}
return leases
}
// Store lease table in DB
func (s *server) dbStore() (err error) {
// Use an empty slice here as opposed to nil so that it doesn't write
// "null" into the database file if leases are empty.
leases := []leaseJSON{}
leases4 := s.srv4.getLeasesRef()
for _, l := range leases4 {
if l.Expiry.Unix() == 0 {
continue
}
lease := leaseJSON{
HWAddr: l.HWAddr,
IP: l.IP.AsSlice(),
Hostname: l.Hostname,
Expiry: l.Expiry.Unix(),
}
leases = append(leases, lease)
}
if s.srv6 != nil {
leases6 := s.srv6.getLeasesRef()
for _, l := range leases6 {
if l.Expiry.Unix() == 0 {
continue
}
lease := leaseJSON{
HWAddr: l.HWAddr,
IP: l.IP.AsSlice(),
Hostname: l.Hostname,
Expiry: l.Expiry.Unix(),
}
leases = append(leases, lease)
}
}
var data []byte
data, err = json.Marshal(leases)
if err != nil {
return fmt.Errorf("encoding db: %w", err)
}
err = maybe.WriteFile(s.conf.DBFilePath, data, 0o644)
if err != nil {
return fmt.Errorf("writing db: %w", err)
}
log.Info("dhcp: stored %d leases in db", len(leases))
return nil
}