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