* dhcp: don't allocate a new lease when processing Request message

This commit is contained in:
Simon Zolin 2019-03-07 16:57:20 +03:00
parent d832d7ce95
commit 542a67b84e
2 changed files with 5 additions and 18 deletions

View File

@ -369,7 +369,6 @@ func (s *Server) handleDiscover(p dhcp4.Packet, options dhcp4.Options) dhcp4.Pac
func (s *Server) handleDHCP4Request(p dhcp4.Packet, options dhcp4.Options) dhcp4.Packet { func (s *Server) handleDHCP4Request(p dhcp4.Packet, options dhcp4.Options) dhcp4.Packet {
var lease *Lease var lease *Lease
var err error
reqIP := net.IP(options[dhcp4.OptionRequestedIPAddress]) reqIP := net.IP(options[dhcp4.OptionRequestedIPAddress])
log.Tracef("Message from client: Request. IP: %s ReqIP: %s HW: %s", log.Tracef("Message from client: Request. IP: %s ReqIP: %s HW: %s",
@ -395,12 +394,8 @@ func (s *Server) handleDHCP4Request(p dhcp4.Packet, options dhcp4.Options) dhcp4
lease = s.findLease(p) lease = s.findLease(p)
if lease == nil { if lease == nil {
lease, err = s.reserveLease(p) log.Tracef("Lease for %s isn't found", p.CHAddr())
if err != nil { return dhcp4.ReplyPacket(p, dhcp4.NAK, s.ipnet.IP, nil, 0, nil)
log.Tracef("Couldn't find free lease: %s", err)
// couldn't find lease, don't respond
return nil
}
} }
if !lease.IP.Equal(reqIP) { if !lease.IP.Equal(reqIP) {

View File

@ -43,11 +43,7 @@ func TestDHCP(t *testing.T) {
lease, _ = s.reserveLease(p) lease, _ = s.reserveLease(p)
check(t, bytes.Equal(lease.HWAddr, hw), "lease.HWAddr") check(t, bytes.Equal(lease.HWAddr, hw), "lease.HWAddr")
check(t, bytes.Equal(lease.IP, []byte{1, 1, 1, 1}), "lease.IP") check(t, bytes.Equal(lease.IP, []byte{1, 1, 1, 1}), "lease.IP")
lease = s.findLease(p)
// Try to reserve another IP for the same machine - no new IP must be reserved
hw = []byte{1, 2, 3, 4, 5, 6}
p.SetCHAddr(hw)
lease, _ = s.reserveLease(p)
check(t, bytes.Equal(lease.HWAddr, hw), "lease.HWAddr") check(t, bytes.Equal(lease.HWAddr, hw), "lease.HWAddr")
check(t, bytes.Equal(lease.IP, []byte{1, 1, 1, 1}), "lease.IP") check(t, bytes.Equal(lease.IP, []byte{1, 1, 1, 1}), "lease.IP")
@ -100,7 +96,7 @@ func misc(t *testing.T, s *Server) {
p = make(dhcp4.Packet, 241) p = make(dhcp4.Packet, 241)
// Commit a lease for an IP without prior Discover-Offer packets // Try to commit a lease for an IP without prior Discover-Offer packets
hw = []byte{2, 2, 3, 4, 5, 6} hw = []byte{2, 2, 3, 4, 5, 6}
p.SetCHAddr(hw) p.SetCHAddr(hw)
p.SetCIAddr([]byte{0, 0, 0, 0}) p.SetCIAddr([]byte{0, 0, 0, 0})
@ -108,11 +104,7 @@ func misc(t *testing.T, s *Server) {
opt[dhcp4.OptionRequestedIPAddress] = []byte{1, 1, 1, 1} opt[dhcp4.OptionRequestedIPAddress] = []byte{1, 1, 1, 1}
p2 = s.handleDHCP4Request(p, opt) p2 = s.handleDHCP4Request(p, opt)
opt = p2.ParseOptions() opt = p2.ParseOptions()
check(t, bytes.Equal(opt[dhcp4.OptionDHCPMessageType], []byte{byte(dhcp4.ACK)}), "dhcp4.ACK") check(t, bytes.Equal(opt[dhcp4.OptionDHCPMessageType], []byte{byte(dhcp4.NAK)}), "dhcp4.NAK")
check(t, bytes.Equal(p2.YIAddr(), []byte{1, 1, 1, 1}), "p2.YIAddr")
check(t, bytes.Equal(p2.CHAddr(), hw), "p2.CHAddr")
check(t, bytes.Equal(opt[dhcp4.OptionIPAddressLeaseTime], dhcp4.OptionsLeaseTime(5*time.Second)), "OptionIPAddressLeaseTime")
check(t, bytes.Equal(opt[dhcp4.OptionServerIdentifier], s.ipnet.IP), "OptionServerIdentifier")
} }
// Leases database store/load // Leases database store/load