blob: f3ff963a688df2e6bd6415d5e45e4edc1c33b7ba [file] [log] [blame]
Mike Yubab3daa2018-10-19 22:11:43 +08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _DNS_DNSTLSQUERYMAP_H
18#define _DNS_DNSTLSQUERYMAP_H
19
20#include <future>
21#include <map>
22#include <mutex>
23#include <vector>
24
25#include <android-base/thread_annotations.h>
26#include <netdutils/Slice.h>
27
28#include "DnsTlsServer.h"
Mike Yubab3daa2018-10-19 22:11:43 +080029
30namespace android {
31namespace net {
32
Mike Yubab3daa2018-10-19 22:11:43 +080033// Keeps track of queries and responses. This class matches responses with queries.
34// All methods are thread-safe and non-blocking.
Mike Yub601ff72018-11-01 20:07:00 +080035class DnsTlsQueryMap {
36 public:
Mike Yubd136992019-12-04 15:01:07 +080037 enum class Response : uint8_t { success, network_error, limit_error, internal_error };
38
Mike Yubb499092020-08-28 19:18:42 +080039 DnsTlsQueryMap();
40
Mike Yubab3daa2018-10-19 22:11:43 +080041 struct Query {
42 // The new ID number assigned to this query.
43 uint16_t newId;
44 // A query that has been passed to recordQuery(), with its original ID number.
Mike Yu7e08b852019-10-18 18:27:43 +080045 const std::vector<uint8_t> query;
Mike Yubab3daa2018-10-19 22:11:43 +080046 };
47
Mike Yubd136992019-12-04 15:01:07 +080048 struct Result {
49 Response code;
50 std::vector<uint8_t> response;
51 };
Mike Yubab3daa2018-10-19 22:11:43 +080052
53 struct QueryFuture {
Bernie Innocentiec4219b2019-01-30 11:16:36 +090054 QueryFuture(Query query, std::future<Result> result)
55 : query(query), result(std::move(result)) {}
Mike Yubab3daa2018-10-19 22:11:43 +080056 Query query;
57 // A future which will resolve to the result of this query.
58 std::future<Result> result;
59 };
60
61 // Returns an object containing everything needed to complete processing of
62 // this query, or null if the query could not be recorded.
Bernie Innocentiec4219b2019-01-30 11:16:36 +090063 std::unique_ptr<QueryFuture> recordQuery(const netdutils::Slice query);
Mike Yubab3daa2018-10-19 22:11:43 +080064
65 // Process a response, including a new ID. If the response
66 // is not recognized as matching any query, it will be ignored.
67 void onResponse(std::vector<uint8_t> response);
68
69 // Clear all map contents. This causes all pending queries to resolve with failure.
70 void clear();
71
72 // Get all pending queries. This returns a shallow copy, mostly for thread-safety.
73 std::vector<Query> getAll();
74
75 // Mark a query has having been retried. If the query hits the retry limit, it will
76 // be expired at the next call to cleanup.
77 void markTried(uint16_t newId);
78 void cleanup();
79
80 // Returns true if there are no pending queries.
81 bool empty();
82
Mike Yucb2bb7c2019-11-22 20:42:13 +080083 // The maximum number of times we will send a query before abandoning it.
84 static constexpr int kMaxTries = 3;
Mike Yubb499092020-08-28 19:18:42 +080085 int mMaxTries;
Mike Yucb2bb7c2019-11-22 20:42:13 +080086
Bernie Innocentiec4219b2019-01-30 11:16:36 +090087 private:
Mike Yubab3daa2018-10-19 22:11:43 +080088 std::mutex mLock;
89
90 struct QueryPromise {
91 QueryPromise(Query query) : query(query) {}
92 Query query;
93 // Number of times the query has been tried. Limited to kMaxTries.
94 int tries = 0;
95 // A promise whose future is returned by recordQuery()
96 // It is fulfilled by onResponse().
97 std::promise<Result> result;
98 };
99
Mike Yubab3daa2018-10-19 22:11:43 +0800100 // Outstanding queries by newId.
101 std::map<uint16_t, QueryPromise> mQueries GUARDED_BY(mLock);
102
103 // Get a "newId" number that is not currently in use. Returns -1 if there are none.
104 int32_t getFreeId() REQUIRES(mLock);
105
106 // Fulfill the result with an error code.
107 static void expire(QueryPromise* _Nonnull p);
108};
109
110} // end of namespace net
111} // end of namespace android
112
113#endif // _DNS_DNSTLSQUERYMAP_H