#
# $Id$
+
+# *****************
+# * DHCP DISCOVER *
+# *****************
+
#
# SQLite does not implement SELECT FOR UPDATE which is normally used to place
# an exclusive lock over rows to prevent the same address from being
# This series of queries allocates an IP address - used in response to
# DHCP Discover packets
#
-# Either pull the most recent allocated IP for this client or the
-# oldest expired one. The first sub query returns the most recent
-# lease for the client (if there is one), the second returns the
-# oldest expired one.
-# Sorting the result by expiry_time DESC will return the client specific
-# IP if it exists, otherwise an expired one.
+
#
-alloc_find = "\
- SELECT framedipaddress, expiry_time \
- FROM ( \
- SELECT framedipaddress, expiry_time \
- FROM ${ippool_table} \
- JOIN dhcpstatus \
- ON ${ippool_table}.status_id = dhcpstatus.status_id \
- WHERE pool_name = '%{control:${pool_name}}' \
- AND pool_key = '${pool_key}' \
- AND status IN ('dynamic', 'static') \
- ORDER BY expiry_time DESC \
- LIMIT 1 \
- ) UNION \
- SELECT framedipaddress, expiry_time \
- FROM ( \
- SELECT framedipaddress, expiry_time \
- FROM ${ippool_table} \
- JOIN dhcpstatus \
- ON ${ippool_table}.status_id = dhcpstatus.status_id \
- WHERE pool_name = '%{control:${pool_name}}' \
- AND expiry_time < datetime('now') \
- AND status = 'dynamic' \
- ORDER BY expiry_time LIMIT 1 \
- ) \
- ORDER BY \
- expiry_time DESC \
+# Is there an exsiting address for this client
+#
+alloc_existing = "SELECT framedipaddress \
+ FROM ${ippool_table} \
+ JOIN dhcpstatus \
+ ON ${ippool_table}.status_id = dhcpstatus.status_id \
+ WHERE pool_name = '%{control:${pool_name}}' \
+ AND pool_key = '${pool_key}' \
+ AND status IN ('dynamic', 'static') \
+ ORDER BY expiry_time DESC \
LIMIT 1"
+#
+# If the client has requested an address, is it available
+#
+alloc_requested = "SELECT framedipaddress \
+ FROM ${ippool_table} \
+ JOIN dhcpstatus \
+ ON ${ippool_table}.status_id = dhcpstatus.status_id \
+ WHERE pool_name = '%{control:${pool_name}}' \
+ AND framedipaddress = '%{%{${req_attribute_name}}:-0.0.0.0}' \
+ AND status = 'dynamic'"
+
+#
+# Finally find a free address
+#
+alloc_find = "\
+ SELECT framedipaddress \
+ FROM ${ippool_table} \
+ JOIN dhcpstatus \
+ ON ${ippool_table}.status_id = dhcpstatus.status_id \
+ WHERE pool_name = '%{control:${pool_name}}' \
+ AND expiry_time < datetime('now') \
+ AND status = 'dynamic' \
+ ORDER BY expiry_time LIMIT 1"
+
#
# If you prefer to allocate a random IP address every time, use this query instead
#
# SELECT framedipaddress FROM ${ippool_table} \
# JOIN dhcpstatus \
# ON ${ippool_table}.status_id = dhcpstatus.status_id \
-# WHERE pool_name = '%{control:Pool-Name}' \
+# WHERE pool_name = '%{control:${pool-name}}' \
# AND expiry_time IS NULL \
# AND status = 'dynamic' \
# ORDER BY RAND() \
-# LIMIT 1 \
-# FOR UPDATE"
+# LIMIT 1"
+
#
# If an IP could not be allocated, check to see if the pool exists or not
SET \
gateway = '%{DHCP-Gateway-IP-Address}', \
pool_key = '${pool_key}', \
- expiry_time = datetime(strftime('%%s', 'now') + ${lease_duration}, 'unixepoch') \
+ expiry_time = datetime(strftime('%%s', 'now') + ${offer_duration}, 'unixepoch') \
WHERE framedipaddress = '%I' \
AND expiry_time IS NULL"
+
+# ****************
+# * DHCP REQUEST *
+# ****************
+
+#
+# Query used to clear any other addresses that have been offered to the client
+#
+update_free = "\
+ UPDATE ${ippool_table} \
+ SET \
+ gateway = '', \
+ pool_key = '', \
+ expiry_time = datetime('now') \
+ WHERE pool_name = '%{control:${pool_name}}' \
+ AND pool_key = '${pool_key}' \
+ AND framedipaddress <> '%{DHCP-Requested-IP-Address}' \
+ AND expiry_time > datetime('now') \
+ AND ${ippool_table}.status_id IN \
+ (SELECT status_id FROM dhcpstatus WHERE status = 'dynamic')"
+
#
# Queries to update a lease - used in response to DHCP-Request packets
#
AND pool_key = '${pool_key}' \
AND framedipaddress = '%{%{DHCP-Requested-IP-Address}:-%{DHCP-Client-IP-Address}}'"
+
+# ****************
+# * DHCP RELEASE *
+# ****************
+
#
# Queries to release a lease - used in response to DHCP-Release packets
#
expiry_time = datetime('now') \
WHERE pool_name = '%{control:${pool_name}}' \
AND pool_key = '${pool_key}' \
- AND framedipaddress = '%{DHCP-Client-IP-Address}'"
+ AND framedipaddress = '%{DHCP-Client-IP-Address}' \
+ AND ${ippool_table}.status_id IN \
+ (SELECT status_id FROM dhcpstatus WHERE status = 'dynamic')"
+
+
+# ****************
+# * DHCP DECLINE *
+# ****************
#
# Queries to mark leases as "bad" - used in response to DHCP-Decline
WHERE pool_name = '%{control:${pool_name}}' \
AND framedipaddress = '%{DHCP-Requested-IP-Address}' \
AND pool_key = '${pool_key}'"
-