InetAddress selectionBase, long requestedLeaseTime, OptionsField options ) throws DhcpException
{
// try to find existing lease. if we don't find a lease based on the
// client's
// hardware address, we send a NAK.
Lease lease = null;
lease = findExistingLease( hardwareAddress, lease );
if ( null == lease )
{
return null;
}
// check whether the notions of the client address match
if ( !lease.getClientAddress().equals( requestedAddress ) )
{
logger.warn( "Requested address " + requestedAddress + " for " + hardwareAddress
+ " doesn't match existing lease " + lease );
return null;
}
// check whether addresses and subnet match
Subnet subnet = findSubnet( selectionBase );
if ( null == subnet )
{
logger.warn( "No subnet found for existing lease " + lease );
return null;
}
if ( !subnet.contains( lease.getClientAddress() ) )
{
logger.warn( "Client with existing lease " + lease + " is on wrong subnet " + subnet );
return null;
}
if ( !subnet.isInRange( lease.getClientAddress() ) )
{
logger.warn( "Client with existing lease " + lease + " is out of valid range for subnet " + subnet );
return null;
}
// build properties map
Map properties = getProperties( subnet );
// update lease options
OptionsField o = lease.getOptions();
o.clear();
// add subnet settings
o.add( new SubnetMask( subnet.getNetmask() ) );
o.merge( subnet.getOptions() );
// check whether there is a designated host.
Host host = findDesignatedHost( hardwareAddress );
if ( null != host )
{
// check whether the host matches the address (using a fixed
// host address is mandatory).
if ( host.getAddress() != null && !host.getAddress().equals( lease.getClientAddress() ) )
{
logger.warn( "Existing fixed address for " + hardwareAddress + " conflicts with existing lease "
+ lease );
return null;
}
properties.putAll( getProperties( host ) );
// set (client) host name
o.add( new HostName( host.getName() ) );
// add the host's options
o.merge( host.getOptions() );
}
// update other lease fields
long leaseTime = determineLeaseTime( requestedLeaseTime, properties );
lease.setExpires( System.currentTimeMillis() + leaseTime );
lease.setHardwareAddress( hardwareAddress );
// update the lease state
if ( lease.getState() != Lease.STATE_ACTIVE )
{
lease.setState( Lease.STATE_ACTIVE );
updateLease( lease );
}
// store information about the lease
updateLease( lease );