dhcpd.go (AdGuardHome-0.104.1) | : | dhcpd.go (AdGuardHome-0.104.3) | ||
---|---|---|---|---|
// Package dhcpd provides a DHCP server. | ||||
package dhcpd | package dhcpd | |||
import ( | import ( | |||
"encoding/hex" | "encoding/hex" | |||
"net" | "net" | |||
"net/http" | "net/http" | |||
"path/filepath" | "path/filepath" | |||
"runtime" | ||||
"strconv" | "strconv" | |||
"strings" | "strings" | |||
"time" | "time" | |||
"github.com/AdguardTeam/AdGuardHome/internal/util" | "github.com/AdguardTeam/AdGuardHome/internal/util" | |||
"github.com/AdguardTeam/golibs/log" | "github.com/AdguardTeam/golibs/log" | |||
) | ) | |||
const defaultDiscoverTime = time.Second * 3 | const ( | |||
const leaseExpireStatic = 1 | defaultDiscoverTime = time.Second * 3 | |||
leaseExpireStatic = 1 | ||||
) | ||||
var webHandlersRegistered = false | var webHandlersRegistered = false | |||
// Lease contains the necessary information about a DHCP lease | // Lease contains the necessary information about a DHCP lease | |||
type Lease struct { | type Lease struct { | |||
HWAddr net.HardwareAddr `json:"mac"` | HWAddr net.HardwareAddr `json:"mac"` | |||
IP net.IP `json:"ip"` | IP net.IP `json:"ip"` | |||
Hostname string `json:"hostname"` | Hostname string `json:"hostname"` | |||
// Lease expiration time | // Lease expiration time | |||
skipping to change at line 85 | skipping to change at line 89 | |||
SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT) | SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT) | |||
} | } | |||
// CheckConfig checks the configuration | // CheckConfig checks the configuration | |||
func (s *Server) CheckConfig(config ServerConfig) error { | func (s *Server) CheckConfig(config ServerConfig) error { | |||
return nil | return nil | |||
} | } | |||
// 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.Enabled = config.Enabled | |||
s.conf.InterfaceName = config.InterfaceName | 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) | |||
if !webHandlersRegistered && s.conf.HTTPRegister != nil { | if !webHandlersRegistered && s.conf.HTTPRegister != nil { | |||
if runtime.GOOS == "windows" { | ||||
// Our DHCP server doesn't work on Windows yet, so | ||||
// signal that to the front with an HTTP 501. | ||||
// | ||||
// TODO(a.garipov): This needs refactoring. We | ||||
// shouldn't even try and initialize a DHCP server on | ||||
// Windows, but there are currently too many | ||||
// interconnected parts--such as HTTP handlers and | ||||
// frontend--to make that work properly. | ||||
s.registerNotImplementedHandlers() | ||||
} else { | ||||
s.registerHandlers() | ||||
} | ||||
webHandlersRegistered = true | webHandlersRegistered = true | |||
s.registerHandlers() | ||||
} | } | |||
var err4, err6 error | var err4, err6 error | |||
v4conf := config.Conf4 | v4conf := config.Conf4 | |||
v4conf.Enabled = s.conf.Enabled | v4conf.Enabled = s.conf.Enabled | |||
if len(v4conf.RangeStart) == 0 { | if len(v4conf.RangeStart) == 0 { | |||
v4conf.Enabled = false | v4conf.Enabled = false | |||
} | } | |||
v4conf.InterfaceName = s.conf.InterfaceName | v4conf.InterfaceName = s.conf.InterfaceName | |||
v4conf.notify = s.onNotify | v4conf.notify = s.onNotify | |||
skipping to change at line 133 | skipping to change at line 151 | |||
} | } | |||
if s.conf.Enabled && !v4conf.Enabled && !v6conf.Enabled { | if s.conf.Enabled && !v4conf.Enabled && !v6conf.Enabled { | |||
log.Error("Can't enable DHCP server because neither DHCPv4 nor DH CPv6 servers are configured") | log.Error("Can't enable DHCP server because neither DHCPv4 nor DH CPv6 servers are configured") | |||
return nil | return nil | |||
} | } | |||
// we can't delay database loading until DHCP server is started, | // we can't delay database loading until DHCP server is started, | |||
// because we need static leases functionality available beforehand | // because we need static leases functionality available beforehand | |||
s.dbLoad() | s.dbLoad() | |||
return &s | return s | |||
} | } | |||
// server calls this function after DB is updated | // server calls this function after DB is updated | |||
func (s *Server) onNotify(flags uint32) { | func (s *Server) onNotify(flags uint32) { | |||
if flags == LeaseChangedDBStore { | if flags == LeaseChangedDBStore { | |||
s.dbStore() | s.dbStore() | |||
return | return | |||
} | } | |||
s.notify(int(flags)) | s.notify(int(flags)) | |||
End of changes. 7 change blocks. | ||||
5 lines changed or deleted | 23 lines changed or added |