From: Francis Dupont Date: Thu, 25 May 2023 00:29:39 +0000 (+0200) Subject: [#2869] Implemented new deletes X-Git-Tag: Kea-2.5.0~36 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5ab389783db0293a47c2f0ec679ffcd49feecc43;p=thirdparty%2Fkea.git [#2869] Implemented new deletes --- diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index 48e7f6d273..a6aef3ad20 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -3812,8 +3812,69 @@ MySqlLeaseMgr::checkError(MySqlLeaseContextPtr& ctx, } void -MySqlLeaseMgr::deleteExtendedInfo6(const IOAddress& /* addr */) { - isc_throw(NotImplemented, "MySqlLeaseMgr::deleteExtendedInfo6 not implemented"); +MySqlLeaseMgr::deleteExtendedInfo6(const IOAddress& addr) { + deleteRelayId6(addr); + deleteRemoteId6(addr); +} + +void +MySqlLeaseMgr::deleteRelayId6(const IOAddress& addr) { + // Get a context. + MySqlLeaseContextAlloc get_context(*this); + MySqlLeaseContextPtr ctx = get_context.ctx_; + + // Bind the lease address. + MYSQL_BIND bind[1]; + memset(bind, 0, sizeof(bind)); + + std::vector addr_data = addr.toBytes(); + // Do not check the address length as it does not really matter. + unsigned long addr_length = addr_data.size(); + bind[0].buffer_type = MYSQL_TYPE_BLOB; + bind[0].buffer = reinterpret_cast(&addr_data[0]); + bind[0].buffer_length = addr_length; + bind[0].length = &addr_length; + + // Delete from lease6_relay_id table. + StatementIndex stindex = DELETE_RELAY_ID6; + + // Bind the input parameters to the statement. + int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind); + checkError(ctx, status, stindex, "unable to bind WHERE clause parameter"); + + // Execute. + status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]); + checkError(ctx, status, stindex, "unable to execute"); +} + +void +MySqlLeaseMgr::deleteRemoteId6(const IOAddress& addr) { + // Get a context. + MySqlLeaseContextAlloc get_context(*this); + MySqlLeaseContextPtr ctx = get_context.ctx_; + + // Bind the lease address. + MYSQL_BIND bind[1]; + memset(bind, 0, sizeof(bind)); + + std::vector addr_data = addr.toBytes(); + // Do not check the address length as it does not really matter. + unsigned long addr_length = addr_data.size(); + bind[0].buffer_type = MYSQL_TYPE_BLOB; + bind[0].buffer = reinterpret_cast(&addr_data[0]); + bind[0].buffer_length = addr_length; + bind[0].length = &addr_length; + + // Delete from lease6_remote_id table. + StatementIndex stindex = DELETE_REMOTE_ID6; + + // Bind the input parameters to the statement. + int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind); + checkError(ctx, status, stindex, "unable to bind WHERE clause parameter"); + + // Execute. + status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]); + checkError(ctx, status, stindex, "unable to execute"); } void diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index 5472db03c4..925731d292 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -1071,6 +1071,7 @@ private: int status, StatementIndex index, const char* what) const; +public: /// The following queries are used to fulfill Bulk Lease Query /// queries. They rely on relay data contained in lease's /// user-context when the extended-store-info flag is enabled. @@ -1186,6 +1187,7 @@ private: /// @brief Wipe by-relay-id table (v6). virtual void wipeExtendedInfoTables6() override; +private: /// @brief Context RAII allocator. class MySqlLeaseContextAlloc { public: @@ -1289,6 +1291,15 @@ protected: const std::vector& remote_id) override; private: + /// @brief Delete lease6 extended info from by-relay-id table. + /// + /// @param addr The address of the lease. + void deleteRelayId6(const isc::asiolink::IOAddress& addr); + + /// @brief Delete lease6 extended info from by-remote-id table. + /// + /// @param addr The address of the lease. + void deleteRemoteId6(const isc::asiolink::IOAddress& addr); // Members diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index a0c2f67167..45a0ca44d4 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -2978,8 +2978,67 @@ PgSqlLeaseMgr::rollback() { } void -PgSqlLeaseMgr::deleteExtendedInfo6(const IOAddress& /* addr */) { - isc_throw(NotImplemented, "PgSqlLeaseMgr::deleteExtendedInfo6 not implemented"); +PgSqlLeaseMgr::deleteExtendedInfo6(const IOAddress& addr) { + deleteRelayId6(addr); + deleteRemoteId6(addr); +} + +void +PgSqlLeaseMgr::deleteRelayId6(const IOAddress& addr) { + // Set up the WHERE clause value. + PsqlBindArray bind_array; + + std::vector addr_data = addr.toBytes(); + // Do not check the address length as it does not really matter. + bind_array.add(addr_data); + + // Get a context. + PgSqlLeaseContextAlloc get_context(*this); + PgSqlLeaseContextPtr ctx = get_context.ctx_; + + // Delete from lease6_relay_id table. + StatementIndex stindex = DELETE_RELAY_ID6; + + PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name, + tagged_statements[stindex].nbparams, + &bind_array.values_[0], + &bind_array.lengths_[0], + &bind_array.formats_[0], 0)); + + int s = PQresultStatus(r); + + if (s != PGRES_COMMAND_OK) { + ctx->conn_.checkStatementError(r, tagged_statements[stindex]); + } +} + +void +PgSqlLeaseMgr::deleteRemoteId6(const IOAddress& addr) { + // Set up the WHERE clause value. + PsqlBindArray bind_array; + + std::vector addr_data = addr.toBytes(); + // Do not check the address length as it does not really matter. + bind_array.add(addr_data); + + // Get a context. + PgSqlLeaseContextAlloc get_context(*this); + PgSqlLeaseContextPtr ctx = get_context.ctx_; + + // Delete from lease6_remote_id table. + StatementIndex stindex = DELETE_REMOTE_ID6; + + PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name, + tagged_statements[stindex].nbparams, + &bind_array.values_[0], + &bind_array.lengths_[0], + &bind_array.formats_[0], 0)); + + int s = PQresultStatus(r); + + if (s != PGRES_COMMAND_OK) { + ctx->conn_.checkStatementError(r, tagged_statements[stindex]); + } } void diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.h b/src/lib/dhcpsrv/pgsql_lease_mgr.h index b94de61494..fafeebfc2e 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.h +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.h @@ -972,6 +972,7 @@ private: std::string checkLimits(isc::data::ConstElementPtr const& user_context, StatementIndex const stindex) const; +public: /// @brief Checks if the IPv4 lease limits set in the given user context are exceeded. /// PostgreSQL implementation. /// @@ -1146,6 +1147,7 @@ private: /// @brief Write V6 leases to a file. virtual void writeLeases6(const std::string& /*filename*/) override; +private: /// @brief Context RAII allocator. class PgSqlLeaseContextAlloc { public: @@ -1249,6 +1251,15 @@ protected: const std::vector& remote_id) override; private: + /// @brief Delete lease6 extended info from by-relay-id table. + /// + /// @param addr The address of the lease. + void deleteRelayId6(const isc::asiolink::IOAddress& addr); + + /// @brief Delete lease6 extended info from by-remote-id table. + /// + /// @param addr The address of the lease. + void deleteRemoteId6(const isc::asiolink::IOAddress& addr); // Members