• 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     resolv_set_log_severity(android::base::WARNING);
35     doh_init_logger(DOH_LOG_LEVEL_WARN);
36     using android::net::gApiLevel;
37     gApiLevel = getApiLevel();
38     using android::net::gResNetdCallbacks;
39     gResNetdCallbacks.check_calling_permission = callbacks->check_calling_permission;
40     gResNetdCallbacks.get_network_context = callbacks->get_network_context;
41     gResNetdCallbacks.log = callbacks->log;
42     if (gApiLevel >= 30) {
43         gResNetdCallbacks.tagSocket = callbacks->tagSocket;
44         gResNetdCallbacks.evaluate_domain_name = callbacks->evaluate_domain_name;
45     }
46     android::net::gDnsResolv = android::net::DnsResolver::getInstance();
47     return android::net::gDnsResolv->start();
48 }
49 
50 namespace android {
51 namespace net {
52 
53 namespace {
54 
verifyCallbacks()55 bool verifyCallbacks() {
56     if (!(gResNetdCallbacks.check_calling_permission && gResNetdCallbacks.get_network_context &&
57           gResNetdCallbacks.log)) {
58         return false;
59     }
60     if (gApiLevel >= 30) {
61         return gResNetdCallbacks.tagSocket != nullptr;
62     }
63     return true;
64 }
65 
66 }  // namespace
67 
68 DnsResolver* gDnsResolv = nullptr;
69 ResolverNetdCallbacks gResNetdCallbacks;
70 netdutils::Log gDnsResolverLog("dnsResolver");
71 uint64_t gApiLevel = 0;
72 
getInstance()73 DnsResolver* DnsResolver::getInstance() {
74     // Instantiated on first use.
75     static DnsResolver instance;
76     return &instance;
77 }
78 
DnsResolver()79 DnsResolver::DnsResolver() {
80     // TODO: make them member variables after fixing the circular dependency:
81     //   DnsTlsDispatcher.h -> resolv_private.h -> DnsResolver.h -> DnsTlsDispatcher.h
82     auto& dnsTlsDispatcher = DnsTlsDispatcher::getInstance();
83     auto& privateDnsConfiguration = PrivateDnsConfiguration::getInstance();
84     privateDnsConfiguration.setObserver(&dnsTlsDispatcher);
85     if (isDoHEnabled()) privateDnsConfiguration.initDoh();
86 }
87 
start()88 bool DnsResolver::start() {
89     if (!verifyCallbacks()) {
90         LOG(ERROR) << __func__ << ": Callback verification failed";
91         return false;
92     }
93     if (mDnsProxyListener.startListener()) {
94         PLOG(ERROR) << __func__ << ": Unable to start DnsProxyListener";
95         return false;
96     }
97     binder_status_t ret;
98     if ((ret = DnsResolverService::start()) != STATUS_OK) {
99         LOG(ERROR) << __func__ << ": Unable to start DnsResolverService: " << ret;
100         return false;
101     }
102     return true;
103 }
104 
setLogSeverity(int32_t logSeverity)105 int DnsResolver::setLogSeverity(int32_t logSeverity) {
106     return resolv_set_log_severity(logSeverity);
107 }
108 
109 }  // namespace net
110 }  // namespace android
111