blob: c5ab023b532f49fd55de29c83c372e8abb053976 [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 Yubab3daa2018-10-19 22:11:43 +080037 struct Query {
38 // The new ID number assigned to this query.
39 uint16_t newId;
40 // A query that has been passed to recordQuery(), with its original ID number.
Bernie Innocentiec4219b2019-01-30 11:16:36 +090041 const netdutils::Slice query;
Mike Yubab3daa2018-10-19 22:11:43 +080042 };
43
44 typedef DnsTlsServer::Response Response;
45 typedef DnsTlsServer::Result Result;
46
47 struct QueryFuture {
Bernie Innocentiec4219b2019-01-30 11:16:36 +090048 QueryFuture(Query query, std::future<Result> result)
49 : query(query), result(std::move(result)) {}
Mike Yubab3daa2018-10-19 22:11:43 +080050 Query query;
51 // A future which will resolve to the result of this query.
52 std::future<Result> result;
53 };
54
55 // Returns an object containing everything needed to complete processing of
56 // this query, or null if the query could not be recorded.
Bernie Innocentiec4219b2019-01-30 11:16:36 +090057 std::unique_ptr<QueryFuture> recordQuery(const netdutils::Slice query);
Mike Yubab3daa2018-10-19 22:11:43 +080058
59 // Process a response, including a new ID. If the response
60 // is not recognized as matching any query, it will be ignored.
61 void onResponse(std::vector<uint8_t> response);
62
63 // Clear all map contents. This causes all pending queries to resolve with failure.
64 void clear();
65
66 // Get all pending queries. This returns a shallow copy, mostly for thread-safety.
67 std::vector<Query> getAll();
68
69 // Mark a query has having been retried. If the query hits the retry limit, it will
70 // be expired at the next call to cleanup.
71 void markTried(uint16_t newId);
72 void cleanup();
73
74 // Returns true if there are no pending queries.
75 bool empty();
76
Bernie Innocentiec4219b2019-01-30 11:16:36 +090077 private:
Mike Yubab3daa2018-10-19 22:11:43 +080078 std::mutex mLock;
79
80 struct QueryPromise {
81 QueryPromise(Query query) : query(query) {}
82 Query query;
83 // Number of times the query has been tried. Limited to kMaxTries.
84 int tries = 0;
85 // A promise whose future is returned by recordQuery()
86 // It is fulfilled by onResponse().
87 std::promise<Result> result;
88 };
89
90 // The maximum number of times we will send a query before abandoning it.
91 static constexpr int kMaxTries = 3;
92
93 // Outstanding queries by newId.
94 std::map<uint16_t, QueryPromise> mQueries GUARDED_BY(mLock);
95
96 // Get a "newId" number that is not currently in use. Returns -1 if there are none.
97 int32_t getFreeId() REQUIRES(mLock);
98
99 // Fulfill the result with an error code.
100 static void expire(QueryPromise* _Nonnull p);
101};
102
103} // end of namespace net
104} // end of namespace android
105
106#endif // _DNS_DNSTLSQUERYMAP_H