From aaa2b0f830cbb33c842155cff88710ef58f0d137 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Fri, 29 May 2020 13:26:04 +0300 Subject: [PATCH] dhcp: servers use the module's settings "enabled", "interface_name" --- AGHTechDoc.md | 5 ++--- dhcpd/dhcp_http.go | 40 ++++++++++++++++++---------------------- dhcpd/dhcpd.go | 11 +++++++++++ dhcpd/server.go | 8 ++++---- dhcpd/v4.go | 6 +++--- dhcpd/v6.go | 9 +++------ 6 files changed, 41 insertions(+), 38 deletions(-) diff --git a/AGHTechDoc.md b/AGHTechDoc.md index 01e11959..fbfad586 100644 --- a/AGHTechDoc.md +++ b/AGHTechDoc.md @@ -395,9 +395,9 @@ Response: 200 OK { + "enabled":false, + "interface_name":"...", "v4":{ - "enabled":false, - "interface_name":"...", "gateway_ip":"...", "subnet_mask":"...", "range_start":"...", @@ -406,7 +406,6 @@ Response: "icmp_timeout_msec":0 }, "v6":{ - "enabled":false, "range_start":"...", "lease_duration":60, } diff --git a/dhcpd/dhcp_http.go b/dhcpd/dhcp_http.go index e50c14a7..a6cfca26 100644 --- a/dhcpd/dhcp_http.go +++ b/dhcpd/dhcp_http.go @@ -41,8 +41,6 @@ func convertLeases(inputLeases []Lease, includeExpires bool) []map[string]string } type v4ServerConfJSON struct { - Enabled bool `json:"enabled"` - InterfaceName string `json:"interface_name"` GatewayIP string `json:"gateway_ip"` SubnetMask string `json:"subnet_mask"` RangeStart string `json:"range_start"` @@ -53,8 +51,6 @@ type v4ServerConfJSON struct { func v4ServerConfToJSON(c V4ServerConf) v4ServerConfJSON { return v4ServerConfJSON{ - Enabled: c.Enabled, - InterfaceName: c.InterfaceName, GatewayIP: c.GatewayIP, SubnetMask: c.SubnetMask, RangeStart: c.RangeStart, @@ -66,8 +62,6 @@ func v4ServerConfToJSON(c V4ServerConf) v4ServerConfJSON { func v4JSONToServerConf(j v4ServerConfJSON) V4ServerConf { return V4ServerConf{ - Enabled: j.Enabled, - InterfaceName: j.InterfaceName, GatewayIP: j.GatewayIP, SubnetMask: j.SubnetMask, RangeStart: j.RangeStart, @@ -78,16 +72,12 @@ func v4JSONToServerConf(j v4ServerConfJSON) V4ServerConf { } type v6ServerConfJSON struct { - Enabled bool `json:"enabled"` - InterfaceName string `json:"interface_name"` RangeStart string `json:"range_start"` LeaseDuration uint32 `json:"lease_duration"` } func v6ServerConfToJSON(c V6ServerConf) v6ServerConfJSON { return v6ServerConfJSON{ - Enabled: c.Enabled, - InterfaceName: c.InterfaceName, RangeStart: c.RangeStart, LeaseDuration: c.LeaseDuration, } @@ -95,8 +85,6 @@ func v6ServerConfToJSON(c V6ServerConf) v6ServerConfJSON { func v6JSONToServerConf(j v6ServerConfJSON) V6ServerConf { return V6ServerConf{ - Enabled: j.Enabled, - InterfaceName: j.InterfaceName, RangeStart: j.RangeStart, LeaseDuration: j.LeaseDuration, } @@ -134,8 +122,10 @@ type staticLeaseJSON struct { } type dhcpServerConfigJSON struct { - V4 v4ServerConfJSON `json:"v4"` - V6 v6ServerConfJSON `json:"v6"` + Enabled bool `json:"enabled"` + InterfaceName string `json:"interface_name"` + V4 v4ServerConfJSON `json:"v4"` + V6 v6ServerConfJSON `json:"v6"` } func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { @@ -147,6 +137,8 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { } v4conf := v4JSONToServerConf(newconfig.V4) + v4conf.Enabled = newconfig.Enabled + v4conf.InterfaceName = newconfig.InterfaceName v4conf.notify = s.onNotify s4, err := v4Create(v4conf) if err != nil { @@ -155,6 +147,8 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { } v6conf := v6JSONToServerConf(newconfig.V6) + v6conf.Enabled = newconfig.Enabled + v6conf.InterfaceName = newconfig.InterfaceName v6conf.notify = s.onNotify s6, err := v6Create(v6conf) if s6 == nil { @@ -167,26 +161,28 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) { log.Error("failed to stop the DHCP server: %s", err) } + s.conf.Enabled = newconfig.Enabled + s.conf.InterfaceName = newconfig.InterfaceName s.srv4 = s4 s.srv6 = s6 s.conf.ConfigModified() - if newconfig.V4.Enabled { - staticIP, err := HasStaticIP(newconfig.V4.InterfaceName) + if newconfig.Enabled { + staticIP, err := HasStaticIP(newconfig.InterfaceName) if !staticIP && err == nil { - err = SetStaticIP(newconfig.V4.InterfaceName) + err = SetStaticIP(newconfig.InterfaceName) if err != nil { httpError(r, w, http.StatusInternalServerError, "Failed to configure static IP: %s", err) return } } - } - err = s.Start() - if err != nil { - httpError(r, w, http.StatusBadRequest, "Failed to start DHCP server: %s", err) - return + err = s.Start() + if err != nil { + httpError(r, w, http.StatusBadRequest, "Failed to start DHCP server: %s", err) + return + } } } diff --git a/dhcpd/dhcpd.go b/dhcpd/dhcpd.go index ac1a23a6..06c82b5d 100644 --- a/dhcpd/dhcpd.go +++ b/dhcpd/dhcpd.go @@ -28,6 +28,9 @@ type Lease struct { // ServerConfig - DHCP server configuration // field ordering is important -- yaml fields will mirror ordering from here type ServerConfig struct { + Enabled bool `yaml:"enabled"` + InterfaceName string `yaml:"interface_name"` + Conf4 V4ServerConf `yaml:"dhcpv4"` Conf6 V6ServerConf `yaml:"dhcpv6"` @@ -72,6 +75,8 @@ func (s *Server) CheckConfig(config ServerConfig) error { // Create - create object func Create(config ServerConfig) *Server { s := Server{} + s.conf.Enabled = config.Enabled + s.conf.InterfaceName = config.InterfaceName s.conf.HTTPRegister = config.HTTPRegister s.conf.ConfigModified = config.ConfigModified s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename) @@ -82,6 +87,8 @@ func Create(config ServerConfig) *Server { } var err error + config.Conf4.Enabled = s.conf.Enabled + config.Conf4.InterfaceName = s.conf.InterfaceName config.Conf4.notify = s.onNotify s.srv4, err = v4Create(config.Conf4) if err != nil { @@ -89,6 +96,8 @@ func Create(config ServerConfig) *Server { return nil } + config.Conf6.Enabled = s.conf.Enabled + config.Conf6.InterfaceName = s.conf.InterfaceName config.Conf6.notify = s.onNotify s.srv6, err = v6Create(config.Conf6) if err != nil { @@ -126,6 +135,8 @@ func (s *Server) notify(flags int) { // WriteDiskConfig - write configuration func (s *Server) WriteDiskConfig(c *ServerConfig) { + c.Enabled = s.conf.Enabled + c.InterfaceName = s.conf.InterfaceName s.srv4.WriteDiskConfig4(&c.Conf4) s.srv6.WriteDiskConfig6(&c.Conf6) } diff --git a/dhcpd/server.go b/dhcpd/server.go index 21d8560f..96d89cfa 100644 --- a/dhcpd/server.go +++ b/dhcpd/server.go @@ -33,8 +33,8 @@ type DHCPServer interface { // V4ServerConf - server configuration type V4ServerConf struct { - Enabled bool `yaml:"enabled"` - InterfaceName string `yaml:"interface_name"` + Enabled bool `yaml:"-"` + InterfaceName string `yaml:"-"` GatewayIP string `yaml:"gateway_ip"` SubnetMask string `yaml:"subnet_mask"` RangeStart string `yaml:"range_start"` @@ -58,8 +58,8 @@ type V4ServerConf struct { // V6ServerConf - server configuration type V6ServerConf struct { - Enabled bool `yaml:"enabled"` - InterfaceName string `yaml:"interface_name"` + Enabled bool `yaml:"-"` + InterfaceName string `yaml:"-"` RangeStart string `yaml:"range_start"` LeaseDuration uint32 `yaml:"lease_duration"` // in seconds diff --git a/dhcpd/v4.go b/dhcpd/v4.go index e80eb6e6..88ba3656 100644 --- a/dhcpd/v4.go +++ b/dhcpd/v4.go @@ -525,7 +525,8 @@ func (s *v4Server) Start() error { log.Debug("DHCPv4: starting...") s.conf.dnsIPAddrs = getIfaceIPv4(*iface) 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{ @@ -541,9 +542,8 @@ func (s *v4Server) Start() error { go func() { err = s.srv.Serve() - log.Error("DHCPv4: %s", err) + log.Debug("DHCPv4: srv.Serve: %s", err) }() - return nil } diff --git a/dhcpd/v6.go b/dhcpd/v6.go index cd23e250..5adff676 100644 --- a/dhcpd/v6.go +++ b/dhcpd/v6.go @@ -556,14 +556,11 @@ func (s *v6Server) Start() error { return wrapErrPrint(err, "Couldn't find interface by name %s", s.conf.InterfaceName) } - if !s.conf.Enabled { - return nil - } - log.Debug("DHCPv6: starting...") s.conf.dnsIPAddrs = getIfaceIPv6(*iface) 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 { @@ -586,7 +583,7 @@ func (s *v6Server) Start() error { go func() { err = s.srv.Serve() - log.Error("DHCPv6: %s", err) + log.Debug("DHCPv6: srv.Serve: %s", err) }() return nil }