Skip to content

Commit dcc5754

Browse files
committed
Merge: * clients: update runtime clients of type DHCP by event from DHCP module
Close AdguardTeam#1378 Squashed commit of the following: commit e45e2d0 Merge: bea8f79 5e9c21b Author: Simon Zolin <[email protected]> Date: Wed Jan 29 20:08:20 2020 +0300 Merge remote-tracking branch 'origin/master' into 1378-dhcp-clients commit bea8f79 Author: Simon Zolin <[email protected]> Date: Wed Jan 29 20:08:06 2020 +0300 minor commit 6f1da9c Author: Simon Zolin <[email protected]> Date: Wed Jan 29 19:31:08 2020 +0300 fix commit a88b46c Author: Simon Zolin <[email protected]> Date: Wed Jan 29 12:53:22 2020 +0300 minor commit d2897fe Author: Simon Zolin <[email protected]> Date: Tue Jan 28 19:55:10 2020 +0300 * clients: update runtime clients of type DHCP by event from DHCP module commit 3aa352e Author: Simon Zolin <[email protected]> Date: Tue Jan 28 19:52:08 2020 +0300 * minor commit f5c2291 Author: Simon Zolin <[email protected]> Date: Tue Jan 28 19:08:23 2020 +0300 * clients: remove old entries of source type /etc/hosts or ARP
1 parent 5e9c21b commit dcc5754

File tree

6 files changed

+121
-37
lines changed

6 files changed

+121
-37
lines changed

dhcpd/db.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ type leaseJSON struct {
2323
Expiry int64 `json:"exp"`
2424
}
2525

26+
func normalizeIP(ip net.IP) net.IP {
27+
ip4 := ip.To4()
28+
if ip4 != nil {
29+
return ip4
30+
}
31+
return ip
32+
}
33+
2634
// Safe version of dhcp4.IPInRange()
2735
func ipInRange(start, stop, ip net.IP) bool {
2836
if len(start) != len(stop) ||
@@ -56,6 +64,7 @@ func (s *Server) dbLoad() {
5664

5765
numLeases := len(obj)
5866
for i := range obj {
67+
obj[i].IP = normalizeIP(obj[i].IP)
5968

6069
if obj[i].Expiry != leaseExpireStatic &&
6170
!ipInRange(s.leaseStart, s.leaseStop, obj[i].IP) {

dhcpd/dhcp_http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func convertLeases(inputLeases []Lease, includeExpires bool) []map[string]string
4343
}
4444

4545
func (s *Server) handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
46-
leases := convertLeases(s.Leases(), true)
47-
staticLeases := convertLeases(s.StaticLeases(), false)
46+
leases := convertLeases(s.Leases(LeasesDynamic), true)
47+
staticLeases := convertLeases(s.Leases(LeasesStatic), false)
4848
status := map[string]interface{}{
4949
"config": s.conf,
5050
"leases": leases,

dhcpd/dhcpd.go

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ type ServerConfig struct {
5555
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `json:"-" yaml:"-"`
5656
}
5757

58+
type onLeaseChangedT func(flags int)
59+
60+
// flags for onLeaseChanged()
61+
const (
62+
LeaseChangedAdded = iota
63+
LeaseChangedAddedStatic
64+
LeaseChangedRemovedStatic
65+
LeaseChangedBlacklisted
66+
)
67+
5868
// Server - the current state of the DHCP server
5969
type Server struct {
6070
conn *filterConn // listening UDP socket
@@ -78,6 +88,9 @@ type Server struct {
7888
IPpool map[[4]byte]net.HardwareAddr
7989

8090
conf ServerConfig
91+
92+
// Called when the leases DB is modified
93+
onLeaseChanged onLeaseChangedT
8194
}
8295

8396
// Print information about the available network interfaces
@@ -101,6 +114,13 @@ func Create(config ServerConfig) *Server {
101114
s := Server{}
102115
s.conf = config
103116
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
117+
if s.conf.Enabled {
118+
err := s.setConfig(config)
119+
if err != nil {
120+
log.Error("DHCP: %s", err)
121+
return nil
122+
}
123+
}
104124
if s.conf.HTTPRegister != nil {
105125
s.registerHandlers()
106126
}
@@ -120,6 +140,18 @@ func (s *Server) Init(config ServerConfig) error {
120140
return nil
121141
}
122142

143+
// SetOnLeaseChanged - set callback
144+
func (s *Server) SetOnLeaseChanged(onLeaseChanged onLeaseChangedT) {
145+
s.onLeaseChanged = onLeaseChanged
146+
}
147+
148+
func (s *Server) notify(flags int) {
149+
if s.onLeaseChanged == nil {
150+
return
151+
}
152+
s.onLeaseChanged(flags)
153+
}
154+
123155
// WriteDiskConfig - write configuration
124156
func (s *Server) WriteDiskConfig(c *ServerConfig) {
125157
*c = s.conf
@@ -285,7 +317,6 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
285317
s.leases[i].IP, hwaddr, s.leases[i].HWAddr, s.leases[i].Expiry)
286318
lease.IP = s.leases[i].IP
287319
s.leases[i] = lease
288-
s.dbStore()
289320

290321
s.reserveIP(lease.IP, hwaddr)
291322
return lease, nil
@@ -294,7 +325,6 @@ func (s *Server) reserveLease(p dhcp4.Packet) (*Lease, error) {
294325
log.Tracef("Assigning to %s IP address %s", hwaddr, ip.String())
295326
lease.IP = ip
296327
s.leases = append(s.leases, lease)
297-
s.dbStore()
298328
return lease, nil
299329
}
300330

@@ -449,6 +479,7 @@ func (s *Server) blacklistLease(lease *Lease) {
449479
lease.Expiry = time.Now().Add(s.leaseTime)
450480
s.dbStore()
451481
s.leasesLock.Unlock()
482+
s.notify(LeaseChangedBlacklisted)
452483
}
453484

454485
// Return TRUE if DHCP packet is correct
@@ -538,6 +569,10 @@ func (s *Server) handleDHCP4Request(p dhcp4.Packet, options dhcp4.Options) dhcp4
538569

539570
if lease.Expiry.Unix() != leaseExpireStatic {
540571
lease.Expiry = time.Now().Add(s.leaseTime)
572+
s.leasesLock.Lock()
573+
s.dbStore()
574+
s.leasesLock.Unlock()
575+
s.notify(LeaseChangedAdded) // Note: maybe we shouldn't call this function if only expiration time is updated
541576
}
542577
log.Tracef("Replying with ACK. IP: %s HW: %s Expire: %s",
543578
lease.IP, lease.HWAddr, lease.Expiry)
@@ -578,17 +613,19 @@ func (s *Server) AddStaticLease(l Lease) error {
578613
l.Expiry = time.Unix(leaseExpireStatic, 0)
579614

580615
s.leasesLock.Lock()
581-
defer s.leasesLock.Unlock()
582616

583617
if s.findReservedHWaddr(l.IP) != nil {
584618
err := s.rmDynamicLeaseWithIP(l.IP)
585619
if err != nil {
620+
s.leasesLock.Unlock()
586621
return err
587622
}
588623
}
589624
s.leases = append(s.leases, &l)
590625
s.reserveIP(l.IP, l.HWAddr)
591626
s.dbStore()
627+
s.leasesLock.Unlock()
628+
s.notify(LeaseChangedAddedStatic)
592629
return nil
593630
}
594631

@@ -637,27 +674,38 @@ func (s *Server) RemoveStaticLease(l Lease) error {
637674
}
638675

639676
s.leasesLock.Lock()
640-
defer s.leasesLock.Unlock()
641677

642678
if s.findReservedHWaddr(l.IP) == nil {
679+
s.leasesLock.Unlock()
643680
return fmt.Errorf("Lease not found")
644681
}
645682

646683
err := s.rmLease(l)
647684
if err != nil {
685+
s.leasesLock.Unlock()
648686
return err
649687
}
650688
s.dbStore()
689+
s.leasesLock.Unlock()
690+
s.notify(LeaseChangedRemovedStatic)
651691
return nil
652692
}
653693

694+
// flags for Leases() function
695+
const (
696+
LeasesDynamic = 1
697+
LeasesStatic = 2
698+
LeasesAll = LeasesDynamic | LeasesStatic
699+
)
700+
654701
// Leases returns the list of current DHCP leases (thread-safe)
655-
func (s *Server) Leases() []Lease {
702+
func (s *Server) Leases(flags int) []Lease {
656703
var result []Lease
657704
now := time.Now().Unix()
658705
s.leasesLock.RLock()
659706
for _, lease := range s.leases {
660-
if lease.Expiry.Unix() > now {
707+
if ((flags&LeasesDynamic) != 0 && lease.Expiry.Unix() > now) ||
708+
((flags&LeasesStatic) != 0 && lease.Expiry.Unix() == leaseExpireStatic) {
661709
result = append(result, *lease)
662710
}
663711
}
@@ -666,20 +714,6 @@ func (s *Server) Leases() []Lease {
666714
return result
667715
}
668716

669-
// StaticLeases returns the list of statically-configured DHCP leases (thread-safe)
670-
func (s *Server) StaticLeases() []Lease {
671-
s.leasesLock.Lock()
672-
defer s.leasesLock.Unlock()
673-
674-
var result []Lease
675-
for _, lease := range s.leases {
676-
if lease.Expiry.Unix() == 1 {
677-
result = append(result, *lease)
678-
}
679-
}
680-
return result
681-
}
682-
683717
// Print information about the current leases
684718
func (s *Server) printLeases() {
685719
log.Tracef("Leases:")

dhcpd/dhcpd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func testStaticLeases(t *testing.T, s *Server) {
130130
err = s.AddStaticLease(l)
131131
check(t, err == nil, "AddStaticLease")
132132

133-
ll := s.StaticLeases()
133+
ll := s.Leases(LeasesStatic)
134134
check(t, len(ll) != 0 && bytes.Equal(ll[0].IP, []byte{1, 1, 1, 1}), "StaticLeases")
135135

136136
err = s.RemoveStaticLease(l)

dnsforward/dnsforward.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ func (s *Server) handleDNSRequest(p *proxy.Proxy, d *proxy.DNSContext) error {
685685
processFilteringBeforeRequest,
686686
processUpstream,
687687
processFilteringAfterResponse,
688+
processQueryLogsAndStats,
688689
}
689690
for _, process := range mods {
690691
r := process(ctx)
@@ -699,8 +700,6 @@ func (s *Server) handleDNSRequest(p *proxy.Proxy, d *proxy.DNSContext) error {
699700
if d.Res != nil {
700701
d.Res.Compress = true // some devices require DNS message compression
701702
}
702-
703-
_ = processQueryLogsAndStats(ctx)
704703
return nil
705704
}
706705

0 commit comments

Comments
 (0)