dhcp: servers use the module's settings "enabled", "interface_name"

This commit is contained in:
Simon Zolin
2020-05-29 13:26:04 +03:00
parent d0bb127a11
commit aaa2b0f830
6 changed files with 41 additions and 38 deletions

View File

@@ -395,9 +395,9 @@ Response:
200 OK 200 OK
{ {
"v4":{
"enabled":false, "enabled":false,
"interface_name":"...", "interface_name":"...",
"v4":{
"gateway_ip":"...", "gateway_ip":"...",
"subnet_mask":"...", "subnet_mask":"...",
"range_start":"...", "range_start":"...",
@@ -406,7 +406,6 @@ Response:
"icmp_timeout_msec":0 "icmp_timeout_msec":0
}, },
"v6":{ "v6":{
"enabled":false,
"range_start":"...", "range_start":"...",
"lease_duration":60, "lease_duration":60,
} }

View File

@@ -41,8 +41,6 @@ func convertLeases(inputLeases []Lease, includeExpires bool) []map[string]string
} }
type v4ServerConfJSON struct { type v4ServerConfJSON struct {
Enabled bool `json:"enabled"`
InterfaceName string `json:"interface_name"`
GatewayIP string `json:"gateway_ip"` GatewayIP string `json:"gateway_ip"`
SubnetMask string `json:"subnet_mask"` SubnetMask string `json:"subnet_mask"`
RangeStart string `json:"range_start"` RangeStart string `json:"range_start"`
@@ -53,8 +51,6 @@ type v4ServerConfJSON struct {
func v4ServerConfToJSON(c V4ServerConf) v4ServerConfJSON { func v4ServerConfToJSON(c V4ServerConf) v4ServerConfJSON {
return v4ServerConfJSON{ return v4ServerConfJSON{
Enabled: c.Enabled,
InterfaceName: c.InterfaceName,
GatewayIP: c.GatewayIP, GatewayIP: c.GatewayIP,
SubnetMask: c.SubnetMask, SubnetMask: c.SubnetMask,
RangeStart: c.RangeStart, RangeStart: c.RangeStart,
@@ -66,8 +62,6 @@ func v4ServerConfToJSON(c V4ServerConf) v4ServerConfJSON {
func v4JSONToServerConf(j v4ServerConfJSON) V4ServerConf { func v4JSONToServerConf(j v4ServerConfJSON) V4ServerConf {
return V4ServerConf{ return V4ServerConf{
Enabled: j.Enabled,
InterfaceName: j.InterfaceName,
GatewayIP: j.GatewayIP, GatewayIP: j.GatewayIP,
SubnetMask: j.SubnetMask, SubnetMask: j.SubnetMask,
RangeStart: j.RangeStart, RangeStart: j.RangeStart,
@@ -78,16 +72,12 @@ func v4JSONToServerConf(j v4ServerConfJSON) V4ServerConf {
} }
type v6ServerConfJSON struct { type v6ServerConfJSON struct {
Enabled bool `json:"enabled"`
InterfaceName string `json:"interface_name"`
RangeStart string `json:"range_start"` RangeStart string `json:"range_start"`
LeaseDuration uint32 `json:"lease_duration"` LeaseDuration uint32 `json:"lease_duration"`
} }
func v6ServerConfToJSON(c V6ServerConf) v6ServerConfJSON { func v6ServerConfToJSON(c V6ServerConf) v6ServerConfJSON {
return v6ServerConfJSON{ return v6ServerConfJSON{
Enabled: c.Enabled,
InterfaceName: c.InterfaceName,
RangeStart: c.RangeStart, RangeStart: c.RangeStart,
LeaseDuration: c.LeaseDuration, LeaseDuration: c.LeaseDuration,
} }
@@ -95,8 +85,6 @@ func v6ServerConfToJSON(c V6ServerConf) v6ServerConfJSON {
func v6JSONToServerConf(j v6ServerConfJSON) V6ServerConf { func v6JSONToServerConf(j v6ServerConfJSON) V6ServerConf {
return V6ServerConf{ return V6ServerConf{
Enabled: j.Enabled,
InterfaceName: j.InterfaceName,
RangeStart: j.RangeStart, RangeStart: j.RangeStart,
LeaseDuration: j.LeaseDuration, LeaseDuration: j.LeaseDuration,
} }
@@ -134,6 +122,8 @@ type staticLeaseJSON struct {
} }
type dhcpServerConfigJSON struct { type dhcpServerConfigJSON struct {
Enabled bool `json:"enabled"`
InterfaceName string `json:"interface_name"`
V4 v4ServerConfJSON `json:"v4"` V4 v4ServerConfJSON `json:"v4"`
V6 v6ServerConfJSON `json:"v6"` V6 v6ServerConfJSON `json:"v6"`
} }
@@ -147,6 +137,8 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
} }
v4conf := v4JSONToServerConf(newconfig.V4) v4conf := v4JSONToServerConf(newconfig.V4)
v4conf.Enabled = newconfig.Enabled
v4conf.InterfaceName = newconfig.InterfaceName
v4conf.notify = s.onNotify v4conf.notify = s.onNotify
s4, err := v4Create(v4conf) s4, err := v4Create(v4conf)
if err != nil { if err != nil {
@@ -155,6 +147,8 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
} }
v6conf := v6JSONToServerConf(newconfig.V6) v6conf := v6JSONToServerConf(newconfig.V6)
v6conf.Enabled = newconfig.Enabled
v6conf.InterfaceName = newconfig.InterfaceName
v6conf.notify = s.onNotify v6conf.notify = s.onNotify
s6, err := v6Create(v6conf) s6, err := v6Create(v6conf)
if s6 == nil { if s6 == nil {
@@ -167,21 +161,22 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
log.Error("failed to stop the DHCP server: %s", err) log.Error("failed to stop the DHCP server: %s", err)
} }
s.conf.Enabled = newconfig.Enabled
s.conf.InterfaceName = newconfig.InterfaceName
s.srv4 = s4 s.srv4 = s4
s.srv6 = s6 s.srv6 = s6
s.conf.ConfigModified() s.conf.ConfigModified()
if newconfig.V4.Enabled { if newconfig.Enabled {
staticIP, err := HasStaticIP(newconfig.V4.InterfaceName) staticIP, err := HasStaticIP(newconfig.InterfaceName)
if !staticIP && err == nil { if !staticIP && err == nil {
err = SetStaticIP(newconfig.V4.InterfaceName) err = SetStaticIP(newconfig.InterfaceName)
if err != nil { if err != nil {
httpError(r, w, http.StatusInternalServerError, "Failed to configure static IP: %s", err) httpError(r, w, http.StatusInternalServerError, "Failed to configure static IP: %s", err)
return return
} }
} }
}
err = s.Start() err = s.Start()
if err != nil { if err != nil {
@@ -189,6 +184,7 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
return return
} }
} }
}
type netInterfaceJSON struct { type netInterfaceJSON struct {
Name string `json:"name"` Name string `json:"name"`

View File

@@ -28,6 +28,9 @@ type Lease struct {
// ServerConfig - DHCP server configuration // ServerConfig - DHCP server configuration
// field ordering is important -- yaml fields will mirror ordering from here // field ordering is important -- yaml fields will mirror ordering from here
type ServerConfig struct { type ServerConfig struct {
Enabled bool `yaml:"enabled"`
InterfaceName string `yaml:"interface_name"`
Conf4 V4ServerConf `yaml:"dhcpv4"` Conf4 V4ServerConf `yaml:"dhcpv4"`
Conf6 V6ServerConf `yaml:"dhcpv6"` Conf6 V6ServerConf `yaml:"dhcpv6"`
@@ -72,6 +75,8 @@ func (s *Server) CheckConfig(config ServerConfig) error {
// Create - create object // Create - create object
func Create(config ServerConfig) *Server { func Create(config ServerConfig) *Server {
s := Server{} s := Server{}
s.conf.Enabled = config.Enabled
s.conf.InterfaceName = config.InterfaceName
s.conf.HTTPRegister = config.HTTPRegister s.conf.HTTPRegister = config.HTTPRegister
s.conf.ConfigModified = config.ConfigModified s.conf.ConfigModified = config.ConfigModified
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename) s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
@@ -82,6 +87,8 @@ func Create(config ServerConfig) *Server {
} }
var err error var err error
config.Conf4.Enabled = s.conf.Enabled
config.Conf4.InterfaceName = s.conf.InterfaceName
config.Conf4.notify = s.onNotify config.Conf4.notify = s.onNotify
s.srv4, err = v4Create(config.Conf4) s.srv4, err = v4Create(config.Conf4)
if err != nil { if err != nil {
@@ -89,6 +96,8 @@ func Create(config ServerConfig) *Server {
return nil return nil
} }
config.Conf6.Enabled = s.conf.Enabled
config.Conf6.InterfaceName = s.conf.InterfaceName
config.Conf6.notify = s.onNotify config.Conf6.notify = s.onNotify
s.srv6, err = v6Create(config.Conf6) s.srv6, err = v6Create(config.Conf6)
if err != nil { if err != nil {
@@ -126,6 +135,8 @@ func (s *Server) notify(flags int) {
// WriteDiskConfig - write configuration // WriteDiskConfig - write configuration
func (s *Server) WriteDiskConfig(c *ServerConfig) { func (s *Server) WriteDiskConfig(c *ServerConfig) {
c.Enabled = s.conf.Enabled
c.InterfaceName = s.conf.InterfaceName
s.srv4.WriteDiskConfig4(&c.Conf4) s.srv4.WriteDiskConfig4(&c.Conf4)
s.srv6.WriteDiskConfig6(&c.Conf6) s.srv6.WriteDiskConfig6(&c.Conf6)
} }

View File

@@ -33,8 +33,8 @@ type DHCPServer interface {
// V4ServerConf - server configuration // V4ServerConf - server configuration
type V4ServerConf struct { type V4ServerConf struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"-"`
InterfaceName string `yaml:"interface_name"` InterfaceName string `yaml:"-"`
GatewayIP string `yaml:"gateway_ip"` GatewayIP string `yaml:"gateway_ip"`
SubnetMask string `yaml:"subnet_mask"` SubnetMask string `yaml:"subnet_mask"`
RangeStart string `yaml:"range_start"` RangeStart string `yaml:"range_start"`
@@ -58,8 +58,8 @@ type V4ServerConf struct {
// V6ServerConf - server configuration // V6ServerConf - server configuration
type V6ServerConf struct { type V6ServerConf struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"-"`
InterfaceName string `yaml:"interface_name"` InterfaceName string `yaml:"-"`
RangeStart string `yaml:"range_start"` RangeStart string `yaml:"range_start"`
LeaseDuration uint32 `yaml:"lease_duration"` // in seconds LeaseDuration uint32 `yaml:"lease_duration"` // in seconds

View File

@@ -525,7 +525,8 @@ func (s *v4Server) Start() error {
log.Debug("DHCPv4: starting...") log.Debug("DHCPv4: starting...")
s.conf.dnsIPAddrs = getIfaceIPv4(*iface) s.conf.dnsIPAddrs = getIfaceIPv4(*iface)
if len(s.conf.dnsIPAddrs) == 0 { if len(s.conf.dnsIPAddrs) == 0 {
return fmt.Errorf("DHCPv4: no IPv4 address for interface %s", iface.Name) log.Debug("DHCPv4: no IPv6 address for interface %s", iface.Name)
return nil
} }
laddr := &net.UDPAddr{ laddr := &net.UDPAddr{
@@ -541,9 +542,8 @@ func (s *v4Server) Start() error {
go func() { go func() {
err = s.srv.Serve() err = s.srv.Serve()
log.Error("DHCPv4: %s", err) log.Debug("DHCPv4: srv.Serve: %s", err)
}() }()
return nil return nil
} }

View File

@@ -556,14 +556,11 @@ func (s *v6Server) Start() error {
return wrapErrPrint(err, "Couldn't find interface by name %s", s.conf.InterfaceName) return wrapErrPrint(err, "Couldn't find interface by name %s", s.conf.InterfaceName)
} }
if !s.conf.Enabled {
return nil
}
log.Debug("DHCPv6: starting...") log.Debug("DHCPv6: starting...")
s.conf.dnsIPAddrs = getIfaceIPv6(*iface) s.conf.dnsIPAddrs = getIfaceIPv6(*iface)
if len(s.conf.dnsIPAddrs) == 0 { if len(s.conf.dnsIPAddrs) == 0 {
return fmt.Errorf("DHCPv6: no IPv6 address for interface %s", iface.Name) log.Debug("DHCPv6: no IPv6 address for interface %s", iface.Name)
return nil
} }
if len(iface.HardwareAddr) != 6 { if len(iface.HardwareAddr) != 6 {
@@ -586,7 +583,7 @@ func (s *v6Server) Start() error {
go func() { go func() {
err = s.srv.Serve() err = s.srv.Serve()
log.Error("DHCPv6: %s", err) log.Debug("DHCPv6: srv.Serve: %s", err)
}() }()
return nil return nil
} }