Pull request: dhcpd: actually use hostname method

Updates #2582.

Squashed commit of the following:

commit 456bbafa5a7e84e30d5a5c0ee58f5bacebb2b003
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Mar 12 13:23:32 2021 +0300

    dhcpd: imp more

commit 69258ed0d36e59f6be3c31250ea6fd37a1e4cb53
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Mar 12 13:01:18 2021 +0300

    dhcpd: actually use hostname method
This commit is contained in:
Ainar Garipov 2021-03-12 13:34:43 +03:00
parent 968831c5b9
commit bfbf73f3cd

View file

@ -379,9 +379,8 @@ func (s *v4Server) processDiscover(req, resp *dhcpv4.DHCPv4) *Lease {
s.conf.notify(LeaseChangedDBStore) s.conf.notify(LeaseChangedDBStore)
} else { } else {
reqIP := req.Options.Get(dhcpv4.OptionRequestedIPAddress) reqIP := req.RequestedIPAddress()
if len(reqIP) != 0 && if len(reqIP) != 0 && !reqIP.Equal(lease.IP) {
!bytes.Equal(reqIP, lease.IP) {
log.Debug("dhcpv4: different RequestedIP: %v != %v", reqIP, lease.IP) log.Debug("dhcpv4: different RequestedIP: %v != %v", reqIP, lease.IP)
} }
} }
@ -424,46 +423,49 @@ func (o *optFQDN) ToBytes() []byte {
func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (*Lease, bool) { func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (*Lease, bool) {
var lease *Lease var lease *Lease
mac := req.ClientHWAddr mac := req.ClientHWAddr
hostname := req.Options.Get(dhcpv4.OptionHostName) reqIP := req.RequestedIPAddress()
reqIP := req.Options.Get(dhcpv4.OptionRequestedIPAddress)
if reqIP == nil { if reqIP == nil {
reqIP = req.ClientIPAddr reqIP = req.ClientIPAddr
} }
sid := req.Options.Get(dhcpv4.OptionServerIdentifier) sid := req.ServerIdentifier()
if len(sid) != 0 && if len(sid) != 0 && !sid.Equal(s.conf.dnsIPAddrs[0]) {
!bytes.Equal(sid, s.conf.dnsIPAddrs[0]) { log.Debug("dhcpv4: bad OptionServerIdentifier in request message for %s", mac)
log.Debug("dhcpv4: Bad OptionServerIdentifier in Request message for %s", mac)
return nil, false return nil, false
} }
if len(reqIP) != 4 { if ip4 := reqIP.To4(); ip4 == nil {
log.Debug("dhcpv4: Bad OptionRequestedIPAddress in Request message for %s", mac) log.Debug("dhcpv4: bad OptionRequestedIPAddress in request message for %s", mac)
return nil, false return nil, false
} }
s.leasesLock.Lock() s.leasesLock.Lock()
for _, l := range s.leases { for _, l := range s.leases {
if bytes.Equal(l.HWAddr, mac) { if bytes.Equal(l.HWAddr, mac) {
if !bytes.Equal(l.IP, reqIP) { if !l.IP.Equal(reqIP) {
s.leasesLock.Unlock() s.leasesLock.Unlock()
log.Debug("dhcpv4: Mismatched OptionRequestedIPAddress in Request message for %s", mac) log.Debug("dhcpv4: mismatched OptionRequestedIPAddress in request message for %s", mac)
return nil, true return nil, true
} }
lease = l lease = l
break break
} }
} }
s.leasesLock.Unlock() s.leasesLock.Unlock()
if lease == nil { if lease == nil {
log.Debug("dhcpv4: No lease for %s", mac) log.Debug("dhcpv4: no lease for %s", mac)
return nil, true return nil, true
} }
if lease.Expiry.Unix() != leaseExpireStatic { if lease.Expiry.Unix() != leaseExpireStatic {
lease.Hostname = string(hostname) lease.Hostname = req.HostName()
s.commitLease(lease) s.commitLease(lease)
} else if len(lease.Hostname) != 0 { } else if len(lease.Hostname) != 0 {
o := &optFQDN{ o := &optFQDN{
@ -473,10 +475,12 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (*Lease, bool) {
Code: dhcpv4.OptionFQDN, Code: dhcpv4.OptionFQDN,
Value: o, Value: o,
} }
resp.UpdateOption(fqdn) resp.UpdateOption(fqdn)
} }
resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck)) resp.UpdateOption(dhcpv4.OptMessageType(dhcpv4.MessageTypeAck))
return lease, true return lease, true
} }