From: Terry Burton Date: Tue, 8 Sep 2020 18:08:37 +0000 (+0100) Subject: sqlippools: allocate_requested queries for SQLite X-Git-Tag: release_3_0_22~431 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4e4b2a74f14ad464dc8cbdcdb41bfe2ad84103b;p=thirdparty%2Ffreeradius-server.git sqlippools: allocate_requested queries for SQLite --- diff --git a/raddb/mods-config/sql/ippool-dhcp/sqlite/queries.conf b/raddb/mods-config/sql/ippool-dhcp/sqlite/queries.conf index 611147b4fb2..d1e73ed5890 100644 --- a/raddb/mods-config/sql/ippool-dhcp/sqlite/queries.conf +++ b/raddb/mods-config/sql/ippool-dhcp/sqlite/queries.conf @@ -25,6 +25,47 @@ allocate_begin = "BEGIN EXCLUSIVE" allocate_commit = "COMMIT" +# +# Attempt to find the most recent existing IP address for the client +# +allocate_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" + +# +# Determine whether the requested IP address is available +# +allocate_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' \ + AND ( pool_key = '${pool_key}' OR expiry_time < datetime('now') )" + +# +# If the existing address can't be found this query will be run to +# find a free address +# +allocate_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" + # # This series of queries allocates an IP address # @@ -35,33 +76,43 @@ allocate_commit = "COMMIT" # Sorting the result by expiry_time DESC will return the client specific # IP if it exists, otherwise an expired one. # -allocate_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 \ - LIMIT 1" +#allocate_find = "\ +# SELECT framedipaddress, 1 AS o \ +# FROM ( \ +# 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 \ +# ) UNION \ +# SELECT framedipaddress, 2 AS o \ +# FROM ( \ +# 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' \ +# AND ( pool_key = '${pool_key}' OR expiry_time < datetime('now') ) \ +# ) UNION \ +# SELECT framedipaddress, 3 AS o \ +# FROM ( \ +# 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 \ +# ) \ +# ORDER BY o \ +# LIMIT 1" # # If you prefer to allocate a random IP address every time, i diff --git a/raddb/mods-config/sql/ippool-dhcp/sqlite/schema.sql b/raddb/mods-config/sql/ippool-dhcp/sqlite/schema.sql index 18c34aa1e35..d0174a3bdfd 100644 --- a/raddb/mods-config/sql/ippool-dhcp/sqlite/schema.sql +++ b/raddb/mods-config/sql/ippool-dhcp/sqlite/schema.sql @@ -4,7 +4,7 @@ CREATE TABLE dhcpstatus ( status_id int PRIMARY KEY, status varchar(10) NOT NULL -) +); INSERT INTO dhcpstatus (status_id, status) VALUES (1, 'dynamic'), (2, 'static'), (3, 'declined'), (4, 'disabled');