blob: 9487b51f4b201f4e29ec770b3e6652a769d63c43 [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
20#include <memory>
21#include <map>
22#include <mutex>
23
24#include <android-base/thread_annotations.h>
25
Ben Schwartzded1b702017-10-25 14:41:02 -040026#include <netdutils/Slice.h>
27
Ben Schwartz66810f62017-10-16 19:27:46 -040028#include "dns/DnsTlsServer.h"
Ben Schwartzded1b702017-10-25 14:41:02 -040029#include "dns/DnsTlsSocket.h"
30#include "dns/DnsTlsSocketFactory.h"
31#include "dns/IDnsTlsSocketFactory.h"
Ben Schwartz66810f62017-10-16 19:27:46 -040032#include "dns/DnsTlsTransport.h"
33
34namespace android {
35namespace net {
36
Ben Schwartzded1b702017-10-25 14:41:02 -040037using netdutils::Slice;
38
39// This is a singleton class that manages the collection of active DnsTlsTransports.
Ben Schwartz66810f62017-10-16 19:27:46 -040040// Queries made here are dispatched to an existing or newly constructed DnsTlsTransport.
41class DnsTlsDispatcher {
42public:
Ben Schwartzded1b702017-10-25 14:41:02 -040043 // Default constructor.
44 DnsTlsDispatcher() {
45 mFactory.reset(new DnsTlsSocketFactory());
46 }
47 // Constructor with dependency injection for testing.
48 DnsTlsDispatcher(std::unique_ptr<IDnsTlsSocketFactory> factory) :
49 mFactory(std::move(factory)) {}
50
51 // Given a |query|, sends it to the server on the network indicated by |mark|,
52 // and writes the response into |ans|, and indicates
53 // the number of bytes written in |resplen|. Returns a success or error code.
54 DnsTlsTransport::Response query(const DnsTlsServer& server, unsigned mark,
55 const Slice query, const Slice ans, int * _Nonnull resplen);
Ben Schwartz66810f62017-10-16 19:27:46 -040056
57private:
Ben Schwartzded1b702017-10-25 14:41:02 -040058 // This lock is static so that it can be used to annotate the Transport struct.
59 // DnsTlsDispatcher is a singleton in practice, so making this static does not change
60 // the locking behavior.
Ben Schwartz66810f62017-10-16 19:27:46 -040061 static std::mutex sLock;
62
Ben Schwartzded1b702017-10-25 14:41:02 -040063 // Key = <mark, server>
Ben Schwartz66810f62017-10-16 19:27:46 -040064 typedef std::pair<unsigned, const DnsTlsServer> Key;
65
66 // Transport is a thin wrapper around DnsTlsTransport, adding reference counting and
Ben Schwartzded1b702017-10-25 14:41:02 -040067 // usage monitoring so we can expire idle sessions from the cache.
Ben Schwartz66810f62017-10-16 19:27:46 -040068 struct Transport {
Ben Schwartzded1b702017-10-25 14:41:02 -040069 Transport(const DnsTlsServer& server, unsigned mark,
70 IDnsTlsSocketFactory* _Nonnull factory) :
71 transport(server, mark, factory) {}
72 // DnsTlsTransport is thread-safe, so it doesn't need to be guarded.
Ben Schwartz66810f62017-10-16 19:27:46 -040073 DnsTlsTransport transport;
74 // This use counter and timestamp are used to ensure that only idle sessions are
75 // destroyed.
76 int useCount GUARDED_BY(sLock) = 0;
Ben Schwartzded1b702017-10-25 14:41:02 -040077 // lastUsed is only guaranteed to be meaningful after useCount is decremented to zero.
Ben Schwartz66810f62017-10-16 19:27:46 -040078 std::chrono::time_point<std::chrono::steady_clock> lastUsed GUARDED_BY(sLock);
79 };
80
81 // Cache of reusable DnsTlsTransports. Transports stay in cache as long as
82 // they are in use and for a few minutes after.
83 // The key is a (netid, server) pair. The netid is first for lexicographic comparison speed.
Ben Schwartzded1b702017-10-25 14:41:02 -040084 std::map<Key, std::unique_ptr<Transport>> mStore GUARDED_BY(sLock);
Ben Schwartz66810f62017-10-16 19:27:46 -040085
86 // The last time we did a cleanup. For efficiency, we only perform a cleanup once every
87 // few minutes.
Ben Schwartzded1b702017-10-25 14:41:02 -040088 std::chrono::time_point<std::chrono::steady_clock> mLastCleanup GUARDED_BY(sLock);
Ben Schwartz66810f62017-10-16 19:27:46 -040089
90 // Drop any cache entries whose useCount is zero and which have not been used recently.
Ben Schwartzded1b702017-10-25 14:41:02 -040091 // This function performs a linear scan of mStore.
92 void cleanup(std::chrono::time_point<std::chrono::steady_clock> now) REQUIRES(sLock);
93
94 // Trivial factory for DnsTlsSockets. Dependency injection is only used for testing.
95 std::unique_ptr<IDnsTlsSocketFactory> mFactory;
Ben Schwartz66810f62017-10-16 19:27:46 -040096};
97
Ben Schwartzded1b702017-10-25 14:41:02 -040098} // end of namespace net
99} // end of namespace android
Ben Schwartz66810f62017-10-16 19:27:46 -0400100
101#endif // _DNS_DNSTLSDISPATCHER_H