From: Stephen Morris Date: Fri, 23 Nov 2012 13:48:42 +0000 (+0000) Subject: [2404] Add method to get lease4 by client ID and subnet ID X-Git-Tag: bind10-1.0.0-beta-release~39^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a08894f45157cd19450927d2e8b80ce49a86acaf;p=thirdparty%2Fkea.git [2404] Add method to get lease4 by client ID and subnet ID --- diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index becc0baa13..c11dd116b3 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -71,6 +71,11 @@ TaggedStatement tagged_statements[] = { "valid_lifetime, expire, subnet_id " "FROM lease4 " "WHERE client_id = ?"}, + {MySqlLeaseMgr::GET_LEASE4_CLIENTID_SUBID, + "SELECT address, hwaddr, client_id, " + "valid_lifetime, expire, subnet_id " + "FROM lease4 " + "WHERE client_id = ? AND subnet_id = ?"}, {MySqlLeaseMgr::GET_LEASE4_HWADDR, "SELECT address, hwaddr, client_id, " "valid_lifetime, expire, subnet_id " @@ -1144,7 +1149,6 @@ MySqlLeaseMgr::getLease4(const HWAddr& hwaddr, SubnetID subnet_id) const { } - Lease4Collection MySqlLeaseMgr::getLease4(const ClientId& clientid) const { // Set up the WHERE clause value @@ -1167,14 +1171,28 @@ MySqlLeaseMgr::getLease4(const ClientId& clientid) const { Lease4Ptr -MySqlLeaseMgr::getLease4(const ClientId& /* clientid */, - SubnetID /* subnet_id */) const { - isc_throw(NotImplemented, "MySqlLeaseMgr::getLease4(const ClientID&, SubnetID) " - "not implemented yet"); - return (Lease4Ptr()); -} +MySqlLeaseMgr::getLease4(const ClientId& clientid, SubnetID subnet_id) const { + // Set up the WHERE clause value + MYSQL_BIND inbind[2]; + memset(inbind, 0, sizeof(inbind)); + + std::vector client_data = clientid.getClientId(); + unsigned long client_data_length = client_data.size(); + inbind[0].buffer_type = MYSQL_TYPE_BLOB; + inbind[0].buffer = reinterpret_cast(&client_data[0]); + inbind[0].buffer_length = client_data_length; + inbind[0].length = &client_data_length; + + inbind[1].buffer_type = MYSQL_TYPE_LONG; + inbind[1].buffer = reinterpret_cast(&subnet_id); + inbind[1].is_unsigned = my_bool(1); + // Get the data + Lease4Ptr result; + getLease(GET_LEASE4_CLIENTID_SUBID, inbind, exchange4_, result); + return (result); +} Lease6Ptr diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index eb93ccbefc..2b9071fa13 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -356,6 +356,7 @@ public: DELETE_LEASE6, // Delete from lease6 by address GET_LEASE4_ADDR, // Get lease4 by address GET_LEASE4_CLIENTID, // Get lease4 by Client ID + GET_LEASE4_CLIENTID_SUBID, // Get lease4 by Client ID GET_LEASE4_HWADDR, // Get lease4 by HW address GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID GET_LEASE6_ADDR, // Get lease6 by address diff --git a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc index 6b234c5274..11e3285261 100644 --- a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc @@ -976,6 +976,45 @@ TEST_F(MySqlLeaseMgrTest, getLease4ClientId) { } +// @brief Check GetLease4 methods - Access by Client ID & Subnet ID +// +// Adds leases to the database and checks that they can be accessed via +// a combination of client and subnet IDs. + +TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSubnetId) { + // Get the leases to be used for the test and add to the database + vector leases = createLeases4(); + for (int i = 0; i < leases.size(); ++i) { + EXPECT_TRUE(lmptr_->addLease(leases[i])); + } + + // Get the leases matching the client ID of lease 1 and + // subnet ID of lease 1. Result should be a single lease - lease 1. + Lease4Ptr returned = lmptr_->getLease4(*leases[1]->client_id_, + leases[1]->subnet_id_); + ASSERT_TRUE(returned); + detailCompareLease(leases[1], returned); + + // Try for a match to the client ID of lease 1 and the wrong + // subnet ID. + returned = lmptr_->getLease4(*leases[1]->client_id_, + leases[1]->subnet_id_ + 1); + EXPECT_FALSE(returned); + + // Try for a match to the subnet ID of lease 1 (and lease 4) but + // the wrong client ID + const uint8_t invalid_data[] = {0, 0, 0}; + ClientId invalid(invalid_data, sizeof(invalid_data)); + returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_); + EXPECT_FALSE(returned); + + // Try for a match to an unknown hardware address and an unknown + // subnet ID. + returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_ + 1); + EXPECT_FALSE(returned); +} + + // @brief Check GetLease6 methods - Access by DUID/IAID // // Adds leases to the database and checks that they can be accessed via