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)