From 8e08cddf647226d5e8e5f41307b60274a3f232e5 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Mon, 19 Aug 2019 14:27:02 +0300 Subject: [PATCH] + dhcp: use --workdir value for "leases.db" file path --- dhcpd/db.go | 8 ++++---- dhcpd/dhcpd.go | 4 ++++ dhcpd/dhcpd_test.go | 2 ++ home/dhcp.go | 1 + 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dhcpd/db.go b/dhcpd/db.go index c27bb679..5ab0f452 100644 --- a/dhcpd/db.go +++ b/dhcpd/db.go @@ -37,10 +37,10 @@ func (s *Server) dbLoad() { s.leases = nil s.IPpool = make(map[[4]byte]net.HardwareAddr) - data, err := ioutil.ReadFile(dbFilename) + data, err := ioutil.ReadFile(s.conf.DBFilePath) if err != nil { if !os.IsNotExist(err) { - log.Error("DHCP: can't read file %s: %v", dbFilename, err) + log.Error("DHCP: can't read file %s: %v", s.conf.DBFilePath, err) } return } @@ -99,10 +99,10 @@ func (s *Server) dbStore() { return } - err = file.SafeWrite(dbFilename, data) + err = file.SafeWrite(s.conf.DBFilePath, data) if err != nil { log.Error("DHCP: can't store lease table on disk: %v filename: %s", - err, dbFilename) + err, s.conf.DBFilePath) return } log.Info("DHCP: stored %d leases in DB", len(leases)) diff --git a/dhcpd/dhcpd.go b/dhcpd/dhcpd.go index 391389d4..3d3d0b33 100644 --- a/dhcpd/dhcpd.go +++ b/dhcpd/dhcpd.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net" + "path/filepath" "strings" "sync" "time" @@ -38,6 +39,8 @@ type ServerConfig struct { RangeStart string `json:"range_start" yaml:"range_start"` RangeEnd string `json:"range_end" yaml:"range_end"` LeaseDuration uint `json:"lease_duration" yaml:"lease_duration"` // in seconds + WorkDir string `json:"-" yaml:"-"` + DBFilePath string `json:"-" yaml:"-"` // path to DB file // IP conflict detector: time (ms) to wait for ICMP reply. // 0: disable @@ -97,6 +100,7 @@ func (s *Server) Init(config ServerConfig) error { func (s *Server) setConfig(config ServerConfig) error { s.conf = config + s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename) iface, err := net.InterfaceByName(config.InterfaceName) if err != nil { diff --git a/dhcpd/dhcpd_test.go b/dhcpd/dhcpd_test.go index 01fafb84..5e3494ea 100644 --- a/dhcpd/dhcpd_test.go +++ b/dhcpd/dhcpd_test.go @@ -21,6 +21,7 @@ func check(t *testing.T, result bool, msg string) { // . Handle Request message (lease commit) func TestDHCP(t *testing.T) { var s = Server{} + s.conf.DBFilePath = dbFilename var p, p2 dhcp4.Packet var hw net.HardwareAddr var lease *Lease @@ -156,6 +157,7 @@ func misc(t *testing.T, s *Server) { // Leases database store/load func TestDB(t *testing.T) { var s = Server{} + s.conf.DBFilePath = dbFilename var p dhcp4.Packet var hw1, hw2 net.HardwareAddr var lease *Lease diff --git a/home/dhcp.go b/home/dhcp.go index 078647f0..6d828d01 100644 --- a/home/dhcp.go +++ b/home/dhcp.go @@ -75,6 +75,7 @@ func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { return } + newconfig.ServerConfig.WorkDir = config.ourWorkingDir err = config.dhcpServer.CheckConfig(newconfig.ServerConfig) if err != nil { httpError(w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)