Ben Schwartz | 3386076 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
Mike Yu | 5ae6154 | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 28 | #include "DnsTlsServer.h" |
| 29 | #include "params.h" |
Ben Schwartz | 3386076 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace net { |
| 33 | |
| 34 | using netdutils::Slice; |
| 35 | |
| 36 | // Keeps track of queries and responses. This class matches responses with queries. |
| 37 | // All methods are thread-safe and non-blocking. |
Mike Yu | a46fae7 | 2018-11-01 20:07:00 +0800 | [diff] [blame^] | 38 | class DnsTlsQueryMap { |
| 39 | public: |
Ben Schwartz | 3386076 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 40 | struct Query { |
| 41 | // The new ID number assigned to this query. |
| 42 | uint16_t newId; |
| 43 | // A query that has been passed to recordQuery(), with its original ID number. |
| 44 | const Slice query; |
| 45 | }; |
| 46 | |
| 47 | typedef DnsTlsServer::Response Response; |
| 48 | typedef DnsTlsServer::Result Result; |
| 49 | |
| 50 | struct QueryFuture { |
| 51 | QueryFuture(Query query, std::future<Result> result) : |
| 52 | query(query), result(std::move(result)) {} |
| 53 | Query query; |
| 54 | // A future which will resolve to the result of this query. |
| 55 | std::future<Result> result; |
| 56 | }; |
| 57 | |
| 58 | // Returns an object containing everything needed to complete processing of |
| 59 | // this query, or null if the query could not be recorded. |
| 60 | std::unique_ptr<QueryFuture> recordQuery(const Slice query); |
| 61 | |
| 62 | // Process a response, including a new ID. If the response |
| 63 | // is not recognized as matching any query, it will be ignored. |
| 64 | void onResponse(std::vector<uint8_t> response); |
| 65 | |
| 66 | // Clear all map contents. This causes all pending queries to resolve with failure. |
| 67 | void clear(); |
| 68 | |
| 69 | // Get all pending queries. This returns a shallow copy, mostly for thread-safety. |
| 70 | std::vector<Query> getAll(); |
| 71 | |
| 72 | // Mark a query has having been retried. If the query hits the retry limit, it will |
| 73 | // be expired at the next call to cleanup. |
| 74 | void markTried(uint16_t newId); |
| 75 | void cleanup(); |
| 76 | |
| 77 | // Returns true if there are no pending queries. |
| 78 | bool empty(); |
| 79 | |
| 80 | private: |
| 81 | std::mutex mLock; |
| 82 | |
| 83 | struct QueryPromise { |
| 84 | QueryPromise(Query query) : query(query) {} |
| 85 | Query query; |
| 86 | // Number of times the query has been tried. Limited to kMaxTries. |
| 87 | int tries = 0; |
| 88 | // A promise whose future is returned by recordQuery() |
| 89 | // It is fulfilled by onResponse(). |
| 90 | std::promise<Result> result; |
| 91 | }; |
| 92 | |
| 93 | // The maximum number of times we will send a query before abandoning it. |
| 94 | static constexpr int kMaxTries = 3; |
| 95 | |
| 96 | // Outstanding queries by newId. |
| 97 | std::map<uint16_t, QueryPromise> mQueries GUARDED_BY(mLock); |
| 98 | |
| 99 | // Get a "newId" number that is not currently in use. Returns -1 if there are none. |
| 100 | int32_t getFreeId() REQUIRES(mLock); |
| 101 | |
| 102 | // Fulfill the result with an error code. |
| 103 | static void expire(QueryPromise* _Nonnull p); |
| 104 | }; |
| 105 | |
| 106 | } // end of namespace net |
| 107 | } // end of namespace android |
| 108 | |
| 109 | #endif // _DNS_DNSTLSQUERYMAP_H |