]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
sqlippools: allocate_requested queries for SQLite
authorTerry Burton <tez@terryburton.co.uk>
Tue, 8 Sep 2020 18:08:37 +0000 (19:08 +0100)
committerAlan DeKok <aland@freeradius.org>
Thu, 10 Sep 2020 12:56:41 +0000 (08:56 -0400)
raddb/mods-config/sql/ippool-dhcp/sqlite/queries.conf
raddb/mods-config/sql/ippool-dhcp/sqlite/schema.sql

index 611147b4fb2aed03dfb84729c18306a9b9168254..d1e73ed58904f37c6a6069fa6ed383909e6af459 100644 (file)
 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
index 18c34aa1e3588d5cc9f4fa3b89c1ae7d257ff3fe..d0174a3bdfdad22451f7eda3020732d4526bfa4b 100644 (file)
@@ -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');