Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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_DNSTLSDISPATCHER_H |
| 18 | #define _DNS_DNSTLSDISPATCHER_H |
| 19 | |
| 20 | #include <list> |
| 21 | #include <map> |
| 22 | #include <memory> |
| 23 | #include <mutex> |
| 24 | |
| 25 | #include <android-base/thread_annotations.h> |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 26 | #include <netdutils/Slice.h> |
| 27 | |
| 28 | #include "DnsTlsServer.h" |
| 29 | #include "DnsTlsTransport.h" |
| 30 | #include "IDnsTlsSocketFactory.h" |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 31 | #include "resolv_private.h" |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace net { |
| 35 | |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 36 | // This is a singleton class that manages the collection of active DnsTlsTransports. |
| 37 | // Queries made here are dispatched to an existing or newly constructed DnsTlsTransport. |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 38 | class DnsTlsDispatcher { |
| 39 | public: |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 40 | // Default constructor. |
| 41 | DnsTlsDispatcher(); |
| 42 | |
| 43 | // Constructor with dependency injection for testing. |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 44 | explicit DnsTlsDispatcher(std::unique_ptr<IDnsTlsSocketFactory> factory) |
| 45 | : mFactory(std::move(factory)) {} |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 46 | |
| 47 | // Enqueues |query| for resolution via the given |tlsServers| on the |
| 48 | // network indicated by |mark|; writes the response into |ans|, and stores |
| 49 | // the count of bytes written in |resplen|. Returns a success or error code. |
| 50 | // The order in which servers from |tlsServers| are queried may not be the |
| 51 | // order passed in by the caller. |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 52 | DnsTlsTransport::Response query(const std::list<DnsTlsServer>& tlsServers, |
| 53 | res_state _Nonnull statp, const netdutils::Slice query, |
| 54 | const netdutils::Slice ans, int* _Nonnull resplen); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 55 | |
| 56 | // Given a |query|, sends it to the server on the network indicated by |mark|, |
| 57 | // and writes the response into |ans|, and indicates |
| 58 | // the number of bytes written in |resplen|. Returns a success or error code. |
| 59 | DnsTlsTransport::Response query(const DnsTlsServer& server, unsigned mark, |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 60 | const netdutils::Slice query, const netdutils::Slice ans, |
| 61 | int* _Nonnull resplen); |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 62 | |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 63 | private: |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 64 | // This lock is static so that it can be used to annotate the Transport struct. |
| 65 | // DnsTlsDispatcher is a singleton in practice, so making this static does not change |
| 66 | // the locking behavior. |
| 67 | static std::mutex sLock; |
| 68 | |
| 69 | // Key = <mark, server> |
| 70 | typedef std::pair<unsigned, const DnsTlsServer> Key; |
| 71 | |
| 72 | // Transport is a thin wrapper around DnsTlsTransport, adding reference counting and |
| 73 | // usage monitoring so we can expire idle sessions from the cache. |
| 74 | struct Transport { |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 75 | Transport(const DnsTlsServer& server, unsigned mark, IDnsTlsSocketFactory* _Nonnull factory) |
| 76 | : transport(server, mark, factory) {} |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 77 | // DnsTlsTransport is thread-safe, so it doesn't need to be guarded. |
| 78 | DnsTlsTransport transport; |
| 79 | // This use counter and timestamp are used to ensure that only idle sessions are |
| 80 | // destroyed. |
| 81 | int useCount GUARDED_BY(sLock) = 0; |
| 82 | // lastUsed is only guaranteed to be meaningful after useCount is decremented to zero. |
| 83 | std::chrono::time_point<std::chrono::steady_clock> lastUsed GUARDED_BY(sLock); |
| 84 | }; |
| 85 | |
| 86 | // Cache of reusable DnsTlsTransports. Transports stay in cache as long as |
| 87 | // they are in use and for a few minutes after. |
| 88 | // The key is a (netid, server) pair. The netid is first for lexicographic comparison speed. |
| 89 | std::map<Key, std::unique_ptr<Transport>> mStore GUARDED_BY(sLock); |
| 90 | |
| 91 | // The last time we did a cleanup. For efficiency, we only perform a cleanup once every |
| 92 | // few minutes. |
| 93 | std::chrono::time_point<std::chrono::steady_clock> mLastCleanup GUARDED_BY(sLock); |
| 94 | |
| 95 | // Drop any cache entries whose useCount is zero and which have not been used recently. |
| 96 | // This function performs a linear scan of mStore. |
| 97 | void cleanup(std::chrono::time_point<std::chrono::steady_clock> now) REQUIRES(sLock); |
| 98 | |
| 99 | // Return a sorted list of DnsTlsServers in preference order. |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 100 | std::list<DnsTlsServer> getOrderedServerList(const std::list<DnsTlsServer>& tlsServers, |
| 101 | unsigned mark) const; |
Mike Yu | bab3daa | 2018-10-19 22:11:43 +0800 | [diff] [blame] | 102 | |
| 103 | // Trivial factory for DnsTlsSockets. Dependency injection is only used for testing. |
| 104 | std::unique_ptr<IDnsTlsSocketFactory> mFactory; |
| 105 | }; |
| 106 | |
| 107 | } // end of namespace net |
| 108 | } // end of namespace android |
| 109 | |
| 110 | #endif // _DNS_DNSTLSDISPATCHER_H |