Skip to content

Commit

Permalink
dhcpd: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jun 10, 2021
1 parent e4b9626 commit 1e0f9c1
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (s *v4Server) AddStaticLease(l Lease) (err error) {
}

if hostname := l.Hostname; hostname != "" {
hostname, err = normalizeHostname(l.Hostname)
hostname, err = normalizeHostname(hostname)
if err != nil {
return err
}
Expand Down Expand Up @@ -636,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
Expand All @@ -657,31 +684,8 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, needs
return nil, false
}

if func() (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(reqIP) {
lease = l

return false
}

log.Debug(
`dhcpv4: mismatched OptionRequestedIPAddress in req msg for %s`,
mac,
)

return true
}

return false
}() {
var mismatch bool
if lease, mismatch = s.checkLease(mac, reqIP); mismatch {
return nil, true
}

Expand Down

0 comments on commit 1e0f9c1

Please sign in to comment.