This commit is contained in:
Simon Zolin 2020-05-25 16:29:06 +03:00
parent 2acff7757b
commit c7cc4a9345
4 changed files with 67 additions and 61 deletions

View file

@ -64,21 +64,22 @@ func v4ServerConfToJSON(c V4ServerConf) v4ServerConfJSON {
}
}
func v4JSONToServerConf(c v4ServerConfJSON) V4ServerConf {
func v4JSONToServerConf(j v4ServerConfJSON) V4ServerConf {
return V4ServerConf{
Enabled: c.Enabled,
InterfaceName: c.InterfaceName,
GatewayIP: c.GatewayIP,
SubnetMask: c.SubnetMask,
RangeStart: c.RangeStart,
RangeEnd: c.RangeEnd,
LeaseDuration: c.LeaseDuration,
ICMPTimeout: c.ICMPTimeout,
Enabled: j.Enabled,
InterfaceName: j.InterfaceName,
GatewayIP: j.GatewayIP,
SubnetMask: j.SubnetMask,
RangeStart: j.RangeStart,
RangeEnd: j.RangeEnd,
LeaseDuration: j.LeaseDuration,
ICMPTimeout: j.ICMPTimeout,
}
}
type v6ServerConfJSON struct {
Enabled bool `json:"enabled"`
InterfaceName string `json:"interface_name"`
RangeStart string `json:"range_start"`
LeaseDuration uint32 `json:"lease_duration"`
}
@ -86,25 +87,34 @@ type v6ServerConfJSON struct {
func v6ServerConfToJSON(c V6ServerConf) v6ServerConfJSON {
return v6ServerConfJSON{
Enabled: c.Enabled,
InterfaceName: c.InterfaceName,
RangeStart: c.RangeStart,
LeaseDuration: c.LeaseDuration,
}
}
func v6JSONToServerConf(c v6ServerConfJSON) V6ServerConf {
func v6JSONToServerConf(j v6ServerConfJSON) V6ServerConf {
return V6ServerConf{
Enabled: c.Enabled,
RangeStart: c.RangeStart,
LeaseDuration: c.LeaseDuration,
Enabled: j.Enabled,
InterfaceName: j.InterfaceName,
RangeStart: j.RangeStart,
LeaseDuration: j.LeaseDuration,
}
}
func (s *Server) handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
leases := convertLeases(s.Leases(LeasesDynamic), true)
staticLeases := convertLeases(s.Leases(LeasesStatic), false)
v4conf := V4ServerConf{}
s.srv4.WriteDiskConfig4(&v4conf)
v6conf := V6ServerConf{}
s.srv6.WriteDiskConfig6(&v6conf)
status := map[string]interface{}{
"v4": v4ServerConfToJSON(s.conf.Conf4),
"v6": v6ServerConfToJSON(s.conf.Conf6),
"v4": v4ServerConfToJSON(v4conf),
"v6": v6ServerConfToJSON(v6conf),
"leases": leases,
"static_leases": staticLeases,
}
@ -136,33 +146,27 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
return
}
// err = s.CheckConfig(newconfig.ServerConfig)
// if err != nil {
// httpError(r, w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
// return
// }
err = s.Stop()
if err != nil {
log.Error("failed to stop the DHCP server: %s", err)
}
v4conf := v4JSONToServerConf(newconfig.V4)
v4conf.notify = s.conf.Conf4.notify
v4conf.notify = s.onNotify
s4, err := v4Create(v4conf)
if err != nil {
httpError(r, w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)
httpError(r, w, http.StatusBadRequest, "Invalid DHCPv4 configuration: %s", err)
return
}
v6conf := v6JSONToServerConf(newconfig.V6)
v6conf.notify = s.conf.Conf6.notify
v6conf.notify = s.onNotify
s6, err := v6Create(v6conf)
if s6 == nil {
httpError(r, w, http.StatusBadRequest, "Invalid DHCPv6 configuration: %s", err)
return
}
err = s.Stop()
if err != nil {
log.Error("failed to stop the DHCP server: %s", err)
}
s.srv4 = s4
s.srv6 = s6
@ -177,12 +181,12 @@ func (s *Server) handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
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
}
}

View file

@ -72,8 +72,6 @@ func (s *Server) CheckConfig(config ServerConfig) error {
// Create - create object
func Create(config ServerConfig) *Server {
s := Server{}
config.Conf4.notify = s.onNotify
config.Conf6.notify = s.onNotify
s.conf.HTTPRegister = config.HTTPRegister
s.conf.ConfigModified = config.ConfigModified
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
@ -84,12 +82,14 @@ func Create(config ServerConfig) *Server {
}
var err error
config.Conf4.notify = s.onNotify
s.srv4, err = v4Create(config.Conf4)
if err != nil {
log.Error("%s", err)
return nil
}
config.Conf6.notify = s.onNotify
s.srv6, err = v6Create(config.Conf6)
if err != nil {
log.Error("%s", err)

View file

@ -376,11 +376,8 @@ func (s *v4Server) processRequest(req *dhcpv4.DHCPv4, resp *dhcpv4.DHCPv4) (*Lea
reqIP := req.Options.Get(dhcpv4.OptionRequestedIPAddress)
sid := req.Options.Get(dhcpv4.OptionServerIdentifier)
if len(sid) == 0 {
log.Debug("DHCPv4: No OptionServerIdentifier in Request message for %s", mac)
return nil, false
}
if !bytes.Equal(sid, s.conf.dnsIPAddrs[0]) {
if len(sid) != 0 &&
!bytes.Equal(sid, s.conf.dnsIPAddrs[0]) {
log.Debug("DHCPv4: Bad OptionServerIdentifier in Request message for %s", mac)
return nil, false
}
@ -535,7 +532,7 @@ func (s *v4Server) Start() error {
IP: net.ParseIP("0.0.0.0"),
Port: dhcpv4.ServerPort,
}
server, err := server4.NewServer(iface.Name, laddr, s.packetHandler, server4.WithDebugLogger())
s.srv, err = server4.NewServer(iface.Name, laddr, s.packetHandler, server4.WithDebugLogger())
if err != nil {
return err
}
@ -543,31 +540,33 @@ func (s *v4Server) Start() error {
log.Info("DHCPv4: listening")
go func() {
err = server.Serve()
err = s.srv.Serve()
log.Error("DHCPv4: %s", err)
}()
return nil
}
// Reset - stop server
func (s *v4Server) Reset() {
s.leasesLock.Lock()
s.leases = nil
s.leasesLock.Unlock()
}
// Stop - stop server
func (s *v4Server) Stop() {
if s.srv == nil {
return
}
log.Debug("DHCPv4: stopping")
err := s.srv.Close()
if err != nil {
log.Error("DHCPv4: srv.Close: %s", err)
}
// now server.Serve() will return
// now s.srv.Serve() will return
s.srv = nil
}
// Reset - stop server
func (s *v4Server) Reset() {
s.leasesLock.Lock()
s.leases = nil
s.leasesLock.Unlock()
}
// Create DHCPv4 server
@ -599,6 +598,7 @@ func v4Create(conf V4ServerConf) (DHCPServer, error) {
if s.conf.ipStart[0] == 0 {
return nil, fmt.Errorf("DHCPv4: invalid range start IP")
}
s.conf.ipEnd, err = parseIPv4(conf.RangeEnd)
if s.conf.ipEnd == nil {
return nil, fmt.Errorf("DHCPv4: %s", err)

View file

@ -60,7 +60,7 @@ func (s *v6Server) ResetLeases(ll []*Lease) {
continue
}
s.leases = append(s.leases, l)
s.addLease(l)
}
}
@ -578,36 +578,38 @@ func (s *v6Server) Start() error {
IP: net.ParseIP("::"),
Port: dhcpv6.DefaultServerPort,
}
server, err := server6.NewServer(iface.Name, laddr, s.packetHandler, server6.WithDebugLogger())
s.srv, err = server6.NewServer(iface.Name, laddr, s.packetHandler, server6.WithDebugLogger())
if err != nil {
return err
}
go func() {
err = server.Serve()
err = s.srv.Serve()
log.Error("DHCPv6: %s", err)
}()
return nil
}
// Reset - stop server
func (s *v6Server) Reset() {
s.leasesLock.Lock()
s.leases = nil
s.leasesLock.Unlock()
}
// Stop - stop server
func (s *v6Server) Stop() {
if s.srv == nil {
return
}
log.Debug("DHCPv6: stopping")
err := s.srv.Close()
if err != nil {
log.Error("DHCPv6: srv.Close: %s", err)
}
// now server.Serve() will return
s.srv = nil
}
// Reset - stop server
func (s *v6Server) Reset() {
s.leasesLock.Lock()
s.leases = nil
s.leasesLock.Unlock()
}
// Create DHCPv6 server