blob: cee0205a0265faa195d9b5be2e27c440d52c5da9 [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>
22#include <vector>
23
24#include <android-base/thread_annotations.h>
25
26#include "DnsTlsServer.h"
27#include "resolv.h"
28
29namespace android {
30namespace net {
31
32struct PrivateDnsStatus {
33 PrivateDnsMode mode;
34 std::list<DnsTlsServer> validatedServers;
35};
36
37class PrivateDnsConfiguration {
38 public:
39 int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
40 const std::string& name, const std::set<std::vector<uint8_t>>& fingerprints);
41
42 PrivateDnsStatus getStatus(unsigned netId);
43
44 // Externally used for netd.
45 void getStatus(unsigned netId, ExternalPrivateDnsStatus* status);
46
47 int getPrivateDnsModeAndStatus(unsigned netId);
48 void clear(unsigned netId);
49 void setCallback(private_dns_validated_callback callback);
50
51 private:
52 typedef std::map<DnsTlsServer, Validation, AddressComparator> PrivateDnsTracker;
53
54 void validatePrivateDnsProvider(const DnsTlsServer& server, PrivateDnsTracker& tracker,
55 unsigned netId, uint32_t mark) REQUIRES(mPrivateDnsLock);
56
57 bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success);
58
59 // Start validation for newly added servers as well as any servers that have
60 // landed in Validation::fail state. Note that servers that have failed
61 // multiple validation attempts but for which there is still a validating
62 // thread running are marked as being Validation::in_process.
63 bool needsValidation(const PrivateDnsTracker& tracker, const DnsTlsServer& server);
64
65 std::mutex mPrivateDnsLock;
66 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
67 // Structure for tracking the validation status of servers on a specific netId.
68 // Using the AddressComparator ensures at most one entry per IP address.
69 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
70 private_dns_validated_callback mCallback = nullptr;
71};
72
73extern PrivateDnsConfiguration gPrivateDnsConfiguration;
74
75} // namespace net
76} // namespace android
77
78#endif /* NETD_RESOLV_PRIVATEDNSCONFIGURATION_H */