Merge: + clients: runtime list: add clients from DHCP that have non-empty Hostname property

Close #979

* commit '0c2611ff978e55ee1d6f4dd54b9edd54a3fe5909':
  * minor
  + clients: runtime list: add clients from DHCP that have non-empty Hostname property
This commit is contained in:
Simon Zolin 2019-09-16 13:54:56 +03:00
commit 3862662201
2 changed files with 19 additions and 4 deletions

View File

@ -58,7 +58,7 @@ func (s *Server) dbLoad() {
if obj[i].Expiry != leaseExpireStatic && if obj[i].Expiry != leaseExpireStatic &&
!ipInRange(s.leaseStart, s.leaseStop, obj[i].IP) { !ipInRange(s.leaseStart, s.leaseStop, obj[i].IP) {
log.Tracef("Skipping a lease with IP %s: not within current IP range", obj[i].IP) log.Tracef("Skipping a lease with IP %v: not within current IP range", obj[i].IP)
continue continue
} }

View File

@ -53,10 +53,11 @@ type clientJSON struct {
type clientSource uint type clientSource uint
const ( const (
// Priority: etc/hosts > ARP > rDNS // Priority: etc/hosts > DHCP > ARP > rDNS
ClientSourceRDNS clientSource = 0 // from rDNS ClientSourceRDNS clientSource = 0 // from rDNS
ClientSourceARP clientSource = 1 // from 'arp -a' ClientSourceDHCP clientSource = 1 // from DHCP
ClientSourceHostsFile clientSource = 2 // from /etc/hosts ClientSourceARP clientSource = 2 // from 'arp -a'
ClientSourceHostsFile clientSource = 3 // from /etc/hosts
) )
// ClientHost information // ClientHost information
@ -89,6 +90,7 @@ func (clients *clientsContainer) periodicUpdate() {
for { for {
clients.addFromHostsFile() clients.addFromHostsFile()
clients.addFromSystemARP() clients.addFromSystemARP()
clients.addFromDHCP()
time.Sleep(clientsUpdatePeriod) time.Sleep(clientsUpdatePeriod)
} }
} }
@ -369,6 +371,17 @@ func (clients *clientsContainer) addFromSystemARP() {
log.Info("Added %d client aliases from 'arp -a' command output", n) log.Info("Added %d client aliases from 'arp -a' command output", n)
} }
// add clients from DHCP that have non-empty Hostname property
func (clients *clientsContainer) addFromDHCP() {
leases := config.dhcpServer.Leases()
for _, l := range leases {
if len(l.Hostname) == 0 {
continue
}
config.clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
}
}
type clientHostJSON struct { type clientHostJSON struct {
IP string `json:"ip"` IP string `json:"ip"`
Name string `json:"name"` Name string `json:"name"`
@ -417,6 +430,8 @@ func handleGetClients(w http.ResponseWriter, r *http.Request) {
} }
cj.Source = "etc/hosts" cj.Source = "etc/hosts"
switch ch.Source { switch ch.Source {
case ClientSourceDHCP:
cj.Source = "DHCP"
case ClientSourceRDNS: case ClientSourceRDNS:
cj.Source = "rDNS" cj.Source = "rDNS"
case ClientSourceARP: case ClientSourceARP: