]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2869] Add getExtendedInfo6Common
authorFrancis Dupont <fdupont@isc.org>
Thu, 25 May 2023 15:25:53 +0000 (17:25 +0200)
committerFrancis Dupont <fdupont@isc.org>
Thu, 6 Jul 2023 20:11:32 +0000 (22:11 +0200)
src/lib/dhcpsrv/mysql_lease_mgr.cc
src/lib/dhcpsrv/mysql_lease_mgr.h
src/lib/dhcpsrv/pgsql_lease_mgr.cc
src/lib/dhcpsrv/pgsql_lease_mgr.h

index 60f96c3e4dcc4d48f67f73260f7e6b1bf8411077..bb16b3422367c9a88c843aff655e41ceb7e54c17 100644 (file)
@@ -4299,6 +4299,60 @@ MySqlLeaseMgr::upgradeExtendedInfo4(const LeasePageSize& page_size) {
     return (updated);
 }
 
+std::list<IOAddress>
+MySqlLeaseMgr::getExtendedInfo6Common(MySqlLeaseContextPtr& ctx,
+                                      StatementIndex stindex,
+                                      std::vector<MYSQL_BIND>& bind) {
+    int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], &bind[0]);
+    checkError(ctx, status, stindex, "unable to bind WHERE clause parameters");
+
+    // Set up the MYSQL_BIND array for the data being returned.
+    MYSQL_BIND outbind[1];
+    memset(outbind, 0, sizeof(outbind));
+
+    // Bind the lease address.
+    uint8_t addr_data[16];
+    unsigned long addr_size = 16;
+    outbind[0].buffer_type = MYSQL_TYPE_BLOB;
+    outbind[0].buffer = reinterpret_cast<char*>(addr_data);
+    outbind[0].buffer_length = addr_size;
+    outbind[0].length = &addr_size;
+
+    status = mysql_stmt_bind_result(ctx->conn_.statements_[stindex], &outbind[0]);
+    checkError(ctx, status, stindex, "outbound binding failed");
+
+    // Execute the statement.
+    status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
+    checkError(ctx, status, stindex, "unable to execute");
+
+    // Store all results.
+    status = mysql_stmt_store_result(ctx->conn_.statements_[stindex]);
+    checkError(ctx, status, stindex, "unable to set up for storing all results");
+
+    // Set up the fetch "release" object to release resources associated
+    // with the call to mysql_stmt_fetch when this method exits, then
+    // retrieve the data.
+    MySqlFreeResult fetch_release(ctx->conn_.statements_[stindex]);
+    std::list<IOAddress> result;
+    while ((status = mysql_stmt_fetch(ctx->conn_.statements_[stindex])) == 0) {
+        if (addr_size != 16) {
+            isc_throw(BadValue, "received lease6 address is not 16 byte long");
+        }
+        result.push_back(IOAddress::fromBytes(AF_INET6, addr_data));
+    }
+
+    // How did the fetch end?
+    if (status == 1) {
+        // Error - unable to fetch results.
+        checkError(ctx, status, stindex, "unable to fetch results");
+    } else if (status == MYSQL_DATA_TRUNCATED) {
+        // Data truncated - throw an exception indicating what was at fault.
+        isc_throw(DataTruncated, ctx->conn_.text_statements_[stindex]
+                  << " returned truncated data");
+    }
+    return (result);
+}
+
 Lease6Collection
 MySqlLeaseMgr::getLeases6ByRelayId(const DUID& /* relay_id */,
                                    const IOAddress& /* link_addr */,
index 70a2d5dd9be0d175ae6b5476121c34e8028b8b80..e080f8863cd0524aa284080de3d3aad171fc040c 100644 (file)
@@ -1311,6 +1311,17 @@ private:
     /// @param addr The address of the lease.
     void deleteRemoteId6(const isc::asiolink::IOAddress& addr);
 
+    /// @brief Common part of from lease6 extended info tables.
+    ///
+    /// @param ctx Context.
+    /// @param stindex Index of statement being executed.
+    /// @param bind Selection MYSQL_BIND array.
+    /// @return List of addresses.
+    std::list<isc::asiolink::IOAddress>
+    getExtendedInfo6Common(MySqlLeaseContextPtr& ctx,
+                           StatementIndex stindex,
+                           std::vector<MYSQL_BIND>& bind);
+
     // Members
 
     /// @brief The parameters
index 76a626ec273e18682013590941ee05d3a5645ce8..a87293067999bb5510c088850e69c351cb0bd9b9 100644 (file)
@@ -3403,6 +3403,31 @@ PgSqlLeaseMgr::upgradeExtendedInfo4(const LeasePageSize& page_size) {
     return (updated);
 }
 
+std::list<IOAddress>
+PgSqlLeaseMgr::getExtendedInfo6Common(PgSqlLeaseContextPtr& ctx,
+                                      StatementIndex stindex,
+                                      db::PsqlBindArray& bind_array) {
+    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));
+    ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
+
+    int rows = PQntuples(r);
+    std::list<IOAddress> result;
+    for (int i = 0; i < rows; ++i) {
+        std::vector<uint8_t> addr_data;
+        PgSqlLeaseExchange::convertFromBytea(r, i, 0, addr_data);
+        if (addr_data.size() != 16) {
+            isc_throw(BadValue, "received lease6 address is not 16 byte long");
+        }
+        result.push_back(IOAddress::fromBytes(AF_INET6, &addr_data[0]));
+    }
+    return (result);
+}
+
 Lease6Collection
 PgSqlLeaseMgr::getLeases6ByRelayId(const DUID& /* relay_id */,
                                    const IOAddress& /* link_addr */,
index 6b333e0ba2f574743380371dd3f5b69ca4f3bb92..7f0c3c6bc072699788db4cdedb5f21a59a84085a 100644 (file)
@@ -1271,6 +1271,17 @@ private:
     /// @param addr The address of the lease.
     void deleteRemoteId6(const isc::asiolink::IOAddress& addr);
 
+    /// @brief Common part of from lease6 extended info tables.
+    ///
+    /// @param ctx Context.
+    /// @param stindex Index of statement being executed.
+    /// @bind_array Selection bin array.
+    /// @return List of addresses.
+    std::list<isc::asiolink::IOAddress>
+    getExtendedInfo6Common(PgSqlLeaseContextPtr& ctx,
+                           StatementIndex stindex,
+                           db::PsqlBindArray& bind_array);
+
     // Members
 
     /// @brief The parameters