blob: 7a480899bb05b86470fe39a3646137038050b444 [file] [log] [blame]
Ben Schwartz66810f62017-10-16 19:27:46 -04001/*
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 Kline1564d482018-03-07 17:09:35 +090020#include <list>
Ben Schwartz66810f62017-10-16 19:27:46 -040021#include <map>
Mike Yu5ae61542018-10-19 22:11:43 +080022#include <memory>
Ben Schwartz66810f62017-10-16 19:27:46 -040023#include <mutex>
24
25#include <android-base/thread_annotations.h>
Ben Schwartzded1b702017-10-25 14:41:02 -040026#include <netdutils/Slice.h>
27
Mike Yu5ae61542018-10-19 22:11:43 +080028#include "DnsTlsServer.h"
29#include "DnsTlsTransport.h"
30#include "IDnsTlsSocketFactory.h"
Ben Schwartz66810f62017-10-16 19:27:46 -040031
32namespace android {
33namespace net {
34
Ben Schwartzded1b702017-10-25 14:41:02 -040035// This is a singleton class that manages the collection of active DnsTlsTransports.
Ben Schwartz66810f62017-10-16 19:27:46 -040036// Queries made here are dispatched to an existing or newly constructed DnsTlsTransport.
Mike Yua46fae72018-11-01 20:07:00 +080037class DnsTlsDispatcher {
38 public:
Ben Schwartzded1b702017-10-25 14:41:02 -040039 // Default constructor.
Mike Yu5ae61542018-10-19 22:11:43 +080040 DnsTlsDispatcher();
41
Ben Schwartzded1b702017-10-25 14:41:02 -040042 // Constructor with dependency injection for testing.
Bernie Innocentiad4e26e2019-01-30 11:16:36 +090043 explicit DnsTlsDispatcher(std::unique_ptr<IDnsTlsSocketFactory> factory)
44 : mFactory(std::move(factory)) {}
Ben Schwartzded1b702017-10-25 14:41:02 -040045
Erik Kline1564d482018-03-07 17:09:35 +090046 // Enqueues |query| for resolution via the given |tlsServers| on the
47 // network indicated by |mark|; writes the response into |ans|, and stores
48 // the count of bytes written in |resplen|. Returns a success or error code.
49 // The order in which servers from |tlsServers| are queried may not be the
50 // order passed in by the caller.
Bernie Innocentiad4e26e2019-01-30 11:16:36 +090051 DnsTlsTransport::Response query(const std::list<DnsTlsServer>& tlsServers, unsigned mark,
52 const netdutils::Slice query, const netdutils::Slice ans,
53 int* _Nonnull resplen);
Erik Kline1564d482018-03-07 17:09:35 +090054
Ben Schwartzded1b702017-10-25 14:41:02 -040055 // Given a |query|, sends it to the server on the network indicated by |mark|,
56 // and writes the response into |ans|, and indicates
57 // the number of bytes written in |resplen|. Returns a success or error code.
58 DnsTlsTransport::Response query(const DnsTlsServer& server, unsigned mark,
Bernie Innocentiad4e26e2019-01-30 11:16:36 +090059 const netdutils::Slice query, const netdutils::Slice ans,
60 int* _Nonnull resplen);
Ben Schwartz66810f62017-10-16 19:27:46 -040061
Bernie Innocentiad4e26e2019-01-30 11:16:36 +090062 private:
Ben Schwartzded1b702017-10-25 14:41:02 -040063 // This lock is static so that it can be used to annotate the Transport struct.
64 // DnsTlsDispatcher is a singleton in practice, so making this static does not change
65 // the locking behavior.
Ben Schwartz66810f62017-10-16 19:27:46 -040066 static std::mutex sLock;
67
Ben Schwartzded1b702017-10-25 14:41:02 -040068 // Key = <mark, server>
Ben Schwartz66810f62017-10-16 19:27:46 -040069 typedef std::pair<unsigned, const DnsTlsServer> Key;
70
71 // Transport is a thin wrapper around DnsTlsTransport, adding reference counting and
Ben Schwartzded1b702017-10-25 14:41:02 -040072 // usage monitoring so we can expire idle sessions from the cache.
Ben Schwartz66810f62017-10-16 19:27:46 -040073 struct Transport {
Bernie Innocentiad4e26e2019-01-30 11:16:36 +090074 Transport(const DnsTlsServer& server, unsigned mark, IDnsTlsSocketFactory* _Nonnull factory)
75 : transport(server, mark, factory) {}
Ben Schwartzded1b702017-10-25 14:41:02 -040076 // DnsTlsTransport is thread-safe, so it doesn't need to be guarded.
Ben Schwartz66810f62017-10-16 19:27:46 -040077 DnsTlsTransport transport;
78 // This use counter and timestamp are used to ensure that only idle sessions are
79 // destroyed.
80 int useCount GUARDED_BY(sLock) = 0;
Ben Schwartzded1b702017-10-25 14:41:02 -040081 // lastUsed is only guaranteed to be meaningful after useCount is decremented to zero.
Ben Schwartz66810f62017-10-16 19:27:46 -040082 std::chrono::time_point<std::chrono::steady_clock> lastUsed GUARDED_BY(sLock);
83 };
84
85 // Cache of reusable DnsTlsTransports. Transports stay in cache as long as
86 // they are in use and for a few minutes after.
87 // The key is a (netid, server) pair. The netid is first for lexicographic comparison speed.
Ben Schwartzded1b702017-10-25 14:41:02 -040088 std::map<Key, std::unique_ptr<Transport>> mStore GUARDED_BY(sLock);
Ben Schwartz66810f62017-10-16 19:27:46 -040089
90 // The last time we did a cleanup. For efficiency, we only perform a cleanup once every
91 // few minutes.
Ben Schwartzded1b702017-10-25 14:41:02 -040092 std::chrono::time_point<std::chrono::steady_clock> mLastCleanup GUARDED_BY(sLock);
Ben Schwartz66810f62017-10-16 19:27:46 -040093
94 // Drop any cache entries whose useCount is zero and which have not been used recently.
Ben Schwartzded1b702017-10-25 14:41:02 -040095 // This function performs a linear scan of mStore.
96 void cleanup(std::chrono::time_point<std::chrono::steady_clock> now) REQUIRES(sLock);
97
Erik Kline1564d482018-03-07 17:09:35 +090098 // Return a sorted list of DnsTlsServers in preference order.
Bernie Innocentiad4e26e2019-01-30 11:16:36 +090099 std::list<DnsTlsServer> getOrderedServerList(const std::list<DnsTlsServer>& tlsServers,
100 unsigned mark) const;
Erik Kline1564d482018-03-07 17:09:35 +0900101
Ben Schwartzded1b702017-10-25 14:41:02 -0400102 // Trivial factory for DnsTlsSockets. Dependency injection is only used for testing.
103 std::unique_ptr<IDnsTlsSocketFactory> mFactory;
Ben Schwartz66810f62017-10-16 19:27:46 -0400104};
105
Ben Schwartzded1b702017-10-25 14:41:02 -0400106} // end of namespace net
107} // end of namespace android
Ben Schwartz66810f62017-10-16 19:27:46 -0400108
109#endif // _DNS_DNSTLSDISPATCHER_H