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

Commit 33c3afb9 authored by Mike Yu's avatar Mike Yu
Browse files

Change DnsTlsTransport to store a copy of a query am: 7e08b856

Change-Id: Ifb6714413f89df1de07a031c51643abdc1d6443f
parents 8e2b2281 7e08b856
Loading
Loading
Loading
Loading
+7 −5
Original line number Original line Diff line number Diff line
@@ -37,10 +37,12 @@ std::unique_ptr<DnsTlsQueryMap::QueryFuture> DnsTlsQueryMap::recordQuery(
        LOG(WARNING) << "All query IDs are in use";
        LOG(WARNING) << "All query IDs are in use";
        return nullptr;
        return nullptr;
    }
    }
    Query q = { .newId = static_cast<uint16_t>(newId), .query = query };

    std::map<uint16_t, QueryPromise>::iterator it;
    // Make a copy of the query.
    bool inserted;
    std::vector<uint8_t> tmp(query.base(), query.base() + query.size());
    std::tie(it, inserted) = mQueries.emplace(newId, q);
    Query q = {.newId = static_cast<uint16_t>(newId), .query = std::move(tmp)};

    const auto [it, inserted] = mQueries.try_emplace(newId, q);
    if (!inserted) {
    if (!inserted) {
        LOG(ERROR) << "Failed to store pending query";
        LOG(ERROR) << "Failed to store pending query";
        return nullptr;
        return nullptr;
@@ -137,7 +139,7 @@ void DnsTlsQueryMap::onResponse(std::vector<uint8_t> response) {
    }
    }
    Result r = { .code = Response::success, .response = std::move(response) };
    Result r = { .code = Response::success, .response = std::move(response) };
    // Rewrite ID to match the query
    // 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[0] = data[0];
    r.response[1] = data[1];
    r.response[1] = data[1];
    LOG(DEBUG) << "Sending result to dispatcher";
    LOG(DEBUG) << "Sending result to dispatcher";
+1 −1
Original line number Original line Diff line number Diff line
@@ -40,7 +40,7 @@ class DnsTlsQueryMap {
        // The new ID number assigned to this query.
        // The new ID number assigned to this query.
        uint16_t newId;
        uint16_t newId;
        // A query that has been passed to recordQuery(), with its original ID number.
        // 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 {
    struct Result {
+2 −2
Original line number Original line Diff line number Diff line
@@ -56,9 +56,9 @@ int DnsTlsTransport::getConnectCounter() const {
    return mConnectCounter;
    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.
    // 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) {
    if (sent) {
        mQueries.markTried(q.newId);
        mQueries.markTried(q.newId);
    }
    }
+1 −1
Original line number Original line Diff line number Diff line
@@ -86,7 +86,7 @@ class DnsTlsTransport : public IDnsTlsSocketObserver {
    std::unique_ptr<IDnsTlsSocket> mSocket GUARDED_BY(mLock);
    std::unique_ptr<IDnsTlsSocket> mSocket GUARDED_BY(mLock);


    // Send a query to the socket.
    // 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.
    // The number of times an attempt to connect the nameserver.
    int mConnectCounter GUARDED_BY(mLock) = 0;
    int mConnectCounter GUARDED_BY(mLock) = 0;
+3 −3
Original line number Original line Diff line number Diff line
@@ -887,9 +887,9 @@ TEST(QueryMapTest, Basic) {
    EXPECT_EQ(1, all[1].newId);
    EXPECT_EQ(1, all[1].newId);
    EXPECT_EQ(2, all[2].newId);
    EXPECT_EQ(2, all[2].newId);


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


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