Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7e08b856 authored by Mike Yu's avatar Mike Yu
Browse files

Change DnsTlsTransport to store a copy of a query

This is preparation for configurable per-query timeout in order to
allow a lookup thread to exit while a DnsTlsSocket loop thread is
still waiting the response. Since a DnsTlsTransport instance is
accessible to both lookup threads and a DnsTlsSocket loop thread, change
it to store a copy of a query in case of any operation needed for
the query in the loop thread; for example, the response arrives after
timeout.

Bug: 120182528
Bug: 141218721
Test: cd packages/modules/DnsResolver && atest
Change-Id: I57bf40cb1eb0d688d5328242649c841f20d81430
parent bd13699a
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -37,10 +37,12 @@ std::unique_ptr<DnsTlsQueryMap::QueryFuture> DnsTlsQueryMap::recordQuery(
        LOG(WARNING) << "All query IDs are in use";
        return nullptr;
    }
    Query q = { .newId = static_cast<uint16_t>(newId), .query = query };
    std::map<uint16_t, QueryPromise>::iterator it;
    bool inserted;
    std::tie(it, inserted) = mQueries.emplace(newId, q);

    // Make a copy of the query.
    std::vector<uint8_t> tmp(query.base(), query.base() + query.size());
    Query q = {.newId = static_cast<uint16_t>(newId), .query = std::move(tmp)};

    const auto [it, inserted] = mQueries.try_emplace(newId, q);
    if (!inserted) {
        LOG(ERROR) << "Failed to store pending query";
        return nullptr;
@@ -137,7 +139,7 @@ void DnsTlsQueryMap::onResponse(std::vector<uint8_t> response) {
    }
    Result r = { .code = Response::success, .response = std::move(response) };
    // Rewrite ID to match the query
    const uint8_t* data = it->second.query.query.base();
    const uint8_t* data = it->second.query.query.data();
    r.response[0] = data[0];
    r.response[1] = data[1];
    LOG(DEBUG) << "Sending result to dispatcher";
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ class DnsTlsQueryMap {
        // The new ID number assigned to this query.
        uint16_t newId;
        // A query that has been passed to recordQuery(), with its original ID number.
        const netdutils::Slice query;
        const std::vector<uint8_t> query;
    };

    struct Result {
+2 −2
Original line number Diff line number Diff line
@@ -56,9 +56,9 @@ int DnsTlsTransport::getConnectCounter() const {
    return mConnectCounter;
}

bool DnsTlsTransport::sendQuery(const DnsTlsQueryMap::Query q) {
bool DnsTlsTransport::sendQuery(const DnsTlsQueryMap::Query& q) {
    // Strip off the ID number and send the new ID instead.
    bool sent = mSocket->query(q.newId, netdutils::drop(q.query, 2));
    const bool sent = mSocket->query(q.newId, netdutils::drop(netdutils::makeSlice(q.query), 2));
    if (sent) {
        mQueries.markTried(q.newId);
    }
+1 −1
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ class DnsTlsTransport : public IDnsTlsSocketObserver {
    std::unique_ptr<IDnsTlsSocket> mSocket GUARDED_BY(mLock);

    // Send a query to the socket.
    bool sendQuery(const DnsTlsQueryMap::Query q) REQUIRES(mLock);
    bool sendQuery(const DnsTlsQueryMap::Query& q) REQUIRES(mLock);

    // The number of times an attempt to connect the nameserver.
    int mConnectCounter GUARDED_BY(mLock) = 0;
+3 −3
Original line number Diff line number Diff line
@@ -887,9 +887,9 @@ TEST(QueryMapTest, Basic) {
    EXPECT_EQ(1, all[1].newId);
    EXPECT_EQ(2, all[2].newId);

    EXPECT_EQ(makeSlice(q0), all[0].query);
    EXPECT_EQ(makeSlice(q1), all[1].query);
    EXPECT_EQ(makeSlice(q2), all[2].query);
    EXPECT_EQ(q0, all[0].query);
    EXPECT_EQ(q1, all[1].query);
    EXPECT_EQ(q2, all[2].query);

    bytevec a0 = make_query(0, SIZE);
    bytevec a1 = make_query(1, SIZE);