From 763b98695554f04e89ec1cbf15c47194d3a55d4e Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Tue, 14 May 2019 13:54:35 +0300 Subject: [PATCH] + dhcp: /dhcp/status: return static leases --- dhcp.go | 6 ++++-- dhcpd/db.go | 4 +++- dhcpd/dhcpd.go | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/dhcp.go b/dhcp.go index 9e4f27db..48e3029f 100644 --- a/dhcp.go +++ b/dhcp.go @@ -42,9 +42,11 @@ func convertLeases(inputLeases []dhcpd.Lease, includeExpires bool) []map[string] func handleDHCPStatus(w http.ResponseWriter, r *http.Request) { log.Tracef("%s %v", r.Method, r.URL) leases := convertLeases(dhcpServer.Leases(), true) + staticLeases := convertLeases(dhcpServer.StaticLeases(), false) status := map[string]interface{}{ - "config": config.DHCP, - "leases": leases, + "config": config.DHCP, + "leases": leases, + "static_leases": staticLeases, } w.Header().Set("Content-Type", "application/json") diff --git a/dhcpd/db.go b/dhcpd/db.go index f9c371d3..c27bb679 100644 --- a/dhcpd/db.go +++ b/dhcpd/db.go @@ -55,7 +55,9 @@ func (s *Server) dbLoad() { numLeases := len(obj) for i := range obj { - if !ipInRange(s.leaseStart, s.leaseStop, obj[i].IP) { + if obj[i].Expiry != leaseExpireStatic && + !ipInRange(s.leaseStart, s.leaseStop, obj[i].IP) { + log.Tracef("Skipping a lease with IP %s: not within current IP range", obj[i].IP) continue } diff --git a/dhcpd/dhcpd.go b/dhcpd/dhcpd.go index 85df1590..073ec0fa 100644 --- a/dhcpd/dhcpd.go +++ b/dhcpd/dhcpd.go @@ -606,6 +606,25 @@ func (s *Server) Leases() []Lease { return result } +// StaticLeases returns the list of statically-configured DHCP leases (thread-safe) +func (s *Server) StaticLeases() []Lease { + s.Lock() + if s.IPpool == nil { + s.dbLoad() + } + s.Unlock() + + var result []Lease + s.RLock() + for _, lease := range s.leases { + if lease.Expiry.Unix() == 1 { + result = append(result, *lease) + } + } + s.RUnlock() + return result +} + // Print information about the current leases func (s *Server) printLeases() { log.Tracef("Leases:")