2019-03-05 17:15:38 +03:00
|
|
|
// On-disk database for lease table
|
|
|
|
|
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-06-16 16:48:46 +03:00
|
|
|
"fmt"
|
2019-03-05 17:15:38 +03:00
|
|
|
"os"
|
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-03-05 17:15:38 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-04-06 20:48:36 +03:00
|
|
|
"github.com/google/renameio/maybe"
|
2023-04-18 16:07:11 +03:00
|
|
|
"golang.org/x/exp/slices"
|
2019-03-05 17:15:38 +03:00
|
|
|
)
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
const (
|
|
|
|
// dataFilename contains saved leases.
|
|
|
|
dataFilename = "leases.json"
|
2019-03-05 17:15:38 +03:00
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
// dataVersion is the current version of the stored DHCP leases structure.
|
|
|
|
dataVersion = 1
|
|
|
|
)
|
2019-03-05 17:15:38 +03:00
|
|
|
|
2023-04-18 16:07:11 +03: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.
|
|
|
|
Leases []*Lease `json:"leases"`
|
2020-01-30 10:25:02 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
// dbLoad loads stored leases.
|
2022-09-14 16:36:29 +03:00
|
|
|
func (s *server) dbLoad() (err error) {
|
2023-04-18 16:07:11 +03:00
|
|
|
data, err := os.ReadFile(s.conf.dbFilePath)
|
2019-03-05 17:15:38 +03:00
|
|
|
if err != nil {
|
2021-03-15 14:19:04 +03:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2021-06-16 16:48:46 +03:00
|
|
|
return fmt.Errorf("reading db: %w", err)
|
2019-03-05 17:15:38 +03:00
|
|
|
}
|
2021-03-15 14:19:04 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
return nil
|
2019-03-05 17:15:38 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
dl := &dataLeases{}
|
|
|
|
err = json.Unmarshal(data, dl)
|
2019-03-05 17:15:38 +03:00
|
|
|
if err != nil {
|
2021-06-16 16:48:46 +03:00
|
|
|
return fmt.Errorf("decoding db: %w", err)
|
2019-03-05 17:15:38 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
leases := dl.Leases
|
2019-03-05 17:15:38 +03:00
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
leases4 := []*Lease{}
|
|
|
|
leases6 := []*Lease{}
|
2019-03-05 17:15:38 +03:00
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
for _, l := range leases {
|
|
|
|
if l.IP.Is4() {
|
|
|
|
leases4 = append(leases4, l)
|
2019-12-03 19:24:30 +03:00
|
|
|
} else {
|
2023-04-18 16:07:11 +03:00
|
|
|
leases6 = append(leases6, l)
|
2019-12-03 19:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 17:15:38 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.srv4.ResetLeases(leases4)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("resetting dhcpv4 leases: %w", err)
|
|
|
|
}
|
2019-12-03 19:24:30 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
if s.srv6 != nil {
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.srv6.ResetLeases(leases6)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("resetting dhcpv6 leases: %w", err)
|
|
|
|
}
|
2019-03-05 17:15:38 +03:00
|
|
|
}
|
2019-12-03 19:24:30 +03:00
|
|
|
|
2021-04-06 20:48:36 +03:00
|
|
|
log.Info("dhcp: loaded leases v4:%d v6:%d total-read:%d from DB",
|
2023-04-18 16:07:11 +03:00
|
|
|
len(leases4), len(leases6), len(leases))
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
return nil
|
2019-12-03 19:24:30 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:07:11 +03: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.
|
2019-12-03 19:24:30 +03:00
|
|
|
leases := []*Lease{}
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
leases4 := s.srv4.getLeasesRef()
|
|
|
|
leases = append(leases, leases4...)
|
2019-12-03 19:24:30 +03:00
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
if s.srv6 != nil {
|
|
|
|
leases6 := s.srv6.getLeasesRef()
|
|
|
|
leases = append(leases, leases6...)
|
2019-12-03 19:24:30 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
return writeDB(s.conf.dbFilePath, leases)
|
2019-03-05 17:15:38 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
// writeDB writes leases to file at path.
|
|
|
|
func writeDB(path string, leases []*Lease) (err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "writing db: %w") }()
|
2021-06-16 16:48:46 +03:00
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
slices.SortFunc(leases, func(a, b *Lease) bool {
|
|
|
|
return a.Hostname < b.Hostname
|
|
|
|
})
|
2019-03-05 17:15:38 +03:00
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
dl := &dataLeases{
|
|
|
|
Version: dataVersion,
|
|
|
|
Leases: leases,
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
buf, err := json.Marshal(dl)
|
2019-03-05 17:15:38 +03:00
|
|
|
if err != nil {
|
2023-04-18 16:07:11 +03:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
2019-03-05 17:15:38 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
err = maybe.WriteFile(path, buf, 0o644)
|
2019-03-05 17:15:38 +03:00
|
|
|
if err != nil {
|
2023-04-18 16:07:11 +03:00
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
2019-03-05 17:15:38 +03:00
|
|
|
}
|
2021-04-06 20:48:36 +03:00
|
|
|
|
2023-04-18 16:07:11 +03:00
|
|
|
log.Info("dhcp: stored %d leases in %q", len(leases), path)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
|
|
|
return nil
|
2019-03-05 17:15:38 +03:00
|
|
|
}
|