AND nasipaddress = '%{Nas-IP-Address}'"
#
-# The ORDER BY clause of this query tries to allocate the same IP-address
-# which user had last session...
+# Search for the SAME Calling-Station-ID as last time, OR an entry
+# where there is no Calling-Station-Id.
+#
+# If the lease was expired, the allocate_clear above will reset the
+# Calling-Station-Id to ''.
+#
+# If the lease wasn't expired, we want to give the user the same IP
+# as last time.
+#
+# Then, we order by expiry_time, so that when there is no existing
+# entry for the Calling-Station-Id, it picks the OLDEST expired IP.
#
allocate_find = "\
SELECT framedipaddress \
FROM ${ippool_table} \
WHERE pool_name = '%{control:Pool-Name}' \
- AND (expiry_time < NOW() OR expiry_time IS NULL) \
+ AND (callingstationid = '%{Calling-Station-Id}' or callingstationid = '') \
ORDER BY \
- (username <> '%{User-Name}'), \
- (callingstationid <> '%{Calling-Station-Id}'), \
expiry_time \
LIMIT 1 \
FOR UPDATE"