blob: ed0885d037282099cad558601f7cb28f1ee36fb5 [file] [log] [blame]
Mike Yub601ff72018-11-01 20:07:00 +08001/*
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 NETD_RESOLV_PRIVATEDNSCONFIGURATION_H
18#define NETD_RESOLV_PRIVATEDNSCONFIGURATION_H
19
20#include <list>
21#include <map>
Mike Yu303b0df2018-12-24 17:05:02 +080022#include <mutex>
Mike Yub601ff72018-11-01 20:07:00 +080023#include <vector>
24
25#include <android-base/thread_annotations.h>
Bernie Innocentiec4219b2019-01-30 11:16:36 +090026#include <netd_resolv/resolv.h>
Mike Yub601ff72018-11-01 20:07:00 +080027
28#include "DnsTlsServer.h"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090029
30struct ExternalPrivateDnsStatus; // Defined in netd_resolv/resolv.h
Mike Yub601ff72018-11-01 20:07:00 +080031
32namespace android {
33namespace net {
34
35struct PrivateDnsStatus {
36 PrivateDnsMode mode;
37 std::list<DnsTlsServer> validatedServers;
38};
39
40class PrivateDnsConfiguration {
41 public:
42 int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
43 const std::string& name, const std::set<std::vector<uint8_t>>& fingerprints);
44
45 PrivateDnsStatus getStatus(unsigned netId);
46
47 // Externally used for netd.
48 void getStatus(unsigned netId, ExternalPrivateDnsStatus* status);
49
Mike Yub601ff72018-11-01 20:07:00 +080050 void clear(unsigned netId);
Mike Yub601ff72018-11-01 20:07:00 +080051
52 private:
53 typedef std::map<DnsTlsServer, Validation, AddressComparator> PrivateDnsTracker;
54
55 void validatePrivateDnsProvider(const DnsTlsServer& server, PrivateDnsTracker& tracker,
56 unsigned netId, uint32_t mark) REQUIRES(mPrivateDnsLock);
57
58 bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success);
59
60 // Start validation for newly added servers as well as any servers that have
61 // landed in Validation::fail state. Note that servers that have failed
62 // multiple validation attempts but for which there is still a validating
63 // thread running are marked as being Validation::in_process.
64 bool needsValidation(const PrivateDnsTracker& tracker, const DnsTlsServer& server);
65
66 std::mutex mPrivateDnsLock;
67 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
68 // Structure for tracking the validation status of servers on a specific netId.
69 // Using the AddressComparator ensures at most one entry per IP address.
70 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080071};
72
73extern PrivateDnsConfiguration gPrivateDnsConfiguration;
74
75} // namespace net
76} // namespace android
77
78#endif /* NETD_RESOLV_PRIVATEDNSCONFIGURATION_H */