• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2019, 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 #define LOG_TAG "resolv"
18 
19 #include "DnsResolver.h"
20 
21 #include <android-base/logging.h>
22 
23 #include "DnsProxyListener.h"
24 #include "DnsResolverService.h"
25 #include "DnsTlsDispatcher.h"
26 #include "PrivateDnsConfiguration.h"
27 #include "netd_resolv/resolv.h"
28 #include "res_debug.h"
29 #include "util.h"
30 
resolv_init(const ResolverNetdCallbacks * callbacks)31 bool resolv_init(const ResolverNetdCallbacks* callbacks) {
32     android::base::InitLogging(/*argv=*/nullptr);
33     LOG(INFO) << __func__ << ": Initializing resolver";
34     const bool isDebug = isDebuggable();
35     resolv_set_log_severity(isDebug ? android::base::INFO : android::base::WARNING);
36     doh_init_logger(isDebug ? DOH_LOG_LEVEL_INFO : DOH_LOG_LEVEL_WARN);
37     using android::net::gApiLevel;
38     gApiLevel = getApiLevel();
39     using android::net::gResNetdCallbacks;
40     gResNetdCallbacks.check_calling_permission = callbacks->check_calling_permission;
41     gResNetdCallbacks.get_network_context = callbacks->get_network_context;
42     gResNetdCallbacks.log = callbacks->log;
43     if (gApiLevel >= 30) {
44         gResNetdCallbacks.tagSocket = callbacks->tagSocket;
45         gResNetdCallbacks.evaluate_domain_name = callbacks->evaluate_domain_name;
46     }
47     android::net::gDnsResolv = android::net::DnsResolver::getInstance();
48     return android::net::gDnsResolv->start();
49 }
50 
51 namespace android {
52 namespace net {
53 
54 namespace {
55 
verifyCallbacks()56 bool verifyCallbacks() {
57     if (!(gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context &&
58           gResNetdCallbacks.log)) {
59         return false;
60     }
61     if (gApiLevel >= 30) {
62         return gResNetdCallbacks.tagSocket != nullptr;
63     }
64     return true;
65 }
66 
67 }  // namespace
68 
69 DnsResolver* gDnsResolv = nullptr;
70 ResolverNetdCallbacks gResNetdCallbacks;
71 netdutils::Log gDnsResolverLog("dnsResolver");
72 uint64_t gApiLevel = 0;
73 
getInstance()74 DnsResolver* DnsResolver::getInstance() {
75     // Instantiated on first use.
76     static DnsResolver instance;
77     return &instance;
78 }
79 
DnsResolver()80 DnsResolver::DnsResolver() {
81     // TODO: make them member variables after fixing the circular dependency:
82     //   DnsTlsDispatcher.h -> resolv_private.h -> DnsResolver.h -> DnsTlsDispatcher.h
83     auto& dnsTlsDispatcher = DnsTlsDispatcher::getInstance();
84     auto& privateDnsConfiguration = PrivateDnsConfiguration::getInstance();
85     privateDnsConfiguration.setObserver(&dnsTlsDispatcher);
86     if (isDoHEnabled()) privateDnsConfiguration.initDoh();
87 }
88 
start()89 bool DnsResolver::start() {
90     if (!verifyCallbacks()) {
91         LOG(ERROR) << __func__ << ": Callback verification failed";
92         return false;
93     }
94     if (mDnsProxyListener.startListener()) {
95         PLOG(ERROR) << __func__ << ": Unable to start DnsProxyListener";
96         return false;
97     }
98     binder_status_t ret;
99     if ((ret = DnsResolverService::start()) != STATUS_OK) {
100         LOG(ERROR) << __func__ << ": Unable to start DnsResolverService: " << ret;
101         return false;
102     }
103     return true;
104 }
105 
setLogSeverity(int32_t logSeverity)106 int DnsResolver::setLogSeverity(int32_t logSeverity) {
107     return resolv_set_log_severity(logSeverity);
108 }
109 
110 }  // namespace net
111 }  // namespace android
112