From: Francis Dupont Date: Wed, 16 Oct 2019 17:02:43 +0000 (+0200) Subject: [393-global-search-through-leases-by-mac-or-hostname-w-o-specifying-a-subnet-id]... X-Git-Tag: Kea-1.7.1~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5bd76e38b525e989cebf424043d931c2e0a1d4c7;p=thirdparty%2Fkea.git [393-global-search-through-leases-by-mac-or-hostname-w-o-specifying-a-subnet-id] Added get leases 4 and 4 by hostname to the API --- diff --git a/src/lib/dhcpsrv/cql_lease_mgr.cc b/src/lib/dhcpsrv/cql_lease_mgr.cc index 3bea54355b..55913f554d 100644 --- a/src/lib/dhcpsrv/cql_lease_mgr.cc +++ b/src/lib/dhcpsrv/cql_lease_mgr.cc @@ -231,6 +231,8 @@ public: static constexpr StatementTag GET_LEASE4_PAGE = "GET_LEASE4_PAGE"; // Get lease4 by subnet ID static constexpr StatementTag GET_LEASE4_SUBID = "GET_LEASE4_SUBID"; + // Get lease4 by hostname + static constexpr StatementTag GET_LEASE4_HOSTNAME = "GET_LEASE4_HOSTNAME"; /// @} private: @@ -255,6 +257,7 @@ constexpr StatementTag CqlLease4Exchange::GET_LEASE4_HWADDR_SUBID; constexpr StatementTag CqlLease4Exchange::GET_LEASE4_LIMIT; constexpr StatementTag CqlLease4Exchange::GET_LEASE4_PAGE; constexpr StatementTag CqlLease4Exchange::GET_LEASE4_SUBID; +constexpr StatementTag CqlLease4Exchange::GET_LEASE4_HOSTNAME; StatementMap CqlLease4Exchange::tagged_statements_{ @@ -393,6 +396,16 @@ StatementMap CqlLease4Exchange::tagged_statements_{ "fqdn_fwd, fqdn_rev, hostname, state, user_context " "FROM lease4 " "WHERE subnet_id = ? " + "ALLOW FILTERING "}}, + + // Gets an IPv4 lease(s) with specified hostname + {GET_LEASE4_HOSTNAME, + {GET_LEASE4_HOSTNAME, + "SELECT " + "address, hwaddr, client_id, valid_lifetime, expire, subnet_id, " + "fqdn_fwd, fqdn_rev, hostname, state, user_context " + "FROM lease4 " + "WHERE hostname = ? " "ALLOW FILTERING "}} }; @@ -908,6 +921,7 @@ public: static constexpr StatementTag GET_LEASE6_DUID_IAID_SUBID = "GET_LEASE6_DUID_IAID_SUBID"; static constexpr StatementTag GET_LEASE6_LIMIT = "GET_LEASE6_LIMIT"; static constexpr StatementTag GET_LEASE6_PAGE = "GET_LEASE6_PAGE"; + static constexpr StatementTag GET_LEASE6_HOSTNAME = "GET_LEASE6_HOSTNAME"; // @} private: @@ -949,6 +963,7 @@ constexpr StatementTag CqlLease6Exchange::GET_LEASE6_DUID_IAID; constexpr StatementTag CqlLease6Exchange::GET_LEASE6_DUID_IAID_SUBID; constexpr StatementTag CqlLease6Exchange::GET_LEASE6_LIMIT; constexpr StatementTag CqlLease6Exchange::GET_LEASE6_PAGE; +constexpr StatementTag CqlLease6Exchange::GET_LEASE6_HOSTNAME; StatementMap CqlLease6Exchange::tagged_statements_ = { @@ -1076,7 +1091,20 @@ StatementMap CqlLease6Exchange::tagged_statements_ = { "FROM lease6 " "WHERE TOKEN(address) > TOKEN(?) " "LIMIT ? " - "ALLOW FILTERING "}} + "ALLOW FILTERING "}}, + + // Gets an IPv6 lease(s) with specified hostname + {GET_LEASE6_HOSTNAME, + {GET_LEASE6_HOSTNAME, + "SELECT " + "address, valid_lifetime, expire, subnet_id, pref_lifetime, duid, iaid, " + "lease_type, prefix_len, fqdn_fwd, fqdn_rev, hostname, hwaddr, hwtype, " + "hwaddr_source, state, user_context " + "FROM lease6 " + "WHERE hostname = ? " + "ALLOW FILTERING "}}, + + }; CqlLease6Exchange::CqlLease6Exchange(const CqlConnection &connection) @@ -2216,6 +2244,25 @@ CqlLeaseMgr::getLeases4(SubnetID subnet_id) const { return (result); } +Lease4Collection +CqlLeaseMgr::getLeases4(const std::string& hostname) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_HOSTNAME4) + .arg(hostname); + + // Set up the WHERE clause value + AnyArray data; + + std::string hostname_data(hostname); + data.add(&hostname_data); + + // Get the data. + Lease4Collection result; + std::unique_ptr exchange4(new CqlLease4Exchange(dbconn_)); + exchange4->getLeaseCollection(CqlLease4Exchange::GET_LEASE4_HOSTNAME, data, result); + + return (result); +} + Lease4Collection CqlLeaseMgr::getLeases4() const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET4); @@ -2311,7 +2358,7 @@ CqlLeaseMgr::getLeases6(const DUID& duid) const { // Set up the WHERE clause value AnyArray data; - + CassBlob duid_data(duid.getDuid()); data.add(&duid_data); @@ -2389,6 +2436,25 @@ CqlLeaseMgr::getLeases6(SubnetID) const { isc_throw(NotImplemented, "getLeases6(subnet_id) is not implemented"); } +Lease6Collection +CqlLeaseMgr::getLeases6(const std::string& hostname) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_HOSTNAME6) + .arg(hostname); + + // Set up the WHERE clause value + AnyArray data; + + std::string hostname_data(hostname); + data.add(&hostname_data); + + // Get the data. + Lease6Collection result; + std::unique_ptr exchange6(new CqlLease6Exchange(dbconn_)); + exchange6->getLeaseCollection(CqlLease6Exchange::GET_LEASE6_HOSTNAME, data, result); + + return (result); +} + Lease6Collection CqlLeaseMgr::getLeases6() const { isc_throw(NotImplemented, "getLeases6() is not implemented"); diff --git a/src/lib/dhcpsrv/cql_lease_mgr.h b/src/lib/dhcpsrv/cql_lease_mgr.h index ae042b3c46..4760a3134a 100644 --- a/src/lib/dhcpsrv/cql_lease_mgr.h +++ b/src/lib/dhcpsrv/cql_lease_mgr.h @@ -207,6 +207,13 @@ public: /// this backend. virtual Lease4Collection getLeases4(SubnetID subnet_id) const override; + /// @brief Returns all IPv4 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(const std::string& hostname) const override; + /// @brief Returns all IPv4 leases. /// /// @return Lease collection (may be empty if no IPv4 lease found). @@ -309,6 +316,13 @@ public: /// this backend. virtual Lease6Collection getLeases6(SubnetID subnet_id) const override; + /// @brief Returns all IPv6 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv6 lease found). + virtual Lease6Collection getLeases6(const std::string& hostname) const override; + /// @brief Returns all IPv6 leases. /// /// @return Lease collection (may be empty if no IPv6 lease found). @@ -320,7 +334,7 @@ public: /// /// @return Lease collection (may be empty if no IPv6 lease found). virtual Lease6Collection getLeases6(const DUID& duid) const override; - + /// @brief Returns range of IPv6 leases using paging. /// /// This method implements paged browsing of the lease database. The first diff --git a/src/lib/dhcpsrv/dhcpsrv_messages.cc b/src/lib/dhcpsrv/dhcpsrv_messages.cc index 08c0d9dfac..b866cfe2e4 100644 --- a/src/lib/dhcpsrv/dhcpsrv_messages.cc +++ b/src/lib/dhcpsrv/dhcpsrv_messages.cc @@ -1,4 +1,4 @@ -// File created from ../../../src/lib/dhcpsrv/dhcpsrv_messages.mes on Mon Sep 30 2019 13:41 +// File created from ../../../src/lib/dhcpsrv/dhcpsrv_messages.mes on Wed Oct 16 2019 17:52 #include #include @@ -63,6 +63,8 @@ extern const isc::log::MessageID DHCPSRV_CQL_GET_CLIENTID = "DHCPSRV_CQL_GET_CLI extern const isc::log::MessageID DHCPSRV_CQL_GET_CLIENTID_HWADDR_SUBID = "DHCPSRV_CQL_GET_CLIENTID_HWADDR_SUBID"; extern const isc::log::MessageID DHCPSRV_CQL_GET_EXPIRED4 = "DHCPSRV_CQL_GET_EXPIRED4"; extern const isc::log::MessageID DHCPSRV_CQL_GET_EXPIRED6 = "DHCPSRV_CQL_GET_EXPIRED6"; +extern const isc::log::MessageID DHCPSRV_CQL_GET_HOSTNAME4 = "DHCPSRV_CQL_GET_HOSTNAME4"; +extern const isc::log::MessageID DHCPSRV_CQL_GET_HOSTNAME6 = "DHCPSRV_CQL_GET_HOSTNAME6"; extern const isc::log::MessageID DHCPSRV_CQL_GET_HWADDR = "DHCPSRV_CQL_GET_HWADDR"; extern const isc::log::MessageID DHCPSRV_CQL_GET_IAID_DUID = "DHCPSRV_CQL_GET_IAID_DUID"; extern const isc::log::MessageID DHCPSRV_CQL_GET_IAID_SUBID_DUID = "DHCPSRV_CQL_GET_IAID_SUBID_DUID"; @@ -119,6 +121,8 @@ extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_CLIENTID = "DHCPSRV_MEMFILE extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_CLIENTID_HWADDR_SUBID = "DHCPSRV_MEMFILE_GET_CLIENTID_HWADDR_SUBID"; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_EXPIRED4 = "DHCPSRV_MEMFILE_GET_EXPIRED4"; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_EXPIRED6 = "DHCPSRV_MEMFILE_GET_EXPIRED6"; +extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_HOSTNAME4 = "DHCPSRV_MEMFILE_GET_HOSTNAME4"; +extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_HOSTNAME6 = "DHCPSRV_MEMFILE_GET_HOSTNAME6"; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_HWADDR = "DHCPSRV_MEMFILE_GET_HWADDR"; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_IAID_DUID = "DHCPSRV_MEMFILE_GET_IAID_DUID"; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID = "DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID"; @@ -169,6 +173,8 @@ extern const isc::log::MessageID DHCPSRV_MYSQL_GET_CLIENTID = "DHCPSRV_MYSQL_GET extern const isc::log::MessageID DHCPSRV_MYSQL_GET_DUID = "DHCPSRV_MYSQL_GET_DUID"; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_EXPIRED4 = "DHCPSRV_MYSQL_GET_EXPIRED4"; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_EXPIRED6 = "DHCPSRV_MYSQL_GET_EXPIRED6"; +extern const isc::log::MessageID DHCPSRV_MYSQL_GET_HOSTNAME4 = "DHCPSRV_MYSQL_GET_HOSTNAME4"; +extern const isc::log::MessageID DHCPSRV_MYSQL_GET_HOSTNAME6 = "DHCPSRV_MYSQL_GET_HOSTNAME6"; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_HWADDR = "DHCPSRV_MYSQL_GET_HWADDR"; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_IAID_DUID = "DHCPSRV_MYSQL_GET_IAID_DUID"; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_IAID_SUBID_DUID = "DHCPSRV_MYSQL_GET_IAID_SUBID_DUID"; @@ -207,6 +213,8 @@ extern const isc::log::MessageID DHCPSRV_PGSQL_GET_CLIENTID = "DHCPSRV_PGSQL_GET extern const isc::log::MessageID DHCPSRV_PGSQL_GET_DUID = "DHCPSRV_PGSQL_GET_DUID"; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_EXPIRED4 = "DHCPSRV_PGSQL_GET_EXPIRED4"; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_EXPIRED6 = "DHCPSRV_PGSQL_GET_EXPIRED6"; +extern const isc::log::MessageID DHCPSRV_PGSQL_GET_HOSTNAME4 = "DHCPSRV_PGSQL_GET_HOSTNAME4"; +extern const isc::log::MessageID DHCPSRV_PGSQL_GET_HOSTNAME6 = "DHCPSRV_PGSQL_GET_HOSTNAME6"; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_HWADDR = "DHCPSRV_PGSQL_GET_HWADDR"; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_IAID_DUID = "DHCPSRV_PGSQL_GET_IAID_DUID"; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_IAID_SUBID_DUID = "DHCPSRV_PGSQL_GET_IAID_SUBID_DUID"; @@ -299,6 +307,8 @@ const char* values[] = { "DHCPSRV_CQL_GET_CLIENTID_HWADDR_SUBID", "obtaining IPv4 lease for client ID %1, hardware address %2 and subnet ID %3", "DHCPSRV_CQL_GET_EXPIRED4", "obtaining maximum %1 of expired IPv4 leases", "DHCPSRV_CQL_GET_EXPIRED6", "obtaining maximum %1 of expired IPv6 leases", + "DHCPSRV_CQL_GET_HOSTNAME4", "obtaining IPv4 leases for hostname %1", + "DHCPSRV_CQL_GET_HOSTNAME6", "obtaining IPv6 leases for hostname %1", "DHCPSRV_CQL_GET_HWADDR", "obtaining IPv4 leases for hardware address %1", "DHCPSRV_CQL_GET_IAID_DUID", "obtaining IPv6 leases for IAID %1 and DUID %2 and lease type %3", "DHCPSRV_CQL_GET_IAID_SUBID_DUID", "obtaining IPv6 leases for IAID %1, Subnet ID %2, DUID %3 and lease type %4", @@ -355,6 +365,8 @@ const char* values[] = { "DHCPSRV_MEMFILE_GET_CLIENTID_HWADDR_SUBID", "obtaining IPv4 lease for client ID %1, hardware address %2 and subnet ID %3", "DHCPSRV_MEMFILE_GET_EXPIRED4", "obtaining maximum %1 of expired IPv4 leases", "DHCPSRV_MEMFILE_GET_EXPIRED6", "obtaining maximum %1 of expired IPv6 leases", + "DHCPSRV_MEMFILE_GET_HOSTNAME4", "obtaining IPv4 leases for hostname %1", + "DHCPSRV_MEMFILE_GET_HOSTNAME6", "obtaining IPv6 leases for hostname %1", "DHCPSRV_MEMFILE_GET_HWADDR", "obtaining IPv4 leases for hardware address %1", "DHCPSRV_MEMFILE_GET_IAID_DUID", "obtaining IPv6 leases for IAID %1 and DUID %2 and lease type %3", "DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID", "obtaining IPv6 leases for IAID %1, Subnet ID %2, DUID %3 and lease type %4", @@ -405,6 +417,8 @@ const char* values[] = { "DHCPSRV_MYSQL_GET_DUID", "obtaining IPv6 lease for duid %1,", "DHCPSRV_MYSQL_GET_EXPIRED4", "obtaining maximum %1 of expired IPv4 leases", "DHCPSRV_MYSQL_GET_EXPIRED6", "obtaining maximum %1 of expired IPv6 leases", + "DHCPSRV_MYSQL_GET_HOSTNAME4", "obtaining IPv4 leases for hostname %1", + "DHCPSRV_MYSQL_GET_HOSTNAME6", "obtaining IPv6 leases for hostname %1", "DHCPSRV_MYSQL_GET_HWADDR", "obtaining IPv4 leases for hardware address %1", "DHCPSRV_MYSQL_GET_IAID_DUID", "obtaining IPv6 leases for IAID %1, DUID %2, lease type %3", "DHCPSRV_MYSQL_GET_IAID_SUBID_DUID", "obtaining IPv6 leases for IAID %1, Subnet ID %2, DUID %3, lease type %4", @@ -443,6 +457,8 @@ const char* values[] = { "DHCPSRV_PGSQL_GET_DUID", "obtaining IPv6 leases for DUID %1,", "DHCPSRV_PGSQL_GET_EXPIRED4", "obtaining maximum %1 of expired IPv4 leases", "DHCPSRV_PGSQL_GET_EXPIRED6", "obtaining maximum %1 of expired IPv6 leases", + "DHCPSRV_PGSQL_GET_HOSTNAME4", "obtaining IPv4 leases for hostname %1", + "DHCPSRV_PGSQL_GET_HOSTNAME6", "obtaining IPv6 leases for hostname %1", "DHCPSRV_PGSQL_GET_HWADDR", "obtaining IPv4 leases for hardware address %1", "DHCPSRV_PGSQL_GET_IAID_DUID", "obtaining IPv4 leases for IAID %1 and DUID %2, lease type %3", "DHCPSRV_PGSQL_GET_IAID_SUBID_DUID", "obtaining IPv4 leases for IAID %1, Subnet ID %2, DUID %3, and lease type %4", diff --git a/src/lib/dhcpsrv/dhcpsrv_messages.h b/src/lib/dhcpsrv/dhcpsrv_messages.h index 79e8c072be..8fda345e74 100644 --- a/src/lib/dhcpsrv/dhcpsrv_messages.h +++ b/src/lib/dhcpsrv/dhcpsrv_messages.h @@ -1,4 +1,4 @@ -// File created from ../../../src/lib/dhcpsrv/dhcpsrv_messages.mes on Mon Sep 30 2019 13:41 +// File created from ../../../src/lib/dhcpsrv/dhcpsrv_messages.mes on Wed Oct 16 2019 17:52 #ifndef DHCPSRV_MESSAGES_H #define DHCPSRV_MESSAGES_H @@ -64,6 +64,8 @@ extern const isc::log::MessageID DHCPSRV_CQL_GET_CLIENTID; extern const isc::log::MessageID DHCPSRV_CQL_GET_CLIENTID_HWADDR_SUBID; extern const isc::log::MessageID DHCPSRV_CQL_GET_EXPIRED4; extern const isc::log::MessageID DHCPSRV_CQL_GET_EXPIRED6; +extern const isc::log::MessageID DHCPSRV_CQL_GET_HOSTNAME4; +extern const isc::log::MessageID DHCPSRV_CQL_GET_HOSTNAME6; extern const isc::log::MessageID DHCPSRV_CQL_GET_HWADDR; extern const isc::log::MessageID DHCPSRV_CQL_GET_IAID_DUID; extern const isc::log::MessageID DHCPSRV_CQL_GET_IAID_SUBID_DUID; @@ -120,6 +122,8 @@ extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_CLIENTID; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_CLIENTID_HWADDR_SUBID; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_EXPIRED4; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_EXPIRED6; +extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_HOSTNAME4; +extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_HOSTNAME6; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_HWADDR; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_IAID_DUID; extern const isc::log::MessageID DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID; @@ -170,6 +174,8 @@ extern const isc::log::MessageID DHCPSRV_MYSQL_GET_CLIENTID; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_DUID; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_EXPIRED4; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_EXPIRED6; +extern const isc::log::MessageID DHCPSRV_MYSQL_GET_HOSTNAME4; +extern const isc::log::MessageID DHCPSRV_MYSQL_GET_HOSTNAME6; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_HWADDR; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_IAID_DUID; extern const isc::log::MessageID DHCPSRV_MYSQL_GET_IAID_SUBID_DUID; @@ -208,6 +214,8 @@ extern const isc::log::MessageID DHCPSRV_PGSQL_GET_CLIENTID; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_DUID; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_EXPIRED4; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_EXPIRED6; +extern const isc::log::MessageID DHCPSRV_PGSQL_GET_HOSTNAME4; +extern const isc::log::MessageID DHCPSRV_PGSQL_GET_HOSTNAME6; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_HWADDR; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_IAID_DUID; extern const isc::log::MessageID DHCPSRV_PGSQL_GET_IAID_SUBID_DUID; diff --git a/src/lib/dhcpsrv/dhcpsrv_messages.mes b/src/lib/dhcpsrv/dhcpsrv_messages.mes index e8a544e1bc..0c2d19d29b 100644 --- a/src/lib/dhcpsrv/dhcpsrv_messages.mes +++ b/src/lib/dhcpsrv/dhcpsrv_messages.mes @@ -303,6 +303,16 @@ A debug message issued when the server is attempting to obtain expired IPv6 leases to reclaim them. The maximum number of leases to be retrieved is logged in the message. +% DHCPSRV_CQL_GET_HOSTNAME4 obtaining IPv4 leases for hostname %1 +A debug message issued when the server is attempting to obtain a set of +IPv4 leases from the Cassandra database for a client with the specified +hostname. + +% DHCPSRV_CQL_GET_HOSTNAME6 obtaining IPv6 leases for hostname %1 +A debug message issued when the server is attempting to obtain a set of +IPv6 leases from the Cassandra database for a client with the specified +hostname. + % DHCPSRV_CQL_GET_HWADDR obtaining IPv4 leases for hardware address %1 A debug message issued when the server is attempting to obtain a set of IPv4 leases from the Cassandra database for a client with the specified @@ -579,6 +589,16 @@ A debug message issued when the server is attempting to obtain expired IPv6 leases to reclaim them. The maximum number of leases to be retrieved is logged in the message. +% DHCPSRV_MEMFILE_GET_HOSTNAME4 obtaining IPv4 leases for hostname %1 +A debug message issued when the server is attempting to obtain a set of +IPv4 leases from the memory file database for a client with the specified +hostname. + +% DHCPSRV_MEMFILE_GET_HOSTNAME6 obtaining IPv6 leases for hostname %1 +A debug message issued when the server is attempting to obtain a set of +IPv6 leases from the memory file database for a client with the specified +hostname. + % DHCPSRV_MEMFILE_GET_HWADDR obtaining IPv4 leases for hardware address %1 A debug message issued when the server is attempting to obtain a set of IPv4 leases from the memory file database for a client with the specified @@ -830,6 +850,16 @@ A debug message issued when the server is attempting to obtain expired IPv6 leases to reclaim them. The maximum number of leases to be retrieved is logged in the message. +% DHCPSRV_MYSQL_GET_HOSTNAME4 obtaining IPv4 leases for hostname %1 +A debug message issued when the server is attempting to obtain a set +of IPv4 leases from the MySQL database for a client with the specified +hostname. + +% DHCPSRV_MYSQL_GET_HOSTNAME6 obtaining IPv6 leases for hostname %1 +A debug message issued when the server is attempting to obtain a set +of IPv6 leases from the MySQL database for a client with the specified +hostname. + % DHCPSRV_MYSQL_GET_HWADDR obtaining IPv4 leases for hardware address %1 A debug message issued when the server is attempting to obtain a set of IPv4 leases from the MySQL database for a client with the specified @@ -1014,6 +1044,16 @@ A debug message issued when the server is attempting to obtain expired IPv6 leases to reclaim them. The maximum number of leases to be retrieved is logged in the message. +% DHCPSRV_PGSQL_GET_HOSTNAME4 obtaining IPv4 leases for hostname %1 +A debug message issued when the server is attempting to obtain a set +of IPv4 leases from the PostgreSQL database for a client with the specified +hostname. + +% DHCPSRV_PGSQL_GET_HOSTNAME6 obtaining IPv6 leases for hostname %1 +A debug message issued when the server is attempting to obtain a set +of IPv6 leases from the PostgreSQL database for a client with the specified +hostname. + % DHCPSRV_PGSQL_GET_HWADDR obtaining IPv4 leases for hardware address %1 A debug message issued when the server is attempting to obtain a set of IPv4 leases from the PostgreSQL database for a client with the specified diff --git a/src/lib/dhcpsrv/lease_mgr.h b/src/lib/dhcpsrv/lease_mgr.h index 233f3539c8..eab1f64494 100644 --- a/src/lib/dhcpsrv/lease_mgr.h +++ b/src/lib/dhcpsrv/lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2019 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -344,6 +344,13 @@ public: /// @return Lease collection (may be empty if no IPv4 lease found). virtual Lease4Collection getLeases4(SubnetID subnet_id) const = 0; + /// @brief Returns all IPv4 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(const std::string& hostname) const = 0; + /// @brief Returns all IPv4 leases. /// /// @return Lease collection (may be empty if no IPv4 lease found). @@ -452,6 +459,13 @@ public: /// @return Lease collection (may be empty if no IPv6 lease found). virtual Lease6Collection getLeases6(SubnetID subnet_id) const = 0; + /// @brief Returns all IPv6 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv6 lease found). + virtual Lease6Collection getLeases6(const std::string& hostname) const = 0; + /// @brief Returns all IPv6 leases. /// /// @return Lease collection (may be empty if no IPv6 lease found). @@ -459,9 +473,9 @@ public: /// @brief Returns collection of leases for matching DUID /// - /// @return Lease collection + /// @return Lease collection /// (may be empty if no IPv6 lease found for the DUID). - virtual Lease6Collection getLeases6(const DUID& duid) const = 0; + virtual Lease6Collection getLeases6(const DUID& duid) const = 0; /// @brief Returns range of IPv6 leases using paging. /// diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.cc b/src/lib/dhcpsrv/memfile_lease_mgr.cc index 93f84a5d64..719ddb091e 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.cc +++ b/src/lib/dhcpsrv/memfile_lease_mgr.cc @@ -871,6 +871,24 @@ Memfile_LeaseMgr::getLeases4(SubnetID subnet_id) const { return (collection); } +Lease4Collection +Memfile_LeaseMgr::getLeases4(const std::string& hostname) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_GET_HOSTNAME4) + .arg(hostname); + + Lease4Collection collection; + const Lease4StorageHostnameIndex& idx = storage4_.get(); + std::pair l = + idx.equal_range(hostname); + + for (auto lease = l.first; lease != l.second; ++lease) { + collection.push_back(Lease4Ptr(new Lease4(**lease))); + } + + return (collection); +} + Lease4Collection Memfile_LeaseMgr::getLeases4() const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_GET4); @@ -1002,6 +1020,24 @@ Memfile_LeaseMgr::getLeases6(SubnetID subnet_id) const { return (collection); } +Lease6Collection +Memfile_LeaseMgr::getLeases6(const std::string& hostname) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_GET_HOSTNAME6) + .arg(hostname); + + Lease6Collection collection; + const Lease6StorageHostnameIndex& idx = storage6_.get(); + std::pair l = + idx.equal_range(hostname); + + for (auto lease = l.first; lease != l.second; ++lease) { + collection.push_back(Lease6Ptr(new Lease6(**lease))); + } + + return (collection); +} + Lease6Collection Memfile_LeaseMgr::getLeases6() const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_GET6); diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.h b/src/lib/dhcpsrv/memfile_lease_mgr.h index 49dbf3e0bf..b0832010ae 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.h +++ b/src/lib/dhcpsrv/memfile_lease_mgr.h @@ -229,6 +229,13 @@ public: /// @return Lease collection (may be empty if no IPv4 lease found). virtual Lease4Collection getLeases4(SubnetID subnet_id) const; + /// @brief Returns all IPv4 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(const std::string& hostname) const; + /// @brief Returns all IPv4 leases. /// /// @return Lease collection (may be empty if no IPv4 lease found). @@ -308,6 +315,13 @@ public: /// @return Lease collection (may be empty if no IPv6 lease found). virtual Lease6Collection getLeases6(SubnetID subnet_id) const; + /// @brief Returns all IPv6 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv6 lease found). + virtual Lease6Collection getLeases6(const std::string& hostname) const; + /// @brief Returns all IPv6 leases. /// /// @return Lease collection (may be empty if no IPv6 lease found). @@ -316,10 +330,10 @@ public: /// @brief Returns IPv6 leases for the DUID. /// /// @todo: implement an optimised of the query using index. - /// @return Lease collection (may be empty if no IPv6 lease found) + /// @return Lease collection (may be empty if no IPv6 lease found) /// for the DUID. virtual Lease6Collection getLeases6(const DUID& duid) const; - + /// @brief Returns range of IPv6 leases using paging. /// /// This method implements paged browsing of the lease database. The first diff --git a/src/lib/dhcpsrv/memfile_lease_storage.h b/src/lib/dhcpsrv/memfile_lease_storage.h index 3254eefe3a..fd412e7b74 100644 --- a/src/lib/dhcpsrv/memfile_lease_storage.h +++ b/src/lib/dhcpsrv/memfile_lease_storage.h @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-2019 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -46,7 +46,11 @@ struct ClientIdHWAddressSubnetIdIndexTag { }; struct SubnetIdIndexTag { }; /// @brief Tag for index using DUID. -struct DuidIndexTag { }; +struct DuidIndexTag { }; + +/// @brief Tag for index using hostname. +struct HostnameIndexTag { }; + /// @name Multi index containers holding DHCPv4 and DHCPv6 leases. /// //@{ @@ -125,6 +129,13 @@ typedef boost::multi_index_container< boost::multi_index::const_mem_fun&, &Lease6::getDuidVector> + >, + + // Specification of the sixth index starts here + // This index is used to retrieve leases for matching hostname. + boost::multi_index::ordered_non_unique< + boost::multi_index::tag, + boost::multi_index::member > > > Lease6Storage; // Specify the type name of this container. @@ -239,8 +250,15 @@ typedef boost::multi_index_container< boost::multi_index::ordered_non_unique< boost::multi_index::tag, boost::multi_index::member - > + >, + + // Specification of the seventh index starts here + // This index is used to retrieve leases for matching hostname. + boost::multi_index::ordered_non_unique< + boost::multi_index::tag, + boost::multi_index::member + > > > Lease4Storage; // Specify the type name for this container. @@ -265,6 +283,9 @@ typedef Lease6Storage::index::type Lease6StorageSubnetIdIndex; /// @brief DHCPv6 lease storage index by Subnet-id. typedef Lease6Storage::index::type Lease6StorageDuidIndex; +/// @brief DHCPv6 lease storage index by hostname. +typedef Lease6Storage::index::type Lease6StorageHostnameIndex; + /// @brief DHCPv4 lease storage index by address. typedef Lease4Storage::index::type Lease4StorageAddressIndex; @@ -286,6 +307,9 @@ Lease4StorageClientIdHWAddressSubnetIdIndex; /// @brief DHCPv4 lease storage index by client id, HW address and subnet id. typedef Lease4Storage::index::type Lease4StorageSubnetIdIndex; +/// @brief DHCPv4 lease storage index by hostname. +typedef Lease4Storage::index::type Lease4StorageHostnameIndex; + //@} } // end of isc::dhcp namespace } // end of isc namespace diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index 8d741fa085..599045c860 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -159,6 +159,13 @@ tagged_statements = { { "state, user_context " "FROM lease4 " "WHERE subnet_id = ?"}, + {MySqlLeaseMgr::GET_LEASE4_HOSTNAME, + "SELECT address, hwaddr, client_id, " + "valid_lifetime, expire, subnet_id, " + "fqdn_fwd, fqdn_rev, hostname, " + "state, user_context " + "FROM lease4 " + "WHERE hostname = ?"}, {MySqlLeaseMgr::GET_LEASE4_EXPIRE, "SELECT address, hwaddr, client_id, " "valid_lifetime, expire, subnet_id, " @@ -233,6 +240,15 @@ tagged_statements = { { "state, user_context " "FROM lease6 " "WHERE duid = ?"}, + {MySqlLeaseMgr::GET_LEASE6_HOSTNAME, + "SELECT address, duid, valid_lifetime, " + "expire, subnet_id, pref_lifetime, " + "lease_type, iaid, prefix_len, " + "fqdn_fwd, fqdn_rev, hostname, " + "hwaddr, hwtype, hwaddr_source, " + "state, user_context " + "FROM lease6 " + "WHERE hostname = ?"}, {MySqlLeaseMgr::GET_LEASE6_EXPIRE, "SELECT address, duid, valid_lifetime, " "expire, subnet_id, pref_lifetime, " @@ -2096,6 +2112,27 @@ MySqlLeaseMgr::getLeases4(SubnetID subnet_id) const { return (result); } +Lease4Collection +MySqlLeaseMgr::getLeases4(const string& hostname) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET_HOSTNAME4) + .arg(hostname); + + // Set up the WHERE clause value + MYSQL_BIND inbind[1]; + memset(inbind, 0, sizeof(inbind)); + + // Hostname + inbind[0].buffer_type = MYSQL_TYPE_STRING; + inbind[0].buffer = const_cast(hostname.c_str()); + inbind[0].buffer_length = hostname.length(); + + // ... and get the data + Lease4Collection result; + getLeaseCollection(GET_LEASE4_HOSTNAME, inbind, result); + + return (result); +} + Lease4Collection MySqlLeaseMgr::getLeases4() const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET4); @@ -2312,7 +2349,7 @@ Lease6Collection MySqlLeaseMgr::getLeases6(const DUID& duid) const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET_DUID) .arg(duid.toText()); - + // Set up the WHERE clause value MYSQL_BIND inbind[1]; memset(inbind, 0, sizeof(inbind)); @@ -2325,14 +2362,35 @@ MySqlLeaseMgr::getLeases6(const DUID& duid) const { const_cast(&duid_vector[0])); inbind[0].buffer_length = duid_length; inbind[0].length = &duid_length; - + Lease6Collection result; - + getLeaseCollection(GET_LEASE6_DUID, inbind, result); return result; } +Lease6Collection +MySqlLeaseMgr::getLeases6(const string& hostname) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET_HOSTNAME6) + .arg(hostname); + + // Set up the WHERE clause value + MYSQL_BIND inbind[1]; + memset(inbind, 0, sizeof(inbind)); + + // Hostname + inbind[0].buffer_type = MYSQL_TYPE_STRING; + inbind[0].buffer = const_cast(hostname.c_str()); + inbind[0].buffer_length = hostname.length(); + + // ... and get the data + Lease6Collection result; + getLeaseCollection(GET_LEASE6_HOSTNAME, inbind, result); + + return (result); +} + Lease6Collection MySqlLeaseMgr::getLeases6(const asiolink::IOAddress& lower_bound_address, const LeasePageSize& page_size) const { diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index a11009c874..1524d06676 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2019 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -202,6 +202,13 @@ public: /// @return Lease collection (may be empty if no IPv4 lease found). virtual Lease4Collection getLeases4(SubnetID subnet_id) const; + /// @brief Returns all IPv4 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(const std::string& hostname) const; + /// @brief Returns all IPv4 leases. /// /// @return Lease collection (may be empty if no IPv4 lease found). @@ -305,6 +312,13 @@ public: /// @return Lease collection (may be empty if no IPv6 lease found). virtual Lease6Collection getLeases6(SubnetID subnet_id) const; + /// @brief Returns all IPv6 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv6 lease found). + virtual Lease6Collection getLeases6(const std::string& hostname) const; + /// @brief Returns all IPv6 leases. /// /// @return Lease collection (may be empty if no IPv6 lease found). @@ -316,7 +330,7 @@ public: /// @return Lease collection (may be empty if no IPv6 lease found) /// for the DUID. virtual Lease6Collection getLeases6(const DUID& duid) const; - + /// @brief Returns range of IPv6 leases using paging. /// /// This method implements paged browsing of the lease database. The first @@ -581,6 +595,7 @@ public: GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID GET_LEASE4_PAGE, // Get page of leases beginning with an address GET_LEASE4_SUBID, // Get IPv4 leases by subnet ID + GET_LEASE4_HOSTNAME, // Get IPv4 leases by hostname GET_LEASE4_EXPIRE, // Get lease4 by expiration. GET_LEASE6, // Get all IPv6 leases GET_LEASE6_ADDR, // Get lease6 by address @@ -589,6 +604,7 @@ public: GET_LEASE6_PAGE, // Get page of leases beginning with an address GET_LEASE6_SUBID, // Get IPv6 leases by subnet ID GET_LEASE6_DUID, // Get IPv6 leases by DUID + GET_LEASE6_HOSTNAME, // Get IPv6 leases by hostname GET_LEASE6_EXPIRE, // Get lease6 by expiration. INSERT_LEASE4, // Add entry to lease4 table INSERT_LEASE6, // Add entry to lease6 table diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index 26ff0c1c7b..3a015d574c 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -137,6 +137,16 @@ PgSqlTaggedStatement tagged_statements[] = { "FROM lease4 " "WHERE subnet_id = $1"}, + // GET_LEASE4_HOSTNAME + { 1, { OID_VARCHAR }, + "get_lease4_hostname", + "SELECT address, hwaddr, client_id, " + "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " + "fqdn_fwd, fqdn_rev, hostname, " + "state, user_context " + "FROM lease4 " + "WHERE hostname = $1"}, + // GET_LEASE4_EXPIRE { 3, { OID_INT8, OID_TIMESTAMP, OID_INT8 }, "get_lease4_expire", @@ -228,6 +238,17 @@ PgSqlTaggedStatement tagged_statements[] = { "FROM lease6 " "WHERE duid = $1"}, + // GET_LEASE6_HOSTNAME + { 1, { OID_VARCHAR }, + "get_lease6_hostname", + "SELECT address, duid, valid_lifetime, " + "extract(epoch from expire)::bigint, subnet_id, pref_lifetime, " + "lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname, " + "hwaddr, hwtype, hwaddr_source, " + "state, user_context " + "FROM lease6 " + "WHERE hostname = $1"}, + // GET_LEASE6_EXPIRE { 3, { OID_INT8, OID_TIMESTAMP, OID_INT8 }, "get_lease6_expire", @@ -1382,6 +1403,24 @@ PgSqlLeaseMgr::getLeases4(SubnetID subnet_id) const { return (result); } +Lease4Collection +PgSqlLeaseMgr::getLeases4(const string& hostname) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET_HOSTNAME4) + .arg(hostname); + + // Set up the WHERE clause value + PsqlBindArray bind_array; + + // Hostname + bind_array.add(hostname); + + // ... and get the data + Lease4Collection result; + getLeaseCollection(GET_LEASE4_HOSTNAME, bind_array, result); + + return (result); +} + Lease4Collection PgSqlLeaseMgr::getLeases4() const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET4); @@ -1550,6 +1589,24 @@ PgSqlLeaseMgr::getLeases6(const DUID& duid) const { return (result); } +Lease6Collection +PgSqlLeaseMgr::getLeases6(const string& hostname) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET_HOSTNAME6) + .arg(hostname); + + // Set up the WHERE clause value + PsqlBindArray bind_array; + + // Hostname + bind_array.add(hostname); + + // ... and get the data + Lease6Collection result; + getLeaseCollection(GET_LEASE6_HOSTNAME, bind_array, result); + + return (result); +} + Lease6Collection PgSqlLeaseMgr::getLeases6() const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET6); diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.h b/src/lib/dhcpsrv/pgsql_lease_mgr.h index c779fafaa6..cd1ccbf74d 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.h +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2019 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -183,6 +183,13 @@ public: /// @return Lease collection (may be empty if no IPv4 lease found). virtual Lease4Collection getLeases4(SubnetID subnet_id) const; + /// @brief Returns all IPv4 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(const std::string& hostname) const; + /// @brief Returns all IPv4 leases. /// /// @return Lease collection (may be empty if no IPv4 lease found). @@ -277,6 +284,13 @@ public: /// @return Lease collection (may be empty if no IPv6 lease found). virtual Lease6Collection getLeases6(SubnetID subnet_id) const; + /// @brief Returns all IPv6 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv6 lease found). + virtual Lease6Collection getLeases6(const std::string& hostname) const; + /// @brief Returns all IPv6 leases. /// /// @return Lease collection (may be empty if no IPv6 lease found). @@ -288,7 +302,7 @@ public: /// @return Lease collection (may be empty if no IPv6 lease found) /// for the DUID virtual Lease6Collection getLeases6(const DUID& duid) const; - + /// @brief Returns range of IPv6 leases using paging. /// /// This method implements paged browsing of the lease database. The first @@ -549,6 +563,7 @@ public: GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID GET_LEASE4_PAGE, // Get page of leases beginning with an address GET_LEASE4_SUBID, // Get IPv4 leases by subnet ID + GET_LEASE4_HOSTNAME, // Get IPv4 leases by hostname GET_LEASE4_EXPIRE, // Get expired lease4 GET_LEASE6, // Get all IPv6 leases GET_LEASE6_ADDR, // Get lease6 by address @@ -557,6 +572,7 @@ public: GET_LEASE6_PAGE, // Get page of IPv6 leases beginning with an address GET_LEASE6_SUBID, // Get IPv6 leases by subnet ID GET_LEASE6_DUID, // Get IPv6 leases by DUID + GET_LEASE6_HOSTNAME, // Get IPv6 leases by hostname GET_LEASE6_EXPIRE, // Get expired lease6 INSERT_LEASE4, // Add entry to lease4 table INSERT_LEASE6, // Add entry to lease6 table diff --git a/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc index 8c31f811d0..d07f335629 100644 --- a/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc @@ -582,6 +582,11 @@ TEST_F(CqlLeaseMgrTest, getLeases4SubnetId) { testGetLeases4SubnetId(); } +// This test checks that all IPv4 leases with a specified hostname are returned. +TEST_F(CqlLeaseMgrTest, getLeases4Hostname) { + testGetLeases4Hostname(); +} + // This test checks that all IPv4 leases are returned. TEST_F(CqlLeaseMgrTest, getLeases4) { testGetLeases4(); @@ -708,6 +713,11 @@ TEST_F(CqlLeaseMgrTest, getLease6DuidIaidSubnetIdSize) { testGetLease6DuidIaidSubnetIdSize(); } +// This test checks that all IPv6 leases with a specified hostname are returned. +TEST_F(CqlLeaseMgrTest, getLeases6Hostname) { + testGetLeases6Hostname(); +} + // Test that a range of IPv6 leases is returned with paging. TEST_F(CqlLeaseMgrTest, getLeases6Paged) { testGetLeases6Paged(); diff --git a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc index eceec558d6..c140d8c90f 100644 --- a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc @@ -1246,6 +1246,29 @@ GenericLeaseMgrTest::testGetLeases4SubnetId() { ASSERT_EQ(2, returned.size()); } +void +GenericLeaseMgrTest::testGetLeases4Hostname() { + // Get the leases to be used for the test and add to the database. + vector leases = createLeases4(); + for (size_t i = 0; i < leases.size(); ++i) { + EXPECT_TRUE(lmptr_->addLease(leases[i])); + } + + // There should be no lease for hostname foobar. + Lease4Collection returned = lmptr_->getLeases4(string("foobar")); + EXPECT_TRUE(returned.empty()); + + // There should be exactly 4 leases for the hostname of the second lease. + ASSERT_FALSE(leases[1]->hostname_.empty()); + returned = lmptr_->getLeases4(leases[1]->hostname_); + EXPECT_EQ(4, returned.size()); + + // And 3 for the forth lease. + ASSERT_FALSE(leases[3]->hostname_.empty()); + returned = lmptr_->getLeases4(leases[3]->hostname_); + EXPECT_EQ(3, returned.size()); +} + void GenericLeaseMgrTest::testGetLeases4() { // Get the leases to be used for the test and add to the database @@ -1335,6 +1358,34 @@ GenericLeaseMgrTest::testGetLeases6SubnetId() { EXPECT_EQ(2, returned.size()); } +void +GenericLeaseMgrTest::testGetLeases6Hostname() { + // Get the leases to be used for the test and add to the database. + vector leases = createLeases6(); + for (size_t i = 0; i < leases.size(); ++i) { + EXPECT_TRUE(lmptr_->addLease(leases[i])); + } + + // There should be no lease for hostname foobar. + Lease6Collection returned = lmptr_->getLeases6(string("foobar")); + EXPECT_TRUE(returned.empty()); + + // There should be exactly 4 leases for the hostname of the second lease. + ASSERT_FALSE(leases[1]->hostname_.empty()); + returned = lmptr_->getLeases6(leases[1]->hostname_); + EXPECT_EQ(4, returned.size()); + + // One for the fifth lease. + ASSERT_FALSE(leases[4]->hostname_.empty()); + returned = lmptr_->getLeases6(leases[4]->hostname_); + EXPECT_EQ(1, returned.size()); + + // And 3 for the sixth lease. + ASSERT_FALSE(leases[5]->hostname_.empty()); + returned = lmptr_->getLeases6(leases[5]->hostname_); + EXPECT_EQ(3, returned.size()); +} + void GenericLeaseMgrTest::testGetLeases6() { // Get the leases to be used for the test and add to the database diff --git a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h index a86632b440..ebb3078f46 100644 --- a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h +++ b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h @@ -201,6 +201,9 @@ public: /// @brief Test method which returns all IPv4 leases for Subnet ID. void testGetLeases4SubnetId(); + /// @brief Test method which returns all IPv4 leases for Hostname. + void testGetLeases4Hostname(); + /// @brief Test method which returns all IPv4 leases. void testGetLeases4(); @@ -210,6 +213,9 @@ public: /// @brief Test method which returns all IPv6 leases for Subnet ID. void testGetLeases6SubnetId(); + /// @brief Test method which returns all IPv6 leases for Hostname. + void testGetLeases6Hostname(); + /// @brief Test making/fetching leases with IAIDs > signed 32-bit max. void testLease6LargeIaidCheck(); diff --git a/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc index da389a56aa..3bb7184368 100644 --- a/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2019 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -139,6 +139,15 @@ public: return (Lease4Collection()); } + /// @brief Returns all IPv4 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(const std::string& hostname) const { + return (Lease4Collection()); + } + /// @brief Returns all IPv4 leases. /// /// @return Lease collection (may be empty if no IPv4 lease found). @@ -226,6 +235,15 @@ public: return (Lease6Collection()); } + /// @brief Returns all IPv6 leases for the particular hostname. + /// + /// @param hostname hostname in lower case. + /// + /// @return Lease collection (may be empty if no IPv6 lease found). + virtual Lease6Collection getLeases6(const std::string& hostname) const { + return (Lease6Collection()); + } + /// @brief Returns all IPv6 leases. /// /// @return Lease collection (may be empty if no IPv6 lease found). diff --git a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc index 96a9314ef9..d9548aeb81 100644 --- a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc @@ -926,6 +926,12 @@ TEST_F(MemfileLeaseMgrTest, getLeases4SubnetId) { testGetLeases4SubnetId(); } +// This test checks that all IPv4 leases with a specified hostname are returned. +TEST_F(MemfileLeaseMgrTest, getLeases4Hostname) { + startBackend(V4); + testGetLeases4Hostname(); +} + // This test checks that all IPv4 leases are returned. TEST_F(MemfileLeaseMgrTest, getLeases4) { startBackend(V4); @@ -944,6 +950,12 @@ TEST_F(MemfileLeaseMgrTest, getLeases6SubnetId) { testGetLeases6SubnetId(); } +// This test checks that all IPv6 leases with a specified hostname are returned. +TEST_F(MemfileLeaseMgrTest, getLeases6Hostname) { + startBackend(V6); + testGetLeases6Hostname(); +} + // This test adds 3 leases and verifies fetch by DUID. // Verifies retrival of non existant DUID fails TEST_F(MemfileLeaseMgrTest, getLeases6Duid) { diff --git a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc index 72a8ba8ef7..e84d0f5aae 100644 --- a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc @@ -351,6 +351,11 @@ TEST_F(MySqlLeaseMgrTest, getLeases4SubnetId) { testGetLeases4SubnetId(); } +// This test checks that all IPv4 leases with a specified hostname are returned. +TEST_F(MySqlLeaseMgrTest, getLeases4Hostname) { + testGetLeases4Hostname(); +} + // This test checks that all IPv4 leases are returned. TEST_F(MySqlLeaseMgrTest, getLeases4) { testGetLeases4(); @@ -366,6 +371,11 @@ TEST_F(MySqlLeaseMgrTest, getLeases6SubnetId) { testGetLeases6SubnetId(); } +// This test checks that all IPv6 leases with a specified hostname are returned. +TEST_F(MySqlLeaseMgrTest, getLeases6Hostname) { + testGetLeases6Hostname(); +} + // This test checks that all IPv6 leases are returned. TEST_F(MySqlLeaseMgrTest, getLeases6) { testGetLeases6(); diff --git a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc index ec783594b8..1856186cd7 100644 --- a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc @@ -341,6 +341,11 @@ TEST_F(PgSqlLeaseMgrTest, getLeases4SubnetId) { testGetLeases4SubnetId(); } +// This test checks that all IPv4 leases with a specified hostname are returned. +TEST_F(PgSqlLeaseMgrTest, getLeases4Hostname) { + testGetLeases4Hostname(); +} + // This test checks that all IPv4 leases are returned. TEST_F(PgSqlLeaseMgrTest, getLeases4) { testGetLeases4(); @@ -356,6 +361,11 @@ TEST_F(PgSqlLeaseMgrTest, getLeases6SubnetId) { testGetLeases6SubnetId(); } +// This test checks that all IPv6 leases with a specified hostname are returned. +TEST_F(PgSqlLeaseMgrTest, getLeases6Hostname) { + testGetLeases6Hostname(); +} + // This test checks that all IPv6 leases are returned. TEST_F(PgSqlLeaseMgrTest, getLeases6) { testGetLeases6();