2019-03-05 14:15:38 +00:00
|
|
|
// On-disk database for lease table
|
|
|
|
|
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-06-16 14:48:46 +01:00
|
|
|
"fmt"
|
2023-11-16 11:14:40 +00:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2019-03-05 14:15:38 +00:00
|
|
|
"os"
|
2024-02-08 17:39:18 +00:00
|
|
|
"slices"
|
2023-08-10 18:00:17 +01:00
|
|
|
"strings"
|
2023-11-16 11:14:40 +00:00
|
|
|
"time"
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2024-10-02 19:00:15 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2023-11-16 11:14:40 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
2021-05-24 15:28:11 +01:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-03-05 14:15:38 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2023-07-25 15:47:24 +01:00
|
|
|
"github.com/google/renameio/v2/maybe"
|
2019-03-05 14:15:38 +00:00
|
|
|
)
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
const (
|
|
|
|
// dataFilename contains saved leases.
|
|
|
|
dataFilename = "leases.json"
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
// dataVersion is the current version of the stored DHCP leases structure.
|
|
|
|
dataVersion = 1
|
|
|
|
)
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
// dataLeases is the structure of the stored DHCP leases.
|
|
|
|
type dataLeases struct {
|
|
|
|
// Version is the current version of the structure.
|
|
|
|
Version int `json:"version"`
|
|
|
|
|
|
|
|
// Leases is the list containing stored DHCP leases.
|
2023-11-16 11:14:40 +00:00
|
|
|
Leases []*dbLease `json:"leases"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// dbLease is the structure of stored lease.
|
|
|
|
type dbLease struct {
|
|
|
|
Expiry string `json:"expires"`
|
|
|
|
IP netip.Addr `json:"ip"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
HWAddr string `json:"mac"`
|
|
|
|
IsStatic bool `json:"static"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromLease converts *dhcpsvc.Lease to *dbLease.
|
|
|
|
func fromLease(l *dhcpsvc.Lease) (dl *dbLease) {
|
|
|
|
var expiryStr string
|
|
|
|
if !l.IsStatic {
|
|
|
|
// The front-end is waiting for RFC 3999 format of the time value. It
|
|
|
|
// also shouldn't got an Expiry field for static leases.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/2692.
|
|
|
|
expiryStr = l.Expiry.Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dbLease{
|
|
|
|
Expiry: expiryStr,
|
|
|
|
Hostname: l.Hostname,
|
|
|
|
HWAddr: l.HWAddr.String(),
|
|
|
|
IP: l.IP,
|
|
|
|
IsStatic: l.IsStatic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// toLease converts *dbLease to *dhcpsvc.Lease.
|
|
|
|
func (dl *dbLease) toLease() (l *dhcpsvc.Lease, err error) {
|
|
|
|
mac, err := net.ParseMAC(dl.HWAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing hardware address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expiry := time.Time{}
|
|
|
|
if !dl.IsStatic {
|
|
|
|
expiry, err = time.Parse(time.RFC3339, dl.Expiry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing expiry time: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dhcpsvc.Lease{
|
|
|
|
Expiry: expiry,
|
|
|
|
IP: dl.IP,
|
|
|
|
Hostname: dl.Hostname,
|
|
|
|
HWAddr: mac,
|
|
|
|
IsStatic: dl.IsStatic,
|
|
|
|
}, nil
|
2020-01-30 07:25:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
// dbLoad loads stored leases.
|
2022-09-13 21:45:35 +01:00
|
|
|
func (s *server) dbLoad() (err error) {
|
2023-04-18 13:12:11 +01:00
|
|
|
data, err := os.ReadFile(s.conf.dbFilePath)
|
2019-03-05 14:15:38 +00:00
|
|
|
if err != nil {
|
2021-03-15 11:19:04 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2021-06-16 14:48:46 +01:00
|
|
|
return fmt.Errorf("reading db: %w", err)
|
2019-03-05 14:15:38 +00:00
|
|
|
}
|
2021-03-15 11:19:04 +00:00
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
return nil
|
2019-03-05 14:15:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
dl := &dataLeases{}
|
|
|
|
err = json.Unmarshal(data, dl)
|
2019-03-05 14:15:38 +00:00
|
|
|
if err != nil {
|
2021-06-16 14:48:46 +01:00
|
|
|
return fmt.Errorf("decoding db: %w", err)
|
2019-03-05 14:15:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
leases := dl.Leases
|
2023-11-16 11:14:40 +00:00
|
|
|
leases4 := []*dhcpsvc.Lease{}
|
|
|
|
leases6 := []*dhcpsvc.Lease{}
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
for _, l := range leases {
|
2023-11-16 11:14:40 +00:00
|
|
|
var lease *dhcpsvc.Lease
|
|
|
|
lease, err = l.toLease()
|
|
|
|
if err != nil {
|
|
|
|
log.Info("dhcp: invalid lease: %s", err)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if lease.IP.Is4() {
|
|
|
|
leases4 = append(leases4, lease)
|
2019-12-03 16:24:30 +00:00
|
|
|
} else {
|
2023-11-16 11:14:40 +00:00
|
|
|
leases6 = append(leases6, lease)
|
2019-12-03 16:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.srv4.ResetLeases(leases4)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("resetting dhcpv4 leases: %w", err)
|
|
|
|
}
|
2019-12-03 16:24:30 +00:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
if s.srv6 != nil {
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.srv6.ResetLeases(leases6)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("resetting dhcpv6 leases: %w", err)
|
|
|
|
}
|
2019-03-05 14:15:38 +00:00
|
|
|
}
|
2019-12-03 16:24:30 +00:00
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
log.Info(
|
|
|
|
"dhcp: loaded leases v4:%d v6:%d total-read:%d from DB",
|
|
|
|
len(leases4),
|
|
|
|
len(leases6),
|
|
|
|
len(leases),
|
|
|
|
)
|
2021-06-16 14:48:46 +01:00
|
|
|
|
|
|
|
return nil
|
2019-12-03 16:24:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
// dbStore stores DHCP leases.
|
|
|
|
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.
|
2023-11-16 11:14:40 +00:00
|
|
|
leases := []*dbLease{}
|
2019-12-03 16:24:30 +00:00
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
for _, l := range s.srv4.getLeasesRef() {
|
|
|
|
leases = append(leases, fromLease(l))
|
|
|
|
}
|
2019-12-03 16:24:30 +00:00
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
if s.srv6 != nil {
|
2023-11-16 11:14:40 +00:00
|
|
|
for _, l := range s.srv6.getLeasesRef() {
|
|
|
|
leases = append(leases, fromLease(l))
|
|
|
|
}
|
2019-12-03 16:24:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
return writeDB(s.conf.dbFilePath, leases)
|
2019-03-05 14:15:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
// writeDB writes leases to file at path.
|
2023-11-16 11:14:40 +00:00
|
|
|
func writeDB(path string, leases []*dbLease) (err error) {
|
2023-04-18 13:12:11 +01:00
|
|
|
defer func() { err = errors.Annotate(err, "writing db: %w") }()
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
slices.SortFunc(leases, func(a, b *dbLease) (res int) {
|
2023-08-10 18:00:17 +01:00
|
|
|
return strings.Compare(a.Hostname, b.Hostname)
|
2023-04-18 13:12:11 +01:00
|
|
|
})
|
2019-03-05 14:15:38 +00:00
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
dl := &dataLeases{
|
|
|
|
Version: dataVersion,
|
|
|
|
Leases: leases,
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
buf, err := json.Marshal(dl)
|
2019-03-05 14:15:38 +00:00
|
|
|
if err != nil {
|
2023-04-18 13:12:11 +01:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
2019-03-05 14:15:38 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 19:00:15 +01:00
|
|
|
err = maybe.WriteFile(path, buf, aghos.DefaultPermFile)
|
2019-03-05 14:15:38 +00:00
|
|
|
if err != nil {
|
2023-04-18 13:12:11 +01:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
2019-03-05 14:15:38 +00:00
|
|
|
}
|
2021-04-06 18:48:36 +01:00
|
|
|
|
2023-04-18 13:12:11 +01:00
|
|
|
log.Info("dhcp: stored %d leases in %q", len(leases), path)
|
2021-06-16 14:48:46 +01:00
|
|
|
|
|
|
|
return nil
|
2019-03-05 14:15:38 +00:00
|
|
|
}
|