Pull request: 3166 fix dhcp
Updates #3166.
Squashed commit of the following:
commit cac62f842886f06a70fb50d251938e0feaa06846
Author: Ildar Kamalov <ik@adguard.com>
Date: Thu Jun 10 14:35:40 2021 +0300
client: get dhcp status on static lease submit
commit 2ae4e3ab40041fbc0f53f4902a00263425409a6a
Merge: 1e0f9c15 299ea88c
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jun 10 14:09:05 2021 +0300
Merge branch 'master' into 3166-fix-dhcp
commit 1e0f9c15d98f40a499c75b74bca48fe58512e300
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jun 10 14:03:58 2021 +0300
dhcpd: imp code
commit e4b9626b60a11cd2491c675a297f527f7e12f9b4
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jun 10 13:03:47 2021 +0300
dhcpd: fix static lease load
commit dc449f21986a8ea300d07c081f1e416b2f08bc25
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Tue Jun 1 17:21:10 2021 +0300
home: change clients sources priority
This commit is contained in:
@@ -110,7 +110,9 @@ func (s *v4Server) ResetLeases(leases []*Lease) {
|
||||
s.leases = nil
|
||||
|
||||
for _, l := range leases {
|
||||
l.Hostname = s.validHostnameForClient(l.Hostname, l.IP)
|
||||
if !l.IsStatic() {
|
||||
l.Hostname = s.validHostnameForClient(l.Hostname, l.IP)
|
||||
}
|
||||
err = s.addLease(l)
|
||||
if err != nil {
|
||||
// TODO(a.garipov): Wrap and bubble up the error.
|
||||
@@ -339,9 +341,8 @@ func (s *v4Server) AddStaticLease(l Lease) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if l.Hostname != "" {
|
||||
var hostname string
|
||||
hostname, err = normalizeHostname(l.Hostname)
|
||||
if hostname := l.Hostname; hostname != "" {
|
||||
hostname, err = normalizeHostname(hostname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -635,6 +636,33 @@ func (o *optFQDN) ToBytes() []byte {
|
||||
return b
|
||||
}
|
||||
|
||||
// checkLease checks if the pair of mac and ip is already leased. The mismatch
|
||||
// is true when the existing lease has the same hardware address but differs in
|
||||
// its IP address.
|
||||
func (s *v4Server) checkLease(mac net.HardwareAddr, ip net.IP) (lease *Lease, mismatch bool) {
|
||||
s.leasesLock.Lock()
|
||||
defer s.leasesLock.Unlock()
|
||||
|
||||
for _, l := range s.leases {
|
||||
if !bytes.Equal(l.HWAddr, mac) {
|
||||
continue
|
||||
}
|
||||
|
||||
if l.IP.Equal(ip) {
|
||||
return l, false
|
||||
}
|
||||
|
||||
log.Debug(
|
||||
`dhcpv4: mismatched OptionRequestedIPAddress in req msg for %s`,
|
||||
mac,
|
||||
)
|
||||
|
||||
return nil, true
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// processRequest is the handler for the DHCP Request request.
|
||||
func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, needsReply bool) {
|
||||
mac := req.ClientHWAddr
|
||||
@@ -656,33 +684,8 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, needs
|
||||
return nil, false
|
||||
}
|
||||
|
||||
mismatch := false
|
||||
func() {
|
||||
s.leasesLock.Lock()
|
||||
defer s.leasesLock.Unlock()
|
||||
|
||||
for _, l := range s.leases {
|
||||
if !bytes.Equal(l.HWAddr, mac) {
|
||||
continue
|
||||
}
|
||||
|
||||
if l.IP.Equal(reqIP) {
|
||||
lease = l
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug(
|
||||
`dhcpv4: mismatched OptionRequestedIPAddress in req msg for %s`,
|
||||
mac,
|
||||
)
|
||||
|
||||
mismatch = true
|
||||
|
||||
return
|
||||
}
|
||||
}()
|
||||
if mismatch {
|
||||
var mismatch bool
|
||||
if lease, mismatch = s.checkLease(mac, reqIP); mismatch {
|
||||
return nil, true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user